1 19 20 package org.objectweb.petals.tools.jbiplugin.util; 21 22 import java.io.IOException ; 23 import java.util.HashMap ; 24 import java.util.Hashtable ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 import java.util.Set ; 28 29 import javax.management.MBeanServerConnection ; 30 import javax.management.MalformedObjectNameException ; 31 import javax.management.ObjectName ; 32 import javax.management.remote.JMXConnector ; 33 import javax.management.remote.JMXConnectorFactory ; 34 import javax.management.remote.JMXServiceURL ; 35 36 41 public final class JBIJMXConnectorUtil { 42 43 private final static String PETALS_DOMAIN = "Petals"; 44 45 private final static String TYPE = "type"; 46 47 private final static String NAME = "name"; 48 49 private final static String SERVICE_TYPE = "service"; 50 51 private final static String ADMIN_NAME = "Admin"; 52 53 private final static String INSTALLATION_NAME = "Installation"; 54 55 private final static String DEPLOYMENT_NAME = "Deployment"; 56 57 private JBIJMXConnectorUtil() { 58 } 60 61 71 public static ObjectName getAdminServiceMBeanName( 72 MBeanServerConnection mBeanServer) throws IOException , 73 MalformedObjectNameException , NullPointerException { 74 ObjectName result = null; 75 Set objNames; 76 Hashtable <String , String > attributes = new Hashtable <String , String >(); 77 attributes.put(NAME, ADMIN_NAME); 78 attributes.put(TYPE, SERVICE_TYPE); 79 ObjectName objName = new ObjectName (PETALS_DOMAIN, attributes); 80 objNames = mBeanServer.queryNames(objName, null); 81 if (objNames != null && objNames.size() == 1) { 82 result = (ObjectName ) objNames.iterator().next(); 83 } 84 return result; 85 } 86 87 97 public static ObjectName getInstallerComponentMBeanName( 98 MBeanServerConnection mBeanServer, String name) throws IOException , 99 MalformedObjectNameException , NullPointerException { 100 ObjectName result = null; 101 String pattern = "*:type=installer,name=" + name; 102 Set objNames; 103 ObjectName objName = new ObjectName (pattern); 104 objNames = mBeanServer.queryNames(objName, null); 105 if (objNames != null && objNames.size() == 1) { 106 result = (ObjectName ) objNames.iterator().next(); 107 } 108 return result; 109 } 110 111 121 public static ObjectName getComponentMBeanName( 122 MBeanServerConnection mBeanServer, String name) throws IOException , 123 MalformedObjectNameException , NullPointerException { 124 ObjectName result = null; 125 String pattern = "*:type=engine,name=" + name; 126 Set objNames; 127 ObjectName objName = new ObjectName (pattern); 128 objNames = mBeanServer.queryNames(objName, null); 129 if (objNames != null && objNames.size() == 1) { 130 result = (ObjectName ) objNames.iterator().next(); 131 } 132 if (result == null) { 133 pattern = "*:type=binding,name=" + name; 134 objName = new ObjectName (pattern); 135 objNames = mBeanServer.queryNames(objName, null); 136 if (objNames != null && objNames.size() == 1) { 137 result = (ObjectName ) objNames.iterator().next(); 138 } 139 } 140 return result; 141 } 142 143 157 public static JMXConnector getConnection(String host, String port, 158 String username, String password) throws IOException { 159 JMXConnector connector = null; 160 Properties env = System.getProperties(); 161 env.put("java.naming.factory.initial", 162 "com.sun.jndi.rmi.registry.RegistryContextFactory"); 163 Map <String , Object > args = new HashMap <String , Object >(); 164 if (!"".equals(username)) { 165 if (password == null) { 166 password = ""; 167 } 168 String [] credentials = new String [] {username, password}; 169 args.put("jmx.remote.credentials", credentials); 170 } 171 String urlStr = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port 172 + "/management/rmi-jmx-connector"; 173 JMXServiceURL url = new JMXServiceURL (urlStr); 174 connector = JMXConnectorFactory.connect(url, args); 175 return connector; 176 } 177 178 188 public static ObjectName getDeploymentServiceMBeanName( 189 MBeanServerConnection mBeanServer) throws IOException , 190 MalformedObjectNameException , NullPointerException { 191 ObjectName result = null; 192 Set objNames; 193 Hashtable <String , String > attributes = new Hashtable <String , String >(); 194 attributes.put(NAME, DEPLOYMENT_NAME); 195 attributes.put(TYPE, SERVICE_TYPE); 196 ObjectName objName = new ObjectName (PETALS_DOMAIN, attributes); 197 objNames = mBeanServer.queryNames(objName, null); 198 if (objNames != null && objNames.size() == 1) { 199 result = (ObjectName ) objNames.iterator().next(); 200 } 201 return result; 202 } 203 204 214 public static ObjectName getInstallationServiceMBeanName( 215 MBeanServerConnection mBeanServer) throws IOException , 216 MalformedObjectNameException , NullPointerException { 217 ObjectName result = null; 218 Set objNames; 219 Hashtable <String , String > attributes = new Hashtable <String , String >(); 220 attributes.put(NAME, INSTALLATION_NAME); 221 attributes.put(TYPE, SERVICE_TYPE); 222 ObjectName objName = new ObjectName (PETALS_DOMAIN, attributes); 223 objNames = mBeanServer.queryNames(objName, null); 224 if (objNames != null && objNames.size() == 1) { 225 result = (ObjectName ) objNames.iterator().next(); 226 } 227 return result; 228 } 229 230 } 231 | Popular Tags |