[jboss-cvs] JBossAS SVN: r114611 - in branches/JBPAPP_5: testsuite/src/main/org/jboss/test/scripts/test and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Dec 19 02:19:42 EST 2013


Author: istudens at redhat.com
Date: 2013-12-19 02:19:42 -0500 (Thu, 19 Dec 2013)
New Revision: 114611

Modified:
   branches/JBPAPP_5/server/src/main/org/jboss/Shutdown.java
   branches/JBPAPP_5/testsuite/src/main/org/jboss/test/scripts/test/ShutdownTestCase.java
Log:
JBPAPP-10912 Provide credentials via a property file for the shutdown script

Modified: branches/JBPAPP_5/server/src/main/org/jboss/Shutdown.java
===================================================================
--- branches/JBPAPP_5/server/src/main/org/jboss/Shutdown.java	2013-12-19 06:42:08 UTC (rev 114610)
+++ branches/JBPAPP_5/server/src/main/org/jboss/Shutdown.java	2013-12-19 07:19:42 UTC (rev 114611)
@@ -21,6 +21,8 @@
  */
 package org.jboss;
 
+import java.io.FileInputStream;
+import java.io.IOException;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
@@ -34,7 +36,6 @@
 import javax.naming.Context;
 import javax.naming.InitialContext;
 
-
 import gnu.getopt.Getopt;
 import gnu.getopt.LongOpt;
 
@@ -46,6 +47,8 @@
 import org.jboss.mx.util.ObjectNameFactory;
 
 import java.io.Console;
