1 22 package org.jboss.deployment.spi; 23 24 import java.net.InetAddress ; 25 import java.net.URI ; 26 import java.net.URL ; 27 import java.net.URLDecoder ; 28 import java.net.UnknownHostException ; 29 import java.util.ArrayList ; 30 import java.util.Hashtable ; 31 import java.util.List ; 32 import java.util.Properties ; 33 import java.util.StringTokenizer ; 34 35 import javax.enterprise.deploy.shared.ModuleType ; 36 import javax.enterprise.deploy.spi.TargetModuleID ; 37 import javax.enterprise.deploy.spi.exceptions.TargetException ; 38 import javax.management.MBeanServerConnection ; 39 import javax.management.ObjectName ; 40 import javax.naming.Context ; 41 import javax.naming.InitialContext ; 42 import javax.naming.NamingException ; 43 44 import org.jboss.logging.Logger; 45 46 53 public class JMXTarget implements JBossTarget 54 { 55 private static final Logger log = Logger.getLogger(JMXTarget.class); 56 59 private static final String DEFAULT_ADAPTOR_PATH = "/jmx/invoker/RMIAdaptor"; 60 61 private static final String JSR88_MBEAN = "jboss.management.local:type=JSR88DeploymentManager,name=DefaultManager"; 62 private static final String JSR88_MBEAN_OPT = "jsr88MBean"; 63 64 65 private URI deployURI; 66 67 private Properties jndiEnv; 68 private ObjectName jsr88MBean; 69 70 76 public JMXTarget(URI deployURI) 77 { 78 log.debug("new JMXTarget: " + deployURI); 79 try 80 { 81 String localHostName = InetAddress.getLocalHost().getHostName(); 82 83 String scheme = deployURI.getScheme(); 84 String host = deployURI.getHost(); 85 int port = deployURI.getPort(); 86 String path = deployURI.getPath(); 87 String query = deployURI.getRawQuery(); 88 89 if (new URI (DeploymentManagerImpl.DEPLOYER_URI).equals(deployURI)) 90 { 91 scheme = "jnp"; 93 host = localHostName; 94 port = 1099; 95 path = DEFAULT_ADAPTOR_PATH; 96 97 try 98 { 99 InitialContext iniCtx = new InitialContext (); 100 Hashtable env = iniCtx.getEnvironment(); 101 String providerURL = (String )env.get(InitialContext.PROVIDER_URL); 102 if (providerURL != null) 103 { 104 if (providerURL.indexOf("://") < 0) 106 providerURL = "jnp://" + providerURL; 107 108 URI providerURI = new URI (providerURL); 109 scheme = providerURI.getScheme(); 110 host = providerURI.getHost(); 111 port = providerURI.getPort(); 112 } 113 } 114 catch (NamingException e) 115 { 116 log.error(e); 117 } 118 } 119 120 StringBuffer uri = new StringBuffer (scheme + "://"); 121 uri.append(host != null ? host : localHostName); 122 uri.append(port > 0 ? ":" + port : ""); 123 uri.append(path != null && path.length() > 0 ? path : DEFAULT_ADAPTOR_PATH); 124 uri.append(query != null ? "?" + query : ""); 125 deployURI = new URI (uri.toString()); 126 127 log.debug("URI changed to: " + deployURI); 128 this.deployURI = deployURI; 129 130 parseQuery(); 132 } 133 catch (Exception e) 134 { 135 log.error(e); 136 } 137 138 } 139 140 144 public String getDescription() 145 { 146 return "JBoss JMX deployment target"; 147 } 148 149 153 public String getName() 154 { 155 return deployURI.toString(); 156 } 157 158 161 public String getHostName() 162 { 163 return deployURI.getHost(); 164 } 165 166 169 public void deploy(TargetModuleID targetModuleID) throws Exception 170 { 171 TargetModuleIDImpl moduleID = (TargetModuleIDImpl)targetModuleID; 172 SerializableTargetModuleID smoduleID = new SerializableTargetModuleID(moduleID); 173 MBeanServerConnection server = getMBeanServerConnection(); 174 String url = targetModuleID.getModuleID(); 175 Object [] args = { smoduleID }; 176 String [] sig = { smoduleID.getClass().getName() }; 177 log.info("Begin deploy: " + url); 178 server.invoke(jsr88MBean, "deploy", args, sig); 179 log.info("End deploy"); 180 } 181 182 185 public void start(TargetModuleID targetModuleID) throws Exception 186 { 187 MBeanServerConnection server = getMBeanServerConnection(); 188 URL url = new URL (targetModuleID.getModuleID()); 189 Object [] args = { url }; 190 String [] sig = { URL .class.getName() }; 191 log.debug("Start: " + url); 192 args = new Object [] { url.toExternalForm() }; 193 sig = new String [] { String .class.getName() }; 194 log.info("Begin start: " + url); 195 server.invoke(jsr88MBean, "start", args, sig); 196 log.info("End start"); 197 } 198 199 202 public void stop(TargetModuleID targetModuleID) throws Exception 203 { 204 MBeanServerConnection server = getMBeanServerConnection(); 205 URL url = new URL (targetModuleID.getModuleID()); 206 Object [] args = { url }; 207 String [] sig = { URL .class.getName() }; 208 log.debug("Stop: " + url); 209 args = new Object [] { url.toExternalForm() }; 210 sig = new String [] { String .class.getName() }; 211 log.info("Begin stop: " + url); 212 server.invoke(jsr88MBean, "stop", args, sig); 213 log.info("End stop"); 214 } 215 216 219 public void undeploy(TargetModuleID targetModuleID) throws Exception 220 { 221 MBeanServerConnection server = getMBeanServerConnection(); 222 String url = targetModuleID.getModuleID(); 223 Object [] args = { url }; 224 String [] sig = { String .class.getName() }; 225 log.info("Begin undeploy: " + url); 226 server.invoke(jsr88MBean, "undeploy", args, sig); 227 log.info("End undeploy"); 228 } 229 230 234 public TargetModuleID [] getAvailableModules(ModuleType moduleType) throws TargetException 235 { 236 try 237 { 238 List list = new ArrayList (); 239 MBeanServerConnection server = getMBeanServerConnection(); 240 Object [] args = { new Integer (moduleType.getValue()) }; 241 String [] sig = { int.class.getName() }; 242 SerializableTargetModuleID[] modules = (SerializableTargetModuleID[])server.invoke(jsr88MBean, "getAvailableModules", args, sig); 243 for (int n = 0; n < modules.length; n++) 244 { 245 SerializableTargetModuleID id = modules[n]; 246 String moduleID = id.getModuleID(); 247 boolean isRunning = id.isRunning(); 248 ModuleType type = ModuleType.getModuleType(id.getModuleType()); 249 TargetModuleIDImpl tmid = new TargetModuleIDImpl(this, moduleID, null, isRunning, type); 250 convertChildren(tmid, id); 251 list.add(tmid); 252 } 253 254 TargetModuleID [] targetModuleIDs = new TargetModuleID [list.size()]; 255 list.toArray(targetModuleIDs); 256 return targetModuleIDs; 257 } 258 catch (Exception e) 259 { 260 log.error("Cannot get available modules", e); 261 TargetException tex = new TargetException (""); 262 tex.initCause(e); 263 throw tex; 264 } 265 } 266 267 private void convertChildren(TargetModuleIDImpl parent, SerializableTargetModuleID parentID) 268 { 269 SerializableTargetModuleID[] children = parentID.getChildModuleIDs(); 270 int length = children != null ? children.length : 0; 271 for (int n = 0; n < length; n++) 272 { 273 SerializableTargetModuleID id = children[n]; 274 String moduleID = id.getModuleID(); 275 boolean isRunning = id.isRunning(); 276 ModuleType type = ModuleType.getModuleType(id.getModuleType()); 277 TargetModuleIDImpl child = new TargetModuleIDImpl(this, moduleID, parent, isRunning, type); 278 parent.addChildTargetModuleID(child); 279 convertChildren(child, id); 280 } 281 } 282 283 private MBeanServerConnection getMBeanServerConnection() throws NamingException 285 { 286 Properties env = buildJNDIEnv(); 287 String lookupPath = deployURI.getPath(); 288 log.debug("JNDI lookup: " + lookupPath); 289 log.trace("Creating InitialContext with env: " + env); 290 InitialContext ctx = new InitialContext (env); 291 MBeanServerConnection server = (MBeanServerConnection )ctx.lookup(lookupPath); 292 return server; 293 } 294 295 300 private void parseQuery() throws Exception 301 { 302 String query = deployURI.getRawQuery(); 303 log.debug("DeployURI.rawQuery: " + query); 304 Properties params = new Properties (); 305 if (query != null) 306 { 307 311 String [] options = query.split("[&=]"); 312 for (int n = 0; n < options.length; n += 2) 313 { 314 String name = URLDecoder.decode(options[n], "UTF-8"); 315 String value = URLDecoder.decode(options[n + 1], "UTF-8"); 316 params.setProperty(name, value); 317 } 318 } 319 320 String name = params.getProperty(JSR88_MBEAN_OPT, JSR88_MBEAN); 321 jsr88MBean = new ObjectName (name); 322 } 323 324 private Properties buildJNDIEnv() 325 { 326 if (jndiEnv == null) 327 { 328 jndiEnv = new Properties (); 329 330 String query = deployURI.getQuery(); 332 if (query != null) 333 { 334 log.debug("Parsing query string: " + query); 335 StringTokenizer tokenizer = new StringTokenizer (query, "=&"); 336 while (tokenizer.hasMoreTokens()) 337 { 338 String name = tokenizer.nextToken(); 339 String value = tokenizer.nextToken(); 340 jndiEnv.setProperty(name, value); 341 } 342 } 343 344 if (jndiEnv.getProperty(Context.INITIAL_CONTEXT_FACTORY) == null) 346 { 347 jndiEnv.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); 348 } 349 if (jndiEnv.getProperty(Context.PROVIDER_URL) == null) 350 { 351 String host = deployURI.getHost(); 352 if (host == null) 353 { 354 try 355 { 356 host = InetAddress.getLocalHost().getHostName(); 357 } 358 catch (UnknownHostException e) 359 { 360 host = "localhost"; 361 } 362 } 363 364 int port = deployURI.getPort(); 365 if (port <= 0) 366 { 367 port = 1099; 368 } 369 370 String jnpURL = "jnp://" + host + ':' + port; 371 jndiEnv.setProperty(Context.PROVIDER_URL, jnpURL); 372 } 373 if (jndiEnv.getProperty(Context.OBJECT_FACTORIES) == null) 374 { 375 jndiEnv.setProperty(Context.OBJECT_FACTORIES, "org.jboss.naming"); 376 } 377 } 378 379 return jndiEnv; 380 } 381 } 382 | Popular Tags |