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