1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.config.spring; |
8 | |
|
9 | |
import org.mule.api.MuleContext; |
10 | |
import org.mule.api.context.MuleContextAware; |
11 | |
import org.mule.api.context.notification.ServerNotificationListener; |
12 | |
import org.mule.context.notification.ListenerSubscriptionPair; |
13 | |
import org.mule.context.notification.ServerNotificationManager; |
14 | |
|
15 | |
import java.util.Collection; |
16 | |
import java.util.HashSet; |
17 | |
import java.util.Map; |
18 | |
import java.util.Set; |
19 | |
|
20 | |
import org.springframework.beans.BeansException; |
21 | |
import org.springframework.beans.factory.SmartFactoryBean; |
22 | |
import org.springframework.context.ApplicationContext; |
23 | |
import org.springframework.context.ApplicationContextAware; |
24 | |
|
25 | 0 | public class ServerNotificationManagerConfigurator |
26 | |
implements MuleContextAware, SmartFactoryBean, ApplicationContextAware |
27 | |
{ |
28 | |
|
29 | |
private MuleContext muleContext; |
30 | |
private ApplicationContext applicationContext; |
31 | |
|
32 | |
private Boolean dynamic; |
33 | |
private Map interfaceToEvents; |
34 | |
private Collection interfaces; |
35 | |
private Collection<ListenerSubscriptionPair> pairs; |
36 | |
|
37 | |
public void setMuleContext(MuleContext context) |
38 | |
{ |
39 | 0 | this.muleContext = context; |
40 | 0 | } |
41 | |
|
42 | |
public Object getObject() throws Exception |
43 | |
{ |
44 | 0 | ServerNotificationManager notificationManager = muleContext.getNotificationManager(); |
45 | 0 | if (dynamic != null) |
46 | |
{ |
47 | 0 | notificationManager.setNotificationDynamic(dynamic.booleanValue()); |
48 | |
} |
49 | 0 | if (interfaceToEvents != null) |
50 | |
{ |
51 | 0 | notificationManager.setInterfaceToTypes(interfaceToEvents); |
52 | |
} |
53 | 0 | if (interfaces != null) |
54 | |
{ |
55 | 0 | notificationManager.setDisabledInterfaces(interfaces); |
56 | |
} |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 0 | Set<ListenerSubscriptionPair> subs = getMergedListeners(notificationManager); |
62 | 0 | for (ListenerSubscriptionPair sub : subs) |
63 | |
{ |
64 | |
|
65 | 0 | if(!notificationManager.isListenerRegistered(sub.getListener())) |
66 | |
{ |
67 | 0 | notificationManager.addListenerSubscriptionPair(sub); |
68 | |
} |
69 | |
} |
70 | 0 | return notificationManager; |
71 | |
} |
72 | |
|
73 | |
protected Set<ListenerSubscriptionPair> getMergedListeners(ServerNotificationManager notificationManager) |
74 | |
{ |
75 | 0 | Set<ListenerSubscriptionPair> mergedListeners = new HashSet<ListenerSubscriptionPair>(); |
76 | |
|
77 | |
|
78 | |
|
79 | 0 | String[] listenerBeans = applicationContext.getBeanNamesForType(ServerNotificationListener.class, |
80 | |
false, true); |
81 | 0 | Set<ListenerSubscriptionPair> adhocListeners = new HashSet<ListenerSubscriptionPair>(); |
82 | 0 | for (String name : listenerBeans) |
83 | |
{ |
84 | 0 | adhocListeners.add(new ListenerSubscriptionPair( |
85 | |
(ServerNotificationListener<?>) applicationContext.getBean(name), null)); |
86 | |
} |
87 | |
|
88 | 0 | if (pairs != null) |
89 | |
{ |
90 | 0 | mergedListeners.addAll(pairs); |
91 | |
|
92 | 0 | for (ListenerSubscriptionPair candidate : adhocListeners) |
93 | |
{ |
94 | 0 | boolean explicityDefined = false; |
95 | 0 | for (ListenerSubscriptionPair explicitListener : pairs) |
96 | |
{ |
97 | 0 | if (candidate.getListener().equals(explicitListener.getListener())) |
98 | |
{ |
99 | 0 | explicityDefined = true; |
100 | 0 | break; |
101 | |
} |
102 | |
} |
103 | 0 | if (!explicityDefined) |
104 | |
{ |
105 | 0 | mergedListeners.add(candidate); |
106 | |
} |
107 | 0 | } |
108 | |
} |
109 | |
else |
110 | |
{ |
111 | 0 | mergedListeners.addAll(adhocListeners); |
112 | |
} |
113 | |
|
114 | 0 | return mergedListeners; |
115 | |
} |
116 | |
|
117 | |
public Class getObjectType() |
118 | |
{ |
119 | 0 | return ServerNotificationManager.class; |
120 | |
} |
121 | |
|
122 | |
public boolean isSingleton() |
123 | |
{ |
124 | 0 | return true; |
125 | |
} |
126 | |
|
127 | |
public void setNotificationDynamic(boolean dynamic) |
128 | |
{ |
129 | 0 | this.dynamic = new Boolean(dynamic); |
130 | 0 | } |
131 | |
|
132 | |
public void setInterfaceToTypes(Map interfaceToEvents) throws ClassNotFoundException |
133 | |
{ |
134 | 0 | this.interfaceToEvents = interfaceToEvents; |
135 | 0 | } |
136 | |
|
137 | |
public void setAllListenerSubscriptionPairs(Collection pairs) |
138 | |
{ |
139 | 0 | this.pairs = pairs; |
140 | 0 | } |
141 | |
|
142 | |
public void setDisabledInterfaces(Collection interfaces) throws ClassNotFoundException |
143 | |
{ |
144 | 0 | this.interfaces = interfaces; |
145 | 0 | } |
146 | |
|
147 | |
public boolean isEagerInit() |
148 | |
{ |
149 | 0 | return true; |
150 | |
} |
151 | |
|
152 | |
public boolean isPrototype() |
153 | |
{ |
154 | 0 | return false; |
155 | |
} |
156 | |
|
157 | |
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException |
158 | |
{ |
159 | 0 | this.applicationContext = applicationContext; |
160 | |
|
161 | 0 | } |
162 | |
|
163 | |
} |