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