Clover coverage report - JFox Service-Oriented Application Framework - 1.0
Coverage timestamp: 星期一 八月 21 2006 22:56:01 CST
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%
coverage coverage
 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.4 $ $Date: 2006/02/15 08:45:45 $
 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  15
     public static ServiceLoader getInstance() {
 81  15
         if (serviceLoader == null) {
 82  9
             serviceLoader = new ServiceLoader();
 83   
         }
 84  15
         return serviceLoader;
 85   
     }
 86   
 
 87   
     /**
 88   
      * Init services.
 89   
      * 
 90   
      * @param configFile
 91   
      * @throws ServiceConfigurationException
 92   
      */
 93  9
     public synchronized void initService(String configFile)
 94   
             throws ServiceConfigurationException {
 95  9
         if (!isServiceLoaded()) {
 96  9
             try {
 97  9
                 InputStream is = ResourceHelper.getResourceAsStream(configFile);
 98  9
                 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  9
             serviceContainer.start();
 105  9
             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  9
     private Configuration loadServiceConfigration(InputStream is)
 139   
             throws ServiceConfigurationException {
 140  9
         InputStreamReader isr = null;
 141  9
         Configuration config = null;
 142  9
         try {
 143  9
             isr = new InputStreamReader(is);
 144  9
             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  9
             if (is != null) {
 153  9
                 try {
 154  9
                     is.close();
 155   
                 } catch (IOException e) {
 156  0
                     e.printStackTrace();
 157   
                 }
 158   
             }
 159   
 
 160  9
             if (isr != null) {
 161  9
                 try {
 162  9
                     isr.close();
 163   
                 } catch (IOException e) {
 164  0
                     e.printStackTrace();
 165   
                 }
 166   
             }
 167   
         }
 168  9
         return config;
 169   
     }
 170   
 
 171   
     /**
 172   
      * Load all configuration service.
 173   
      * 
 174   
      * @param service
 175   
      */
 176  9
     public void loadConfig(Configuration config) throws Throwable {
 177  9
         processConfig(config);
 178  9
         loadService(serviceSchemaColl);
 179  9
         interceptorManager
 180   
                 .processInterceptor(interceptorColl, serviceEntryColl);
 181   
     }
 182   
 
 183   
     /**
 184   
      * Load service schema.
 185   
      * 
 186   
      * @param serviceSchemaColl
 187   
      * @throws ServiceConfigurationException
 188   
      */
 189  9
     private void loadService(Collection serviceSchemaColl)
 190   
             throws ServiceConfigurationException {
 191  9
         InputStream is = null;
 192  9
         InputStreamReader isr = null;
 193  9
         Iterator iter = serviceSchemaColl.iterator();
 194  9
         Service service;
 195  9
         try {
 196  9
             while (iter.hasNext()) {
 197  72
                 is = ResourceHelper.getResourceAsStream((String) iter.next());
 198  72
                 isr = new InputStreamReader(is);
 199  72
                 service = Service.unmarshal(isr);
 200  72
                 processService(service);
 201   
             }
 202   
         } catch (Exception e) {
 203  0
             throw new ServiceConfigurationException("Failed to load service "
 204   
                     + e, e);
 205   
         } finally {
 206  9
             if (is != null) {
 207  9
                 try {
 208  9
                     is.close();
 209   
                 } catch (IOException e1) {
 210  0
                     e1.printStackTrace();
 211   
                 }
 212   
 
 213   
             }
 214   
 
 215  9
             if (isr != null) {
 216  9
                 try {
 217  9
                     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  72
     private void processService(Service service) {
 231  72
         ServiceEntry[] serviceEntry = service.getServiceEntry();
 232  72
         for (int i = 0; i < serviceEntry.length; i++) {
 233  126
             serviceEntryColl.add(serviceEntry[i]);
 234   
         }
 235   
     }
 236   
 
 237   
     /**
 238   
      * Is service loaded?
 239   
      * 
 240   
      * @return boolean
 241   
      */
 242  24
     public boolean isServiceLoaded() {
 243  24
         return this.serviceLoaded;
 244   
     }
 245   
 
 246   
     /**
 247   
      * Add service schema and interceptor to collection.
 248   
      * 
 249   
      * @param config
 250   
      */
 251  9
     private void processConfig(Configuration config) {
 252  9
         ConfigurationEntry[] configEntry = config.getServiceConfiguration()
 253   
                 .getConfigurationEntry();
 254  9
         Interceptor[] interceptor = config.getSystemInterceptor()
 255   
                 .getInterceptor();
 256  9
         for (int i = 0; i < configEntry.length; i++) {
 257  72
             logger.info("Load service schema : " + configEntry[i].getValue());
 258  72
             serviceSchemaColl.add(configEntry[i].getValue());
 259   
         }
 260   
 
 261  9
         for (int i = 0; i < interceptor.length; i++) {
 262  18
             interceptorColl.add(interceptor[i].getValue());
 263   
         }
 264   
     }
 265   
 }