[jboss-cvs] JBossAS SVN: r70293 - in projects: test/trunk/src/main/java/org/jboss/ant/taskdefs/server and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sat Mar 1 17:28:37 EST 2008


Author: ALRubinger
Date: 2008-03-01 17:28:37 -0500 (Sat, 01 Mar 2008)
New Revision: 70293

Added:
   projects/server-manager/trunk/src/main/java/org/jboss/servermanager/Argument.java
   projects/server-manager/trunk/src/main/java/org/jboss/servermanager/Property.java
   projects/server-manager/trunk/src/main/java/org/jboss/servermanager/Server.java
   projects/server-manager/trunk/src/main/java/org/jboss/servermanager/ServerController.java
   projects/server-manager/trunk/src/main/java/org/jboss/servermanager/ServerManager.java
   projects/server-manager/trunk/src/main/java/org/jboss/servermanager/ServerShutdownException.java
Removed:
   projects/server-manager/trunk/src/main/java/org/jboss/servermanager/server/
Modified:
   projects/test/trunk/src/main/java/org/jboss/ant/taskdefs/server/ConfigManagerTask.java
   projects/test/trunk/src/main/java/org/jboss/ant/taskdefs/server/StartServerTask.java
   projects/test/trunk/src/main/java/org/jboss/ant/taskdefs/server/StopServerTask.java
Log:
Moved packages

Copied: projects/server-manager/trunk/src/main/java/org/jboss/servermanager/Argument.java (from rev 70290, projects/server-manager/trunk/src/main/java/org/jboss/servermanager/server/Argument.java)
===================================================================
--- projects/server-manager/trunk/src/main/java/org/jboss/servermanager/Argument.java	                        (rev 0)
+++ projects/server-manager/trunk/src/main/java/org/jboss/servermanager/Argument.java	2008-03-01 22:28:37 UTC (rev 70293)
@@ -0,0 +1,63 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.servermanager;
+
+/**
+ * A command line argument.
+ * 
+ * @author <a href="ryan.campbell at jboss.com">Ryan Campbell</a>
+ * @version $Revision$
+ */
+public class Argument
+{
+   String value;
+
+   /**
+    * Get the value.
+    * 
+    * @return the value.
+    */
+   public String getValue()
+   {
+      return value;
+   }
+
+   /**
+    * Set the value.
+    * 
+    * @param value The value to set.
+    */
+   public void setValue(String value)
+   {
+      this.value = value;
+   }
+   
+   /**
+    * Set the line as the value.
+    * 
+    * @param line
+    */
+   public void setLine(String line)
+   {
+      this.value = line;
+   }
+}
\ No newline at end of file


Property changes on: projects/server-manager/trunk/src/main/java/org/jboss/servermanager/Argument.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Copied: projects/server-manager/trunk/src/main/java/org/jboss/servermanager/Property.java (from rev 70290, projects/server-manager/trunk/src/main/java/org/jboss/servermanager/server/Property.java)
===================================================================
--- projects/server-manager/trunk/src/main/java/org/jboss/servermanager/Property.java	                        (rev 0)
+++ projects/server-manager/trunk/src/main/java/org/jboss/servermanager/Property.java	2008-03-01 22:28:37 UTC (rev 70293)
@@ -0,0 +1,77 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.servermanager;
+
+/**
+ * A Property.
+ * 
+ * @author <a href="ryan.campbell at jboss.com">Ryan Campbell</a>
+ * @version $Revision$
+ */
+public class Property
+{
+
+   private String key;
+   /**
+    * Get the key.
+    * 
+    * @return the key.
+    */
+   public String getKey()
+   {
+      return key;
+   }
+   /**
+    * Set the key.
+    * 
+    * @param key The key to set.
+    */
+   public void setKey(String key)
+   {
+      this.key = key;
+   }
+   /**
+    * Get the value.
+    * 
+    * @return the value.
+    */
+   public String getValue()
+   {
+      return value;
+   }
+   /**
+    * Set the value.
+    * 
+    * @param value The value to set.
+    */
+   public void setValue(String value)
+   {
+      this.value = value;
+   }
+   private String value;
+   
+   public Property()
+   {
+
+   }
+   
+}


