FileDocCategorySizeDatePackage
JMXTarget.javaAPI DocJBoss 4.2.113021Fri Jul 13 20:52:34 BST 2007org.jboss.deployment.spi

JMXTarget

public class JMXTarget extends Object implements JBossTarget
A Target that deploys using the JMX adaptor to communicate with the MainDeployer using file URLs to the deployments.
author
Thomas.Diesler@jboss.org
author
Scott.Stark@jboss.com
version
$Revision: 57190 $

Fields Summary
private static final Logger
log
private static final String
DEFAULT_ADAPTOR_PATH
The default RMIAdaptor JNDI location
private static final String
JSR88_MBEAN
The JSR88 deployment manager default and uri option name
private static final String
JSR88_MBEAN_OPT
private URI
deployURI
The deployment target uri
private Properties
jndiEnv
The JNDI properties used to locate the MBeanServer
private ObjectName
jsr88MBean
Constructors Summary
public JMXTarget(URI deployURI)

todo
merge the query parameter parsing for jndi and mbean names and test proper escaping of unsafe url chars.
param
deployURI


                             
     
   
      log.debug("new JMXTarget: " + deployURI);
      try
      {
         String localHostName = InetAddress.getLocalHost().getHostName();

         String scheme = deployURI.getScheme();
         String host = deployURI.getHost();
         int port = deployURI.getPort();
         String path = deployURI.getPath();
         String query = deployURI.getRawQuery();

         if (new URI(DeploymentManagerImpl.DEPLOYER_URI).equals(deployURI))
         {
            // Using JNDI defaults
            scheme = "jnp";
            host = localHostName;
            port = 1099;
            path = DEFAULT_ADAPTOR_PATH;

            try
            {
               InitialContext iniCtx = new InitialContext();
               Hashtable env = iniCtx.getEnvironment();
               String providerURL = (String)env.get(InitialContext.PROVIDER_URL);
               if (providerURL != null)
               {
                  // In case there is no schema returned
                  if (providerURL.indexOf("://") < 0)
                     providerURL = "jnp://" + providerURL;

                  URI providerURI = new URI(providerURL);
                  scheme = providerURI.getScheme();
                  host = providerURI.getHost();
                  port = providerURI.getPort();
               }
            }
            catch (NamingException e)
            {
               log.error(e);
            }
         }

         StringBuffer uri = new StringBuffer(scheme + "://");
         uri.append(host != null ? host : localHostName);
         uri.append(port > 0 ? ":" + port : "");
         uri.append(path != null && path.length() > 0 ? path : DEFAULT_ADAPTOR_PATH);
         uri.append(query != null ? "?" + query : "");
         deployURI = new URI(uri.toString());

         log.debug("URI changed to: " + deployURI);
         this.deployURI = deployURI;

         // Parse the query for options
         parseQuery();
      }
      catch (Exception e)
      {
         log.error(e);
      }

   
Methods Summary
private java.util.PropertiesbuildJNDIEnv()

      if (jndiEnv == null)
      {
         jndiEnv = new Properties();

         // Parse the query string for name=value pairs to put into the env
         String query = deployURI.getQuery();
         if (query != null)
         {
            log.debug("Parsing query string: " + query);
            StringTokenizer tokenizer = new StringTokenizer(query, "=&");
            while (tokenizer.hasMoreTokens())
            {
               String name = tokenizer.nextToken();
               String value = tokenizer.nextToken();
               jndiEnv.setProperty(name, value);
            }
         }

         // Set defaults for missing properties
         if (jndiEnv.getProperty(Context.INITIAL_CONTEXT_FACTORY) == null)
         {
            jndiEnv.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
         }
         if (jndiEnv.getProperty(Context.PROVIDER_URL) == null)
         {
            String host = deployURI.getHost();
            if (host == null)
            {
               try
               {
                  host = InetAddress.getLocalHost().getHostName();
               }
               catch (UnknownHostException e)
               {
                  host = "localhost";
               }
            }

            int port = deployURI.getPort();
            if (port <= 0)
            {
               port = 1099;
            }

            String jnpURL = "jnp://" + host + ':" + port;
            jndiEnv.setProperty(Context.PROVIDER_URL, jnpURL);
         }
         if (jndiEnv.getProperty(Context.OBJECT_FACTORIES) == null)
         {
            jndiEnv.setProperty(Context.OBJECT_FACTORIES, "org.jboss.naming");
         }
      }

      return jndiEnv;
   
private voidconvertChildren(TargetModuleIDImpl parent, SerializableTargetModuleID parentID)

      SerializableTargetModuleID[] children = parentID.getChildModuleIDs();
      int length = children != null ? children.length : 0;
      for (int n = 0; n < length; n++)
      {
         SerializableTargetModuleID id = children[n];
         String moduleID = id.getModuleID();
         boolean isRunning = id.isRunning();
         ModuleType type = ModuleType.getModuleType(id.getModuleType());
         TargetModuleIDImpl child = new TargetModuleIDImpl(this, moduleID, parent, isRunning, type);
         parent.addChildTargetModuleID(child);
         convertChildren(child, id);
      }
   
