View Javadoc

1   /*
2    * $Id: ManagementUtils.java 23030 2011-09-26 18:02:33Z mike.schilling $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.module.management.util;
11  
12  import org.mule.module.management.agent.WrapperManagerAgent;
13  import org.mule.module.management.support.AutoDiscoveryJmxSupportFactory;
14  import org.mule.module.management.support.JmxSupport;
15  import org.mule.module.management.support.JmxSupportFactory;
16  import org.tanukisoftware.wrapper.jmx.WrapperManager;
17  import org.tanukisoftware.wrapper.jmx.WrapperManagerMBean;
18  
19  import javax.management.InstanceAlreadyExistsException;
20  import javax.management.MBeanRegistrationException;
21  import javax.management.MBeanServer;
22  import javax.management.MBeanServerInvocationHandler;
23  import javax.management.MalformedObjectNameException;
24  import javax.management.NotCompliantMBeanException;
25  import javax.management.ObjectName;
26  import java.lang.management.ManagementFactory;
27  
28  public class ManagementUtils
29  {
30  
31      protected static JmxSupportFactory jmxSupportFactory;
32      protected static JmxSupport jmxSupport;
33  
34      public static void restart() throws Exception
35      {
36          WrapperManagerMBean proxy = getProxy();
37          if (proxy != null) {
38              proxy.restart();
39          }
40          else
41          {
42              throw new RuntimeException("The wrapper is not enabled.");
43          }
44      }
45  
46  
47      public static void stop(int exitCode) throws Exception
48      {
49          WrapperManagerMBean proxy = getProxy();
50          if (proxy != null) {
51              proxy.stop(exitCode);
52          }
53          else
54          {
55              throw new RuntimeException("The wrapper is not enabled.");
56          }
57      }
58  
59      protected synchronized static WrapperManagerMBean getProxy() throws MalformedObjectNameException, MBeanRegistrationException, InstanceAlreadyExistsException, NotCompliantMBeanException
60      {
61          if (jmxSupport == null)
62          {
63              jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
64              jmxSupport = jmxSupportFactory.getJmxSupport();
65          }
66  
67          MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
68          final String jmxNameForMule = String.format("%s:%s", JmxSupport.DEFAULT_JMX_DOMAIN_PREFIX, WrapperManagerAgent.WRAPPER_JMX_NAME);
69          ObjectName on = jmxSupport.getObjectName(jmxNameForMule);
70          if (!mBeanServer.isRegistered(on))
71          {
72              mBeanServer.registerMBean(new WrapperManager(), on);
73          }
74  
75          WrapperManagerMBean proxy = MBeanServerInvocationHandler.newProxyInstance(
76              mBeanServer, on, WrapperManagerMBean.class, false);
77          return proxy;
78      }
79  
80  }
81