KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > management > ManagementContextTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
27 import javax.management.MBeanServerDelegateMBean JavaDoc;
28 import javax.management.MBeanServerInvocationHandler JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.remote.JMXConnector JavaDoc;
31 import javax.management.remote.JMXConnectorFactory JavaDoc;
32 import javax.management.remote.JMXServiceURL JavaDoc;
33
34 import junit.framework.TestCase;
35
36 /**
37  * ManagementContextTest
38  */

39 public class ManagementContextTest extends TestCase {
40
41     private Log log = LogFactory.getLog(getClass());
42     
43     // The host, port and path where the rmiregistry runs.
44
private String JavaDoc namingHost = "localhost";
45     private int namingPort = ManagementContext.DEFAULT_CONNECTOR_PORT;
46     private String JavaDoc jndiPath = ManagementContext.DEFAULT_CONNECTOR_PATH;
47     
48     private ManagementContext context;
49     private JBIContainer container;
50
51     protected void setUp() throws Exception JavaDoc {
52         container = new JBIContainer();
53         container.setCreateMBeanServer(true);
54         container.setRmiPort(namingPort);
55         container.init();
56         container.start();
57         context = container.getManagementContext();
58         /*
59         context = new ManagementContext();
60         context.setCreateMBeanServer(true);
61         context.setNamingPort(namingPort);
62         context.init(container, null);
63         */

64     }
65     
66     protected void tearDown() throws Exception JavaDoc {
67         container.shutDown();
68     }
69
70     public void testRemote() throws Exception JavaDoc {
71         // The address of the connector server
72
JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc("service:jmx:rmi:///jndi/rmi://"
73                 + namingHost + ":" + namingPort + jndiPath);
74         // Connect a JSR 160 JMXConnector to the server side
75
JMXConnector JavaDoc connector = JMXConnectorFactory.connect(url);
76         // Retrieve an MBeanServerConnection that represent the MBeanServer the remote
77
// connector server is bound to
78
MBeanServerConnection JavaDoc connection = connector.getMBeanServerConnection();
79         // Call the server side as if it is a local MBeanServer
80
ObjectName JavaDoc delegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");
81         Object JavaDoc proxy = MBeanServerInvocationHandler.newProxyInstance(connection, delegateName,
82                 MBeanServerDelegateMBean JavaDoc.class, true);
83         MBeanServerDelegateMBean JavaDoc delegate = (MBeanServerDelegateMBean JavaDoc) proxy;
84         // The magic of JDK 1.3 dynamic proxy and JSR 160:
85
// delegate.getImplementationVendor() is actually a remote JMX call,
86
// but it looks like a local, old-style, java call.
87
log.info(delegate.getImplementationVendor() + " is cool !");
88         
89         ObjectName JavaDoc 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 JavaDoc {
103         ObjectName JavaDoc[] 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