Clover coverage report - JFox Service-Oriented Application Framework - 1.0-RC1 Coverage timestamp: 星期五 八月 19 2005 13:21:55 CST FRAMES NO FRAMES |
file stats: | LOC: | 265 |  | Methods: | 9
| NCLOC: | 156 |  | 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
ServiceLoader.java | 68.2% | 77.3% | 88.9% | 76.3% |

1 |  |
     /**
2 |  |
      * @(#)ServiceLoader.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 java.io.IOException;
28 |  |
     import java.io.InputStream;
29 |  |
     import java.io.InputStreamReader;
30 |  |
     import java.util.ArrayList;
31 |  |
     import java.util.Collection;
32 |  |
     import java.util.Iterator;
33 |  |
     import java.util.LinkedList;
34 |  |
35 |  |
     import org.apache.commons.logging.Log;
36 |  |
     import org.apache.commons.logging.LogFactory;
37 |  |
     import org.exolab.castor.xml.MarshalException;
38 |  |
     import org.exolab.castor.xml.ValidationException;
39 |  |
     import org.huihoo.jfox.soaf.exception.ServiceConfigurationException;
40 |  |
     import org.huihoo.jfox.soaf.schema.config.Configuration;
41 |  |
     import org.huihoo.jfox.soaf.schema.config.ConfigurationEntry;
42 |  |
     import org.huihoo.jfox.soaf.schema.config.Interceptor;
43 |  |
     import org.huihoo.jfox.soaf.schema.service.Service;
44 |  |
     import org.huihoo.jfox.soaf.schema.service.ServiceEntry;
45 |  |
     import org.huihoo.jfox.soaf.util.resource.ResourceHelper;
46 |  |
47 |  |
     /**
48 |  |
      * <p>
49 |  |
      * Service Configration load proxy.
50 |  |
      * </p>
51 |  |
      *
52 |  |
      * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
53 |  |
      * @version $Revision: 1.3 $ $Date: 2005/05/22 06:46:48 $
54 |  |
      * @version Revision: 1.0
55 |  |
      */
