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 javax.management.MBeanServer;
    13  import javax.management.MBeanServerFactory;
    14  import javax.management.MalformedObjectNameException;
    15  import javax.management.ObjectName;
    16
    17  /***
    18   * <p>
    19   * The manageable service container is a wrapper container on the top of
    20   * ServiceContainer and MBeanServer. It's a manageable Inversion of Control
    21   * (IoC) container which service can be managed by JMX.
    22   * </p>
    23   *
    24   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
    25   * @version $Revision: 1.2 $ $Date: 2004/10/18 14:22:49 $
    26   * @version Revision: 1.0
    27   */
    28
    29  public class ManageableServiceContainer {
    30
    31      private static ManageableServiceContainer manageableServiceContainer;
    32
    33      private ServiceContainer serviceContainer = ServiceContainer.getInstance();
    34
    35      private MBeanServer mBeanServer = MBeanServerFactory
    36              .createMBeanServer("jfoxsoafmx");
    37
    38      public static ManageableServiceContainer getInstance() {
    39          if (manageableServiceContainer == null) {
    40              manageableServiceContainer = new ManageableServiceContainer();
    41          }
    42          return manageableServiceContainer;
    43      }
    44
    45      /***
    46       * Regist managable service.
    47       *
    48       * @param serviceInstance
    49       * @param serviceEntry
    50       * @throws Throwable
    51       */
    52      public void registManagebleService(Object serviceInstance,
    53              ServiceEntry serviceEntry) throws Throwable {
    54          if (isIDEmpty(serviceEntry)) {
    55              mBeanServer
    56                      .registerMBean(serviceInstance, objectName(serviceEntry));
    57              serviceContainer.registerServiceInstance(serviceInstance);
    58          } else {
    59              registManagebleService(serviceEntry.getId(), serviceInstance,
    60                      serviceEntry);
    61          }
    62      }
    63
    64      /***
    65       * Regist manageable service with service id.
    66       *
    67       * @param serviceKey
    68       * @param serviceInstance
    69       * @param serviceEntry
    70       * @throws Throwable
    71       */
    72      private void registManagebleService(Object serviceKey,
    73              Object serviceInstance, ServiceEntry serviceEntry) throws Throwable {
    74          mBeanServer.registerMBean(serviceInstance, objectName(serviceEntry));
    75          serviceContainer.registerServiceInstance(serviceKey, serviceInstance);
    76      }
    77
    78      /***
    79       * Get an MBeanServer instance.
    80       *
    81       * @return MBeanServer
    82       */
    83      public MBeanServer getMBeanServer() {
    84          return this.mBeanServer;
    85      }
    86
    87      /***
    88       * Convert object string to ObjectName.
    89       *
    90       * @param serviceEntry
    91       * @return
    92       * @throws MalformedObjectNameException
    93       */
    94      public ObjectName objectName(ServiceEntry serviceEntry)
    95              throws MalformedObjectNameException {
    96          String objectStr = "";
    97          if (!isIDEmpty(serviceEntry)) {
    98              objectStr = mBeanServer.getDefaultDomain() + ":type="
    99                      + serviceEntry.getId();
    100             return new ObjectName(objectStr);
    101         } else {
    102             objectStr = mBeanServer.getDefaultDomain() + ":type="
    103                     + serviceEntry.getImplementation();
    104             return new ObjectName(objectStr);
    105         }
    106     }
    107
    108     /***
    109      * Is service id empty?
    110      *
    111      * @param serviceEntry
    112      * @return boolean
    113      */
    114     public boolean isIDEmpty(ServiceEntry serviceEntry) {
    115         if (serviceEntry.getId() == null || serviceEntry.getId() == "") {
    116             return true;
    117         } else {
    118             return false;
    119         }
    120     }
    121
    122 }