[jboss-cvs] JBossAS SVN: r80050 - in projects/server-manager/trunk: src/main/java/org/jboss/jbossas/servermanager and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Oct 24 09:19:32 EDT 2008


Author: akostadinov
Date: 2008-10-24 09:19:32 -0400 (Fri, 24 Oct 2008)
New Revision: 80050

Modified:
   projects/server-manager/trunk/pom.xml
   projects/server-manager/trunk/src/main/java/org/jboss/jbossas/servermanager/Server.java
   projects/server-manager/trunk/src/main/java/org/jboss/jbossas/servermanager/ServerController.java
   projects/server-manager/trunk/src/main/java/org/jboss/jbossas/servermanager/ServerManager.java
Log:
JBASM-3 JBASM-12 JBASM-13 - hope everybody is happy now

Modified: projects/server-manager/trunk/pom.xml
===================================================================
--- projects/server-manager/trunk/pom.xml	2008-10-24 12:41:12 UTC (rev 80049)
+++ projects/server-manager/trunk/pom.xml	2008-10-24 13:19:32 UTC (rev 80050)
@@ -58,6 +58,13 @@
         <skip>true</skip>
          <redirectTestOutputToFile>true</redirectTestOutputToFile>
          <printSummary>true</printSummary>
+         <systemProperties>
+           <!-- avoid log4j error message -->
+           <property>
+             <name>org.jboss.logging.Logger.pluginClass</name>
+             <value>logging.JDK14LoggerPlugin</value>
+           </property>
+         </systemProperties>
       </configuration>
       <executions>
         <execution>
@@ -109,8 +116,61 @@
 
   <!-- Dependencies -->
   <dependencies>
-  
+
+    <!-- hardcoding object names to avoid more deps
     <dependency>
+      <groupId>org.jboss.jbossas</groupId>
+      <artifactId>jboss-as-system</artifactId>
+      <version>5.0.0.CR1</version>
+      <classifier>client</classifier>
+    </dependency>
+    <dependency>
+      <groupId>org.jboss.jbossas</groupId>
+      <artifactId>jboss-as-system-jmx</artifactId>
+      <version>5.0.0.CR1</version>
+      <classifier>client</classifier>
+    </dependency>
+    <dependency>
+    -->
+    <dependency>
+      <groupId>org.jboss.naming</groupId>
+      <artifactId>jnp-client</artifactId>
+      <version>5.0.0.CR1</version>
+    </dependency>
+    <dependency>
+      <groupId>org.jboss.security</groupId>
+      <artifactId>jbosssx-client</artifactId>
+      <version>2.0.2.CR7</version>
+    </dependency>
+    <dependency>
+      <groupId>org.jboss.jbossas</groupId>
+      <artifactId>jboss-as-server</artifactId>
+      <version>5.0.0.CR1</version>
+      <scope>runtime</scope>
+      <classifier>client</classifier>
+      <exclusions>
+        <exclusion>
+          <groupId>org.jboss.logging</groupId>
+          <artifactId>jboss-logging-log4j</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+    <dependency>
+      <groupId>org.jboss.jbossas</groupId>
+      <artifactId>jboss-as-server</artifactId>
+      <version>5.0.0.CR1</version>
+      <scope>runtime</scope>
+      <!-- should be jmx-invoker-adaptor-client with CR2+ -->
+      <classifier>jmx-invoker-adaptor</classifier>
+      <exclusions>
+        <exclusion>
+          <groupId>org.jboss.logging</groupId>
+          <artifactId>jboss-logging-log4j</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+
+    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>${version.junit_junit}</version>

Modified: projects/server-manager/trunk/src/main/java/org/jboss/jbossas/servermanager/Server.java
===================================================================
--- projects/server-manager/trunk/src/main/java/org/jboss/jbossas/servermanager/Server.java	2008-10-24 12:41:12 UTC (rev 80049)
+++ projects/server-manager/trunk/src/main/java/org/jboss/jbossas/servermanager/Server.java	2008-10-24 13:19:32 UTC (rev 80050)
@@ -21,6 +21,7 @@
 */
 package org.jboss.jbossas.servermanager;
 
+import java.io.IOException;
 import java.io.File;
 import java.io.PrintWriter;
 import java.net.MalformedURLException;
