View Javadoc

1   /*
2    * $Id: AbstractMuleJmxTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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  
11  package org.mule.management;
12  
13  import org.mule.module.management.agent.RmiRegistryAgent;
14  import org.mule.module.management.support.AutoDiscoveryJmxSupportFactory;
15  import org.mule.module.management.support.JmxSupport;
16  import org.mule.module.management.support.JmxSupportFactory;
17  import org.mule.tck.AbstractMuleTestCase;
18  
19  import java.lang.management.ManagementFactory;
20  import java.util.Set;
21  
22  import javax.management.MBeanServer;
23  import javax.management.ObjectInstance;
24  
25  /**
26   * This base test case will create a new <code>MBean Server</code> if necessary,
27   * and will clean up any registered MBeans in its <code>tearDown()</code> method.
28   */
29  public abstract class AbstractMuleJmxTestCase extends AbstractMuleTestCase
30  {
31      protected MBeanServer mBeanServer;
32      protected JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
33      protected JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
34  
35      protected void doSetUp() throws Exception
36      {
37          RmiRegistryAgent rmiRegistryAgent = new RmiRegistryAgent();
38          rmiRegistryAgent.setMuleContext(muleContext);
39          rmiRegistryAgent.initialise();
40          muleContext.getRegistry().registerAgent(rmiRegistryAgent);
41  
42          mBeanServer = ManagementFactory.getPlatformMBeanServer();
43  
44      }
45  
46      protected void unregisterMBeansByMask(final String mask) throws Exception
47      {
48          Set<ObjectInstance> objectInstances = mBeanServer.queryMBeans(jmxSupport.getObjectName(mask), null);
49          for (ObjectInstance instance : objectInstances)
50          {
51              try
52              {
53                  mBeanServer.unregisterMBean(instance.getObjectName());
54              }
55              catch (Exception e)
56              {
57                  // ignore
58              }
59          }
60      }
61  
62      protected void doTearDown() throws Exception
63      {
64          String domainName = jmxSupport.getDomainName(muleContext);
65          unregisterMBeansByMask(domainName + ":*");
66          unregisterMBeansByMask(domainName + ".1:*");
67          unregisterMBeansByMask(domainName + ".2:*");
68          mBeanServer = null;
69      }
70  
71      public void testDummy()
72      {
73          // this method only exists to silence the test runner
74      }
75  
76  }