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.LinkedHashMap;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Set;
24  
25  public class DefaultComponentDiscovererManager implements ComponentDiscovererManager {
26      private final List<ComponentDiscoverer> componentDiscoverers = new ArrayList<ComponentDiscoverer>();
27  
28      // todo dain change this to LinkedHashSet<ComponentDiscoveryListener> (requires change to maven)
29      private final Map<ComponentDiscoveryListener, Object> listeners =
30              new LinkedHashMap<ComponentDiscoveryListener, Object>();
31  
32      public synchronized void addComponentDiscoverer(ComponentDiscoverer discoverer) {
33          componentDiscoverers.add(discoverer);
34      }
35  
36      // todo this is not thread safe... we are returning the raw collection
37      public synchronized List<ComponentDiscoverer> getComponentDiscoverers() {
38          return componentDiscoverers;
39      }
40  
41      // Listeners
42  
43      // todo this is not thread safe... we are returning the raw collection
44      public synchronized Map<ComponentDiscoveryListener, Object> getComponentDiscoveryListeners() {
45          return listeners;
46      }
47  
48      public synchronized void registerComponentDiscoveryListener(ComponentDiscoveryListener listener) {
49          if (!listeners.containsKey(listener)) {
50              listeners.put(listener, new Object());
51          }
52      }
53  
54      public synchronized void removeComponentDiscoveryListener(ComponentDiscoveryListener listener) {
55          listeners.remove(listener);
56      }
57  
58      public void fireComponentDiscoveryEvent(ComponentDiscoveryEvent event) {
59          Set<ComponentDiscoveryListener> listeners;
60          synchronized (this) {
61              listeners = this.listeners.keySet();
62          }
63  
64          for (ComponentDiscoveryListener listener : listeners) {
65              listener.componentDiscovered(event);
66          }
67      }
68  }