View Javadoc

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