Source Code Cross Referenced for ComponentManager.java in  » J2EE » ow2-easybeans » org » ow2 » easybeans » component » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » ow2 easybeans » org.ow2.easybeans.component 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.