View Javadoc
1   package org.codehaus.plexus.component.discovery;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Set;
22  import java.util.Map;
23  import java.util.LinkedHashMap;
24  
25  public class DefaultComponentDiscovererManager
26      implements ComponentDiscovererManager
27  {
28      private final List<ComponentDiscoverer> componentDiscoverers = new ArrayList<ComponentDiscoverer>();
29  
30      // todo dain change this to LinkedHashSet<ComponentDiscoveryListener> (requires change to maven)
31      private final Map<ComponentDiscoveryListener, Object> listeners = new LinkedHashMap<ComponentDiscoveryListener, Object>();
32  
33      public synchronized void addComponentDiscoverer( ComponentDiscoverer discoverer )
34      {
35          componentDiscoverers.add( discoverer );
36      }
37  
38      // todo this is not thread safe... we are returning the raw collection
39      public synchronized List<ComponentDiscoverer> getComponentDiscoverers()
40      {
41          return componentDiscoverers;
42      }
43  
44      // Listeners
45  
46      // todo this is not thread safe... we are returning the raw collection
47      public synchronized Map<ComponentDiscoveryListener, Object> getComponentDiscoveryListeners()
48      {
49          return listeners;
50      }
51  
52      public synchronized void registerComponentDiscoveryListener( ComponentDiscoveryListener listener )
53      {
54          if ( !listeners.containsKey( listener ) )
55          {
56              listeners.put( listener, new Object() );
57          }
58      }
59  
60      public synchronized void removeComponentDiscoveryListener( ComponentDiscoveryListener listener )
61      {
62          listeners.remove( listener );
63      }
64  
65      public void fireComponentDiscoveryEvent( ComponentDiscoveryEvent event )
66      {
67          Set<ComponentDiscoveryListener> listeners;
68          synchronized ( this )
69          {
70              listeners = this.listeners.keySet();
71          }
72  
73          for ( ComponentDiscoveryListener listener : listeners )
74          {
75              listener.componentDiscovered( event );
76          }
77      }
78  }