public voiddeploy(javax.enterprise.deploy.spi.TargetModuleID targetModuleID)
Deploy a given module

      TargetModuleIDImpl moduleID = (TargetModuleIDImpl)targetModuleID;
      SerializableTargetModuleID smoduleID = new SerializableTargetModuleID(moduleID);
      MBeanServerConnection server = getMBeanServerConnection();
      String url = targetModuleID.getModuleID();
      Object[] args = { smoduleID };
      String[] sig = { smoduleID.getClass().getName() };
      log.info("Begin deploy: " + url);
      server.invoke(jsr88MBean, "deploy", args, sig);
      log.info("End deploy");
   
public javax.enterprise.deploy.spi.TargetModuleID[]getAvailableModules(javax.enterprise.deploy.shared.ModuleType moduleType)
Retrieve the list of all J2EE application modules running or not running on the identified targets.

      try
      {
         List list = new ArrayList();
         MBeanServerConnection server = getMBeanServerConnection();
         Object[] args = { new Integer(moduleType.getValue()) };
         String[] sig = { int.class.getName() };
         SerializableTargetModuleID[] modules = (SerializableTargetModuleID[])server.invoke(jsr88MBean, "getAvailableModules", args, sig);
         for (int n = 0; n < modules.length; n++)
         {
            SerializableTargetModuleID id = modules[n];
            String moduleID = id.getModuleID();
            boolean isRunning = id.isRunning();
            ModuleType type = ModuleType.getModuleType(id.getModuleType());
            TargetModuleIDImpl tmid = new TargetModuleIDImpl(this, moduleID, null, isRunning, type);
            convertChildren(tmid, id);
            list.add(tmid);
         }

         TargetModuleID[] targetModuleIDs = new TargetModuleID[list.size()];
         list.toArray(targetModuleIDs);
         return targetModuleIDs;
      }
      catch (Exception e)
      {
         log.error("Cannot get available modules", e);
         TargetException tex = new TargetException("");
         tex.initCause(e);
         throw tex;
      }
   
public java.lang.StringgetDescription()
Get the target's description

return
the description

      return "JBoss JMX deployment target";
   
public java.lang.StringgetHostName()
Get the target's host name

      return deployURI.getHost();
   
private javax.management.MBeanServerConnectiongetMBeanServerConnection()

      Properties env = buildJNDIEnv();
      String lookupPath = deployURI.getPath();
      log.debug("JNDI lookup: " + lookupPath);
      log.trace("Creating InitialContext with env: " + env);
      InitialContext ctx = new InitialContext(env);
      MBeanServerConnection server = (MBeanServerConnection)ctx.lookup(lookupPath);
      return server;
   
public java.lang.StringgetName()
Get the target's name

return
the name

      return deployURI.toString();
   
private voidparseQuery()
Parse the query portion of the deployment URI to look for options: jsr88MBean : specifies the JSR88 mbean service that provides the deployment manager deploy/start/stop/undeploy operations.

      String query = deployURI.getRawQuery();
      log.debug("DeployURI.rawQuery: " + query);
      Properties params = new Properties();
      if (query != null)
      {
         /* Break the raw query into the name=value pairs. This processing is
          too fragile to how the URI was built as it expects that only
          the name/value portions of the query were encoded.
          */
         String[] options = query.split("[&=]");
         for (int n = 0; n < options.length; n += 2)
         {
            String name = URLDecoder.decode(options[n], "UTF-8");
            String value = URLDecoder.decode(options[n + 1], "UTF-8");
            params.setProperty(name, value);
         }
      }

      String name = params.getProperty(JSR88_MBEAN_OPT, JSR88_MBEAN);
      jsr88MBean = new ObjectName(name);
   
public voidstart(javax.enterprise.deploy.spi.TargetModuleID targetModuleID)
Start a given module

      MBeanServerConnection server = getMBeanServerConnection();
      URL url = new URL(targetModuleID.getModuleID());
      Object[] args = { url };
      String[] sig = { URL.class.getName() };
      log.debug("Start: " + url);
      args = new Object[] { url.toExternalForm() };
      sig = new String[] { String.class.getName() };
      log.info("Begin start: " + url);
      server.invoke(jsr88MBean, "start", args, sig);
      log.info("End start");
   
public voidstop(javax.enterprise.deploy.spi.TargetModuleID targetModuleID)
Stop a given module

      MBeanServerConnection server = getMBeanServerConnection();
      URL url = new URL(targetModuleID.getModuleID());
      Object[] args = { url };
      String[] sig = { URL.class.getName() };
      log.debug("Stop: " + url);
      args = new Object[] { url.toExternalForm() };
      sig = new String[] { String.class.getName() };
      log.info("Begin stop: " + url);
      server.invoke(jsr88MBean, "stop", args, sig);
      log.info("End stop");
   
public voidundeploy(javax.enterprise.deploy.spi.TargetModuleID targetModuleID)
Undeploy a given module

      MBeanServerConnection server = getMBeanServerConnection();
      String url = targetModuleID.getModuleID();
      Object[] args = { url };
      String[] sig = { String.class.getName() };
      log.info("Begin undeploy: " + url);
      server.invoke(jsr88MBean, "undeploy", args, sig);
      log.info("End undeploy");