1 17 package org.apache.servicemix.jbi.management; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.servicemix.components.util.EchoComponent; 22 import org.apache.servicemix.jbi.container.JBIContainer; 23 import org.apache.servicemix.jbi.management.ManagementContext; 24 25 import javax.jbi.management.LifeCycleMBean; 26 import javax.management.MBeanServerConnection ; 27 import javax.management.MBeanServerDelegateMBean ; 28 import javax.management.MBeanServerInvocationHandler ; 29 import javax.management.ObjectName ; 30 import javax.management.remote.JMXConnector ; 31 import javax.management.remote.JMXConnectorFactory ; 32 import javax.management.remote.JMXServiceURL ; 33 34 import junit.framework.TestCase; 35 36 39 public class ManagementContextTest extends TestCase { 40 41 private Log log = LogFactory.getLog(getClass()); 42 43 private String namingHost = "localhost"; 45 private int namingPort = ManagementContext.DEFAULT_CONNECTOR_PORT; 46 private String jndiPath = ManagementContext.DEFAULT_CONNECTOR_PATH; 47 48 private ManagementContext context; 49 private JBIContainer container; 50 51 protected void setUp() throws Exception { 52 container = new JBIContainer(); 53 container.setCreateMBeanServer(true); 54 container.setRmiPort(namingPort); 55 container.init(); 56 container.start(); 57 context = container.getManagementContext(); 58 64 } 65 66 protected void tearDown() throws Exception { 67 container.shutDown(); 68 } 69 70 public void testRemote() throws Exception { 71 JMXServiceURL url = new JMXServiceURL ("service:jmx:rmi:///jndi/rmi://" 73 + namingHost + ":" + namingPort + jndiPath); 74 JMXConnector connector = JMXConnectorFactory.connect(url); 76 MBeanServerConnection connection = connector.getMBeanServerConnection(); 79 ObjectName delegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate"); 81 Object proxy = MBeanServerInvocationHandler.newProxyInstance(connection, delegateName, 82 MBeanServerDelegateMBean .class, true); 83 MBeanServerDelegateMBean delegate = (MBeanServerDelegateMBean ) proxy; 84 log.info(delegate.getImplementationVendor() + " is cool !"); 88 89 ObjectName objName = context.createObjectName(context); 90 91 92 proxy = MBeanServerInvocationHandler.newProxyInstance(connection, objName, 93 LifeCycleMBean.class, true); 94 LifeCycleMBean mc = (LifeCycleMBean) proxy; 95 log.info("STATE = " + mc.getCurrentState()); 96 mc.start(); 97 log.info("STATE = " + mc.getCurrentState()); 98 mc.stop(); 99 log.info("STATE = " + mc.getCurrentState()); 100 } 101 102 public void testComponent() throws Exception { 103 ObjectName [] names = context.getPojoComponents(); 104 assertEquals(1, names.length); 105 EchoComponent echo = new EchoComponent(); 106 container.activateComponent(echo, "echo"); 107 names = context.getPojoComponents(); 108 assertNotNull(names); 109 assertEquals(2, names.length); 110 assertEquals(LifeCycleMBean.STARTED, echo.getCurrentState()); 111 context.stopComponent("echo"); 112 assertEquals(LifeCycleMBean.STOPPED, echo.getCurrentState()); 113 } 114 115 } 116 | Popular Tags |