View Javadoc

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