Property changes on: projects/server-manager/trunk/src/main/java/org/jboss/servermanager/Property.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Copied: projects/server-manager/trunk/src/main/java/org/jboss/servermanager/Server.java (from rev 70290, projects/server-manager/trunk/src/main/java/org/jboss/servermanager/server/Server.java)
===================================================================
--- projects/server-manager/trunk/src/main/java/org/jboss/servermanager/Server.java	                        (rev 0)
+++ projects/server-manager/trunk/src/main/java/org/jboss/servermanager/Server.java	2008-03-01 22:28:37 UTC (rev 70293)
@@ -0,0 +1,487 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.servermanager;
+
+import java.io.File;
+import java.io.PrintWriter;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * A Server.
+ *
+ * @author <a href="ryan.campbell at jboss.com">Ryan Campbell</a>
+ * @version $Revision$
+ */
+public class Server
+{
+   /** the handle for the server **/
+   private String name;
+
+   /** the config to start **/
+   private String config;
+
+   /** jmx username **/
+   private String username;
+
+   /** jmx password **/
+   private String password;
+
+   /** the arguments to pass to jboss **/
+   private List<Argument> arguments = new ArrayList<Argument>();
+
+   /** the server's process, if running **/
+   private Process process;
+
+   /** the arguments for the jvm **/
+   private List<Argument> jvmArguments = new ArrayList<Argument>();
+
+   /** system properties for the jvm **/
+   private List<Property> sysProperties = new ArrayList<Property>();
+
+   /** the port used to determine if jboss started **/
+   private Integer httpPort = new Integer(8080);
+
+   /** where to find the rmi port **/
+   private Integer rmiPort = new Integer(1099);
+
+   /** the name or IP address to bind to **/
+   private String host = "localhost";
+
+   /** used for global config info **/
+   private ServerManager manager;
+
+   /** the output log **/
+   private PrintWriter outWriter;
+
+   /** the error log **/
+   private PrintWriter errorWriter;
+
+   /** Is there a servlet engine? **/
+   private boolean hasWebServer = true;
+
+   /**
+    * Get the name.
+    *
+    * @return the name.
+    */
+   public String getName()
+   {
+      return name;
+   }
+
+   /**
+    * Set the name.
+    *
+    * @param name The name to set.
+    */
+   public void setName(String name)
+   {
+      this.name = name;
+   }
+
+   /**
+    * Get username to pass to org.jboss.Shutdown using
+    * the -u option.
+    *
+    * @return the server jmx username
+    */
+   public String getUsername()
+   {
+      return username;
+   }
+
+   /**
+    * Set username to pass to org.jboss.Shutdown using
+    * the -u option.
+    *
+    * @param username the server jmx username
+    */
+   public void setUsername(String username)
+   {
+      this.username=username;
+   }
+
+   /**
+    * Get password to pass to org.jboss.Shutdown using
+    * the -p option.
+    *
+    * @return the server jmx password
+    */
+
+   public String getPassword()
+   {
+      return password;
+   }
+
+   /**
+    * Set password to pass to org.jboss.Shutdown using
+    * the -p option.
+    *
+    * @param password the server jmx password
+    */
+   public void setPassword(String password)
+   {
+      this.password=password;
+   }
+
+   /**
+    * Set the manager.
+    * @param manager
+    */
+   protected void setManager(ServerManager manager)
+   {
+      this.manager = manager;
+   }
+
+   /**
+    * Add an argument.
+    *
+    * @param arg
+    */
+   public void addArg(Argument arg)
+   {
+      arguments.add(arg);
+   }
+
+   /**
+    * Get the arguments as a string for the command line.
+    *
+    * @return the arguments as a string
+    */
+   public String getArgs()
+   {
+      StringBuffer args = new StringBuffer();
+      for (Iterator iter = arguments.iterator(); iter.hasNext();)
+      {
+         Argument argument = (Argument) iter.next();
+         args.append(argument.getValue() + " ");
+      }
+      return args.toString();
+   }
+
+   /**
+    * Add a jvm arg.
+    *
+    * @param arg
+    */
+   public void addJvmArg(Argument arg)
+   {
+      jvmArguments.add(arg);
+   }
+
+   /**
+    * Get the JVM args for the command line.
+    *
+    * @return the arguments as a string
+    */
+   public String getJvmArgs()
+   {
+      StringBuffer args = new StringBuffer();
+      for (Iterator iter = jvmArguments.iterator(); iter.hasNext();)
+      {
+         Argument argument = (Argument) iter.next();
+         args.append(argument.getValue() + " ");
+      }
+      return args.toString();
+   }
+
+   /**
+    * Add a system property.
+    *
+    * @param property
+    */
+   public void addSysProperty(Property property)
+   {
+      sysProperties.add(property);
+   }
+
+   /**
+    * Get the system properties for the command line.
+    *
+    * @return the properties as a string
+    */
+   public String getSysProperties()
+   {
+      StringBuffer args = new StringBuffer();
+      for (Iterator iter = sysProperties.iterator(); iter.hasNext();)
+      {
+         Property property = (Property) iter.next();
+         args.append("-D" + property.getKey() + "=" + property.getValue() + " ");
+      }
+      return args.toString();
+   }
+
+   /**
+    * The running process of this server.
+    * @param process
+    */
+   public void setProcess(Process process)
+   {
+      this.process = process;
+   }
+
+   /**
+    * Is the server actually running?
+    *
+    * @return whether the server is running
+    */
+   public boolean isRunning()
+   {
+      if (isStopped())
+      {
+         return false;
+      }
+      else
+      {
+         try
+         {
+            //exitValue() only returns if process has ended.
+            process.exitValue();
+            return false;
+         }
+         catch (IllegalThreadStateException e)
+         {
+            return true;
+         }
+      }
+   }
+
+   /**
+    * Has the server been intentionally stopped?
+    *
+    * @return whether the server is stopped
+    */
+   public boolean isStopped()
+   {
+      return process == null;
+   }
+
+   /**
+    * Get the process.
+    *
+    * @return the process
+    */
+   public Process getProcess()
+   {
+      return process;
+   }
+
+   /**
+    * Where is the HTTP service listening?
+    *
+    * @return whether the service is listening
+    * @throws MalformedURLException for a malformed url
+    */
+   public URL getHttpUrl() throws MalformedURLException
+   {
+      return new URL("http://" + host + ":" + httpPort);
+   }
+
+   /**
+    * The URl for the RMI listener.
+    *
+    * @return the rmi url
+    */
+   public String getRmiUrl()
+   {
+      return "jnp://" + host + ":" + rmiPort;
+   }
+
+   /**
+    * Get the config. Defaults to the server name.
+    *
+    * @return the config.
+    */
+   public String getConfig()
+   {
+      if (config != null)
+      {
+         return config;
+      }
+      else
+      {
+         return name;
+      }
+   }
+
+   /**
+    * Set the config.
+    *
+    * @param config The config to set.
+    */
+   public void setConfig(String config)
+   {
+      this.config = config;
+   }
+
+   /**
+    * Get the host.
+    *
+    * @return the host.
+    */
+   public String getHost()
+   {
+      return host;
+   }
+
+   /**
+    * Set the host.
+    *
+    * @param host The host to set.
+    */
+   public void setHost(String host)
+   {
+      this.host = host;
+   }
+
+   /**
+    * Set the httpPort.
+    *
+    * @param httpPort The httpPort to set.
+    */
+   public void setHttpPort(Integer httpPort)
+   {
+      this.httpPort = httpPort;
+   }
+
+   /**
+    * Set the rmiPort.
+    *
+    * @param rmiPort The rmiPort to set.
+    */
+   public void setRmiPort(Integer rmiPort)
+   {
+      this.rmiPort = rmiPort;
+   }
+
+   /**
+    * Get the rmiPort
+    *
+    * @return the rmi port
+    */
+   public Integer getRmiPort()
+   {
+      return rmiPort;
+   }
+   /**
+    * Where should the server's std err log go?
+    *
+    * @return the error log file
+    */
+   public File getErrorLog()
+   {
+      return new File(getLogDir(), "error.log");
+   }
+
+   /**
+    * Where should the servers's std out go?
+    *
+    * @return the output log file
+    */
+   public File getOutputLog()
+   {
+      return new File(getLogDir(), "output.log");
+   }
+
+   /**
+    * The server's log directory
+    *
+    * @return the log directory
+    */
+   private File getLogDir()
+   {
+      return new File(getConfDir(), "log");
+   }
+
+   /**
+    * The server's directory (ie, all, default)
+    *
+    * @return the configuration directory
+    */
+   private File getConfDir()
+   {
+      return new File(manager.getJBossHome(), "server/" + getConfig());
+   }
+
+   /**
+    * Set the output log's writer
+    *
+    * @param outlog the log writer
+    */
+   public void setOutWriter(PrintWriter outlog)
+   {
+      outWriter = outlog;
+   }
+
+   /**
+    * The writer for the output log.
+    *
+    * @return the output writer
+    */
+   public PrintWriter getOutWriter()
+   {
+      return outWriter;
+   }
+
+   /**
+    * The error log's writer.
+    *
+    * @return the log writer
+    */
+   public PrintWriter getErrorWriter()
+   {
+      return errorWriter;
+   }
+
+   /**
+    * Set the error writer.
+    * @param errorlog
+    */
+   public void setErrorWriter(PrintWriter errorlog)
+   {
+      errorWriter = errorlog;
+   }
+
+   /**
+    * Get the hasWebServer.
+    *
+    * @return the hasWebServer.
+    */
+   public boolean hasWebServer()
+   {
+      return hasWebServer;
+   }
+   /**
+    * Set the hasWebServer.
+    *
+    * @param hasWebServer The hasWebServer to set.
+    */
+   public void setHasWebServer(boolean hasWebServer)
+   {
+      this.hasWebServer = hasWebServer;
+   }
+
+}
\ No newline at end of file


