Clover coverage report - JFox Service-Oriented Application Framework - 1.0-M2 Coverage timestamp: 星期四 十一月 25 2004 17:14:11 PST FRAMES NO FRAMES |
file stats: | LOC: | 206 |  | Methods: | 12
| NCLOC: | 97 |  | Classes: | 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
Source file | Conditionals | Statements | Methods | TOTAL
ServiceFactory.java | 75% | 67.6% | 58.3% | 66.7% |

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 | 3 |
         public static synchronized ServiceFactory getInstance() {
56 | 3 |
             if (serviceFactory == null) {
57 | 2 |
                 serviceFactory = new ServiceFactory();
58 |  |
             }
59 |  |
60 | 3 |
             if (!serviceFactory.serviceLoadProxy.isServiceLoaded()) {
61 | 2 |
                 serviceFactory.constructServices();
62 |  |
             }
63 | 3 |
             return serviceFactory;
64 |  |
         }
65 |  |
66 |  |
         /**
67 |  |
          * Default Constructor.
68 |  |
          */
69 | 2 |
         public ServiceFactory() {
70 |  |
71 | 2 |
             serviceLoadProxy = new ServiceLoadProxy();
72 |  |
         }
73 |  |
74 |  |
         /**
75 |  |
          * Lookup service by serviceKey
76 |  |
          *
77 |  |
          * @param componentKey
78 |  |
          * @return Service Object
79 |  |
          */
80 | 1 |
         public synchronized Object getService(Object serviceKey)
81 |  |
                 throws PicoException, ServiceConfigurationException {
82 | 1 |
             return serviceContainer.getService(serviceKey);
83 |  |
         }
84 |  |
85 |  |
         /**
86 |  |
          * Lookup service by serviceType
87 |  |
          *
88 |  |
          * @param componentKey
89 |  |
          * @return Service Object
90 |  |
          */
91 | 8 |
         public synchronized Object getService(Class serviceType)
92 |  |
                 throws PicoException {
93 | 8 |
             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 | 2 |
         private void constructServices() throws ServiceConfigurationException {
102 | 2 |
             try {
103 | 2 |
                 serviceLoadProxy.loadConfig(loadServiceConfigration());
104 |  |
             } catch (Throwable e) {
105 | 0 |
                 e.printStackTrace();
106 | 0 |
                 throw new ServiceConfigurationException(e);
107 |  |
             }
108 |  |
         }
109 |  |
110 |  |
         /**
111 |  |
          * Load service configration file.
112 |  |
          *
113 |  |
          * @return Service
114 |  |
          * @throws Throwable
115 |  |
          */
116 | 2 |
         private Configuration loadServiceConfigration()
117 |  |
                 throws ServiceConfigurationException {
118 | 2 |
             Configuration config = null;
119 | 2 |
             try {
120 | 2 |
                 is = ResourceHelper.getResourceAsStream(Globals.JFOXSOAF_CONFIG);
121 | 2 |
                 isr = new InputStreamReader(is);
122 | 2 |
                 config = Configuration.unmarshal(isr);
123 |  |
             } catch (Exception e) {
124 | 0 |
                 e.printStackTrace();
125 | 0 |
                 throw new ServiceConfigurationException(e);
126 |  |
             } finally {
127 | 2 |
                 if (is != null) {
128 | 2 |
                     try {
129 | 2 |
                         is.close();
130 |  |
                     } catch (IOException e) {
131 | 0 |
                         e.printStackTrace();
132 |  |
                     }
133 |  |
                 }
134 |  |
135 | 2 |
                 if (isr != null) {
136 | 2 |
                     try {
137 | 2 |
                         isr.close();
138 |  |
                     } catch (IOException e) {
139 | 0 |
                         e.printStackTrace();
140 |  |
                     }
141 |  |
                 }
142 |  |
143 |  |
             }
144 |  |
145 | 2 |
             return config;
146 |  |
         }
147 |  |
148 |  |
         /**
149 |  |
          * Unregister service.
150 |  |
          *
151 |  |
          * @param service
152 |  |
          */
153 | 1 |
         public void unRegisterService(Object service) {
154 | 1 |
             serviceContainer.unregisterComponent(service);
155 |  |
         }
156 |  |
157 |  |
         /**
158 |  |
          * Unregister service instance.
159 |  |
          *
160 |  |
          * @param serviceInstance
161 |  |
          */
162 | 0 |
         public void unRegisterServiceInstance(Object serviceInstance) {
163 | 0 |
             serviceContainer.unregisterComponentByInstance(serviceInstance);
164 |  |
         }
165 |  |
166 |  |
         /**
167 |  |
          * Register service implementation.
168 |  |
          *
169 |  |
          * @param service
170 |  |
          */
171 | 0 |
         public void registerService(Class serviceImpl)
172 |  |
                 throws PicoRegistrationException {
173 | 0 |
             serviceContainer.registerServiceImplementation(serviceImpl);
174 |  |
         }
175 |  |
176 |  |
         /**
177 |  |
          * Register service with service key.
178 |  |
          *
179 |  |
          * @param serviceKey
180 |  |
          * @param serviceImpl
181 |  |
          */
182 | 0 |
         public void registerService(Object serviceKey, Class serviceImpl)
183 |  |
                 throws PicoRegistrationException {
184 | 0 |
             serviceContainer.registerServiceImplementation(serviceKey, serviceImpl);
185 |  |
         }
186 |  |
187 |  |
         /**
188 |  |
          * Register service instance.
189 |  |
          *
190 |  |
          * @param serviceInstance
191 |  |
          */
192 | 0 |
         public void registerServiceInstance(Object serviceInstance)
193 |  |
                 throws PicoRegistrationException {
194 | 0 |
             serviceContainer.registerServiceInstance(serviceInstance);
195 |  |
         }
196 |  |
197 |  |
         /**
198 |  |
          * Return MBeanServer
199 |  |
          *
200 |  |
          * @return
201 |  |
          */
202 | 0 |
         public MBeanServer getMBeanServer() {
203 | 0 |
             return manageableServiceContainer.getMBeanServer();
204 |  |
         }
205 |  |
206 |  |
     }