View Javadoc

1   /***
2    * JFoxSOAF, Service-Oriented Application Framework
3    * 
4    * Copyright (C) www.huihoo.org
5    * 
6    * Distributable under GNU LGPL For more information, please visit:
7    * http://www.huihoo.org/jfox/jfoxsoaf
8    */
9   
10  package org.huihoo.jfox.soaf.container;
11  
12  import java.io.IOException;
13  import java.io.InputStream;
14  import java.io.InputStreamReader;
15  
16  import javax.management.MBeanServer;
17  
18  import org.huihoo.jfox.soaf.exception.ServiceConfigurationException;
19  import org.huihoo.jfox.soaf.schema.config.Configuration;
20  import org.huihoo.jfox.soaf.util.Globals;
21  import org.huihoo.jfox.soaf.util.resource.ResourceHelper;
22  import org.picocontainer.PicoException;
23  import org.picocontainer.PicoRegistrationException;
24  
25  /***
26   * <p>
27   * The ServiceFactory is a singleton wrapper on the service container instance.
28   * </p>
29   * 
30   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
31   * @version $Revision: 1.8 $ $Date: 2004/10/28 05:12:04 $
32   * @version Revision: 1.0
33   */
34  
35  public class ServiceFactory {
36  
37      private static ServiceFactory serviceFactory;
38  
39      private ClassLoader cl = Thread.currentThread().getContextClassLoader();
40  
41      private InputStream is = null;
42  
43      private InputStreamReader isr = null;
44  
45      private ServiceLoadProxy serviceLoadProxy;
46  
47      private ServiceContainer serviceContainer = ServiceContainer.getInstance();
48  
49      private ManageableServiceContainer manageableServiceContainer = ManageableServiceContainer
50              .getInstance();
51  
52      /***
53       * Singleton instance.
54       */
55      public static synchronized ServiceFactory getInstance() {
56          if (serviceFactory == null) {
57              serviceFactory = new ServiceFactory();
58          }
59  
60          if (!serviceFactory.serviceLoadProxy.isServiceLoaded()) {
61              serviceFactory.constructServices();
62          }
63          return serviceFactory;
64      }
65  
66      /***
67       * Default Constructor.
68       */
69      public ServiceFactory() {
70  
71          serviceLoadProxy = new ServiceLoadProxy();
72      }
73  
74      /***
75       * Lookup service by serviceKey
76       * 
77       * @param componentKey
78       * @return Service Object
79       */
80      public synchronized Object getService(Object serviceKey)
81              throws PicoException, ServiceConfigurationException {
82          return serviceContainer.getService(serviceKey);
83      }
84  
85      /***
86       * Lookup service by serviceType
87       * 
88       * @param componentKey
89       * @return Service Object
90       */
91      public synchronized Object getService(Class serviceType)
92              throws PicoException {
93          return serviceContainer.getServiceOfType(serviceType);
94      }
95  
96      /***
97       * Construct all default services from service configration files.
98       * 
99       * @throws org.huihoo.jfox.soaf.exception.ServiceConfigurationException
100      */
101     private void constructServices() throws ServiceConfigurationException {
102         try {
103             serviceLoadProxy.loadConfig(loadServiceConfigration());
104         } catch (Throwable e) {
105             e.printStackTrace();
106             throw new ServiceConfigurationException(e);
107         }
108     }
109 
110     /***
111      * Load service configration file.
112      * 
113      * @return Service
114      * @throws Throwable
115      */
116     private Configuration loadServiceConfigration()
117             throws ServiceConfigurationException {
118         Configuration config = null;
119         try {
120             is = ResourceHelper.getResourceAsStream(Globals.JFOXSOAF_CONFIG);
121             isr = new InputStreamReader(is);
122             config = Configuration.unmarshal(isr);
123         } catch (Exception e) {
124             e.printStackTrace();
125             throw new ServiceConfigurationException(e);
126         } finally {
127             if (is != null) {
128                 try {
129                     is.close();
130                 } catch (IOException e) {
131                     e.printStackTrace();
132                 }
133             }
134 
135             if (isr != null) {
136                 try {
137                     isr.close();
138                 } catch (IOException e) {
139                     e.printStackTrace();
140                 }
141             }
142 
143         }
144 
145         return config;
146     }
147 
148     /***
149      * Unregister service.
150      * 
151      * @param service
152      */
153     public void unRegisterService(Object service) {
154         serviceContainer.unregisterComponent(service);
155     }
156 
157     /***
158      * Unregister service instance.
159      * 
160      * @param serviceInstance
161      */
162     public void unRegisterServiceInstance(Object serviceInstance) {
163         serviceContainer.unregisterComponentByInstance(serviceInstance);
164     }
165 
166     /***
167      * Register service implementation.
168      * 
169      * @param service
170      */
171     public void registerService(Class serviceImpl)
172             throws PicoRegistrationException {
173         serviceContainer.registerServiceImplementation(serviceImpl);
174     }
175 
176     /***
177      * Register service with service key.
178      * 
179      * @param serviceKey
180      * @param serviceImpl
181      */
182     public void registerService(Object serviceKey, Class serviceImpl)
183             throws PicoRegistrationException {
184         serviceContainer.registerServiceImplementation(serviceKey, serviceImpl);
185     }
186 
187     /***
188      * Register service instance.
189      * 
190      * @param serviceInstance
191      */
192     public void registerServiceInstance(Object serviceInstance)
193             throws PicoRegistrationException {
194         serviceContainer.registerServiceInstance(serviceInstance);
195     }
196 
197     /***
198      * Return MBeanServer
199      * 
200      * @return
201      */
202     public MBeanServer getMBeanServer() {
203         return manageableServiceContainer.getMBeanServer();
204     }
205 
206 }