View Javadoc

    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 static ServiceLoader serviceLoader;
    60
    61      private boolean serviceLoaded = false;
    62
    63      private Collection serviceSchemaColl = new LinkedList();
    64
    65      private Collection interceptorColl = new ArrayList();
    66
    67      private Collection serviceEntryColl = new ArrayList();
    68
    69      private InterceptorManager interceptorManager = new InterceptorManagerImpl();
    70
    71      private final Log logger = LogFactory.getLog(getClass());
    72
    73      /***
    74       * Singleton instance
    75       * @return
    76       */
    77      public static ServiceLoader getInstance() {
    78          if (serviceLoader == null) {
    79              serviceLoader = new ServiceLoader();
    80          }
    81          return serviceLoader;
    82      }
    83
    84      /***
    85       * Init services.
    86       *
    87       * @param configFile
    88       * @throws ServiceConfigurationException
    89       */
    90      public synchronized void initService(String configFile)
    91              throws ServiceConfigurationException {
    92          if (!isServiceLoaded()) {
    93              try {
    94                  InputStream is = ResourceHelper.getResourceAsStream(configFile);
    95                  loadConfig(loadServiceConfigration(is));
    96              } catch (Throwable e) {
    97                  throw new ServiceConfigurationException(
    98                          "Init serivce config file jfoxsoaf-config.xml failed "
    99                                  + e.getMessage());
    100             }
    101             serviceLoaded = true;
    102         }
    103     }
    104
    105     /***
    106      * Init services from input stream.
    107      *
    108      * @param is
    109      * @throws ServiceConfigurationException
    110      */
    111     public synchronized void initService(InputStream is)
    112             throws ServiceConfigurationException {
    113         if (!isServiceLoaded()) {
    114             try {
    115                 loadConfig(loadServiceConfigration(is));
    116             } catch (Throwable e) {
    117                 throw new ServiceConfigurationException(
    118                         "Init serivce config file jfoxsoaf-config.xml failed "
    119                                 + e.getMessage());
    120             }
    121             serviceLoaded = true;
    122         }
    123     }
    124
    125     /***
    126      * Load service configration file.
    127      *
    128      * @return Service
    129      * @throws Throwable
    130      */
    131     private Configuration loadServiceConfigration(InputStream is)
    132             throws ServiceConfigurationException {
    133         InputStreamReader isr = null;
    134         Configuration config = null;
    135         try {
    136             isr = new InputStreamReader(is);
    137             config = Configuration.unmarshal(isr);
    138         } catch (MarshalException e) {
    139             throw new ServiceConfigurationException(e);
    140         } catch (ValidationException e) {
    141             throw new ServiceConfigurationException(e);
    142         } finally {
    143             if (is != null) {
    144                 try {
    145                     is.close();
    146                 } catch (IOException e) {
    147                     e.printStackTrace();
    148                 }
    149             }
    150
    151             if (isr != null) {
    152                 try {
    153                     isr.close();
    154                 } catch (IOException e) {
    155                     e.printStackTrace();
    156                 }
    157             }
    158         }
    159         return config;
    160     }
    161
    162     /***
    163      * Load all configuration service.
    164      *
    165      * @param service
    166      */
    167     public void loadConfig(Configuration config) throws Throwable {
    168         processConfig(config);
    169         loadService(serviceSchemaColl);
    170         interceptorManager
    171                 .processInterceptor(interceptorColl, serviceEntryColl);
    172     }
    173
    174     /***
    175      * Load service schema.
    176      *
    177      * @param serviceSchemaColl
    178      * @throws ServiceConfigurationException
    179      */
    180     private void loadService(Collection serviceSchemaColl)
    181             throws ServiceConfigurationException {
    182         InputStream is = null;
    183         InputStreamReader isr = null;
    184         Iterator iter = serviceSchemaColl.iterator();
    185         Service service;
    186         try {
    187             while (iter.hasNext()) {
    188                 is = ResourceHelper.getResourceAsStream((String) iter.next());
    189                 isr = new InputStreamReader(is);
    190                 service = Service.unmarshal(isr);
    191                 processService(service);
    192             }
    193         } catch (Exception e) {
    194             throw new ServiceConfigurationException(e);
    195         } finally {
    196             if (is != null) {
    197                 try {
    198                     is.close();
    199                 } catch (IOException e1) {
    200                     e1.printStackTrace();
    201                 }
    202
    203             }
    204
    205             if (isr != null) {
    206                 try {
    207                     isr.close();
    208                 } catch (IOException e1) {
    209                     e1.printStackTrace();
    210                 }
    211             }
    212
    213         }
    214
    215     }
    216
    217     /***
    218      * Add service to service entry collection.
    219      *
    220      * @param service
    221      */
    222     private void processService(Service service) {
    223         ServiceEntry[] serviceEntry = service.getServiceEntry();
    224         for (int i = 0; i < serviceEntry.length; i++) {
    225             serviceEntryColl.add(serviceEntry[i]);
    226         }
    227     }
    228
    229     /***
    230      * Is service loaded?
    231      *
    232      * @return boolean
    233      */
    234     public boolean isServiceLoaded() {
    235         return this.serviceLoaded;
    236     }
    237
    238     /***
    239      * Add service schema and interceptor to collection.
    240      *
    241      * @param config
    242      */
    243     private void processConfig(Configuration config) {
    244         ConfigurationEntry[] configEntry = config.getServiceConfiguration()
    245                 .getConfigurationEntry();
    246         Interceptor[] interceptor = config.getSystemInterceptor()
    247                 .getInterceptor();
    248         for (int i = 0; i < configEntry.length; i++) {
    249             logger.info("Load service schema : " + configEntry[i].getValue());
    250             serviceSchemaColl.add(configEntry[i].getValue());
    251         }
    252
    253         for (int i = 0; i < interceptor.length; i++) {
    254             interceptorColl.add(interceptor[i].getValue());
    255         }
    256     }
    257 }