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