1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jmonit.monitors;
17
18 import java.util.Collections;
19 import java.util.Set;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentMap;
22 import java.util.concurrent.CopyOnWriteArraySet;
23
24 import org.jmonit.Monitor;
25 import org.jmonit.events.DataMonitoredEvent;
26 import org.jmonit.events.DefaultMonitoringEventBus;
27 import org.jmonit.events.MonitoringEvent;
28 import org.jmonit.events.MonitoringEventBus;
29 import org.jmonit.events.MonitoringEventListener;
30 import org.jmonit.spi.Plugin;
31
32
33
34
35
36
37
38
39
40 public abstract class AbstractMonitor
41 implements Monitor, MonitoringEventBus
42 {
43 protected String name;
44
45 protected Set<String> tags = new CopyOnWriteArraySet<String>();
46
47
48
49
50 protected ConcurrentMap<Class<?>, Plugin> components =
51 new ConcurrentHashMap<Class<?>, Plugin>();
52
53 public AbstractMonitor( String name )
54 {
55 super();
56 this.name = name;
57 }
58
59
60
61
62
63
64 public void add( long value )
65 {
66 DataMonitoredEvent event = new DataMonitoredEvent( value );
67 bus.fireMonitoringEvent( event );
68 }
69
70
71
72
73
74
75 public void fireMonitoringEvent( MonitoringEvent event )
76 {
77 bus.fireMonitoringEvent( event );
78 }
79
80
81
82
83
84
85 public void clear()
86 {
87
88 }
89
90 public Plugin get( Class role )
91 {
92 if ( role == null )
93 {
94 return null;
95 }
96 return components.get( role );
97 }
98
99 public boolean isFeatureSupported( Class clazz )
100 {
101 return hasFeatures( new Class[] { clazz } );
102 }
103
104
105
106
107
108
109 public boolean hasFeatures( Class[] features )
110 {
111 for ( Class feature : features )
112 {
113 if ( !components.containsKey( feature ) )
114 {
115 return false;
116 }
117 }
118 return true;
119 }
120
121
122
123
124
125
126 public <T> T getFeature( Class<T> clazz )
127 {
128 Plugin<T> feature = components.get( clazz );
129 if ( feature != null )
130 {
131 return feature.getFeature();
132 }
133 return null;
134 }
135
136
137
138
139
140
141 public Set<Class> getFeatures()
142 {
143 return Collections.<Class> unmodifiableSet( components.keySet() );
144 }
145
146 public void register( Plugin plugin, Class role )
147 {
148 plugin.setMonitoringEventBus( bus );
149 plugin.setMonitor( this );
150 components.putIfAbsent( role, plugin );
151 }
152
153 public void remove( Class role )
154 {
155 components.remove( role );
156 }
157
158
159
160
161 public String getName()
162 {
163 return name;
164 }
165
166
167
168
169
170
171 public Set<String> getTags()
172 {
173 return Collections.unmodifiableSet( tags );
174 }
175
176
177
178
179
180
181 public boolean isTagged( String tag )
182 {
183 return tags.contains( tag );
184 }
185
186
187
188
189
190
191 public Monitor tag( String tag )
192 {
193 boolean added = tags.add( tag );
194 if ( added )
195 {
196 onMonitorTaggedEvent( tag );
197 }
198 return this;
199 }
200
201
202
203
204 protected abstract void onMonitorTaggedEvent( String tag );
205
206
207
208
209 private DefaultMonitoringEventBus bus = new DefaultMonitoringEventBus();
210
211 public void addListener( MonitoringEventListener listener )
212 {
213 bus.addListener( listener );
214 }
215
216 public boolean hasListener( MonitoringEventListener listener )
217 {
218 return bus.hasListener( listener );
219 }
220
221 public void removeListener( MonitoringEventListener listener )
222 {
223 bus.removeListener( listener );
224 }
225
226 }