[jboss-cvs] JBossAS SVN: r103438 - in trunk: testsuite/src/main/org/jboss/test/util/test and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Apr 1 10:57:03 EDT 2010


Author: smarlow at redhat.com
Date: 2010-04-01 10:57:02 -0400 (Thu, 01 Apr 2010)
New Revision: 103438

Modified:
   trunk/console/src/main/java/org/jboss/console/twiddle/Twiddle.java
   trunk/testsuite/src/main/org/jboss/test/util/test/TwiddleUnitTestCase.java
Log:
JBAS-7825 Port Twiddle to JSR-160 changes

Modified: trunk/console/src/main/java/org/jboss/console/twiddle/Twiddle.java
===================================================================
--- trunk/console/src/main/java/org/jboss/console/twiddle/Twiddle.java	2010-04-01 14:56:48 UTC (rev 103437)
+++ trunk/console/src/main/java/org/jboss/console/twiddle/Twiddle.java	2010-04-01 14:57:02 UTC (rev 103438)
@@ -25,28 +25,27 @@
 import gnu.getopt.LongOpt;
 
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.PrintWriter;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
-
 import javax.management.MBeanServerConnection;
-import javax.naming.Context;
-import javax.naming.InitialContext;
+import javax.management.remote.JMXConnector;
+import javax.management.remote.JMXConnectorFactory;
+import javax.management.remote.JMXServiceURL;
 import javax.naming.NamingException;
 
 import org.jboss.console.twiddle.command.Command;
 import org.jboss.console.twiddle.command.CommandContext;
 import org.jboss.console.twiddle.command.CommandException;
 import org.jboss.console.twiddle.command.NoSuchCommandException;
-import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
 import org.jboss.logging.Logger;
 import org.jboss.security.SecurityAssociation;
 import org.jboss.security.SimplePrincipal;
