View Javadoc

    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  import java.util.ArrayList;
    16  import java.util.Collection;
    17  import java.util.Iterator;
    18  import java.util.LinkedList;
    19
    20  import org.apache.commons.logging.Log;
    21  import org.apache.commons.logging.LogFactory;
    22  import org.huihoo.jfox.soaf.exception.ServiceConfigurationException;
    23  import org.huihoo.jfox.soaf.schema.config.Configuration;
    24  import org.huihoo.jfox.soaf.schema.config.ConfigurationEntry;
    25  import org.huihoo.jfox.soaf.schema.config.Interceptor;
    26  import org.huihoo.jfox.soaf.schema.service.Service;
    27  import org.huihoo.jfox.soaf.schema.service.ServiceEntry;
    28  import org.huihoo.jfox.soaf.util.resource.ResourceHelper;
    29
    30  /***
    31   * <p>
    32   * Service Configration load proxy.
    33   * </p>
    34   *
    35   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
    36   * @version $Revision: 1.7 $ $Date: 2004/10/28 05:11:43 $
    37   * @version Revision: 1.0
    38   */
    39
    40  public class ServiceLoadProxy {
    41
    42      private boolean serviceLoaded = false;
    43
    44      private Collection serviceSchemaColl = new LinkedList();
    45
    46      private Collection interceptorColl = new ArrayList();
    47
    48      private Collection serviceEntryColl = new ArrayList();
    49
    50      private InterceptorManager interceptorManager = new InterceptorManagerImpl();
    51
    52      private final Log logger = LogFactory.getLog(getClass());
    53
    54      /***
    55       * Default Constructor.
    56       */
    57      public ServiceLoadProxy() {
    58          super();
    59      }
    60
    61      /***
    62       * Load all configuration service.
    63       * @param service
    64       */
    65      public void loadConfig(Configuration config)
    66              throws Throwable {
    67          if (!serviceLoaded) {
    68              processConfig(config);
    69              loadService(serviceSchemaColl);
    70              interceptorManager.processInterceptor(interceptorColl,
    71                      serviceEntryColl);
    72              this.serviceLoaded = true;
    73          }
    74      }
    75
    76      /***
    77       * Load service schema.
    78       * @param serviceSchemaColl
    79       *
    80       * @throws ServiceConfigurationException
    81       */
    82      private void loadService(Collection serviceSchemaColl) throws ServiceConfigurationException {
    83          InputStream is = null;
    84          InputStreamReader isr = null;
    85          Iterator iter = serviceSchemaColl.iterator();
    86          Service service;
    87          try {
    88              while (iter.hasNext()) {
    89                  is = ResourceHelper.getResourceAsStream((String) iter.next());
    90                  isr = new InputStreamReader(is);
    91                  service = Service.unmarshal(isr);
    92                  processService(service);
    93              }
    94          } catch (Exception e) {
    95              throw new ServiceConfigurationException(e);
    96          } finally {
    97              if (is != null) {
    98                  try {
    99                      is.close();
    100                 } catch (IOException e1) {
    101                     e1.printStackTrace();
    102                 }
    103
    104             }
    105
    106             if (isr != null) {
    107                 try {
    108                     isr.close();
    109                 } catch (IOException e1) {
    110                     e1.printStackTrace();
    111                 }
    112             }
    113
    114         }
    115
    116     }
    117
    118     /***
    119      * Add service to service entry collection.
    120      *
    121      * @param service
    122      */
    123     private void processService(Service service) {
    124         ServiceEntry[] serviceEntry = service.getServiceEntry();
    125         for (int i = 0; i < serviceEntry.length; i++) {
    126             serviceEntryColl.add(serviceEntry[i]);
    127         }
    128     }
    129
    130     /***
    131      * Is service loaded?
    132      *
    133      * @return boolean
    134      */
    135     public boolean isServiceLoaded() {
    136         return this.serviceLoaded;
    137     }
    138
    139     /***
    140      * Add service schema and interceptor to collection.
    141      *
    142      * @param config
    143      */
    144     private void processConfig(Configuration config) {
    145         ConfigurationEntry[] configEntry = config.getServiceConfiguration()
    146                 .getConfigurationEntry();
    147         Interceptor[] interceptor = config.getSystemInterceptor()
    148                 .getInterceptor();
    149         for (int i = 0; i < configEntry.length; i++) {
    150             logger.info("Load service schema : " + configEntry[i].getValue());
    151             serviceSchemaColl.add(configEntry[i].getValue());
    152         }
    153
    154         for (int i = 0; i < interceptor.length; i++) {
    155             interceptorColl.add(interceptor[i].getValue());
    156         }
    157     }
    158 }