@@ -31,13 +32,26 @@
 import java.util.List;
 import java.util.Properties;
 
+import javax.management.InstanceNotFoundException;
+import javax.management.MalformedObjectNameException;
+import javax.management.MBeanException;
 import javax.management.MBeanServerConnection;
-import javax.management.MalformedObjectNameException;
 import javax.management.ObjectName;
+import javax.management.ReflectionException;
+
 import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
+import javax.naming.NameNotFoundException;
 
+import org.jnp.interfaces.NamingContext;
+
+import org.jboss.security.SecurityAssociation;
+import org.jboss.security.SimplePrincipal;
+
+// import org.jboss.system.server.ServerInfoMBean;
+// import org.jboss.system.server.ServerImplMBean;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -64,10 +78,10 @@
    private String config;
 
    /** jmx username **/
-   private String username;
+   private String username = null;
 
    /** jmx password **/
-   private String password;
+   private String password = null;
 
    /** the arguments to pass to jboss **/
    private List<Argument> arguments = new ArrayList<Argument>();
@@ -105,15 +119,18 @@
    /** the Naming Context */
    private Context namingContext;
    
-   /** The MBean Server Connection for JMX Invocation **/
-   private MBeanServerConnection mBeanServerConnection;
-   
    /** ObjectName of the Main Deployer **/
    public final static String DEPLOYER_NAME = "jboss.system:service=MainDeployer";
 
-   /** Server URL Override */
+   /** the MBean Server Connection */
+   private MBeanServerConnection serverConnection;
+
+   /** the URL to use for connection */
    private String serverUrl = null;
 
+   /** the InitialContext factory class name to use */
+   private String initialContextFactoryClassName = null;
+
    /**
     * Get the name.
     *
@@ -463,11 +480,21 @@
    }
 
    /**
+    * Where should the servers's trace dump go?
+    *
+    * @return the thread dump file
+    */
+   public File getDumpFile()
+   {
+      return new File(getLogDir(), "threadDump.log");
+   }
+
+   /**
     * The server's log directory
     *
     * @return the log directory
     */
-   private File getLogDir()
+   public File getLogDir()
    {
       return new File(getConfDir(), "log");
    }
@@ -477,7 +504,7 @@
     *
     * @return the configuration directory
     */
-   private File getConfDir()
+   protected File getConfDir()
    {
       return new File(manager.getJBossHome(), "server/" + getConfig());
    }
@@ -519,7 +546,7 @@
    public void setErrorWriter(PrintWriter errorlog)
    {
       errorWriter = errorlog;
-    }
+   }
 
    /**
     * Get the hasWebServer.
@@ -528,7 +555,7 @@
     */
    public boolean hasWebServer()
    {
-	return hasWebServer;
+      return hasWebServer;
    }
 
    /**
@@ -545,32 +572,193 @@
     * Get the Naming Context.
     *
     * @return the namingContext.
+    * @throws NamingException
     */
    public Context getNamingContext() throws NamingException
    {
-       if (namingContext == null)
-       {
-          Properties properties = new Properties();
-          properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, ("org.jboss.naming.NamingContextFactory"));
-          properties.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
-          properties.setProperty(Context.PROVIDER_URL, "jnp://"+ getHost() + ':' + getRmiPort());
+      if (namingContext == null)
+      {
+         Properties properties = new Properties();
+         properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, getInitialContextFactoryClassName());
+         properties.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
+         properties.setProperty(Context.PROVIDER_URL, getServerUrl());
+         properties.setProperty("j2ee.clientName", "JBoss Server Manager");
+         properties.setProperty(NamingContext.JNP_DISABLE_DISCOVERY, "true");
 
-          setNamingContext(new InitialContext(properties));
-        }
+         if (username!=null)
+         {
+            SecurityAssociation.setPrincipal(new SimplePrincipal(username));
+            SecurityAssociation.setCredential(password);
+         }
 
-        return namingContext; 
+         setNamingContext(new InitialContext(properties));
+      }
+
+      return namingContext; 
    }
 
    /**
-    * Set the NamingContext in case the default is inadequate.
+    * Set the NamingContext
     *
     * @param namingContext
     */
    public void setNamingContext(Context namingContext)
    {
-	this.namingContext = namingContext;
+      this.namingContext = namingContext;
    }
    