Property changes on: projects/server-manager/trunk/src/main/java/org/jboss/servermanager/Server.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Copied: projects/server-manager/trunk/src/main/java/org/jboss/servermanager/ServerController.java (from rev 70290, projects/server-manager/trunk/src/main/java/org/jboss/servermanager/server/ServerController.java)
===================================================================
--- projects/server-manager/trunk/src/main/java/org/jboss/servermanager/ServerController.java	                        (rev 0)
+++ projects/server-manager/trunk/src/main/java/org/jboss/servermanager/ServerController.java	2008-03-01 22:28:37 UTC (rev 70293)
@@ -0,0 +1,412 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.servermanager;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.net.HttpURLConnection;
+import java.net.Socket;
+import java.net.URL;
+import java.net.URLConnection;
+
+/**
+ * Starts, stops, and (eventually) reboots server instances.
+ * 
+ * @author <a href="ryan.campbell at jboss.com">Ryan Campbell</a>
+ * @version $Revision$
+ */
+public abstract class ServerController
+{
+
+   private static final String SHUTDOWN_CLASS = "org.jboss.Shutdown";
+
+   private static final String MAIN = "org.jboss.Main"; 
+   
+   private ServerController()
+   {
+   }
+
+   /**
+    * Start the server and pump its output and error streams.
+    * 
+    * @param server
+    * @param manager
+    * @throws IOException
+    */
+   public static void startServer(Server server, ServerManager manager) throws IOException
+   {
+      if (server.isRunning())
+      {
+         throw new IllegalArgumentException("The " + server.getName() + " server is already running.");
+      }
+
+      if (isServerStarted(server))
+      {
+         throw new IOException("Found a process already listening on:" + server.getHttpUrl() + " or "+ server.getRmiUrl());
+      }
+
+      String execCmd = getStartCommandLine(server, manager);
+
+      System.out.println("Starting server \"" + server.getName() + "\" with command: \n" + execCmd);
+
+      File binDir = new File(manager.getJBossHome(), "/bin");
+      final Process process = Runtime.getRuntime().exec(execCmd, null, binDir);
+
+      final BufferedReader errStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
+      final BufferedReader inStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
+
+      final File outFile = server.getOutputLog();
+      initalizeLog(outFile);
+      final PrintWriter outlog = new PrintWriter(new FileWriter(outFile));
+      server.setOutWriter(outlog);
+
+      Thread outPump = new OutputPumper(inStream, outlog);
+      outPump.start();
+
+      final File errorFile = server.getErrorLog();
+      initalizeLog(errorFile);
+      final PrintWriter errorlog = new PrintWriter(new FileWriter(errorFile));
+      server.setErrorWriter(errorlog);
+
+      Thread errorPump = new OutputPumper(errStream, errorlog);
+      errorPump.start();
+
+      /*
+       * TODO: -TME This is a real problem.  If maintain reference
+       * to the process, even for a short period of time, willl
+       * cause the spawned process' threads to block when this process
+       * blocks.  So if uncomment following line, then the ServerTestHarness
+       * will block abnormally, thus causing the tests not to run correctly.
+       * 
+       * Is this true for our environment? - rcampbell
+       */
+      server.setProcess(process);
+
+      try
+      {
+         waitForServer(server, manager);
+      }
+      catch (IOException e)
+      {
+         server.setProcess(null);
+         throw e;
+      }
+
+   }
+
+   /** 
+    * Delete & create log files
+    * @param logFile
+    * @throws IOException
+    */
+   private static void initalizeLog(final File logFile) throws IOException
+   {
+      if (logFile.exists())
+      {
+         logFile.delete();
+      }
+      if (!logFile.getParentFile().exists())
+      {
+         logFile.getParentFile().mkdir();
+      }
+
+      logFile.createNewFile();
+   }
+
+   /** 
+    * Create the command line to execute
+    * 
+    * @param server the server
+    * @param manager the manager
+    * @return the command line
+    * @throws IOException for any error
+    */
+   private static String getStartCommandLine(Server server, ServerManager manager) throws IOException
+   {
+      String execCmd = manager.getJavaExecutable() + " -cp " + manager.getStartClasspath() + " ";
+      execCmd = execCmd + server.getJvmArgs() + server.getSysProperties();
+      execCmd = execCmd + " " + MAIN + " -c " + server.getConfig() + " -b " + server.getHost();
+      
+      if (manager.getUdpGroup() != null && ! manager.getUdpGroup().equals(""))
+      {
+         execCmd = execCmd + " -u " + manager.getUdpGroup();   
+      }
+      execCmd = execCmd + " " + server.getArgs();
+      return execCmd;
+   }
+
+   /** 
+    * Wait until the jboss instance is full initialized
+    * @param server
+    * @param manager 
+    * @throws IOException
+    */
+   private static void waitForServer(Server server, ServerManager manager) throws IOException
+   {
+
+      int tries = 0;
+      while (tries++ < manager.getStartupTimeout())
+      {
+         if (!server.isRunning())
+         {
+            throw new IOException("Server failed to start; see logs. exit code: " + server.getProcess().exitValue());
+         }
+
+         try
+         {
+            Thread.sleep(1000);
+         }
+         catch (InterruptedException e)
+         {
+         }
+         if (isServerStarted(server))
+         {
+            return;
+         }
+      }
+
+      Process process = server.getProcess();
+      System.err.println("Failed to start server \"" + server.getName()
+            + "\" before timeout. Destroying the process.");
+      process.destroy();
+
+      throw new IOException("Server failed to start; see logs.");
+
+   }
+
+   /**
+    * Check if the server is fully intialized by trying to 
+    * open a connection to tomcat.
+    * 
+    * @param server the server
+    * @return whether it is started
+    * @throws IOException for any error
+    */
+   private static boolean isServerStarted(Server server) throws IOException
+   {
+      URL url = server.getHttpUrl();
+      if (server.hasWebServer())
+      {
+         try
+         {
+            URLConnection conn = url.openConnection();
+            if (conn instanceof HttpURLConnection)
+            {
+               HttpURLConnection http = (HttpURLConnection) conn;
+               int responseCode = http.getResponseCode();
+
+               if (responseCode > 0 && responseCode < 400)
+               {
+                  return true;
+               }
+            }
+         }
+         catch (java.io.IOException e)
+         {
+            return false;
+         }
+         return false;
+      }
+      else
+      {
+         //see if the rmi port is active
+         Socket socket = null;
+         try
+         {
+            socket = new Socket(server.getHost(), server.getRmiPort().intValue());
+            return true;
+         }
+         catch (IOException e)
+         {
+            return false;
+         }
+         finally
+         {
+            if (socket != null)
+            {
+               socket.close();
+            }
+         }
+      }
+   }
+
+   /**
+    * Stop the server using shutdown.jar.
+    * Process.destroy() the server if it fails to shutdown.
+    * 
+    * @param server
+    * @param manager
+    * @throws IOException
+    */
+   public static void stopServer(Server server, ServerManager manager) throws IOException
+   {
+		boolean cleanShutdown = true;
+		
+      if (!server.isRunning())
+      {
+         throw new IllegalArgumentException("The " + server.getName() + " is not running; it cannot be stopped.");
+      }
+
+      System.out.println("Shutting down server: " + server.getName());
+
+      String shutdownCmd = getStopCommandLine(server, manager);
+      System.out.println("Shutting down server: " + shutdownCmd);
+
+      Runtime.getRuntime().exec(shutdownCmd);
+
+      Process process = server.getProcess();
+      if (!waitOnShutdown(server, manager))
+      {
+         System.err.println("Failed to shutdown server \"" + server.getName()
+               + "\" before timeout. Destroying the process.");
+         process.destroy();
+			cleanShutdown = false;
+      }
+
+      closeAllStreams(process);
+      server.getErrorWriter().close();
+      server.getOutWriter().close();
+
+      server.setProcess(null);
+      try
+      {
+         Thread.sleep(45000);
+      }
+      catch (InterruptedException e)
+      {
+      }
+		
+		if (!cleanShutdown)
+		{
+			throw new ServerShutdownException("Failed to shutdown server before timeout. Process was destroyed.");
+		}
+   }
+
+   /**
+    * Wait for the server to shutdown. 
+    * @param server
+    * @param manager 
+    * @return true if server process ends before timeout
+    */
+   private static boolean waitOnShutdown(Server server, ServerManager manager)
+   {
+      int shutdownTimeout = manager.getShutdownTimeout();
+      System.out.println("shutdownTimeout will be="+shutdownTimeout);
+      for (int tries = 0; tries < shutdownTimeout; tries++)
+      {  
+         try
+         {
+            if (!server.isRunning())
+            {
+               return true;
+            }
+            Thread.sleep(1000);
+         }
+         catch (InterruptedException e)
+         {
+         }
+      }
+
+      return false;
+   }
+
+   /** 
+    * Get the server shutdown command line.
+    * 
+    * @param server the server
+    * @param manager the manager
+    * @return the shutdown command
+    * @throws IOException for any error
+    */
+   private static String getStopCommandLine(Server server, ServerManager manager) throws IOException
+   {
+      String strAuth="";
+      String username = server.getUsername();
+      String password = server.getPassword();
+      if ( username != null && password != null )
+      {
+         strAuth = " -u " + username + " -p " + password;
+      }
+
+      String execCmd = manager.getJavaExecutable() + " -cp " + manager.getStopClasspath() + " ";
+      execCmd = execCmd + SHUTDOWN_CLASS + " --server " + server.getRmiUrl();
+      execCmd = execCmd + strAuth +" --shutdown";
+      return execCmd;
+   }
+
+   /**
+    * Close the streams of a process.
+    * 
+    * @param process
+    */
+   private static void closeAllStreams(Process process)
+   {
+      try
+      {
+         process.getInputStream().close();
+         process.getOutputStream().close();
+         process.getErrorStream().close();
+      }
+      catch (IOException e)
+      {
+      }
+   }
+
+   /**
+    * A OutputPumper.  Redirect std err & out to log files.
+    * 
+    * @author <a href="ryan.campbell at jboss.com">Ryan Campbell</a>
+    * @version $Revision$
+    */
+   private static class OutputPumper extends Thread
+   {
+      private BufferedReader outputReader;
+
+      private PrintWriter logWriter;
+
+      public OutputPumper(BufferedReader outputReader, PrintWriter logWriter)
+      {
+         this.outputReader = outputReader;
+         this.logWriter = logWriter;
+      }
+
+      public void run()
+      {
+         try
+         {
+            String line = null;
+            while ((line = outputReader.readLine()) != null)
+            {
+               logWriter.println(line);
+            }
+         }
+         catch (IOException e)
+         {
+         }
+      }
+   }
+
+}


