KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > facade > ServiceFacadeRegistry


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.facade;
19
20 import java.util.Hashtable JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.columba.api.exception.ServiceNotFoundException;
24
25 /**
26  * Service registry and locator. Upper layers can register a service which can
27  * be used by others. A service is registered using its full interface name
28  * including package and the name of the implementation.
29  * <p>
30  * <code>ServiceManager</code> uses reflection to instanciate the
31  * implementation.
32  * <p>
33  * This registry should be only used to enable different components to interact
34  * with each other. For example: Mail component makes use of the addressbook
35  * component
36  *
37  * @author fdietz
38  */

39 public class ServiceFacadeRegistry {
40
41     private static ServiceFacadeRegistry instance = new ServiceFacadeRegistry();
42
43     private Map JavaDoc<Class JavaDoc, Service> map = new Hashtable JavaDoc<Class JavaDoc, Service>();
44
45     private ServiceFacadeRegistry() {
46     }
47
48     public static ServiceFacadeRegistry getInstance() {
49         return instance;
50     }
51
52     public void register(Class JavaDoc serviceInterface, Object JavaDoc serviceInstance) {
53         Service service = new Service(serviceInterface, serviceInstance);
54
55         map.put(serviceInterface, service);
56     }
57
58     public void unregister(Class JavaDoc serviceInterface) {
59         map.remove(serviceInterface);
60     }
61
62     public Object JavaDoc getService(Class JavaDoc serviceInterface)
63             throws ServiceNotFoundException {
64         Object JavaDoc o = null;
65         Service service = null;
66
67         // check if service is registered
68
if (map.containsKey(serviceInterface)) {
69             service = map.get(serviceInterface);
70
71             // retrieve service instance
72
if (service != null)
73                 o = service.getServiceInstance();
74         }
75
76         if (o == null)
77             throw new ServiceNotFoundException(serviceInterface);
78
79         return o;
80     }
81
82     /**
83      * A service is described with its service name, which is usually the name
84      * of the interface.
85      * <p>
86      * Keeps a reference to the service instanciation.
87      *
88      * @author Frederik Dietz
89      */

90     private class Service {
91         private Class JavaDoc serviceInterface;
92
93         private String JavaDoc implementationName;
94
95         private Object JavaDoc serviceInstance;
96
97         Service(Class JavaDoc serviceInterface, String JavaDoc implementationName) {
98             this.serviceInterface = serviceInterface;
99             this.implementationName = implementationName;
100         }
101
102         public Service(Class JavaDoc serviceInterface, Object JavaDoc serviceInstance) {
103             this.serviceInterface = serviceInterface;
104
105             this.serviceInstance = serviceInstance;
106         }
107
108         /**
109          * @return Returns the serviceInstance.
110          */

111         public Object JavaDoc 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 JavaDoc 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 JavaDoc loadInstance(String JavaDoc className) {
138             Object JavaDoc object = null;
139
140             try {
141                 Class JavaDoc clazz = this.getClass().getClassLoader().loadClass(
142                         className);
143
144                 object = clazz.newInstance();
145
146             } catch (ClassNotFoundException JavaDoc e) {
147
148                 e.printStackTrace();
149             } catch (InstantiationException JavaDoc e) {
150
151                 e.printStackTrace();
152             } catch (IllegalAccessException JavaDoc e) {
153
154                 e.printStackTrace();
155             }
156
157             return object;
158         }
159     }
160 }
Popular Tags