KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jbi.management.AdminServiceMBean;
20 import javax.management.MBeanServerConnection JavaDoc;
21 import javax.management.MBeanServerInvocationHandler JavaDoc;
22 import javax.management.ObjectName JavaDoc;
23 import javax.management.remote.JMXConnector JavaDoc;
24 import javax.management.remote.JMXConnectorFactory JavaDoc;
25 import javax.management.remote.JMXServiceURL JavaDoc;
26
27 import junit.framework.TestCase;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.servicemix.jbi.container.JBIContainer;
32
33 /**
34  * ManagementContextTest
35  */

36 public class AdminServiceTest extends TestCase {
37     
38     private Log log = LogFactory.getLog(getClass());
39     
40     private JBIContainer container;
41
42     // The host, port and path where the rmiregistry runs.
43
private String JavaDoc namingHost = "localhost";
44     private int namingPort = ManagementContext.DEFAULT_CONNECTOR_PORT;
45     private String JavaDoc jndiPath = ManagementContext.DEFAULT_CONNECTOR_PATH;
46     
47     protected void setUp() throws Exception JavaDoc {
48         container = new JBIContainer();
49         container.setRmiPort(namingPort);
50         container.setCreateMBeanServer(true);
51         container.init();
52     }
53     
54     protected void tearDown() throws Exception JavaDoc {
55         container.shutDown();
56     }
57
58     public void testAdminService() throws Exception JavaDoc {
59         // The address of the connector server
60
JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc("service:jmx:rmi:///jndi/rmi://"
61                 + namingHost + ":" + namingPort + jndiPath);
62         // Connect a JSR 160 JMXConnector to the server side
63
JMXConnector JavaDoc connector = JMXConnectorFactory.connect(url);
64         // Retrieve an MBeanServerConnection that represent the MBeanServer the remote
65
// connector server is bound to
66
MBeanServerConnection JavaDoc connection = connector.getMBeanServerConnection();
67         // Call the server side as if it is a local MBeanServer
68
ObjectName JavaDoc asmName = getObjectName(ManagementContext.class);
69         Object JavaDoc proxy = MBeanServerInvocationHandler.newProxyInstance(connection, asmName,
70                 AdminServiceMBean.class, true);
71         AdminServiceMBean asm = (AdminServiceMBean) proxy;
72
73         log.info(asm.getBindingComponents());
74         log.info(asm.getComponentByName("toto"));
75     }
76     
77     protected ObjectName JavaDoc getObjectName (Class JavaDoc systemClass){
78         return ManagementContext.getSystemObjectName(ManagementContext.DEFAULT_DOMAIN, JBIContainer.DEFAULT_NAME, systemClass);
79     }
80 }
81
Popular Tags