+   /**
+    * Get server connection.
+    *
+    * @return the serverConnection
+    * @throws NamingException
+    */
+   public MBeanServerConnection getServerConnection() throws NamingException
+   {
+      String adapterName = "jmx/rmi/RMIAdaptor";
+
+      if ( serverConnection == null)
+      {
+         Object obj = getNamingContext().lookup(adapterName);
+         if ( obj == null )
+         {
+            throw new NameNotFoundException("Object " + adapterName + " not found.");
+         }
+
+         setServerConnection((MBeanServerConnection) obj);
+      }
+
+      return serverConnection;
+   }
+
+   /**
+    * Set server connection.
+    *
+    * @param serverConnection
+    */
+   public void setServerConnection(MBeanServerConnection serverConnection)
+   {
+      this.serverConnection = serverConnection;
+   }
+
+   /**
+    * Get server thread dump.
+    *
+    * @return the thread dump
+    * @throws NamingException for contect lookup
+    * @throws InstanceNotFoundException MBeanServerConnection method invoke()
+    * @throws MBeanException MBeanServerConnection method invoke()
+    * @throws ReflectionException MBeanServerConnection method invoke()
+    * @throws IOException MBeanServerConnection method invoke()
+    */
+   public String listThreadDump() throws NamingException, InstanceNotFoundException, MBeanException, ReflectionException, IOException
+   {
+      final ObjectName serverInfoName;
+      try
+      {
+         serverInfoName = new ObjectName("jboss.system","type","ServerInfo");
+         // serverInfoName = new ObjectName(ServerInfoMBean.OBJECT_NAME_STR);
+      }
+      catch (MalformedObjectNameException mone)
+      {
+         // should never happen anyways
+         throw new RuntimeException("Bad object name, something is totally broken", mone);
+      }
+
+      return (String) getServerConnection().invoke(serverInfoName, "listThreadDump",null,null);
+   }
+
+   /**
+    * Send server shutdown command.
+    *
+    * @throws NamingException for context lookup
+    * @throws InstanceNotFoundException MBeanServerConnection method invoke()
+    * @throws MBeanException MBeanServerConnection method invoke()
+    * @throws ReflectionException MBeanServerConnection method invoke()
+    * @throws IOException MBeanServerConnection method invoke()
+    */
+   public void doShutdown() throws NamingException, InstanceNotFoundException, MBeanException, ReflectionException, IOException
+   {
+      final ObjectName serverObjectName;
+
+      try
+      {
+         serverObjectName = new ObjectName("jboss.system","type","Server");
+      }
+      catch (MalformedObjectNameException mone)
+      {
+         // should never happen anyways
+         throw new RuntimeException("Bad object name, something is totally broken", mone);
+      }
+      // serverObjectName = ServerImplMBean.OBJECT_NAME;
+
+      getServerConnection().invoke(serverObjectName, "shutdown",null,null);
+
+      // some clean up
+      try
+      {
+         getNamingContext().close();
+      }
+      catch (Exception e)
+      {
+      }
+      setNamingContext(null);
+      setServerConnection(null);
+   }
+
+   /**
+    *
+    * Get the URL to connect server
+    *
+    */
+   public String getServerUrl()
+   {
+      if (null == this.serverUrl ) 
+         return getRmiUrl();
+
+      return serverUrl;
+   }
+
+   /**
+    * Set the URL to connect server
+    *
+    * @param serverUrl
+    */
+   public void setServerUrl(String serverUrl)
+   {
+      this.serverUrl = serverUrl;
+   }
+
+   /**
+    *
+    * Get the InitialContext factory class name
+    *
+    */
+   public String getInitialContextFactoryClassName()
+   {
+      if (this.initialContextFactoryClassName != null) 
+         return this.initialContextFactoryClassName;
+
+      if (getServerUrl().startsWith("http"))
+      {
+         return "org.jboss.naming.HttpNamingContextFactory";
+      }
+      else
+      {
+         return "org.jboss.naming.NamingContextFactory";
+      }
+   }
+
+   /**
+    * Set the InitialContext factory class name
+    *
+    * @param initialContextFactoryClassName
+    */
+   public void setInitialContextFactoryClassName(String initialContextFactoryClassName)
+   {
+      this.initialContextFactoryClassName = initialContextFactoryClassName;
+   }
+
    /*
     * Code below this marker has been ported from jboss-test to supply 
     * the Server with invokable operations support
@@ -645,23 +833,6 @@
    }
    
    /**
-    * Gets the Server attribute of the JBossTestCase object
-    *
-    * @return   The Server value
-    * @throws Exception for any error
-    */
-   protected MBeanServerConnection getMBeanServerConnection() throws Exception
-   {
-      if (mBeanServerConnection == null)
-      {
-         String adaptorName = System.getProperty("jbosstest.server.name", "jmx/invoker/RMIAdaptor");
-         Context context = this.getNamingContext();
-         mBeanServerConnection = (MBeanServerConnection) context.lookup(adaptorName);
-      }
-      return mBeanServerConnection;
-   }
-   
-   /**
     * invoke wraps an invoke call to the mbean server in a lot of exception
     * unwrapping.
     *
@@ -674,7 +845,7 @@
     */
    public Object invoke(ObjectName name, String method, Object[] args, String[] sig) throws Exception
    {
-      return invoke(this.getMBeanServerConnection(), name, method, args, sig);
+      return invoke(this.getServerConnection(), name, method, args, sig);
    }
 
    public Object invoke(MBeanServerConnection server, ObjectName name, String method, Object[] args, String[] sig)
