1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
46
47
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
77
78
79 protected abstract Monitor newMonitorInstance( String name );
80
81
82
83
84
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
101
102
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
120
121
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
138
139
140
141 public Collection<Monitor> getMonitors()
142 {
143 return new ArrayList<Monitor>( monitors.values() );
144 }
145
146
147
148
149 public void setFeatureManager( PluginManager featureManager )
150 {
151 this.featureManager = featureManager;
152 }
153
154
155
156
157 public PluginManager getFeatureManager()
158 {
159 return featureManager;
160 }
161 }