KickJava   Java API By Example, From Geeks To Geeks.

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


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: ComponentManager.java 1081 2006-08-10 11:22:56Z sauthieg $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.component;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.objectweb.easybeans.component.api.EZBComponent;
32 import org.objectweb.easybeans.component.api.EZBComponentException;
33 import org.objectweb.easybeans.log.JLog;
34 import org.objectweb.easybeans.log.JLogFactory;
35
36 /**
37  * Create and destroy components.
38  * @author Florent Benoit
39  */

40 public class ComponentManager {
41
42     /**
43      * If Component classname ends with "Component", safely remove it.
44      */

45     private static final String JavaDoc COMPONENT_STR = "Component";
46
47     /**
48      * Logger.
49      */

50     private JLog logger = JLogFactory.getLog(ComponentManager.class);
51
52     /**
53      * Components names that are managed.
54      */

55     private List JavaDoc<String JavaDoc> componentNames = null;
56
57     /**
58      * Components objects. (that were set by configuration).
59      */

60     private Components components = null;
61
62
63     /**
64      * Link to the registry of components (key=component name/value=EZB component).
65      */

66     private ComponentRegistry componentRegistry = null;
67
68     /**
69      * Build a component manager.
70      */

71     public ComponentManager() {
72         this.componentRegistry = new ComponentRegistry();
73         this.componentNames = new ArrayList JavaDoc<String JavaDoc>();
74     }
75
76     /**
77      * Build a new component manager with the given set of components.
78      * @param components the given set of components
79      */

80     public ComponentManager(final Components components) {
81         this();
82         setComponents(components);
83     }
84
85
86     /**
87      * Gets the set of components.
88      * @return the set of components.
89      */

90     public Components getComponents() {
91         return this.components;
92     }
93
94     /**
95      * Sets the components object.
96      * @param components the set of components.
97      */

98     public void setComponents(final Components components) {
99         this.components = components;
100     }
101
102     /**
103      * Add the given component.
104      * @param component the component to register.
105      * @throws EZBComponentException if the component is not added.
106      */

107     public void addComponent(final EZBComponent component) throws EZBComponentException {
108         // Add component
109
addComponent(getComponentName(component), component);
110     }
111
112     /**
113      * Remove the given component.
114      * @param component the component to unregister.
115      * @throws EZBComponentException if the component is not removed.
116      */

117     public void removeComponent(final EZBComponent component) throws EZBComponentException {
118         // Remove component
119
String JavaDoc componentName = this.componentRegistry.getComponentName(component);
120         this.componentNames.remove(componentName);
121         this.componentRegistry.unregister(componentName);
122     }
123
124     /**
125      * Gets the name for a given component.
126      * @param component the component instance.
127      * @return the name of the component.
128      */

129     private String JavaDoc getComponentName(final EZBComponent component) {
130         // get name
131
String JavaDoc componentName = component.getClass().getCanonicalName();
132
133         // exist ? (increment counter to get an unique id)
134
int index = 2;
135         if (componentNames.contains(componentName)) {
136             while (componentNames.contains(componentName)) {
137                 componentName = componentName + (index++);
138             }
139         }
140         return componentName;
141     }
142
143     /**
144      * Add a component.
145      * @param componentName the name of the component to add
146      * @param component the component to add.
147      * @throws EZBComponentException if adds fails.
148      */

149     private void addComponent(final String JavaDoc componentName, final EZBComponent component) throws EZBComponentException {
150         // register component
151
componentRegistry.register(componentName, component);
152
153         // add to manage list
154
componentNames.add(componentName);
155     }
156
157     /**
158      * Init the components by calling init() method.
159      * @throws EZBComponentException if initialization fails
160      */

161     public void initComponents() throws EZBComponentException {
162
163         // Exit soon if there is no components
164
if (components == null) {
165             return;
166         }
167
168         // Register component
169
List JavaDoc<EZBComponent> componentList = components.getEZBComponents();
170         if (componentList != null) {
171             for (EZBComponent component : componentList) {
172                 addComponent(component);
173             }
174
175
176             // Call init method if any on each component
177
for (String JavaDoc componentName : componentNames) {
178                 EZBComponent component = componentRegistry.getComponent(componentName);
179                 component.init();
180             }
181
182         }
183     }
184
185     /**
186      * Start the components.
187      * @throws EZBComponentException if starting is failing
188      */

189     public void startComponents() throws EZBComponentException {
190
191         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
192         sb.append("[ Component(s) started : ");
193
194         // Call init method if any on each component
195
for (String JavaDoc componentName : componentNames) {
196             EZBComponent component = componentRegistry.getComponent(componentName);
197             component.start();
198
199             // append the component name
200
String JavaDoc name = component.getClass().getSimpleName();
201             // remove "Component" substring if any
202
if (name.endsWith(COMPONENT_STR)) {
203                 name = name.substring(0, name.lastIndexOf(COMPONENT_STR));
204             }
205             sb.append(name);
206             sb.append(" ");
207         }
208
209         sb.append("]");
210         logger.info(sb.toString());
211
212     }
213
214     /**
215      * Stop the components.
216      */

217     public void stopComponents() {
218
219         // Call stop method if any on each component
220
for (String JavaDoc componentName : componentNames) {
221             EZBComponent component = componentRegistry.getComponent(componentName);
222             try {
223                 component.stop();
224             } catch (EZBComponentException e) {
225                 logger.error("Cannot stop component with name '" + componentName + "'.", e);
226             }
227         }
228     }
229
230     /**
231      * @return the component registry used by this manager.
232      */

233     public ComponentRegistry getComponentRegistry() {
234         return componentRegistry;
235     }
236 }
237
Popular Tags