KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > services > ServiceFactory


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jmanage.core.services;
17
18 import org.jmanage.core.remote.client.HttpServiceProxy;
19
20 import java.util.Map JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.lang.reflect.Proxy JavaDoc;
23
24 /**
25  * ServiceFactory is used to create instance of Service interfaces. This
26  * factory needs to be initialized by calling the <code>init()</code> method.
27  * The factory runs in local or remote mode. When in local mode, local
28  * Service objects are created. When running in remote mode, an instance
29  * of HttpServiceProxy is returned, which uses java serialization over HTTP
30  * to talk to the jmanage web server.
31  * <p>
32  * When creating the local instance of Service interfaces, ServiceFactory
33  * determines the implementation class name by appending "Impl" to the
34  * Service interface name..
35  *
36  * @see org.jmanage.core.remote.client.HttpServiceProxy
37  * date: Jan 18, 2005
38  * @author Rakesh Kalra
39  */

40 public class ServiceFactory {
41
42     private static Map JavaDoc serviceClassToObjectMap = new Hashtable JavaDoc();
43
44     public static final Integer JavaDoc MODE_LOCAL = new Integer JavaDoc(0);
45     public static final Integer JavaDoc MODE_REMOTE = new Integer JavaDoc(1);
46
47     private static Integer JavaDoc mode;
48
49     /**
50      * Initializes the ServiceFactory. This method can only be called once.
51      * Initializing the factory in local mode, will result in local service
52      * method calls. Initializing the factory in remove mode, will result in
53      * remote service method calls.
54      *
55      * @param mode the mode this factory should run in: local or remote
56      */

57     public static void init(Integer JavaDoc mode){
58         assert ServiceFactory.mode == null :
59                 "ServiceFactory already initialized";
60         assert mode.equals(MODE_LOCAL) || mode.equals(MODE_REMOTE) :
61                 "Invalid mode:" + mode;
62         ServiceFactory.mode = mode;
63     }
64
65     /**
66      * Gets the service object based on the factory mode.
67      *
68      * @param serviceInterface the Service interface.
69      * @return instance of service interface
70      */

71     public static Object JavaDoc getService(Class JavaDoc serviceInterface){
72         Object JavaDoc service = serviceClassToObjectMap.get(serviceInterface);
73         if(service == null){
74             if(mode.equals(MODE_LOCAL)){
75                 service = createService(serviceInterface);
76             }else{
77                 service = createServiceProxy(serviceInterface);
78             }
79             serviceClassToObjectMap.put(serviceInterface, service);
80         }
81         return service;
82     }
83
84     public static ConfigurationService getConfigurationService() {
85         return (ConfigurationService)getService(ConfigurationService.class);
86     }
87
88     public static AuthService getAuthService() {
89         return (AuthService)getService(AuthService.class);
90     }
91
92     public static MBeanService getMBeanService() {
93         return (MBeanService)getService(MBeanService.class);
94     }
95
96     public static AlertService getAlertService() {
97         return (AlertService)getService(AlertService.class);
98     }
99
100     private static Object JavaDoc createService(Class JavaDoc serviceClass){
101         final String JavaDoc implClassName = serviceClass.getName() + "Impl";
102         try {
103             Class JavaDoc clazz = Class.forName(implClassName);
104             return clazz.newInstance();
105         } catch (ClassNotFoundException JavaDoc e) {
106             throw new RuntimeException JavaDoc("Service impl. not found:" +
107                     implClassName, e);
108         } catch (InstantiationException JavaDoc e) {
109             throw new RuntimeException JavaDoc("Service impl.:" +
110                     implClassName, e);
111         } catch (IllegalAccessException JavaDoc e) {
112             throw new RuntimeException JavaDoc("Service impl.:" +
113                     implClassName, e);
114         }
115     }
116
117     private static Object JavaDoc createServiceProxy(Class JavaDoc serviceClass){
118         return Proxy.newProxyInstance
119                 (serviceClass.getClassLoader(),
120                         new Class JavaDoc[]{serviceClass},
121                         new HttpServiceProxy());
122     }
123 }
124
Popular Tags