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.16 $ $Date: 2006/02/15 08:45:45 $
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          return serviceFactory;
58      }
59  
60      /***
61       * Default Constructor.
62       */
63      public ServiceFactory() {
64      }
65  
66      /***
67       * Lookup service by serviceKey
68       * 
69       * @param componentKey
70       * @return Service Object
71       */
72      public synchronized Object getService(Object serviceKey)
73              throws PicoException, ServiceConfigurationException {
74          return serviceContainer.getService(serviceKey);
75      }
76  
77      /***
78       * Lookup service by serviceType
79       * 
80       * @param componentKey
81       * @return Service Object
82       */
83      public synchronized Object getService(Class serviceType)
84              throws PicoException {
85          return serviceContainer.getServiceOfType(serviceType);
86      }
87  
88      /***
89       * Unregister service.
90       * 
91       * @param service
92       */
93      public void unRegisterService(Object service) {
94          serviceContainer.unregisterComponent(service);
95      }
96  
97      /***
98       * Unregister service instance.
99       * 
100      * @param serviceInstance
101      */
102     public void unRegisterServiceInstance(Object serviceInstance) {
103         serviceContainer.unregisterComponentByInstance(serviceInstance);
104     }
105 
106     /***
107      * Register service implementation.
108      * 
109      * @param service
110      */
111     public void registerService(Class serviceImpl)
112             throws PicoRegistrationException {
113         serviceContainer.registerServiceImplementation(serviceImpl);
114     }
115 
116     /***
117      * Register service with service key.
118      * 
119      * @param serviceKey
120      * @param serviceImpl
121      */
122     public void registerService(Object serviceKey, Class serviceImpl)
123             throws PicoRegistrationException {
124         serviceContainer.registerServiceImplementation(serviceKey, serviceImpl);
125     }
126 
127     /***
128      * Register service instance.
129      * 
130      * @param serviceInstance
131      */
132     public void registerServiceInstance(Object serviceInstance)
133             throws PicoRegistrationException {
134         serviceContainer.registerServiceInstance(serviceInstance);
135     }
136 
137 }