+import java.util.Properties;
+
 /**
  * A JMX client that uses an MBeanServerConnection to shutdown a remote JBoss
  * server.
@@ -69,19 +72,23 @@
       System.out.println("usage: " + PROGRAM_NAME + " [options] <operation>");
       System.out.println();
       System.out.println("options:");
-      System.out.println("    -h, --help                Show this help message (default)");
-      System.out.println("    -D<name>[=<value>]        Set a system property");
-      System.out.println("    --                        Stop processing options");
-      System.out.println("    -s, --server=<url>        Specify the JNDI URL of the remote server");
-      System.out.println("    -n, --serverName=<url>    Specify the JMX name of the ServerImpl");
-      System.out.println("    -a, --adapter=<name>      Specify JNDI name of the MBeanServerConnection to use");
-      System.out.println("    -u, --user=<name>         Specify the username for authentication");
-      System.out.println("    -p, --password=<name>     Specify the password for authentication");
+      System.out.println("    -h, --help                    Show this help message (default)");
+      System.out.println("    -D<name>[=<value>]            Set a system property");
+      System.out.println("    --                            Stop processing options");
+      System.out.println("    -s, --server=<url>            Specify the JNDI URL of the remote server");
+      System.out.println("    -n, --serverName=<url>        Specify the JMX name of the ServerImpl");
+      System.out.println("    -a, --adapter=<name>          Specify JNDI name of the MBeanServerConnection to use");
+      System.out.println("    -u, --user=<name>             Specify the username for authentication");
+      System.out.println("    -p, --password=<name>         Specify the password for authentication");
+      System.out.println("    -P, --properties=<filename>   Load options from a property file");
+      System.out.println("                                  Command line options override duplicates from the file");
+      System.out.println("                                  Options: shutdown.user, shutdown.password,");
+      System.out.println("                                  shutdown.server, shutdown.serverName, shutdown.adapter");
       System.out.println();
       System.out.println("operations:");
-      System.out.println("    -S, --shutdown            Shutdown the server");
-      System.out.println("    -e, --exit=<code>         Force the VM to exit with a status code");
-      System.out.println("    -H, --halt=<code>         Force the VM to halt with a status code");
+      System.out.println("    -S, --shutdown                Shutdown the server");
+      System.out.println("    -e, --exit=<code>             Force the VM to exit with a status code");
+      System.out.println("    -H, --halt=<code>             Force the VM to halt with a status code");
       System.out.println();
    }
 
@@ -93,7 +100,7 @@
          System.exit(0);
       }
       
-      String sopts = "-:hD:s:n:a:u:p:Se:H:";
+      String sopts = "-:hD:s:n:a:u:p:P:Se:H:";
       LongOpt[] lopts =
       {
          new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'),
@@ -105,6 +112,7 @@
          new LongOpt("halt", LongOpt.REQUIRED_ARGUMENT, null, 'H'),
          new LongOpt("user", LongOpt.REQUIRED_ARGUMENT, null, 'u'),
          new LongOpt("password", LongOpt.REQUIRED_ARGUMENT, null, 'p'),
+         new LongOpt("properties", LongOpt.REQUIRED_ARGUMENT, null, 'P'),
       };
 
       Getopt getopt = new Getopt(PROGRAM_NAME, args, sopts, lopts);
@@ -113,12 +121,15 @@
 
       String serverURL = null;
       String adapterName = "jmx/rmi/RMIAdaptor";
+      String clAdapterName = null;
       String username = null;
       String password = null;
       ObjectName serverJMXName = ObjectNameFactory.create("jboss.system:type=JVMShutdown");
+      String clServerJMXName = null;
       boolean exit = false;
       boolean halt = false;
       int exitcode = -1;
+      String propertyFile = null;
 
       while ((code = getopt.getopt()) != -1)
       {
@@ -163,22 +174,23 @@
                serverURL = getopt.getOptarg();
                break;
             case 'n':
-               serverJMXName = new ObjectName(getopt.getOptarg());
+               clServerJMXName = getopt.getOptarg();
                break;
             case 'S':
                // nothing...
                break;
             case 'a':
-               adapterName = getopt.getOptarg();
+               clAdapterName = getopt.getOptarg();
                break;
             case 'u':
                username = getopt.getOptarg();
-               SecurityAssociation.setPrincipal(new SimplePrincipal(username));
                break;
             case 'p':
                password = getopt.getOptarg();
-               SecurityAssociation.setCredential(password);
                break;
+            case 'P':
+ 	 	 	   propertyFile = getopt.getOptarg();
+ 	 	 	   break;
             case 'e':
                exitcode = Integer.parseInt(getopt.getOptarg());
                exit = true;
@@ -189,7 +201,35 @@
                break;
          }
       }
-      
+
+      // cmd line options override the property file entries
+      if( propertyFile != null )
+      {
+         Properties props = loadPropertyFile(propertyFile);
+         if( props != null )
+         {
+            if( username == null && props.containsKey("shutdown.user") )
+               username = props.getProperty("shutdown.user");
+            if( password == null && props.containsKey("shutdown.password") )
+               password = props.getProperty("shutdown.password");
+            if( serverURL == null && props.containsKey("shutdown.server") )
+               serverURL = props.getProperty("shutdown.server");
+            if( clServerJMXName != null )
+               serverJMXName = new ObjectName(clServerJMXName);
+            else if( props.containsKey("shutdown.serverName") )
+               serverJMXName = new ObjectName(props.getProperty("shutdown.serverName"));
+            if( clAdapterName != null )
+               adapterName = clAdapterName;
+            else if( props.containsKey("shutdown.adapter") )
+               adapterName = props.getProperty("shutdown.adapter");
+         }
+      }
+
+      if( username != null )
+         SecurityAssociation.setPrincipal(new SimplePrincipal(username));
+      if( password != null )
+         SecurityAssociation.setCredential(password);
+
       InitialContext ctx;
 
       // If there was a username specified, but no password prompt for it
@@ -251,6 +291,27 @@
       System.out.println("Server shutdown may take a while - check logfiles for completion");
    }
 
+   /*
+    * Options on the command line take precedence.
+    */
+   private static Properties loadPropertyFile(final String propertyFile) throws IOException
+   {
+      Properties fileProps = new Properties();
+      FileInputStream in = null;
+      try
+      {
+         in = new FileInputStream(propertyFile);
+         fileProps.load(in);
+         return fileProps;
+      }
+      finally
+      {
+         if( in != null )
+            try { in.close(); } catch (Exception e) {}
+      }
+   }
+
+
    private static class ServerProxyHandler implements InvocationHandler
    {
       ObjectName serverName;

Modified: branches/JBPAPP_5/testsuite/src/main/org/jboss/test/scripts/test/ShutdownTestCase.java
===================================================================
--- branches/JBPAPP_5/testsuite/src/main/org/jboss/test/scripts/test/ShutdownTestCase.java	2013-12-19 06:42:08 UTC (rev 114610)
+++ branches/JBPAPP_5/testsuite/src/main/org/jboss/test/scripts/test/ShutdownTestCase.java	2013-12-19 07:19:42 UTC (rev 114611)
@@ -23,7 +23,9 @@
 package org.jboss.test.scripts.test;
 
 import java.io.File ;
+import java.io.FileWriter;
 import java.io.IOException ;
+import java.util.Properties;
 import javax.management.ObjectName ;
 import javax.management.MalformedObjectNameException ;
 
@@ -163,6 +165,63 @@
 	   }
    }  
 
