View Javadoc

1   /*
2    ~ Copyright 2006-2007 Nicolas De Loof.
3    ~
4    ~ Licensed under the Apache License, Version 2.0 (the "License");
5    ~ you may not use this file except in compliance with the License.
6    ~ You may obtain a copy of the License at
7    ~
8    ~      http://www.apache.org/licenses/LICENSE-2.0
9    ~
10   ~ Unless required by applicable law or agreed to in writing, software
11   ~ distributed under the License is distributed on an "AS IS" BASIS,
12   ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   ~ See the License for the specific language governing permissions and
14   ~ limitations under the License.
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   * A Monitor implementation that delegates to multiple monitors. Each monitor in
34   * the composite has a ROLE and can retrieve other monitors from the composite
35   * based on it. By convention, defaults monitors are registered using class name
36   * as role (or public interface class name)
37   * 
38   * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
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       * Components of the composite, with public "feature" API class name as key
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       * {@inheritDoc}
61       * 
62       * @see org.jmonit.Monitor#add(long)
63       */
64      public void add( long value )
65      {
66          DataMonitoredEvent event = new DataMonitoredEvent( value );
67          bus.fireMonitoringEvent( event );
68      }
69  
70      /**
71       * {@inheritDoc}
72       * 
73       * @see org.jmonit.Monitor#fireEvent(MonitoringEvent)
74       */
75      public void fireMonitoringEvent( MonitoringEvent event )
76      {
77          bus.fireMonitoringEvent( event );
78      }
79  
80      /**
81       * {@inheritDoc}
82       * 
83       * @see org.jmonit.Monitor#clear()
84       */
85      public void clear()
86      {
87          // fireEvent( new RefreshEvent() );
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      * {@inheritDoc}
106      * 
107      * @see org.jmonit.Monitor#hasFeatures(java.lang.Class[])
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      * {@inheritDoc}
123      * 
124      * @see org.jmonit.Monitor#getFeature(Class)
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      * {@inheritDoc}
138      * 
139      * @see org.jmonit.Monitor#getFeatures()
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      * @return the name
160      */
161     public String getName()
162     {
163         return name;
164     }
165 
166     /**
167      * {@inheritDoc}
168      * 
169      * @see org.jmonit.Monitor#getTags()
170      */
171     public Set<String> getTags()
172     {
173         return Collections.unmodifiableSet( tags );
174     }
175 
176     /**
177      * {@inheritDoc}
178      * 
179      * @see org.jmonit.Monitor#isTagged(java.lang.String)
180      */
181     public boolean isTagged( String tag )
182     {
183         return tags.contains( tag );
184     }
185 
186     /**
187      * {@inheritDoc}
188      * 
189      * @see org.jmonit.Monitor#tag(java.lang.String)
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      * @param tag
203      */
204     protected abstract void onMonitorTaggedEvent( String tag );
205 
206     /**
207      * Event bus for MonitoringEvents related to this monitor
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 }