View Javadoc

1   /***
2    * @(#)ServiceFactory.java
3    * 
4    * JFoxSOAF, Service-Oriented Application Framework
5    * 
6    * Copyright(c) JFoxSOAF Team
7    * 
8    * Licensed under the GNU LGPL, Version 2.1 (the "License"); 
9    * you may not use this file except in compliance with the License. 
10   * You may obtain a copy of the License at  
11   * 
12   * http://www.gnu.org/copyleft/lesser.html
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, 
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
17   * See the License for the specific language governing permissions and 
18   * limitations under the License. 
19   * 
20   * For more information, please visit:
21   * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22   * http://www.huihoo.org/jfox/jfoxsoaf
23   */
24  
25  package org.huihoo.jfox.soaf.container;
26  
27  import org.huihoo.jfox.soaf.exception.ServiceConfigurationException;
28  import org.picocontainer.PicoException;
29  import org.picocontainer.PicoRegistrationException;
30  
31  /***
32   * <p>
33   * The ServiceFactory is a singleton wrapper on the service container instance.
34   * </p>
35   * 
36   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
37   * @version $Revision: 1.15 $ $Date: 2005/05/22 06:46:49 $
38   * @version Revision: 1.0
39   */
40  
41  public class ServiceFactory {
42  
43      private static ServiceFactory serviceFactory;    
44  
45      private ServiceContainer serviceContainer = ServiceContainer.getInstance();
46  
47      private ManageableServiceContainer manageableServiceContainer = ManageableServiceContainer
48              .getInstance();
49  
50      /***
51       * Singleton instance.
52       */
53      public static synchronized ServiceFactory getInstance() {
54          if (serviceFactory == null) {
55              serviceFactory = new ServiceFactory();
56          }
57  
58          //        if (!serviceFactory.serviceLoadProxy.isServiceLoaded()) {
59          //            serviceFactory.constructServices();
60          //        }
61          return serviceFactory;
62      }
63  
64      /***
65       * Default Constructor.
66       */
67      public ServiceFactory() {
68      }
69  
70      /***
71       * Lookup service by serviceKey
72       * 
73       * @param componentKey
74       * @return Service Object
75       */
76      public synchronized Object getService(Object serviceKey)
77              throws PicoException, ServiceConfigurationException {
78          return serviceContainer.getService(serviceKey);
79      }
80  
81      /***
82       * Lookup service by serviceType
83       * 
84       * @param componentKey
85       * @return Service Object
86       */
87      public synchronized Object getService(Class serviceType)
88              throws PicoException {
89          return serviceContainer.getServiceOfType(serviceType);
90      }
91  
92      /***
93       * Unregister service.
94       * 
95       * @param service
96       */
97      public void unRegisterService(Object service) {
98          serviceContainer.unregisterComponent(service);
99      }
100 
101     /***
102      * Unregister service instance.
103      * 
104      * @param serviceInstance
105      */
106     public void unRegisterServiceInstance(Object serviceInstance) {
107         serviceContainer.unregisterComponentByInstance(serviceInstance);
108     }
109 
110     /***
111      * Register service implementation.
112      * 
113      * @param service
114      */
115     public void registerService(Class serviceImpl)
116             throws PicoRegistrationException {
117         serviceContainer.registerServiceImplementation(serviceImpl);
118     }
119 
120     /***
121      * Register service with service key.
122      * 
123      * @param serviceKey
124      * @param serviceImpl
125      */
126     public void registerService(Object serviceKey, Class serviceImpl)
127             throws PicoRegistrationException {
128         serviceContainer.registerServiceImplementation(serviceKey, serviceImpl);
129     }
130 
131     /***
132      * Register service instance.
133      * 
134      * @param serviceInstance
135      */
136     public void registerServiceInstance(Object serviceInstance)
137             throws PicoRegistrationException {
138         serviceContainer.registerServiceInstance(serviceInstance);
139     }
140 
141 }