+   /**
+    * Tests shutdown command with -S option
+    *
+    * @throws Exception
+    */
+   public void testPropertiesNonLoopbackShutdown() throws Exception
+   {
+	   // build the shell command to execute
+	   // supply the command name prefix, any options as a string, and any args
+	   String[] shellCommand = getShellCommand("run","-c " + getServerConfig() + " -b " + getServerHost(), null) ;
+	   String[] envp = null ;
+	   File workingDir = new File(getBinDir()) ;
+
+	   // execute command
+	   getAsyncShellScriptExecutor().startShellCommand(shellCommand, envp, workingDir) ;
+
+	   // waitForServerStart kills the process and throws an exception if server does not start
+	   try {
+		   waitForServerStart(getAsyncShellScriptExecutor(), getServerHost(), START_TIMEOUT) ;
+		   System.out.println("Server started successfully") ;
+	   }
+	   catch(IOException e) {
+		   System.out.println("IOException: message = " + e.getMessage()) ;
+		   writeServerLogsToTestCase() ;
+		   fail("Server failed to start") ;
+	   }
+
+	   // check assertions on the console output generated by the run command
+	   getAsyncShellScriptExecutor().assertOnOutputStream("Started in","Started string not found in command output") ;
+
+	   File propFile = new File("shutdown.properties");
+
+	   // set properties
+	   Properties props = new Properties();
+	   props.setProperty("shutdown.server", getJndiURL());
+	   props.store(new FileWriter(propFile), null);
+
+	   // shutdown the server using the shutdown command
+	   String[] shutdownCommand = getShellCommand("shutdown", "-P " + propFile.getCanonicalPath() + " -S", null) ;
+	   getShellScriptExecutor().runShellCommand(shutdownCommand, envp, workingDir) ;
+
+	   System.out.println("shutdown output = " + getShellScriptExecutor().getOutput()) ;
+	   System.out.println("shutdown error = " + getShellScriptExecutor().getError()) ;
+
+	   // waitForServerStop kills the process and throws an exception if server does not stop
+	   try {
+		   waitForServerStop(getAsyncShellScriptExecutor(), STOP_TIMEOUT) ;
+		   System.out.println("Server stopped successfully") ;
+		   getAsyncShellScriptExecutor().assertOnOutputStream(SERVER_STOPPED_MESSAGE,"Server shutdown message did not appear in logs") ;
+	   }
+	   catch(IOException e) {
+		   System.out.println("IOException: message = " + e.getMessage()) ;
+		   writeServerLogsToTestCase() ;
+		   fail("Server failed to stop") ;
+	   }
+   }
+
    /* for the two following tests, see JBPAPP-3035 */
    
    /**



More information about the jboss-cvs-commits mailing list