KickJava   Java API By Example, From Geeks To Geeks.

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


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.jbi.container.JBIContainer;
22
23 import javax.management.Attribute JavaDoc;
24 import javax.management.AttributeList JavaDoc;
25 import javax.management.MBeanAttributeInfo JavaDoc;
26 import javax.management.MBeanInfo JavaDoc;
27 import javax.management.MBeanServerConnection JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.remote.JMXConnector JavaDoc;
30 import javax.management.remote.JMXConnectorFactory JavaDoc;
31 import javax.management.remote.JMXServiceURL JavaDoc;
32
33 import java.util.Iterator JavaDoc;
34 import java.util.Set JavaDoc;
35
36 import junit.framework.TestCase;
37
38 /**
39  * ManagementContextTest
40  */

41 public class ManagementAttributesTest extends TestCase {
42
43     private Log log = LogFactory.getLog(getClass());
44     
45     private JBIContainer container;
46
47     // The host, port and path where the rmiregistry runs.
48
private String JavaDoc namingHost = "localhost";
49     private int namingPort = ManagementContext.DEFAULT_CONNECTOR_PORT;
50     private String JavaDoc jndiPath = ManagementContext.DEFAULT_CONNECTOR_PATH;
51     
52     protected void setUp() throws Exception JavaDoc {
53         container = new JBIContainer();
54         container.setRmiPort(namingPort);
55         container.setCreateMBeanServer(true);
56         container.init();
57     }
58     
59     protected void tearDown() throws Exception JavaDoc {
60         container.shutDown();
61     }
62
63     public void testRemote() throws Exception JavaDoc {
64         // The address of the connector server
65
JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc("service:jmx:rmi:///jndi/rmi://"
66                 + namingHost + ":" + namingPort + jndiPath);
67         // Connect a JSR 160 JMXConnector to the server side
68
JMXConnector JavaDoc connector = JMXConnectorFactory.connect(url);
69         // Retrieve an MBeanServerConnection that represent the MBeanServer the remote
70
// connector server is bound to
71
MBeanServerConnection JavaDoc connection = connector.getMBeanServerConnection();
72         
73         log.info(connection.getMBeanCount());
74
75
76         Set JavaDoc set = connection.queryNames(new ObjectName JavaDoc(connection.getDefaultDomain() + ":*"), null);
77         for (Iterator JavaDoc iter = set.iterator(); iter.hasNext();) {
78             ObjectName JavaDoc name = (ObjectName JavaDoc)iter.next();
79             log.info(name.toString());
80             MBeanInfo JavaDoc info = connection.getMBeanInfo(name);
81             MBeanAttributeInfo JavaDoc[] mia = info.getAttributes();
82             String JavaDoc[] attrNames = new String JavaDoc[mia.length];
83             for (int i = 0; i < mia.length; i++) {
84                 attrNames[i] = mia[i].getName();
85                 log.info("attr " + mia[i].getName() + " " + mia[i].getType() + " " + connection.getAttribute(name,mia[i].getName()));
86             }
87
88             AttributeList JavaDoc attributeList = (AttributeList JavaDoc) connection.getAttributes(name,attrNames);
89             for (int i = 0; i < attributeList.size(); i++) {
90                 Attribute JavaDoc attribute = (Attribute JavaDoc) attributeList.get(i);
91                 log.info("bulk " + attribute.getName() + " " + attribute.getValue() + " " + attribute.toString());
92             }
93
94         }
95
96         
97     }
98     
99 }
100
Popular Tags