View Javadoc

1   /*
2    * $Id: NotificationDefinitionParser.java 19191 2010-08-25 21:05:23Z tcarlson $
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.ModelNotificationListener;
20  import org.mule.api.context.notification.MuleContextNotificationListener;
21  import org.mule.api.context.notification.RegistryNotificationListener;
22  import org.mule.api.context.notification.RoutingNotificationListener;
23  import org.mule.api.context.notification.SecurityNotificationListener;
24  import org.mule.api.context.notification.ServiceNotificationListener;
25  import org.mule.api.context.notification.TransactionNotificationListener;
26  import org.mule.config.spring.parsers.PreProcessor;
27  import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration;
28  import org.mule.config.spring.parsers.collection.ChildMapEntryDefinitionParser;
29  import org.mule.config.spring.parsers.processors.CheckExclusiveAttributes;
30  import org.mule.config.spring.parsers.processors.CheckRequiredAttributes;
31  import org.mule.context.notification.ComponentMessageNotification;
32  import org.mule.context.notification.ConnectionNotification;
33  import org.mule.context.notification.CustomNotification;
34  import org.mule.context.notification.EndpointMessageNotification;
35  import org.mule.context.notification.ExceptionNotification;
36  import org.mule.context.notification.ManagementNotification;
37  import org.mule.context.notification.ModelNotification;
38  import org.mule.context.notification.MuleContextNotification;
39  import org.mule.context.notification.RegistryNotification;
40  import org.mule.context.notification.RoutingNotification;
41  import org.mule.context.notification.SecurityNotification;
42  import org.mule.context.notification.ServiceNotification;
43  import org.mule.context.notification.TransactionNotification;
44  
45  import java.util.HashMap;
46  import java.util.Map;
47  
48  import org.w3c.dom.Element;
49  
50  public class NotificationDefinitionParser extends ChildMapEntryDefinitionParser
51  {
52  
53      public static final Map EVENT_MAP;
54      public static final Map INTERFACE_MAP;
55      public static final String INTERFACE = "interface";
56      public static final String INTERFACE_CLASS = "interface-class";
57      public static final String EVENT = "event";
58      public static final String EVENT_CLASS = "event-class";
59      public static final String[][] INTERFACE_ATTRIBUTES =
60              new String[][]{new String[]{INTERFACE}, new String[]{INTERFACE_CLASS}};
61      public static final String[][] EVENT_ATTRIBUTES =
62              new String[][]{new String[]{EVENT}, new String[]{EVENT_CLASS}};
63      public static final String[][] ALL_ATTRIBUTES =
64              new String[][]{
65                      new String[]{EVENT}, new String[]{EVENT_CLASS},
66                      new String[]{INTERFACE}, new String[]{INTERFACE_CLASS}};
67  
68      static
69      {
70          EVENT_MAP = new HashMap();
71          EVENT_MAP.put("CONTEXT", MuleContextNotification.class.getName());
72          EVENT_MAP.put("MODEL", ModelNotification.class.getName());
73          EVENT_MAP.put("SERVICE", ServiceNotification.class.getName());
74          EVENT_MAP.put("SECURITY", SecurityNotification.class.getName());
75      EVENT_MAP.put("ENDPOINT-MESSAGE", EndpointMessageNotification.class.getName());
76          EVENT_MAP.put("COMPONENT-MESSAGE", ComponentMessageNotification.class.getName());
77          EVENT_MAP.put("MANAGEMENT", ManagementNotification.class.getName());
78          EVENT_MAP.put("CONNECTION", ConnectionNotification.class.getName());
79          EVENT_MAP.put("REGISTRY", RegistryNotification.class.getName());
80          EVENT_MAP.put("CUSTOM", CustomNotification.class.getName());
81          EVENT_MAP.put("EXCEPTION", ExceptionNotification.class.getName());
82          EVENT_MAP.put("TRANSACTION", TransactionNotification.class.getName());
83          EVENT_MAP.put("ROUTING", RoutingNotification.class.getName());
84  
85          INTERFACE_MAP = new HashMap();
86          INTERFACE_MAP.put("CONTEXT", MuleContextNotificationListener.class.getName());
87          INTERFACE_MAP.put("MODEL", ModelNotificationListener.class.getName());
88          INTERFACE_MAP.put("SERVICE", ServiceNotificationListener.class.getName());
89          INTERFACE_MAP.put("SECURITY", SecurityNotificationListener.class.getName());
90          INTERFACE_MAP.put("MANAGEMENT", ManagementNotificationListener.class.getName());
91          INTERFACE_MAP.put("CONNECTION", ConnectionNotificationListener.class.getName());
92          INTERFACE_MAP.put("REGISTRY", RegistryNotificationListener.class.getName());
93          INTERFACE_MAP.put("CUSTOM", CustomNotificationListener.class.getName());
94          INTERFACE_MAP.put("ENDPOINT-MESSAGE", EndpointMessageNotificationListener.class.getName());
95          INTERFACE_MAP.put("COMPONENT-MESSAGE", ComponentMessageNotificationListener.class.getName());
96          INTERFACE_MAP.put("EXCEPTION", ExceptionNotificationListener.class.getName());
97          INTERFACE_MAP.put("TRANSACTION", TransactionNotificationListener.class.getName());
98          INTERFACE_MAP.put("ROUTING", RoutingNotificationListener.class.getName());
99          
100         // Deprecated
101         EVENT_MAP.put("MESSAGE", EndpointMessageNotification.class.getName());
102 
103     }
104 
105     public NotificationDefinitionParser()
106     {
107         super("interfaceToType", INTERFACE_CLASS, EVENT_CLASS);
108         addMapping(EVENT, EVENT_MAP);
109         addAlias(EVENT, VALUE);
110         addMapping(INTERFACE, INTERFACE_MAP);
111         addAlias(INTERFACE, KEY);
112         registerPreProcessor(new CheckExclusiveAttributes(INTERFACE_ATTRIBUTES));
113         registerPreProcessor(new CheckExclusiveAttributes(EVENT_ATTRIBUTES));
114         registerPreProcessor(new CheckRequiredAttributes(INTERFACE_ATTRIBUTES));
115         registerPreProcessor(new CheckRequiredAttributes(EVENT_ATTRIBUTES));
116         registerPreProcessor(new SetDefaults());
117     }
118 
119     /**
120      * If only one of event or interface is set, use it as default for the other
121      */
122     private class SetDefaults implements PreProcessor
123     {
124 
125         public void preProcess(PropertyConfiguration config, Element element)
126         {
127             copy(element, INTERFACE, EVENT, EVENT_CLASS);
128             copy(element, EVENT, INTERFACE, INTERFACE_CLASS);
129         }
130 
131         private void copy(Element element, String from, String to, String blocker)
132         {
133             if (element.hasAttribute(from) && !element.hasAttribute(to) && !element.hasAttribute(blocker))
134             {
135                 element.setAttribute(to, element.getAttribute(from));
136             }
137         }
138 
139     }
140 
141 }