Property changes on: projects/server-manager/trunk/src/main/java/org/jboss/servermanager/ServerController.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Copied: projects/server-manager/trunk/src/main/java/org/jboss/servermanager/ServerManager.java (from rev 70290, projects/server-manager/trunk/src/main/java/org/jboss/servermanager/server/ServerManager.java)
===================================================================
--- projects/server-manager/trunk/src/main/java/org/jboss/servermanager/ServerManager.java	                        (rev 0)
+++ projects/server-manager/trunk/src/main/java/org/jboss/servermanager/ServerManager.java	2008-03-01 22:28:37 UTC (rev 70293)
@@ -0,0 +1,262 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.servermanager;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+
+/**
+ * A ServerManager.
+ * 
+ * @author <a href="ryan.campbell at jboss.com">Ryan Campbell</a>
+ * @version $Revision$
+ */
+public class ServerManager
+{ 
+   /**
+    * Wait for 45 secs
+    */
+   // set jbossas.startup.timeout property in env.ANT_OPTS
+   private static final int WAIT_TIME = Integer.parseInt(System.getProperty("jbossas.startup.timeout", "45"));
+   private static final int START_TIME = Integer.parseInt(System.getProperty("jbossas.startup.timeout", "120"));
+   
+   /** jboss root **/
+   private String jbossHome;
+
+   /** the jvm executable **/
+   private String jvm = "java";
+
+   /** the java home to use**/
+   private String javaHome;
+
+   private String udpGroup;
+   
+   /** list of all configured servers **/
+   private List<Server> servers = new ArrayList<Server>();
+
+   /**
+    * Create a new ServerManager.  Make sure all servers are killed on shutdown.
+    * 
+    */
+   public ServerManager()
+   {
+      Thread cleanupThread = new Thread()
+      {
+         public void run()
+         {
+            for (Iterator<Server> iter = servers.iterator(); iter.hasNext();)
+            {
+               Server server = iter.next();
+               if (server.isRunning())
+               {
+                  System.err.println("Found server \""+server.getName()+"\" still running; stopping it.");
+                  try
+                  {
+                     ServerController.stopServer(server, ServerManager.this);
+                  }
+                  catch (IOException e)
+                  {
+                     System.err.println("Failed to stop server(s) on shutdown.");
+                     e.printStackTrace(System.err);
+                  }
+               }
+            }
+         }
+      };
+
+      Runtime.getRuntime().addShutdownHook(cleanupThread);
+
+   }
+
+   /**
+    * Add a server
+    * 
+    * @param newServer
+    */
+   public void addServer(Server newServer)
+   {
+      newServer.setManager(this);
+      servers.add(newServer);
+   }
+
+   /**
+    * Get the server by name.  Can't use a hashmap because of 
+    * Ant.
+    * 
+    * @param name the name of the server
+    * @return the server
+    */
+   public Server getServer(String name)
+   {
+      for (Iterator<Server> iter = servers.iterator(); iter.hasNext();)
+      {
+         Server server = iter.next();
+         if (server.getName().equals(name))
+         {
+            return server;
+         }
+      }
+      throw new IllegalArgumentException("There is no server named: " + name);
+   }
+
+   /**
+    * Start the named server.
+    * 
+    * @param serverName
+    * @throws IOException
+    */
+   public void startServer(String serverName) throws IOException
+   {
+      ServerController.startServer(getServer(serverName), this);
+   }
+
+   /** 
+    * Stop the named server 
+    *
+    * @param name
+    * @throws IOException
+    */
+   public void stopServer(String name) throws IOException
+   {
+      ServerController.stopServer(getServer(name), this);
+   }
+
+   /**
+    * Get the jvm.
+    * 
+    * @return the jvm.
+    * @throws IOException
+    */
+   public String getJavaExecutable() throws IOException
+   {
+      return new File(javaHome + File.separator + "bin",jvm).getCanonicalPath();
+   }
+
+   /**
+    * Set the jvm.
+    * 
+    * @param jvm The jvm to set.
+    */
+   public void setJvm(String jvm)
+   {
+      this.jvm = jvm;
+   }
+
+   /**
+    * Set the javaHome.
+    * 
+    * @param javaHome The javaHome to set.
+    */
+   public void setJavaHome(String javaHome)
+   {
+      this.javaHome = javaHome;
+   }
+
+   /**
+    * @param jbossHome The jBossHome to set.
+    */
+   public void setJbossHome(String jbossHome)
+   {
+      this.jbossHome = jbossHome;
+   }
+
+   /** 
+    * The classpath to run the server
+    * 
+    * @return the classpath as a string
+    */
+   protected String getStartClasspath()
+   {
+      File runjar = new File(jbossHome + "/bin/run.jar");
+      File javaJar = new File(javaHome + "/lib/tools.jar");
+      return runjar.toString() + File.pathSeparator + javaJar.toString();
+   }
+
+   /**
+    * The classpath used to stop the server.
+    * 
+    * @return the string to put on the classpath
+    */
+   protected String getStopClasspath()
+   {
+      File shutdownJar = new File(jbossHome + "/bin/shutdown.jar");
+      File clientJar = new File(jbossHome + "/client/jbossall-client.jar");
+      File logJar = new File(jbossHome + "/client/jboss-common.jar");
+      return shutdownJar.toString() + File.pathSeparator + clientJar.toString()+ File.pathSeparator 
+           + logJar.toString();
+   }
+
+   /** 
+    * Get the jboss home.
+    * 
+    * @return the home directory as a string
+    */
+   public String getJBossHome()
+   {
+      return jbossHome;
+   }
+
+   /** 
+    * How long to wait (in seconds) for a server to shutdown
+    * 
+    * @return the shutdown timeout
+    */
+   public int getShutdownTimeout()
+   {
+      return WAIT_TIME;
+   }
+
+   /** 
+    * How long to wait (in seconds) for a server to startup
+    * 
+    * @return the startup timeout
+    */
+   public int getStartupTimeout()
+   {
+      return START_TIME;
+   }
+
+   /**
+    * Get the udpGroup.
+    * 
+    * @return the udpGroup.
+    */
+   protected String getUdpGroup()
+   {
+      return udpGroup;
+   }
+
+   /**
+    * Set the udpGroup.
+    * 
+    * @param udpGroup The udpGroup to set.
+    */
+   public void setUdpGroup(String udpGroup)
+   {
+      this.udpGroup = udpGroup;
+   }
+   
+}


