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.repositories;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Collection;
21  import java.util.HashSet;
22  import java.util.Iterator;
23  import java.util.LinkedList;
24  import java.util.List;
25  import java.util.Set;
26  import java.util.concurrent.ConcurrentHashMap;
27  import java.util.concurrent.ConcurrentMap;
28  
29  import org.jmonit.Monitor;
30  import org.jmonit.Repository;
31  import org.jmonit.spi.PluginManager;
32  import org.jmonit.spi.PluginManagerAware;
33  
34  /**
35   * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
36   */
37  public abstract class AbstractRepository
38      implements Repository
39  {
40      private ConcurrentMap<String, Monitor> monitors = new ConcurrentHashMap<String, Monitor>();
41  
42      private PluginManager featureManager;
43  
44      /**
45       * {@inheritDoc}
46       * 
47       * @see org.jmonit.Repository#getMonitor(java.lang.String)
48       */
49      public Monitor getMonitor( String name )
50      {
51          Monitor monitor = monitors.get( name );
52          if ( monitor != null )
53          {
54              return monitor;
55          }
56          monitor = newMonitorInstance( name );
57          if ( monitor instanceof PluginManagerAware )
58          {
59              ( (PluginManagerAware) monitor ).setPluginManager( featureManager );
60          }
61          Monitor previous = monitors.putIfAbsent( name, monitor );
62          return ( previous != null ? previous : monitor );
63      }
64  
65      public Set<String> getTags()
66      {
67          Set<String> tags = new HashSet<String>();
68          for ( Monitor monitor : this.monitors.values() )
69          {
70              tags.addAll( monitor.getTags() );
71          }
72          return tags;
73      }
74  
75      /**
76       * @param name
77       * @return
78       */
79      protected abstract Monitor newMonitorInstance( String name );
80  
81      /**
82       * {@inheritDoc}
83       * 
84       * @see org.jmonit.Repository#getMonitorsWithTag(java.lang.String)
85       */
86      public Collection<Monitor> getMonitorsWithTag( String tag )
87      {
88          Collection<Monitor> tagged = new LinkedList<Monitor>();
89          for ( Monitor monitor : monitors.values() )
90          {
91              if ( monitor.isTagged( tag ) )
92              {
93                  tagged.add( monitor );
94              }
95          }
96          return tagged;
97      }
98  
99      /**
100      * {@inheritDoc}
101      * 
102      * @see org.jmonit.Repository#getMonitorsWithTags(java.lang.String[])
103      */
104     public Collection<Monitor> getMonitorsWithTags( String[] tags )
105     {
106         List<String> expected = Arrays.asList( tags );
107         Collection<Monitor> tagged = new LinkedList<Monitor>();
108         for ( Monitor monitor : monitors.values() )
109         {
110             if ( monitor.getTags().containsAll( expected ) )
111             {
112                 tagged.add( monitor );
113             }
114         }
115         return tagged;
116     }
117 
118     /**
119      * {@inheritDoc}
120      * 
121      * @see org.jmonit.Repository#getMonitorsWithFeatures(java.lang.Class[])
122      */
123     public Collection<Monitor> getMonitorsWithFeatures( Class[] features )
124     {
125         Collection<Monitor> filtered = new ArrayList<Monitor>( monitors.values() );
126         for ( Iterator<Monitor> iterator = filtered.iterator(); iterator.hasNext(); )
127         {
128             if ( !iterator.next().hasFeatures( features ) )
129             {
130                 iterator.remove();
131             }
132         }
133         return filtered;
134     }
135 
136     /**
137      * {@inheritDoc}
138      * 
139      * @see org.jmonit.Repository#getMonitors()
140      */
141     public Collection<Monitor> getMonitors()
142     {
143         return new ArrayList<Monitor>( monitors.values() );
144     }
145 
146     /**
147      * @param featureManager the featureManager to set
148      */
149     public void setFeatureManager( PluginManager featureManager )
150     {
151         this.featureManager = featureManager;
152     }
153 
154     /**
155      * @return the featureManager
156      */
157     public PluginManager getFeatureManager()
158     {
159         return featureManager;
160     }
161 }