KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > component > ComponentRegistry


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ComponentRegistry.java 1066 2006-08-09 17:00:56Z sauthieg $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.component;
27
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import org.objectweb.easybeans.component.api.EZBComponent;
34 import org.objectweb.easybeans.component.api.EZBComponentException;
35 import org.objectweb.easybeans.log.JLog;
36 import org.objectweb.easybeans.log.JLogFactory;
37
38 /**
39  * Registry that manages components. It allows to get components.
40  * @author Florent Benoit
41  */

42 public class ComponentRegistry {
43
44     /**
45      * Logger.
46      */

47     private JLog logger = JLogFactory.getLog(ComponentRegistry.class);
48
49     /**
50      * Map of components.<br/> Name <--> Implementation of the component
51      */

52     private Map JavaDoc<String JavaDoc, EZBComponent> components = null;
53
54     /**
55      * Constructor.
56      */

57     public ComponentRegistry() {
58         // init map
59
components = new HashMap JavaDoc<String JavaDoc, EZBComponent>();
60     }
61
62     /**
63      * Register a component.
64      * @param componentName the name of the component to register
65      * @param component the component to register.
66      * @throws EZBComponentException if registering fails.
67      */

68     public void register(final String JavaDoc componentName, final EZBComponent component) throws EZBComponentException {
69         // Existing ?
70
if (components.containsKey(componentName)) {
71             throw new EZBComponentException("Cannot register the component with the name '" + componentName
72                     + "'. There is an existing component with this name.");
73         }
74
75         logger.debug("Registering component with name {0}.", componentName);
76         components.put(componentName, component);
77     }
78
79     /**
80      * Unregister a component.
81      * @param componentName the component name to unregister.
82      * @throws EZBComponentException if unregistering fails.
83      */

84     public void unregister(final String JavaDoc componentName) throws EZBComponentException {
85         // Exist ?
86
if (!components.containsKey(componentName)) {
87             throw new EZBComponentException("No component with the name '" + componentName
88                     + "' found. Component not unregistered");
89         }
90
91         logger.info("Unregistering component with name {0}.", componentName);
92         components.remove(componentName);
93     }
94
95     /**
96      * Unregister a component.
97      * @param component the instance of the component to unregister.
98      * @throws EZBComponentException if unregistering fails.
99      */

100     public void unregister(final EZBComponent component) throws EZBComponentException {
101         String JavaDoc name = null;
102
103         // Find component
104
Set JavaDoc<String JavaDoc> keys = components.keySet();
105         for (String JavaDoc key : keys) {
106             EZBComponent foundComponent = components.get(key);
107             if (foundComponent.equals(component)) {
108                 // got it !
109
name = key;
110                 break;
111             }
112         }
113         // found --> unregister.
114
if (name != null) {
115             unregister(name);
116         }
117         throw new EZBComponentException("No component found in the registry with the given component '" + component + "'.");
118
119     }
120
121     /**
122      * Allow to get a reference on another component.
123      * @param componentName the name of the component
124      * @return the component.
125      */

126     public EZBComponent getComponent(final String JavaDoc componentName) {
127         return components.get(componentName);
128     }
129
130     /**
131      * @param component EZBComponent instance.
132      * @return Returns the component name from the EZBComponent instance.
133      */

134     public String JavaDoc getComponentName(final EZBComponent component) {
135
136         // Iterates over the components to find the component's name
137
String JavaDoc match = null;
138         for(Iterator JavaDoc<String JavaDoc> i = this.components.keySet().iterator();
139             i.hasNext() && (match == null);) {
140             String JavaDoc key = i.next();
141             EZBComponent candidate = this.components.get(key);
142             if (component == candidate) {
143                 match = key;
144             }
145         }
146         if (match == null) {
147             throw new IllegalStateException JavaDoc("Each component should be registered in the registry.");
148         }
149         return match;
150     }
151 }
152
Popular Tags