Property changes on: projects/server-manager/trunk/src/main/java/org/jboss/servermanager/ServerManager.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Copied: projects/server-manager/trunk/src/main/java/org/jboss/servermanager/ServerShutdownException.java (from rev 70290, projects/server-manager/trunk/src/main/java/org/jboss/servermanager/server/ServerShutdownException.java)
===================================================================
--- projects/server-manager/trunk/src/main/java/org/jboss/servermanager/ServerShutdownException.java	                        (rev 0)
+++ projects/server-manager/trunk/src/main/java/org/jboss/servermanager/ServerShutdownException.java	2008-03-01 22:28:37 UTC (rev 70293)
@@ -0,0 +1,44 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.servermanager;
+
+import java.io.IOException;
+
+/**
+ * Signals server shutdown problems.
+ * 
+ * @author <a href="mvecera at redhat.com">Martin Vecera</a>
+ * @version $Revision: 1 $
+ */
+public class ServerShutdownException extends IOException
+{
+   public ServerShutdownException() 
+   {
+      super();
+   }
+   
+   public ServerShutdownException(String s) 
+   {
+      super(s);
+   }
+
+}

Modified: projects/test/trunk/src/main/java/org/jboss/ant/taskdefs/server/ConfigManagerTask.java
===================================================================
--- projects/test/trunk/src/main/java/org/jboss/ant/taskdefs/server/ConfigManagerTask.java	2008-03-01 22:28:11 UTC (rev 70292)
+++ projects/test/trunk/src/main/java/org/jboss/ant/taskdefs/server/ConfigManagerTask.java	2008-03-01 22:28:37 UTC (rev 70293)
@@ -23,9 +23,8 @@
 
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Task;
-import org.jboss.test.util.server.Server;
-import org.jboss.test.util.server.ServerManager;
-
+import org.jboss.servermanager.Server;
+import org.jboss.servermanager.ServerManager;
 /**
  * A ConfigManagerTask.  Delegates to the Server manager.
  * 

Modified: projects/test/trunk/src/main/java/org/jboss/ant/taskdefs/server/StartServerTask.java
===================================================================
--- projects/test/trunk/src/main/java/org/jboss/ant/taskdefs/server/StartServerTask.java	2008-03-01 22:28:11 UTC (rev 70292)
+++ projects/test/trunk/src/main/java/org/jboss/ant/taskdefs/server/StartServerTask.java	2008-03-01 22:28:37 UTC (rev 70293)
@@ -25,8 +25,9 @@
 
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Task;
-import org.jboss.test.util.server.ServerManager;
+import org.jboss.servermanager.ServerManager;
 
+
 /**
  * A StopServerTask.
  * 

Modified: projects/test/trunk/src/main/java/org/jboss/ant/taskdefs/server/StopServerTask.java
===================================================================
--- projects/test/trunk/src/main/java/org/jboss/ant/taskdefs/server/StopServerTask.java	2008-03-01 22:28:11 UTC (rev 70292)
+++ projects/test/trunk/src/main/java/org/jboss/ant/taskdefs/server/StopServerTask.java	2008-03-01 22:28:37 UTC (rev 70293)
@@ -24,11 +24,12 @@
 import java.io.IOException;
 
 import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
 import org.apache.tools.ant.Task;
-import org.apache.tools.ant.Project;
-import org.jboss.test.util.server.ServerManager;
-import org.jboss.test.util.server.ServerShutdownException;
+import org.jboss.servermanager.ServerManager;
+import org.jboss.servermanager.ServerShutdownException;
 
+
 /**
  * A StopServerTask.
  * 




More information about the jboss-cvs-commits mailing list