@@ -713,21 +884,4 @@
          throw e.getTargetError();
       }
    }
-
-   /**
-   *
-   * Get the URL to pass to --server 
-   *
-   */
-  public String getServerUrl()
-  {
-   serverUrl = getSysProperty("jbosstest.server.url");
-
-   if (null == serverUrl ) 
-       return getRmiUrl();
-
-   return serverUrl;
-  }
-   
-   
 }

Modified: projects/server-manager/trunk/src/main/java/org/jboss/jbossas/servermanager/ServerController.java
===================================================================
--- projects/server-manager/trunk/src/main/java/org/jboss/jbossas/servermanager/ServerController.java	2008-10-24 12:41:12 UTC (rev 80049)
+++ projects/server-manager/trunk/src/main/java/org/jboss/jbossas/servermanager/ServerController.java	2008-10-24 13:19:32 UTC (rev 80050)
@@ -27,6 +27,7 @@
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.net.HttpURLConnection;
 import java.net.Socket;
 import java.net.URL;
@@ -41,8 +42,6 @@
 public abstract class ServerController
 {
 
-   private static final String SHUTDOWN_CLASS = "org.jboss.Shutdown";
-
    private static final String MAIN = "org.jboss.Main"; 
    
    private ServerController()
@@ -68,6 +67,10 @@
          throw new IOException("Found a process already listening on:" + server.getHttpUrl() + " or "+ server.getRmiUrl());
       }
 
+      // make sure these are initialized
+      server.setNamingContext(null);
+      server.setServerConnection(null);
+
       String execCmd = getStartCommandLine(server, manager);
 
       System.out.println("Starting server \"" + server.getName() + "\", with command (start timeout is " + manager.getStartupTimeout() + " seconds ): \n" + execCmd);
@@ -193,7 +196,7 @@
             + "\" before timeout. Destroying the process.");
       process.destroy();
 
-      throw new IOException("Server failed to start; see logs.");
+      throw new IOException("Server failed to start in time; see logs.");
 
    }
 
