001: /**
002: * EasyBeans
003: * Copyright (C) 2006-2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ComponentRegistry.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.component;
025:
026: import java.util.ArrayList;
027: import java.util.Arrays;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Map;
032: import java.util.Set;
033:
034: import org.ow2.easybeans.api.components.EZBComponentRegistry;
035: import org.ow2.easybeans.component.api.EZBComponent;
036: import org.ow2.easybeans.component.api.EZBComponentException;
037: import org.ow2.util.log.Log;
038: import org.ow2.util.log.LogFactory;
039:
040: /**
041: * Registry that manages components. It allows to get components.
042: * @author Florent Benoit
043: */
044: public class ComponentRegistry implements EZBComponentRegistry {
045:
046: /**
047: * Logger.
048: */
049: private Log logger = LogFactory.getLog(ComponentRegistry.class);
050:
051: /**
052: * Map of components.<br/> Name <--> Implementation of the component
053: */
054: private Map<String, EZBComponent> components = null;
055:
056: /**
057: * Constructor.
058: */
059: public ComponentRegistry() {
060: // init map
061: components = new HashMap<String, EZBComponent>();
062: }
063:
064: /**
065: * Register a component.
066: * @param componentName the name of the component to register
067: * @param component the component to register.
068: * @throws EZBComponentException if registering fails.
069: */
070: public void register(final String componentName,
071: final EZBComponent component) throws EZBComponentException {
072: // Existing ?
073: if (components.containsKey(componentName)) {
074: throw new EZBComponentException(
075: "Cannot register the component with the name '"
076: + componentName
077: + "'. There is an existing component with this name.");
078: }
079:
080: logger.debug("Registering component with name {0}.",
081: componentName);
082: components.put(componentName, component);
083: }
084:
085: /**
086: * Unregister a component.
087: * @param componentName the component name to unregister.
088: * @throws EZBComponentException if unregistering fails.
089: */
090: public void unregister(final String componentName)
091: throws EZBComponentException {
092: // Exist ?
093: if (!components.containsKey(componentName)) {
094: throw new EZBComponentException(
095: "No component with the name '" + componentName
096: + "' found. Component not unregistered");
097: }
098:
099: logger.info("Unregistering component with name {0}.",
100: componentName);
101: components.remove(componentName);
102: }
103:
104: /**
105: * Unregister a component.
106: * @param component the instance of the component to unregister.
107: * @throws EZBComponentException if unregistering fails.
108: */
109: public void unregister(final EZBComponent component)
110: throws EZBComponentException {
111: String name = null;
112:
113: // Find component
114: Set<String> keys = components.keySet();
115: for (String key : keys) {
116: EZBComponent foundComponent = components.get(key);
117: if (foundComponent.equals(component)) {
118: // got it !
119: name = key;
120: break;
121: }
122: }
123: // found --> unregister.
124: if (name != null) {
125: unregister(name);
126: }
127: throw new EZBComponentException(
128: "No component found in the registry with the given component '"
129: + component + "'.");
130:
131: }
132:
133: /**
134: * Allow to get a reference on another component.
135: * @param componentName the name of the component
136: * @return the component.
137: */
138: public EZBComponent getComponent(final String componentName) {
139: return components.get(componentName);
140: }
141:
142: /**
143: * @param component EZBComponent instance.
144: * @return Returns the component name from the EZBComponent instance.
145: */
146: public String getComponentName(final EZBComponent component) {
147:
148: // Iterates over the components to find the component's name
149: String match = null;
150: for (Iterator<String> i = this .components.keySet().iterator(); i
151: .hasNext()
152: && (match == null);) {
153: String key = i.next();
154: EZBComponent candidate = this .components.get(key);
155: if (component.equals(candidate)) {
156: match = key;
157: break;
158: }
159: }
160: if (match == null) {
161: throw new IllegalStateException(
162: "Each component should be registered in the registry. No component found for '"
163: + component + "'.");
164: }
165: return match;
166: }
167:
168: /**
169: * Get the components that implements the given interface.
170: * @param itf the given interface
171: * @return an array of components implementing the given interface
172: * @param <T> an interface extending EZBComponent.
173: */
174: @SuppressWarnings("unchecked")
175: public <T extends EZBComponent> List<T> getComponents(
176: final Class<T> itf) {
177: // Check not null
178: if (itf == null) {
179: throw new IllegalArgumentException(
180: "Cannot find component with a null interface");
181: }
182:
183: // Check interface
184: if (!itf.isInterface()) {
185: throw new IllegalArgumentException("The given class '"
186: + itf + "' is not an interface");
187: }
188:
189: // Iterates over the components to find a matching component
190: List<T> matchComponents = new ArrayList<T>();
191: for (EZBComponent component : components.values()) {
192: // Component is implemeting the given interface ?
193: if (Arrays.asList(component.getClass().getInterfaces())
194: .contains(itf)) {
195: matchComponents.add((T) component);
196: }
197: }
198: return matchComponents;
199: }
200: }
|