001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.core.facade;
019:
020: import java.util.Hashtable;
021: import java.util.Map;
022:
023: import org.columba.api.exception.ServiceNotFoundException;
024:
025: /**
026: * Service registry and locator. Upper layers can register a service which can
027: * be used by others. A service is registered using its full interface name
028: * including package and the name of the implementation.
029: * <p>
030: * <code>ServiceManager</code> uses reflection to instanciate the
031: * implementation.
032: * <p>
033: * This registry should be only used to enable different components to interact
034: * with each other. For example: Mail component makes use of the addressbook
035: * component
036: *
037: * @author fdietz
038: */
039: public class ServiceFacadeRegistry {
040:
041: private static ServiceFacadeRegistry instance = new ServiceFacadeRegistry();
042:
043: private Map<Class, Service> map = new Hashtable<Class, Service>();
044:
045: private ServiceFacadeRegistry() {
046: }
047:
048: public static ServiceFacadeRegistry getInstance() {
049: return instance;
050: }
051:
052: public void register(Class serviceInterface, Object serviceInstance) {
053: Service service = new Service(serviceInterface, serviceInstance);
054:
055: map.put(serviceInterface, service);
056: }
057:
058: public void unregister(Class serviceInterface) {
059: map.remove(serviceInterface);
060: }
061:
062: public Object getService(Class serviceInterface)
063: throws ServiceNotFoundException {
064: Object o = null;
065: Service service = null;
066:
067: // check if service is registered
068: if (map.containsKey(serviceInterface)) {
069: service = map.get(serviceInterface);
070:
071: // retrieve service instance
072: if (service != null)
073: o = service.getServiceInstance();
074: }
075:
076: if (o == null)
077: throw new ServiceNotFoundException(serviceInterface);
078:
079: return o;
080: }
081:
082: /**
083: * A service is described with its service name, which is usually the name
084: * of the interface.
085: * <p>
086: * Keeps a reference to the service instanciation.
087: *
088: * @author Frederik Dietz
089: */
090: private class Service {
091: private Class serviceInterface;
092:
093: private String implementationName;
094:
095: private Object serviceInstance;
096:
097: Service(Class serviceInterface, String implementationName) {
098: this .serviceInterface = serviceInterface;
099: this .implementationName = implementationName;
100: }
101:
102: public Service(Class serviceInterface, Object serviceInstance) {
103: this .serviceInterface = serviceInterface;
104:
105: this .serviceInstance = serviceInstance;
106: }
107:
108: /**
109: * @return Returns the serviceInstance.
110: */
111: public Object getServiceInstance() {
112: // check if there's already an instanciation available
113: // we can reuse here
114: if (serviceInstance == null) {
115: // load instance of service
116: serviceInstance = loadInstance(implementationName);
117: }
118:
119: return serviceInstance;
120: }
121:
122: /**
123: * @return Returns the serviceName.
124: */
125: public Class getServiceInterface() {
126: return serviceInterface;
127: }
128:
129: /**
130: * Load instance of class.
131: * <p>
132: *
133: * @param className
134: * class name
135: * @return instance of class
136: */
137: private Object loadInstance(String className) {
138: Object object = null;
139:
140: try {
141: Class clazz = this .getClass().getClassLoader()
142: .loadClass(className);
143:
144: object = clazz.newInstance();
145:
146: } catch (ClassNotFoundException e) {
147:
148: e.printStackTrace();
149: } catch (InstantiationException e) {
150:
151: e.printStackTrace();
152: } catch (IllegalAccessException e) {
153:
154: e.printStackTrace();
155: }
156:
157: return object;
158: }
159: }
160: }
|