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: ComponentManager.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.List;
028:
029: import org.ow2.easybeans.api.components.EZBComponentManager;
030: import org.ow2.easybeans.api.components.EZBComponentRegistry;
031: import org.ow2.easybeans.component.api.EZBComponent;
032: import org.ow2.easybeans.component.api.EZBComponentException;
033: import org.ow2.util.log.Log;
034: import org.ow2.util.log.LogFactory;
035:
036: /**
037: * Create and destroy components.
038: * @author Florent Benoit
039: */
040: public class ComponentManager implements EZBComponentManager {
041:
042: /**
043: * If Component classname ends with "Component", safely remove it.
044: */
045: private static final String COMPONENT_STR = "Component";
046:
047: /**
048: * Logger.
049: */
050: private Log logger = LogFactory.getLog(ComponentManager.class);
051:
052: /**
053: * Components names that are managed.
054: */
055: private List<String> componentNames = null;
056:
057: /**
058: * Components objects. (that were set by configuration).
059: */
060: private Components components = null;
061:
062: /**
063: * Link to the registry of components (key=component name/value=EZB component).
064: */
065: private ComponentRegistry componentRegistry = null;
066:
067: /**
068: * Build a component manager.
069: */
070: public ComponentManager() {
071: this .componentRegistry = new ComponentRegistry();
072: this .componentNames = new ArrayList<String>();
073: }
074:
075: /**
076: * Build a new component manager with the given set of components.
077: * @param components the given set of components
078: */
079: public ComponentManager(final Components components) {
080: this ();
081: setComponents(components);
082: }
083:
084: /**
085: * Gets the set of components.
086: * @return the set of components.
087: */
088: public Components getComponents() {
089: return this .components;
090: }
091:
092: /**
093: * Sets the components object.
094: * @param components the set of components.
095: */
096: public void setComponents(final Components components) {
097: this .components = components;
098: }
099:
100: /**
101: * Add the given component.
102: * @param component the component to register.
103: * @throws EZBComponentException if the component is not added.
104: */
105: public void addComponent(final EZBComponent component)
106: throws EZBComponentException {
107: // Add component
108: addComponent(getComponentName(component), component);
109: }
110:
111: /**
112: * Remove the given component.
113: * @param component the component to unregister.
114: * @throws EZBComponentException if the component is not removed.
115: */
116: public void removeComponent(final EZBComponent component)
117: throws EZBComponentException {
118: // Remove component
119: String componentName = this .componentRegistry
120: .getComponentName(component);
121: this .componentNames.remove(componentName);
122: this .componentRegistry.unregister(componentName);
123: }
124:
125: /**
126: * Gets the name for a given component.
127: * @param component the component instance.
128: * @return the name of the component.
129: */
130: private String getComponentName(final EZBComponent component) {
131: // get name
132: String componentName = component.getClass().getCanonicalName();
133:
134: // exist ? (increment counter to get an unique id)
135: int index = 2;
136: if (componentNames.contains(componentName)) {
137: while (componentNames.contains(componentName)) {
138: componentName = componentName + (index++);
139: }
140: }
141: return componentName;
142: }
143:
144: /**
145: * Add a component.
146: * @param componentName the name of the component to add
147: * @param component the component to add.
148: * @throws EZBComponentException if adds fails.
149: */
150: private void addComponent(final String componentName,
151: final EZBComponent component) throws EZBComponentException {
152: // register component
153: componentRegistry.register(componentName, component);
154:
155: // add to manage list
156: componentNames.add(componentName);
157: }
158:
159: /**
160: * Init the components by calling init() method.
161: * @throws EZBComponentException if initialization fails
162: */
163: public void initComponents() throws EZBComponentException {
164:
165: // Exit soon if there is no components
166: if (components == null) {
167: return;
168: }
169:
170: // Register component
171: List<EZBComponent> componentList = components
172: .getEZBComponents();
173: if (componentList != null) {
174: for (EZBComponent component : componentList) {
175: addComponent(component);
176: }
177:
178: // Call init method if any on each component
179: for (String componentName : componentNames) {
180: EZBComponent component = componentRegistry
181: .getComponent(componentName);
182: component.init();
183: }
184:
185: }
186: }
187:
188: /**
189: * Start the components.
190: * @throws EZBComponentException if starting is failing
191: */
192: public void startComponents() throws EZBComponentException {
193:
194: StringBuilder sb = new StringBuilder();
195: sb.append("[ Component(s) started : ");
196:
197: // Call init method if any on each component
198: for (String componentName : componentNames) {
199: EZBComponent component = componentRegistry
200: .getComponent(componentName);
201: component.start();
202:
203: // append the component name
204: String name = component.getClass().getSimpleName();
205: // remove "Component" substring if any
206: if (name.endsWith(COMPONENT_STR)) {
207: name = name.substring(0, name
208: .lastIndexOf(COMPONENT_STR));
209: }
210: sb.append(name);
211: sb.append(" ");
212: }
213:
214: sb.append("]");
215: logger.info(sb.toString());
216:
217: }
218:
219: /**
220: * Stop the components.
221: */
222: public void stopComponents() {
223:
224: // Call stop method if any on each component in the reverse order of the start.
225: int size = componentNames.size();
226: for (int i = size - 1; i >= 0; i--) {
227: String componentName = componentNames.get(i);
228: EZBComponent component = componentRegistry
229: .getComponent(componentName);
230: try {
231: component.stop();
232: } catch (EZBComponentException e) {
233: logger.error("Cannot stop component with name '"
234: + componentName + "'.", e);
235: }
236: }
237: }
238:
239: /**
240: * @return the component registry used by this manager.
241: */
242: public EZBComponentRegistry getComponentRegistry() {
243: return componentRegistry;
244: }
245: }
|