1 19 20 package org.objectweb.petals.tools.ant.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 getComponentMBeanName( 98 MBeanServerConnection mBeanServer, String name) throws IOException , 99 MalformedObjectNameException , NullPointerException { 100 ObjectName result = null; 101 String pattern = "*:type=engine,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 if (result == null) { 109 pattern = "*:type=binding,name=" + name; 110 objName = new ObjectName (pattern); 111 objNames = mBeanServer.queryNames(objName, null); 112 if (objNames != null && objNames.size() == 1) { 113 result = (ObjectName ) objNames.iterator().next(); 114 } 115 } 116 return result; 117 } 118 119 133 public static JMXConnector getConnection(String host, String port, 134 String username, String password) throws IOException { 135 JMXConnector connector = null; 136 Properties env = System.getProperties(); 137 env.put("java.naming.factory.initial", 138 "com.sun.jndi.rmi.registry.RegistryContextFactory"); 139 Map <String , Object > args = new HashMap <String , Object >(); 140 if (!"".equals(username)) { 141 if (password == null) { 142 password = ""; 143 } 144 String [] credentials = new String [] {username, password}; 145 args.put("jmx.remote.credentials", credentials); 146 } 147 String urlStr = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port 148 + "/management/rmi-jmx-connector"; 149 JMXServiceURL url = new JMXServiceURL (urlStr); 150 connector = JMXConnectorFactory.connect(url, args); 151 return connector; 152 } 153 154 164 public static ObjectName getDeploymentServiceMBeanName( 165 MBeanServerConnection mBeanServer) throws IOException , 166 MalformedObjectNameException , NullPointerException { 167 ObjectName result = null; 168 Set objNames; 169 Hashtable <String , String > attributes = new Hashtable <String , String >(); 170 attributes.put(NAME, DEPLOYMENT_NAME); 171 attributes.put(TYPE, SERVICE_TYPE); 172 ObjectName objName = new ObjectName (PETALS_DOMAIN, attributes); 173 objNames = mBeanServer.queryNames(objName, null); 174 if (objNames != null && objNames.size() == 1) { 175 result = (ObjectName ) objNames.iterator().next(); 176 } 177 return result; 178 } 179 180 190 public static ObjectName getInstallationServiceMBeanName( 191 MBeanServerConnection mBeanServer) throws IOException , 192 MalformedObjectNameException , NullPointerException { 193 ObjectName result = null; 194 Set objNames; 195 Hashtable <String , String > attributes = new Hashtable <String , String >(); 196 attributes.put(NAME, INSTALLATION_NAME); 197 attributes.put(TYPE, SERVICE_TYPE); 198 ObjectName objName = new ObjectName (PETALS_DOMAIN, attributes); 199 objNames = mBeanServer.queryNames(objName, null); 200 if (objNames != null && objNames.size() == 1) { 201 result = (ObjectName ) objNames.iterator().next(); 202 } 203 return result; 204 } 205 206 } 207 | Popular Tags |