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.InputStream;
    13  import java.io.InputStreamReader;
    14
    15  import javax.management.MBeanServer;
    16
    17  import org.exolab.castor.xml.MarshalException;
    18  import org.exolab.castor.xml.ValidationException;
    19  import org.huihoo.jfox.soaf.exception.ServiceConfigurationException;
    20  import org.huihoo.jfox.soaf.schema.config.Configuration;
    21  import org.huihoo.jfox.soaf.util.Globals;
    22  import org.picocontainer.PicoException;
    23  import org.picocontainer.PicoRegistrationException;
    24
    25  /***
    26   * <p>
    27   * The ServiceFactory is a singleton wrapper on the service container instance.
    28   * </p>
    29   *
    30   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
    31   * @version $Revision: 1.6 $ $Date: 2004/10/18 15:17:11 $
    32   * @version Revision: 1.0
    33   */
    34
    35  public class ServiceFactory {
    36
    37      private static ServiceFactory serviceFactory;
    38
    39      private ClassLoader cl = Thread.currentThread().getContextClassLoader();
    40
    41      private InputStream is = null;
    42
    43      private InputStreamReader isr = null;
    44
    45      private ServiceLoadProxy serviceLoadProxy;
    46
    47      private ServiceContainer serviceContainer = ServiceContainer.getInstance();
    48
    49      private ManageableServiceContainer manageableServiceContainer = ManageableServiceContainer
    50              .getInstance();
    51
    52      /***
    53       * Singleton instance.
    54       */
    55      public static synchronized ServiceFactory getInstance() {
    56          if (serviceFactory == null) {
    57              serviceFactory = new ServiceFactory();
    58
    59          }
    60
    61          if (!serviceFactory.serviceLoadProxy.isServiceLoaded()) {
    62              serviceFactory.constructServices();
    63          }
    64          return serviceFactory;
    65      }
    66
    67      /***
    68       * Default Constructor.
    69       */
    70      public ServiceFactory() {
    71
    72          serviceLoadProxy = new ServiceLoadProxy();
    73      }
    74
    75      /***
    76       * Lookup service by serviceKey
    77       *
    78       * @param componentKey
    79       * @return Service Object
    80       */
    81      public synchronized Object getService(Object serviceKey)
    82              throws PicoException, ServiceConfigurationException {
    83          return serviceContainer.getService(serviceKey);
    84      }
    85
    86      /***
    87       * Lookup service by serviceType
    88       *
    89       * @param componentKey
    90       * @return Service Object
    91       */
    92      public synchronized Object getService(Class serviceType)
    93              throws PicoException {
    94          return serviceContainer.getServiceOfType(serviceType);
    95      }
    96
    97      /***
    98       * Construct all default services from service configration files.
    99       */
    100     private void constructServices() throws ServiceConfigurationException {
    101         try {
    102             serviceLoadProxy.loadConfig(loadServiceConfigration(), cl);
    103         } catch (Throwable e) {
    104             e.printStackTrace();
    105             throw new ServiceConfigurationException(e);
    106         }
    107     }
    108
    109     /***
    110      * Load service configration file.
    111      *
    112      * @return Service
    113      * @throws Throwable
    114      */
    115     private Configuration loadServiceConfigration() throws Throwable {
    116         Configuration config = null;
    117         is = cl.getResourceAsStream(Globals.JFOXSOAF_CONFIG);
    118         isr = new InputStreamReader(is);
    119         try {
    120             config = Configuration.unmarshal(isr);
    121         } catch (MarshalException e) {
    122             e.printStackTrace();
    123             throw new ServiceConfigurationException(e);
    124         } catch (ValidationException e) {
    125             e.printStackTrace();
    126             throw new ServiceConfigurationException(e);
    127         }
    128         is.close();
    129         isr.close();
    130         return config;
    131     }
    132
    133     /***
    134      * Unregister service.
    135      *
    136      * @param service
    137      */
    138     public void unRegisterService(Object service) {
    139         serviceContainer.unregisterComponent(service);
    140     }
    141
    142     /***
    143      * Unregister service instance.
    144      *
    145      * @param serviceInstance
    146      */
    147     public void unRegisterServiceInstance(Object serviceInstance) {
    148         serviceContainer.unregisterComponentByInstance(serviceInstance);
    149     }
    150
    151     /***
    152      * Register service implementation.
    153      *
    154      * @param service
    155      */
    156     public void registerService(Class serviceImpl)
    157             throws PicoRegistrationException {
    158         serviceContainer.registerServiceImplementation(serviceImpl);
    159     }
    160
    161     /***
    162      * Register service with service key.
    163      *
    164      * @param serviceKey
    165      * @param serviceImpl
    166      */
    167     public void registerService(Object serviceKey, Class serviceImpl)
    168             throws PicoRegistrationException {
    169         serviceContainer.registerServiceImplementation(serviceKey, serviceImpl);
    170     }
    171
    172     /***
    173      * Register service instance.
    174      *
    175      * @param serviceInstance
    176      */
    177     public void registerServiceInstance(Object serviceInstance)
    178             throws PicoRegistrationException {
    179         serviceContainer.registerServiceInstance(serviceInstance);
    180     }
    181
    182     /***
    183      * Return MBeanServer
    184      *
    185      * @return
    186      */
    187     public MBeanServer getMBeanServer() {
    188         return manageableServiceContainer.getMBeanServer();
    189     }
    190
    191 }