1 package org.objectweb.petals.tools.petalsadmin.utils; 2 3 import java.io.IOException ; 4 import java.util.HashMap ; 5 import java.util.Hashtable ; 6 import java.util.Map ; 7 import java.util.Properties ; 8 import java.util.Set ; 9 10 import javax.management.MBeanServerConnection ; 11 import javax.management.MalformedObjectNameException ; 12 import javax.management.ObjectName ; 13 import javax.management.remote.JMXConnector ; 14 import javax.management.remote.JMXConnectorFactory ; 15 import javax.management.remote.JMXServiceURL ; 16 17 22 public final class JBIJMXConnectorUtil { 23 24 private final static String PETALS_DOMAIN = "Petals"; 25 26 private final static String TYPE = "type"; 27 28 private final static String NAME = "name"; 29 30 private final static String SERVICE_TYPE = "service"; 31 32 private final static String ADMIN_NAME = "Admin"; 33 34 private final static String INSTALLATION_NAME = "Installation"; 35 36 private final static String DEPLOYMENT_NAME = "Deployment"; 37 38 private JBIJMXConnectorUtil() { 39 } 41 42 52 public static ObjectName getAdminServiceMBeanName( 53 MBeanServerConnection mBeanServer) throws IOException , 54 MalformedObjectNameException , NullPointerException { 55 ObjectName result = null; 56 Set objNames; 57 Hashtable <String , String > attributes = new Hashtable <String , String >(); 58 attributes.put(NAME, ADMIN_NAME); 59 attributes.put(TYPE, SERVICE_TYPE); 60 ObjectName objName = new ObjectName (PETALS_DOMAIN, attributes); 61 objNames = mBeanServer.queryNames(objName, null); 62 if (objNames != null && objNames.size() == 1) { 63 result = (ObjectName ) objNames.iterator().next(); 64 } 65 return result; 66 } 67 68 78 public static ObjectName getComponentMBeanName( 79 MBeanServerConnection mBeanServer, String name) throws IOException , 80 MalformedObjectNameException , NullPointerException { 81 ObjectName result = null; 82 String pattern = "*:type=engine,name=" + name; 83 Set objNames; 84 ObjectName objName = new ObjectName (pattern); 85 objNames = mBeanServer.queryNames(objName, null); 86 if (objNames != null && objNames.size() == 1) { 87 result = (ObjectName ) objNames.iterator().next(); 88 } 89 if (result == null) { 90 pattern = "*:type=binding,name=" + name; 91 objName = new ObjectName (pattern); 92 objNames = mBeanServer.queryNames(objName, null); 93 if (objNames != null && objNames.size() == 1) { 94 result = (ObjectName ) objNames.iterator().next(); 95 } 96 } 97 return result; 98 } 99 100 114 public static JMXConnector getConnection(String host, String port, 115 String username, String password) throws IOException { 116 JMXConnector connector = null; 117 Properties env = System.getProperties(); 118 env.put("java.naming.factory.initial", 119 "com.sun.jndi.rmi.registry.RegistryContextFactory"); 120 Map <String , Object > args = new HashMap <String , Object >(); 121 if (!"".equals(username)) { 122 if (password == null) { 123 password = ""; 124 } 125 String [] credentials = new String [] {username, password}; 126 args.put("jmx.remote.credentials", credentials); 127 } 128 String urlStr = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port 129 + "/management/rmi-jmx-connector"; 130 JMXServiceURL url = new JMXServiceURL (urlStr); 131 connector = JMXConnectorFactory.connect(url, args); 132 return connector; 133 } 134 135 145 public static ObjectName getDeploymentServiceMBeanName( 146 MBeanServerConnection mBeanServer) throws IOException , 147 MalformedObjectNameException , NullPointerException { 148 ObjectName result = null; 149 Set objNames; 150 Hashtable <String , String > attributes = new Hashtable <String , String >(); 151 attributes.put(NAME, DEPLOYMENT_NAME); 152 attributes.put(TYPE, SERVICE_TYPE); 153 ObjectName objName = new ObjectName (PETALS_DOMAIN, attributes); 154 objNames = mBeanServer.queryNames(objName, null); 155 if (objNames != null && objNames.size() == 1) { 156 result = (ObjectName ) objNames.iterator().next(); 157 } 158 return result; 159 } 160 161 171 public static ObjectName getInstallationServiceMBeanName( 172 MBeanServerConnection mBeanServer) throws IOException , 173 MalformedObjectNameException , NullPointerException { 174 ObjectName result = null; 175 Set objNames; 176 Hashtable <String , String > attributes = new Hashtable <String , String >(); 177 attributes.put(NAME, INSTALLATION_NAME); 178 attributes.put(TYPE, SERVICE_TYPE); 179 ObjectName objName = new ObjectName (PETALS_DOMAIN, attributes); 180 objNames = mBeanServer.queryNames(objName, null); 181 if (objNames != null && objNames.size() == 1) { 182 result = (ObjectName ) objNames.iterator().next(); 183 } 184 return result; 185 } 186 187 } 188 | Popular Tags |