View Javadoc

1   /*
2    * $Id: Configuration.java 10489 2008-01-23 17:53:38Z 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.context.notification;
12  
13  import org.mule.api.context.notification.ServerNotification;
14  import org.mule.api.context.notification.ServerNotificationListener;
15  import org.mule.config.i18n.CoreMessages;
16  
17  import java.util.Collection;
18  import java.util.Collections;
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * This acts as a synchronized collection.  No call blocks and all are synchronized.
30   */
31  class Configuration
32  {
33  
34      protected Log logger = LogFactory.getLog(getClass());
35      private Map interfaceToTypes = new HashMap(); // map from interface to collection of events
36      private Set listenerSubscriptionPairs = new HashSet();
37      private Set disabledInterfaces = new HashSet();
38      private Set disabledNotificationTypes = new HashSet();
39      private boolean dirty = true;
40      private Policy policy;
41  
42      synchronized void addInterfaceToType(Class iface, Class type)
43      {
44          dirty = true;
45          if (!ServerNotification.class.isAssignableFrom(type))
46          {
47              throw new IllegalArgumentException(
48                      CoreMessages.propertyIsNotSupportedType("type",
49                              ServerNotification.class, type).getMessage());
50          }
51          if (!interfaceToTypes.containsKey(iface))
52          {
53              interfaceToTypes.put(iface, new HashSet());
54          }
55          Set events = (Set) interfaceToTypes.get(iface);
56          events.add(type);
57          if (logger.isDebugEnabled())
58          {
59              logger.debug("Registered event type: " + type);
60              logger.debug("Binding listener type '" + iface + "' to event type '" + type + "'");
61          }
62      }
63  
64      /**
65       * @param interfaceToTypes map from interace to a particular event
66       * @throws ClassNotFoundException
67       */
68      synchronized void addAllInterfaceToTypes(Map interfaceToTypes) throws ClassNotFoundException
69      {
70          dirty = true;
71          for (Iterator ifaces = interfaceToTypes.keySet().iterator(); ifaces.hasNext();)
72          {
73              Object iface = ifaces.next();
74              addInterfaceToType(ServerNotificationManager.toClass(iface), ServerNotificationManager.toClass(interfaceToTypes.get(iface)));
75          }
76      }
77  
78      synchronized void addListenerSubscriptionPair(ListenerSubscriptionPair pair)
79      {
80          dirty = true;
81          listenerSubscriptionPairs.add(pair);
82      }
83  
84      synchronized void addAllListenerSubscriptionPairs(Collection pairs)
85      {
86          dirty = true;
87          for (Iterator listener = pairs.iterator(); listener.hasNext();)
88          {
89              addListenerSubscriptionPair((ListenerSubscriptionPair) listener.next());
90          }
91      }
92  
93      synchronized void removeListener(ServerNotificationListener listener)
94      {
95          dirty = true;
96          Set toRemove = new HashSet();
97          for (Iterator listeners = listenerSubscriptionPairs.iterator(); listeners.hasNext();)
98          {
99              ListenerSubscriptionPair pair = (ListenerSubscriptionPair) listeners.next();
100             if (pair.getListener().equals(listener))
101             {
102                 toRemove.add(pair);
103             }
104         }
105         listenerSubscriptionPairs.removeAll(toRemove);
106     }
107 
108     synchronized void removeAllListeners(Collection listeners)
109     {
110         dirty = true;
111         for (Iterator listener = listeners.iterator(); listener.hasNext();)
112         {
113             removeListener((ServerNotificationListener) listener.next());
114         }
115     }
116 
117     synchronized void disableInterface(Class iface)
118     {
119         dirty = true;
120         disabledInterfaces.add(iface);
121     }
122 
123     synchronized void disabledAllInterfaces(Collection interfaces) throws ClassNotFoundException
124     {
125         dirty = true;
126         for (Iterator iface = interfaces.iterator(); iface.hasNext();)
127         {
128             disableInterface(ServerNotificationManager.toClass(iface.next()));
129         }
130     }
131 
132     synchronized void disableType(Class type)
133     {
134         dirty = true;
135         disabledNotificationTypes.add(type);
136     }
137 
138     synchronized void disableAllTypes(Collection types) throws ClassNotFoundException
139     {
140         dirty = true;
141         for (Iterator event = types.iterator(); event.hasNext();)
142         {
143             disableType(ServerNotificationManager.toClass(event.next()));
144         }
145     }
146 
147     synchronized Policy getPolicy()
148     {
149         if (dirty)
150         {
151             policy = new Policy(interfaceToTypes, listenerSubscriptionPairs, disabledInterfaces, disabledNotificationTypes);
152             dirty = false;
153         }
154         return policy;
155     }
156 
157     // for tests -------------------------------
158 
159     Map getInterfaceToTypes()
160     {
161         return Collections.unmodifiableMap(interfaceToTypes);
162     }
163 
164     Collection getListeners()
165     {
166         return Collections.unmodifiableCollection(listenerSubscriptionPairs);
167     }
168 
169 }