56 |  |
57 |  |
     public class ServiceLoader {
58 |  |
59 |  |
         private ServiceContainer serviceContainer = ServiceContainer.getInstance();
60 |  |
61 |  |
         private static ServiceLoader serviceLoader;
62 |  |
63 |  |
         private boolean serviceLoaded = false;
64 |  |
65 |  |
         private Collection serviceSchemaColl = new LinkedList();
66 |  |
67 |  |
         private Collection interceptorColl = new ArrayList();
68 |  |
69 |  |
         private Collection serviceEntryColl = new ArrayList();
70 |  |
71 |  |
         private InterceptorManager interceptorManager = new InterceptorManagerImpl();
72 |  |
73 |  |
         private final Log logger = LogFactory.getLog(getClass());
74 |  |
75 |  |
         /**
76 |  |
          * Singleton instance
77 |  |
          *
78 |  |
          * @return
79 |  |
          */
80 | 8 |
         public static ServiceLoader getInstance() {
81 | 8 |
             if (serviceLoader == null) {
82 | 4 |
                 serviceLoader = new ServiceLoader();
83 |  |
             }
84 | 8 |
             return serviceLoader;
85 |  |
         }
86 |  |
87 |  |
         /**
88 |  |
          * Init services.
89 |  |
          *
90 |  |
          * @param configFile
91 |  |
          * @throws ServiceConfigurationException
92 |  |
          */
93 | 4 |
         public synchronized void initService(String configFile)
94 |  |
                 throws ServiceConfigurationException {
95 | 4 |
             if (!isServiceLoaded()) {
96 | 4 |
                 try {
97 | 4 |
                     InputStream is = ResourceHelper.getResourceAsStream(configFile);
98 | 4 |
                     loadConfig(loadServiceConfigration(is));
99 |  |
                 } catch (Throwable e) {
100 | 0 |
                     throw new ServiceConfigurationException(
101 |  |
                             "Failed to init serivce config file jfoxsoaf-config.xml "
102 |  |
                                     + e, e);
103 |  |
                 }
104 | 4 |
                 serviceContainer.start();
105 | 4 |
                 serviceLoaded = true;
106 |  |
             }
107 |  |
         }
108 |  |
109 |  |
         /**
110 |  |
          * Init services from input stream.
111 |  |
          *
112 |  |
          * @param is
113 |  |
          * @throws ServiceConfigurationException
114 |  |
          */
115 | 0 |
         public synchronized void initService(InputStream is)
116 |  |
                 throws ServiceConfigurationException {
117 | 0 |
             if (!isServiceLoaded()) {
118 | 0 |
                 try {
119 | 0 |
                     loadConfig(loadServiceConfigration(is));
120 |  |
                 } catch (Throwable e) {
121 | 0 |
                     e.printStackTrace();
122 | 0 |
                     throw new ServiceConfigurationException(
123 |  |
                             "Failed to init serivce config file jfoxsoaf-config.xml "
124 |  |
                                     + e, e);
125 |  |
126 |  |
                 }
127 | 0 |
                 serviceContainer.start();
128 | 0 |
                 serviceLoaded = true;
129 |  |
             }
130 |  |
         }
131 |  |
132 |  |
         /**
133 |  |
          * Load service configration file.
134 |  |
          *
135 |  |
          * @return Service
136 |  |
          * @throws Throwable
137 |  |
          */
138 | 4 |
         private Configuration loadServiceConfigration(InputStream is)
139 |  |
                 throws ServiceConfigurationException {
140 | 4 |
             InputStreamReader isr = null;
141 | 4 |
             Configuration config = null;
142 | 4 |
             try {
143 | 4 |
                 isr = new InputStreamReader(is);
144 | 4 |
                 config = Configuration.unmarshal(isr);
145 |  |
             } catch (MarshalException e) {
146 | 0 |
                 throw new ServiceConfigurationException(
147 |  |
                         "Failed to marshal service configuration " + e, e);
148 |  |
             } catch (ValidationException e) {
149 | 0 |
                 throw new ServiceConfigurationException(
150 |  |
                         "Failed to validate service configuration " + e, e);
151 |  |
             } finally {
152 | 4 |
                 if (is != null) {
153 | 4 |
                     try {
154 | 4 |
                         is.close();
155 |  |
                     } catch (IOException e) {
156 | 0 |
                         e.printStackTrace();
157 |  |
                     }
158 |  |
                 }
159 |  |
160 | 4 |
                 if (isr != null) {
161 | 4 |
                     try {
162 | 4 |
                         isr.close();
163 |  |
                     } catch (IOException e) {
164 | 0 |
                         e.printStackTrace();
165 |  |
                     }
166 |  |
                 }
167 |  |
             }
168 | 4 |
             return config;
169 |  |
         }
170 |  |
171 |  |
         /**
172 |  |
          * Load all configuration service.
173 |  |
          *
174 |  |
          * @param service
175 |  |
          */
176 | 4 |
         public void loadConfig(Configuration config) throws Throwable {
177 | 4 |
             processConfig(config);
178 | 4 |
             loadService(serviceSchemaColl);
179 | 4 |
             interceptorManager
180 |  |
                     .processInterceptor(interceptorColl, serviceEntryColl);
181 |  |
         }
182 |  |
183 |  |
         /**
184 |  |
          * Load service schema.
185 |  |
          *
186 |  |
          * @param serviceSchemaColl
187 |  |
          * @throws ServiceConfigurationException
188 |  |
          */
189 | 4 |
         private void loadService(Collection serviceSchemaColl)
190 |  |
                 throws ServiceConfigurationException {
191 | 4 |
             InputStream is = null;
192 | 4 |
             InputStreamReader isr = null;
193 | 4 |
             Iterator iter = serviceSchemaColl.iterator();
194 | 4 |
             Service service;
195 | 4 |
             try {
196 | 4 |
                 while (iter.hasNext()) {
197 | 16 |
                     is = ResourceHelper.getResourceAsStream((String) iter.next());
198 | 16 |
                     isr = new InputStreamReader(is);
199 | 16 |
                     service = Service.unmarshal(isr);
200 | 16 |
                     processService(service);
201 |  |
                 }
202 |  |
             } catch (Exception e) {
203 | 0 |
                 throw new ServiceConfigurationException("Failed to load service "
204 |  |
                         + e, e);
205 |  |
             } finally {
206 | 4 |
                 if (is != null) {
207 | 4 |
                     try {
208 | 4 |
                         is.close();
209 |  |
                     } catch (IOException e1) {
210 | 0 |
                         e1.printStackTrace();
211 |  |
                     }
212 |  |
213 |  |
                 }
214 |  |
215 | 4 |
                 if (isr != null) {
216 | 4 |
                     try {
217 | 4 |
                         isr.close();
218 |  |
                     } catch (IOException e1) {
219 | 0 |
                         e1.printStackTrace();
220 |  |
                     }
221 |  |
                 }
222 |  |
             }
223 |  |
         }
224 |  |
225 |  |
         /**
226 |  |
          * Add service to service entry collection.
227 |  |
          *
228 |  |
          * @param service
229 |  |
          */
230 | 16 |
         private void processService(Service service) {
231 | 16 |
             ServiceEntry[] serviceEntry = service.getServiceEntry();
232 | 16 |
             for (int i = 0; i < serviceEntry.length; i++) {
233 | 28 |
                 serviceEntryColl.add(serviceEntry[i]);
234 |  |
             }
235 |  |
         }
236 |  |
237 |  |
         /**
238 |  |
          * Is service loaded?
239 |  |
          *
240 |  |
          * @return boolean
241 |  |
          */
242 | 12 |
         public boolean isServiceLoaded() {
243 | 12 |
             return this.serviceLoaded;
244 |  |
         }
245 |  |
246 |  |
         /**
247 |  |
          * Add service schema and interceptor to collection.
248 |  |
          *
249 |  |
          * @param config
250 |  |
          */
251 | 4 |
         private void processConfig(Configuration config) {
252 | 4 |
             ConfigurationEntry[] configEntry = config.getServiceConfiguration()
253 |  |
                     .getConfigurationEntry();
254 | 4 |
             Interceptor[] interceptor = config.getSystemInterceptor()
255 |  |
                     .getInterceptor();
256 | 4 |
             for (int i = 0; i < configEntry.length; i++) {
257 | 16 |
                 logger.info("Load service schema : " + configEntry[i].getValue());
258 | 16 |
                 serviceSchemaColl.add(configEntry[i].getValue());
259 |  |
             }
260 |  |
261 | 4 |
             for (int i = 0; i < interceptor.length; i++) {
262 | 8 |
                 interceptorColl.add(interceptor[i].getValue());
263 |  |
             }
264 |  |
         }
265 |  |
     }