You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by we...@apache.org on 2004/01/15 20:53:28 UTC

cvs commit: jakarta-jetspeed-2/portal/src/java/org/apache/jetspeed/tools/pamanager/servletcontainer TomcatManager.java

weaver      2004/01/15 11:53:28

  Modified:    portal/src/java/org/apache/jetspeed/tools/pamanager
                        CatalinaPAM.java
  Added:       portal/src/java/org/apache/jetspeed/tools/pamanager/servletcontainer
                        TomcatManager.java
  Log:
  - Added TomcatManager that wraps all calls to the tomcat container regarding app management
  - CatalinaPAM now extends FileSystemPAM and wraps TomcatManager
  
  Revision  Changes    Path
  1.2       +116 -35   jakarta-jetspeed-2/portal/src/java/org/apache/jetspeed/tools/pamanager/CatalinaPAM.java
  
  Index: CatalinaPAM.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed-2/portal/src/java/org/apache/jetspeed/tools/pamanager/CatalinaPAM.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CatalinaPAM.java	28 Jul 2003 23:47:52 -0000	1.1
  +++ CatalinaPAM.java	15 Jan 2004 19:53:27 -0000	1.2
  @@ -53,6 +53,8 @@
    */
   package org.apache.jetspeed.tools.pamanager;
   
  +import org.apache.jetspeed.tools.pamanager.servletcontainer.TomcatManager;
  +
   /**
    * This is the catalina specific implemenation for deplyment of Portlet Applications.
    *
  @@ -60,17 +62,37 @@
     * @version $Id$
    */
   
  -public class CatalinaPAM implements Deployment, Lifecycle
  +public class CatalinaPAM extends FileSystemPAM implements Deployment, Lifecycle
   {
       // Implementaion of deplyment interface
   
  +    private TomcatManager tomcatManager;
  +
  +    public CatalinaPAM(int port, String user, String password) throws PortletApplicationException
  +    {
  +        try
  +        {
  +            tomcatManager = new TomcatManager("localhost", port, user, password);
  +        }
  +        catch (Exception e)
  +        {
  +            throw new PortletApplicationException(e);
  +        }
  +
  +    }
  +
       // Interface not supported by this implementation 
  -    public void deploy(String webAppsDir, 
  -                       String warFile ,
  -                       String paName
  -                       ) throws PortletApplicationException
  +    public void deploy(String webAppsDir, String warFile, String paName) throws PortletApplicationException
       {
  -        System.out.println("Not supported");
  +        super.deploy(webAppsDir, warFile, paName);
  +        try
  +        {
  +            checkResponse(tomcatManager.install(warFile, paName));
  +        }
  +        catch (Exception e)
  +        {
  +            throw new PortletApplicationException(e);
  +        }
       }
   
       /**
  @@ -82,10 +104,18 @@
        * @param paName The Portlet Application name
        * @throws PortletApplicationException
        */
  -    
  -    public void deploy(String warFile,
  -                       String paName) throws PortletApplicationException
  +
  +    public void deploy(String warFile, String paName) throws PortletApplicationException
       {
  +        super.deploy(warFile, paName);
  +        try
  +        {
  +            checkResponse(tomcatManager.install(warFile, paName));
  +        }
  +        catch (Exception e)
  +        {
  +            throw new PortletApplicationException(e);
  +        }
       }
   
       /**
  @@ -94,35 +124,63 @@
        * @param paName The Portlet Application name 
        * @throws PortletApplicationException
        */
  -    
  +
       public void undeploy(String paName) throws PortletApplicationException
       {
  +        try
  +        {
  +            checkResponse(tomcatManager.remove(paName));
  +            super.undeploy(paName);
  +        }
  +        catch (UnsupportedOperationException usoe)
  +        {
  +            // ignore FS PAM not suporting this
  +        }
  +        catch (Exception e)
  +        {
  +            throw new PortletApplicationException(e);
  +        }
  +    }
  +
  +    /** Undeploys application.
  +    * 
  +    * @param webAppsDir The webapps directory inside the Application Server
  +    * @param paName The Portlet Application name 
  +    * @throws PortletApplicationException
  +    */
  +
  +    public void undeploy(String webAppsDir, String paName) throws PortletApplicationException
  +    {
  +        try
  +        {
  +            checkResponse(tomcatManager.remove(paName));
  +            super.undeploy(webAppsDir, paName);
  +        }
  +        catch (Exception e)
  +        {
  +            throw new PortletApplicationException(e);
  +        }
       }
   
  -   /** Undeploys application.
  -   * 
  -   * @param webAppsDir The webapps directory inside the Application Server
  -   * @param paName The Portlet Application name 
  -   * @throws PortletApplicationException
  -   */
  -
  -  public void undeploy(String webAppsDir,
  -                       String paName) throws PortletApplicationException
  -      {
  -        System.out.println("Not supported");
  -      }
  -    
  -
       // Implementaion of Lifecycle interface
  -       /**
  -     * Starts the specified Portlet Application on the Application Server
  -     * 
  -     * @param paName The Portlet Application name 
  -     * @throws PortletApplicationException
  -     */
  -    
  +    /**
  +    * Starts the specified Portlet Application on the Application Server
  +    * 
  +    * @param paName The Portlet Application name 
  +    * @throws PortletApplicationException
  +    */
  +
       public void start(String paName) throws PortletApplicationException
       {
  +        try
  +        {
  +            checkResponse(tomcatManager.start(paName));
  +        }
  +        catch (Exception e)
  +        {
  +            throw new PortletApplicationException(e);
  +        }
  +
       }
   
       /**
  @@ -131,9 +189,17 @@
        * @param paName The Portlet Application name 
        * @throws PortletApplicationException
        */
  -    
  +
       public void stop(String paName) throws PortletApplicationException
       {
  +        try
  +        {
  +            checkResponse(tomcatManager.stop(paName));
  +        }
  +        catch (Exception e)
  +        {
  +            throw new PortletApplicationException(e);
  +        }
       }
   
       /**
  @@ -142,10 +208,25 @@
        * @param paName The Portlet Application name 
        * @throws PortletApplicationException
        */
  -    
  +
       public void reload(String paName) throws PortletApplicationException
       {
  +        try
  +        {
  +            checkResponse(tomcatManager.reload(paName));
  +        }
  +        catch (Exception e)
  +        {
  +            throw new PortletApplicationException(e);
  +        }
  +    }
  +
  +    private void checkResponse(String response) throws PortletApplicationException
  +    {
  +        if (response == null || !response.startsWith("OK"))
  +        {
  +            throw new PortletApplicationException("Catalina container action failed, \"" + response + "\"");
  +        }
       }
   
   }
  -
  
  
  
  1.1                  jakarta-jetspeed-2/portal/src/java/org/apache/jetspeed/tools/pamanager/servletcontainer/TomcatManager.java
  
  Index: TomcatManager.java
  ===================================================================
  /**
   * Created on Sep 9, 2003
   *
   * 
   * @author
   */
  package org.apache.jetspeed.tools.pamanager.servletcontainer;
  
  import java.io.File;
  import java.io.IOException;
  import java.io.InputStream;
  
  import org.apache.commons.httpclient.HostConfiguration;
  import org.apache.commons.httpclient.HttpClient;
  import org.apache.commons.httpclient.HttpException;
  import org.apache.commons.httpclient.HttpMethod;
  import org.apache.commons.httpclient.NameValuePair;
  import org.apache.commons.httpclient.UsernamePasswordCredentials;
  import org.apache.commons.httpclient.methods.GetMethod;
  import org.apache.commons.httpclient.methods.PutMethod;
  
  /**
   * <p>
   * TomcatManager
   * </p>
   * 
   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
   * @version $Id: TomcatManager.java,v 1.1 2004/01/15 19:53:28 weaver Exp $
   *
   */
  public class TomcatManager 
  {
      private static final String DEFUALT_MANAGER_APP_PATH = "/manager";
  
      private String hostUrl;
      private int hostPort;
      private String userName;
      private String password;
  
      private String managerAppPath = DEFUALT_MANAGER_APP_PATH;
      private String stopPath = managerAppPath + "/stop";
      private String startPath = managerAppPath + "/start";
      private String removePath = managerAppPath + "/remove";
      private String deployPath = managerAppPath + "/deploy";
      private String installPath = managerAppPath + "/install";
  	private String reloadPath = managerAppPath + "/reload";
      private String serverInfoPath = managerAppPath + "/serverinfo";
  
      private HttpClient client;
  
      private HttpMethod start;
  
      private HttpMethod stop;
      
  	private HttpMethod reload;
  
      private HttpMethod remove;
  
      private PutMethod deploy;
  
      private HttpMethod install;
  
      public TomcatManager(String hostName, int hostPort, String userName, String password) throws HttpException, IOException
      {
          super();
          init(hostName, hostPort, userName, password);
      }
  
      /**
       * do nothing constructor
       *
       */
      protected TomcatManager()
      {
          super();
      }
  
      protected void init(String hostName, int hostPort, String userName, String password) throws IOException, HttpException
      {
          this.hostUrl = hostName;
          this.hostPort = hostPort;
          this.userName = userName;
          this.password = password;
  
          client = new HttpClient();
  
          HostConfiguration hostConfig = new HostConfiguration();
          hostConfig.setHost(hostUrl, hostPort, "http");
  
          client.setHostConfiguration(hostConfig);
          // Fix for non-buffereing large WAR files during deploy
          client.getState().setAuthenticationPreemptive(true);
          client.getState().setCredentials(null, hostUrl, new UsernamePasswordCredentials(userName, password));
  
          // perform a test, we can use this to close a
          GetMethod test = new GetMethod(serverInfoPath);
          try
          {
              client.executeMethod(test);
          }
          finally
          {
              test.releaseConnection();
          }
          start = new GetMethod(startPath);
          stop = new GetMethod(stopPath);
          remove = new GetMethod(removePath);
          install = new GetMethod(installPath);
  		reload = new GetMethod(reloadPath);
          deploy = new PutMethod(deployPath);
      }
  
      public String start(String appPath) throws HttpException, IOException
      {
          try
          {
              start.setQueryString(buildPathQueryArgs(appPath));
              client.executeMethod(start);
              return start.getResponseBodyAsString();
          }
          finally
          {
              start.recycle();
              start.setPath(startPath);
          }
      }
  
      public String stop(String appPath) throws HttpException, IOException
      {
          try
          {
              stop.setQueryString(buildPathQueryArgs(appPath));
              client.executeMethod(stop);
              return stop.getResponseBodyAsString();
          }
          finally
          {
              stop.recycle();
              stop.setPath(stopPath);
          }
      }
      
  	public String reload(String appPath) throws HttpException, IOException
  	{
  		try
  		{
  			reload.setQueryString(buildPathQueryArgs(appPath));
  			client.executeMethod(reload);
  			return reload.getResponseBodyAsString();
  		}
  		finally
  		{
  			reload.recycle();
  			reload.setPath(reloadPath);
  		}
  	}
  
      public String remove(String appPath) throws HttpException, IOException
      {
  
          try
          {
              remove.setQueryString(buildPathQueryArgs(appPath));
              client.executeMethod(remove);
              return remove.getResponseBodyAsString();
          }
          finally
          {
              remove.recycle();
              remove.setPath(removePath);
          }
      }
  
      public String install(String warPath, String contexPath) throws HttpException, IOException
      {
          try
          {
              install.setQueryString(buildWarQueryArgs(warPath));
              if (contexPath != null)
              {
                  install.setQueryString(buildPathQueryArgs(contexPath));
              }
  
              client.executeMethod(install);
              return deploy.getResponseBodyAsString();
          }
          finally
          {
              install.recycle();
              install.setPath(installPath);
          }
  
      }
  
  
  
      public String deploy(String appPath, InputStream is, int size) throws HttpException, IOException
      {
          try
          {
              deploy.setQueryString(buildPathQueryArgs(appPath));
  
              //deploy.setRequestContentLength(PutMethod.CONTENT_LENGTH_CHUNKED);
  
              if (size != -1)
              {
                  deploy.setRequestContentLength(size);
              }
              deploy.setRequestBody(is);
  
              client.executeMethod(deploy);
              return deploy.getResponseBodyAsString();
          }
          finally
          {
              deploy.recycle();
              deploy.setPath(deployPath);
          }
      }
  
      protected NameValuePair[] buildPathQueryArgs(String appPath)
      {
          return new NameValuePair[] { new NameValuePair("path", appPath)};
      }
  
      protected NameValuePair[] buildWarQueryArgs(String appPath)
      {
          return new NameValuePair[] { new NameValuePair("war", appPath)};
      }
  
      /**
       * @return
       */
      public int getHostPort()
      {
          return hostPort;
      }
  
      /**
       * @return
       */
      public String getHostUrl()
      {
          return hostUrl;
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org