@@ -64,7 +63,12 @@
 {
    public static final String PROGRAM_NAME = System.getProperty("program.name", "twiddle");
    public static final String CMD_PROPERTIES = "/org/jboss/console/twiddle/commands.properties";
-   public static final String DEFAULT_JNDI_NAME = "jmx/invoker/RMIAdaptor";
+   public static final String DEFAULT_BASEURL = "service:jmx:rmi:///jndi/rmi://";
+   public static final String DEFAULT_RMIOBJECTNAME = "/jmxrmi";
+   public static final String DEFAULT_JMXSERVICEURL = "service:jmx:rmi:///jndi/rmi://localhost:1090/jmxrmi";
+   public static final String DEFAULT_HOSTNAME = "localhost";
+   public static final String DEFAULT_PORT ="1090";
+
    private static final Logger log = Logger.getLogger(Twiddle.class);
    // Command Line Support
    private static Twiddle twiddle = new Twiddle(new PrintWriter(System.out, true),
@@ -78,10 +82,14 @@
    private Map commandProtoMap = new HashMap();
    private PrintWriter out;
    private PrintWriter err;
-   private String serverURL;
-   private String adapterName;
+   private String serverURL = buildJMXServiceUrl();
+   private String hostname;
+   private String port;
    private boolean quiet;
    private MBeanServerConnection server;
+   private String username;
+   private String password;
+   private boolean verbose;
 
    public Twiddle(final PrintWriter out, final PrintWriter err)
    {
@@ -89,16 +97,43 @@
       this.err = err;
    }
 
-   public void setServerURL(final String url)
+   // build JMXServiceURL, should look like  "service:jmx:rmi:///jndi/rmi://localhost:1090/jmxrmi";
+   private String buildJMXServiceUrl()
    {
-      this.serverURL = url;
+      return
+         DEFAULT_BASEURL +
+         (hostname != null ? hostname : DEFAULT_HOSTNAME) +
+         ":" +
+         (port != null ? port : DEFAULT_PORT) +
+         DEFAULT_RMIOBJECTNAME;
    }
 
-   public void setAdapterName(final String name)
+   public void setHostname(String hostname)
    {
-      this.adapterName = name;
+      this.hostname = hostname;
+      setServerURL(buildJMXServiceUrl());    // use new hostname
    }
 
+   public void setPort(String port)
+   {
+      this.port = port;
+      setServerURL(buildJMXServiceUrl());  // use new port setting
+   }
+
+   public void setVerbose( boolean verbose )
+   {
+      this.verbose = verbose;
+   }
+
+   public void setServerURL(final String url)
+   {
+      if (verbose)
+      {
+         log.info("replacing JMX service url (" + this.serverURL + ") with " + url);
+      }
+      this.serverURL = url;
+   }
+
    public void setQuiet(final boolean flag)
    {
       this.quiet = flag;
@@ -227,42 +262,21 @@
    }
 
    private MBeanServerConnection createMBeanServerConnection()
-      throws NamingException
+      throws NamingException, IOException
    {
-      InitialContext ctx;
-
-      if (serverURL == null)
-      {
-         ctx = new InitialContext();
-      }
-      else
-      {
-         Hashtable props = new Hashtable(System.getProperties());
-         props.put(Context.PROVIDER_URL, serverURL);
-         ctx = new InitialContext(props);
-      }
-
-      // if adapter is null, the use the default
-      if (adapterName == null)
-      {
-         adapterName = DEFAULT_JNDI_NAME;
-      }
-
-      Object obj = ctx.lookup(adapterName);
-      ctx.close();
-
-      if (!(obj instanceof RMIAdaptor))
-      {
-         throw new ClassCastException
-            ("Object not of type: RMIAdaptorImpl, but: " +
-            (obj == null ? "not found" : obj.getClass().getName()));
-      }
-
-      return (MBeanServerConnection) obj;
+      HashMap env = new HashMap();
+      JMXServiceURL url = new JMXServiceURL(this.serverURL);
+      JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
+      //Subject delegationSubject =
+      //   new Subject(true,
+      //      Collections.singleton(new JMXPrincipal("delegate")),
+      //      Collections.EMPTY_SET,
+      //      Collections.EMPTY_SET);
+      return jmxc.getMBeanServerConnection(SecurityAssociation.getSubject());
    }
 
    private void connect()
-      throws NamingException
+      throws NamingException, IOException
    {
       if (server == null)
       {
@@ -392,11 +406,15 @@
       out.println("    -c=command.properties     Specify the command.properties file to use");
       out.println("    -D<name>[=<value>]        Set a system property");
       out.println("    --                        Stop processing options");
-      out.println("    -s, --server=<url>        The JNDI URL of the remote server");
-      out.println("    -a, --adapter=<name>      The JNDI name of the RMI adapter to use");
+      out.println("    -s, --server=<url>        The JMX service URL of the remote server (e.g. "+ DEFAULT_JMXSERVICEURL +") ");
+      out.println("    -o, --host=<HOSTNAME>     The name of the remote server (e.g. "+ DEFAULT_HOSTNAME +") ");
+      out.println("    -r, --port=<PORTNUMBER>   The rmiRegistryPort of the remote server (e.g. "+ DEFAULT_PORT +") ");
       out.println("    -u, --user=<name>         Specify the username for authentication");
       out.println("    -p, --password=<name>     Specify the password for authentication"); 
       out.println("    -q, --quiet               Be somewhat more quiet");
+      out.println("    -v, --verbose             Be noisy");
+      out.println();
+      out.println("for convenience, you can specify --host and --port but not with the --server=<url> which overrides host + port. ");
       out.flush();
    }
 
@@ -407,7 +425,7 @@
          if (!logPassword(args, a))
             log.debug("args["+a+"]="+args[a]);
       }
-      String sopts = "-:hH:u:p:c:D:s:a:q";
+      String sopts = "-:hH:u:p:c:D:s:a:q::v::o:r:";
       LongOpt[] lopts =
          {
             new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'),
@@ -415,7 +433,10 @@
             new LongOpt("server", LongOpt.REQUIRED_ARGUMENT, null, 's'),
             new LongOpt("adapter", LongOpt.REQUIRED_ARGUMENT, null, 'a'),
             new LongOpt("quiet", LongOpt.NO_ARGUMENT, null, 'q'),
+            new LongOpt("verbose", LongOpt.NO_ARGUMENT, null, 'v'),
             new LongOpt("user", LongOpt.REQUIRED_ARGUMENT, null, 'u'),
+            new LongOpt("host", LongOpt.REQUIRED_ARGUMENT, null, 'o'),
+            new LongOpt("port", LongOpt.REQUIRED_ARGUMENT, null, 'r'),
             new LongOpt("password", LongOpt.REQUIRED_ARGUMENT, null, 'p'),
          };
 
@@ -518,14 +539,25 @@
                     break;
                  }
 
+                 // host name
+              case 'o':
+                 twiddle.setHostname(getopt.getOptarg());
+                 break;
+
+              // host port
+              case 'r':
+                 twiddle.setPort(getopt.getOptarg());
+                 break;
+
                  // Set the JNDI server URL
               case 's':
                  twiddle.setServerURL(getopt.getOptarg());
                  break;
 
-                 // Set the adapter JNDI name
+                 // adapter JNDI name is not supported anymore
               case 'a':
-                 twiddle.setAdapterName(getopt.getOptarg());
+                 String arg = getopt.getOptarg();
+                 log.info("adapter name is ignored " + arg);
                  break;
               case 'u':
                  String username = getopt.getOptarg();
@@ -536,6 +568,11 @@
                  SecurityAssociation.setCredential(password);
                  break;
 
+              // be noisy
+              case 'v':
+                 twiddle.setVerbose(true);
+                 break;
+
               // Enable quiet operations
               case 'q':
                  twiddle.setQuiet(true);
@@ -574,4 +611,5 @@
       }
       return false;
    }
+
 }

Modified: trunk/testsuite/src/main/org/jboss/test/util/test/TwiddleUnitTestCase.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/util/test/TwiddleUnitTestCase.java	2010-04-01 14:56:48 UTC (rev 103437)
+++ trunk/testsuite/src/main/org/jboss/test/util/test/TwiddleUnitTestCase.java	2010-04-01 14:57:02 UTC (rev 103438)
@@ -107,7 +107,7 @@
         command.add("cmd");
         command.add("/C");
         command.add("twiddle");
-        command.add("-s");
+        command.add("-o ");
         command.add(getServerHost());
         command.addAll(Arrays.asList(args));
      }
@@ -116,7 +116,7 @@
         command.add("/bin/sh");
         command.add("-c");
         String twiddleCmd = "./twiddle.sh ";
-        twiddleCmd += "-s ";
+        twiddleCmd += "-o ";
         twiddleCmd += getServerHost();
         twiddleCmd += makeTwiddleArgs(args);
         command.add(twiddleCmd);




More information about the jboss-cvs-commits mailing list