@@ -254,8 +257,9 @@
    }
 
    /**
-    * Stop the server using shutdown.jar.
-    * Process.destroy() the server if it fails to shutdown.
+    * Stop the server.
+    * Get thread dump and Process.destroy() the server 
+    * if it fails to shutdown.
     * 
     * @param server
     * @param manager
@@ -263,7 +267,8 @@
     */
    public static void stopServer(Server server, ServerManager manager) throws IOException
    {
-		boolean cleanShutdown = true;
+      boolean cleanShutdown = true;
+      Throwable shutdownException = null;
 		
       if (!server.isRunning())
       {
@@ -272,18 +277,41 @@
 
       System.out.println("Shutting down server: " + server.getName());
 
-      String shutdownCmd = getStopCommandLine(server, manager);
-      System.out.println("Shutting down server: " + shutdownCmd);
+      try
+      {
+         server.doShutdown();
+      }
+      catch (Exception e)
+      {
+         shutdownException = e;
+         cleanShutdown = false;
+      }
 
-      Runtime.getRuntime().exec(shutdownCmd);
-
       Process process = server.getProcess();
-      if (!waitOnShutdown(server, manager))
+      if (cleanShutdown && !waitOnShutdown(server, manager))
       {
+         cleanShutdown = false;
+      }
+
+      if (!cleanShutdown)
+      {
+         // try to dump server trace
+         try
+         {
+            writeServerDump(server);
+         }
+         catch (Exception e)
+         {
+            System.err.println("Cannot write to "
+               + server.getDumpFile().getAbsolutePath());
+            e.printStackTrace();
+         }
+
+         // destroy process and print an error messsage
+         process.destroy();
          System.err.println("Failed to shutdown server \"" + server.getName()
-               + "\" before timeout. Destroying the process.");
-         process.destroy();
-			cleanShutdown = false;
+            + "\"" + (shutdownException == null ? " before timeout." : ".")
+               + " Destroying the process.");
       }
 
       closeAllStreams(process);
@@ -298,14 +326,56 @@
       catch (InterruptedException e)
       {
       }
-		
-		if (!cleanShutdown)
-		{
-			throw new ServerShutdownException("Failed to shutdown server before timeout. Process was destroyed.");
-		}
+
+      if (!cleanShutdown)
+      {
+         throw (ServerShutdownException) new ServerShutdownException(
+            "Failed to shutdown server"
+            + (shutdownException == null ? " before timeout." : ".")
+            + "Process was destroyed."
+            ).initCause(shutdownException);
+      }
    }
 
    /**
+    * Dump Server trace to file 
+    * @param server
+    * @throws IOException on faled dump file write
+    */
+   private static void writeServerDump(final Server server) throws IOException
+   {
+         String threadDump = null;
+         Exception dumpException = null;
+         try
+         {
+            threadDump = server.listThreadDump();
+         }
+         catch (Exception e)
+         {
+            dumpException = e;
+         }
+         if ( threadDump == null )
+         {
+            threadDump = "Unable to get server thread dump: ";
+            if ( dumpException == null )
+            {
+               dumpException = (Exception) new RuntimeException("threadDump and dumpException null - something broken").fillInStackTrace();
+               
+            }
+            StringWriter dumpExceptionWriter = new StringWriter(512);
+            dumpException.printStackTrace(new PrintWriter(dumpExceptionWriter));
+            threadDump = threadDump + dumpExceptionWriter.toString();
+         }
+         File dumpFile = server.getDumpFile();
+         System.out.println("Writing server thread dump to "
+            + dumpFile.getAbsolutePath());
+         FileWriter dumpFW = new FileWriter(dumpFile);
+         dumpFW.write(threadDump);
+         dumpFW.flush();
+         dumpFW.close();
+   }
+
+   /**
     * Wait for the server to shutdown. 
     * @param server
     * @param manager 
@@ -333,30 +403,6 @@
       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.getServerUrl();
-      execCmd = execCmd + strAuth +" --shutdown";
-      return execCmd;
-   }
-
    /**
     * Close the streams of a process.
     * 

Modified: projects/server-manager/trunk/src/main/java/org/jboss/jbossas/servermanager/ServerManager.java
===================================================================
--- projects/server-manager/trunk/src/main/java/org/jboss/jbossas/servermanager/ServerManager.java	2008-10-24 12:41:12 UTC (rev 80049)
+++ projects/server-manager/trunk/src/main/java/org/jboss/jbossas/servermanager/ServerManager.java	2008-10-24 13:19:32 UTC (rev 80050)
@@ -123,6 +123,24 @@
       }
       throw new IllegalArgumentException("There is no server named: " + name);
    }
+
+   /**
+    * Stop managing server. Can't use a hashmap because of Ant.
+    * 
+    * @param name the name of the server
+    * @return the server
+    */
+   public void delServer(String name)
+   {
+      try
+      {
+         servers.remove(name);
+      }
+      catch (IllegalArgumentException e)
+      {
+         throw new IllegalArgumentException("There is no server named: " + name, e);
+      }
+   }
    
    /**
     * Retreives a list of all running servers
@@ -207,20 +225,6 @@
       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.
     * 




More information about the jboss-cvs-commits mailing list