View Javadoc

1   /*
2    * $Id: NotificationDefinitionParser.java 20335 2010-11-24 18:14:29Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config.spring.parsers.specific;
12  
13  import org.mule.api.context.notification.ComponentMessageNotificationListener;
14  import org.mule.api.context.notification.ConnectionNotificationListener;
15  import org.mule.api.context.notification.CustomNotificationListener;
16  import org.mule.api.context.notification.EndpointMessageNotificationListener;
17  import org.mule.api.context.notification.ExceptionNotificationListener;
18  import org.mule.api.context.notification.ManagementNotificationListener;
19  import org.mule.api.context.notification.MessageProcessorNotificationListener;
20  import org.mule.api.context.notification.ModelNotificationListener;
21  import org.mule.api.context.notification.MuleContextNotificationListener;
22  import org.mule.api.context.notification.RegistryNotificationListener;
23  import org.mule.api.context.notification.RoutingNotificationListener;
24  import org.mule.api.context.notification.SecurityNotificationListener;
25  import org.mule.api.context.notification.ServiceNotificationListener;
26  import org.mule.api.context.notification.TransactionNotificationListener;
27  import org.mule.config.spring.parsers.PreProcessor;
28  import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration;
29  import org.mule.config.spring.parsers.collection.ChildMapEntryDefinitionParser;
30  import org.mule.config.spring.parsers.processors.CheckExclusiveAttributes;
31  import org.mule.config.spring.parsers.processors.CheckRequiredAttributes;
32  import org.mule.context.notification.ComponentMessageNotification;
33  import org.mule.context.notification.ConnectionNotification;
34  import org.mule.context.notification.CustomNotification;
35  import org.mule.context.notification.EndpointMessageNotification;
36  import org.mule.context.notification.ExceptionNotification;
37  import org.mule.context.notification.ManagementNotification;
38  import org.mule.context.notification.MessageProcessorNotification;
39  import org.mule.context.notification.ModelNotification;
40  import org.mule.context.notification.MuleContextNotification;
41  import org.mule.context.notification.RegistryNotification;
42  import org.mule.context.notification.RoutingNotification;
43  import org.mule.context.notification.SecurityNotification;
44  import org.mule.context.notification.ServiceNotification;
45  import org.mule.context.notification.TransactionNotification;
46  
47  import java.util.HashMap;
48  import java.util.Map;
49  
50  import org.w3c.dom.Element;
51  
52  public class NotificationDefinitionParser extends ChildMapEntryDefinitionParser
53  {
54  
55      public static final Map EVENT_MAP;
56      public static final Map INTERFACE_MAP;
57      public static final String INTERFACE = "interface";
58      public static final String INTERFACE_CLASS = "interface-class";
59      public static final String EVENT = "event";
60      public static final String EVENT_CLASS = "event-class";
61      public static final String[][] INTERFACE_ATTRIBUTES =
62              new String[][]{new String[]{INTERFACE}, new String[]{INTERFACE_CLASS}};
63      public static final String[][] EVENT_ATTRIBUTES =
64              new String[][]{new String[]{EVENT}, new String[]{EVENT_CLASS}};
65      public static final String[][] ALL_ATTRIBUTES =
66              new String[][]{
67                      new String[]{EVENT}, new String[]{EVENT_CLASS},
68                      new String[]{INTERFACE}, new String[]{INTERFACE_CLASS}};
69  
70      static
71      {
72          EVENT_MAP = new HashMap();
73          EVENT_MAP.put("CONTEXT", MuleContextNotification.class.getName());
74          EVENT_MAP.put("MODEL", ModelNotification.class.getName());
75          EVENT_MAP.put("SERVICE", ServiceNotification.class.getName());
76          EVENT_MAP.put("SECURITY", SecurityNotification.class.getName());
77          EVENT_MAP.put("ENDPOINT-MESSAGE", EndpointMessageNotification.class.getName());
78          EVENT_MAP.put("COMPONENT-MESSAGE", ComponentMessageNotification.class.getName());
79          EVENT_MAP.put("MANAGEMENT", ManagementNotification.class.getName());
80          EVENT_MAP.put("MESSAGE-PROCESSOR", MessageProcessorNotification.class.getName());
81          EVENT_MAP.put("CONNECTION", ConnectionNotification.class.getName());
82          EVENT_MAP.put("REGISTRY", RegistryNotification.class.getName());
83          EVENT_MAP.put("CUSTOM", CustomNotification.class.getName());
84          EVENT_MAP.put("EXCEPTION", ExceptionNotification.class.getName());
85          EVENT_MAP.put("TRANSACTION", TransactionNotification.class.getName());
86          EVENT_MAP.put("ROUTING", RoutingNotification.class.getName());
87  
88          INTERFACE_MAP = new HashMap();
89          INTERFACE_MAP.put("CONTEXT", MuleContextNotificationListener.class.getName());
90          INTERFACE_MAP.put("MODEL", ModelNotificationListener.class.getName());
91          INTERFACE_MAP.put("SERVICE", ServiceNotificationListener.class.getName());
92          INTERFACE_MAP.put("SECURITY", SecurityNotificationListener.class.getName());
93          INTERFACE_MAP.put("MANAGEMENT", ManagementNotificationListener.class.getName());
94          INTERFACE_MAP.put("MESSAGE-PROCESSOR", MessageProcessorNotificationListener.class.getName());
95          INTERFACE_MAP.put("CONNECTION", ConnectionNotificationListener.class.getName());
96          INTERFACE_MAP.put("REGISTRY", RegistryNotificationListener.class.getName());
97          INTERFACE_MAP.put("CUSTOM", CustomNotificationListener.class.getName());
98          INTERFACE_MAP.put("ENDPOINT-MESSAGE", EndpointMessageNotificationListener.class.getName());
99          INTERFACE_MAP.put("COMPONENT-MESSAGE", ComponentMessageNotificationListener.class.getName());
100         INTERFACE_MAP.put("EXCEPTION", ExceptionNotificationListener.class.getName());
101         INTERFACE_MAP.put("TRANSACTION", TransactionNotificationListener.class.getName());
102         INTERFACE_MAP.put("ROUTING", RoutingNotificationListener.class.getName());
103         
104         // Deprecated
105         EVENT_MAP.put("MESSAGE", EndpointMessageNotification.class.getName());
106 
107     }
108 
109     public NotificationDefinitionParser()
110     {
111         super("interfaceToType", INTERFACE_CLASS, EVENT_CLASS);
112         addMapping(EVENT, EVENT_MAP);
113         addAlias(EVENT, VALUE);
114         addMapping(INTERFACE, INTERFACE_MAP);
115         addAlias(INTERFACE, KEY);
116         registerPreProcessor(new CheckExclusiveAttributes(INTERFACE_ATTRIBUTES));
117         registerPreProcessor(new CheckExclusiveAttributes(EVENT_ATTRIBUTES));
118         registerPreProcessor(new CheckRequiredAttributes(INTERFACE_ATTRIBUTES));
119         registerPreProcessor(new CheckRequiredAttributes(EVENT_ATTRIBUTES));
120         registerPreProcessor(new SetDefaults());
121     }
122 
123     /**
124      * If only one of event or interface is set, use it as default for the other
125      */
126     private class SetDefaults implements PreProcessor
127     {
128 
129         public void preProcess(PropertyConfiguration config, Element element)
130         {
131             copy(element, INTERFACE, EVENT, EVENT_CLASS);
132             copy(element, EVENT, INTERFACE, INTERFACE_CLASS);
133         }
134 
135         private void copy(Element element, String from, String to, String blocker)
136         {
137             if (element.hasAttribute(from) && !element.hasAttribute(to) && !element.hasAttribute(blocker))
138             {
139                 element.setAttribute(to, element.getAttribute(from));
140             }
141         }
142 
143     }
144 
145 }