User: schulze 
  Date: 00/10/27 12:10:28

  Modified:    src/main/org/jboss/deployment J2eeDeployer.java
                        J2eeDeployerMBean.java Deployment.java
  Log:
  updated (rewritten ;-) the J2eeDeployer.
  He gives now more precise information in case of any error (at least I think so)I 
also changed his way storing deployments:
  On deploy, a subdirectory in the deployment dir is created for the app and all its 
modules and the serialized Deploymnet object for this app come in there.
  There is no central config file anymore.
  Also the start/stop behavior has changed.
  
  Revision  Changes    Path
  1.4       +582 -421  jboss/src/main/org/jboss/deployment/J2eeDeployer.java
  
  Index: J2eeDeployer.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jboss/src/main/org/jboss/deployment/J2eeDeployer.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- J2eeDeployer.java 2000/10/23 19:10:29     1.3
  +++ J2eeDeployer.java 2000/10/27 19:10:27     1.4
  @@ -1,9 +1,9 @@
   /*
  - * jBoss, the OpenSource EJB server
  - *
  - * Distributable under GPL license.
  - * See terms of license at gnu.org.
  - */
  +* jBoss, the OpenSource EJB server
  +*
  +* Distributable under GPL license.
  +* See terms of license at gnu.org.
  +*/
   package org.jboss.deployment;
   
   import java.net.URL;
  @@ -39,43 +39,43 @@
   
   
   /** J2eeDeployer allows to deploy single EJB.jars as well as Web.wars
  - *  (if Tomcat runs within the same VM as jBoss) or even Application.ears. <br>
  - *  The deployment is done by determining the file type of the given url.
  - *  The file must be a valid zip file and must contain either a META-INF/ejb-jar.xml
  - *  or META-INF/application.xml or a WEB-INF/web.xml file.
  - *  Depending on the file type, the whole file (EJBs, WARs)
  - *  or only the relevant packages (EAR) becoming downloaded. <br>
  - *  <i> replacing alternative DDs and validation is not yet implementet! </i>
  - *  The uploaded files are getting passed through to the responsible deployer
  - *  (ContainerFactory for jBoss and EmbededTomcatService for Tomcat).
  - *  The state of deployments is persistent and becomes recovered after shutdown
  - *  or crash.
  - *
  - *   @author Daniel Schulze ([EMAIL PROTECTED])
  - *   @version $Revision: 1.3 $
  - */
  +*  (if Tomcat runs within the same VM as jBoss) or even Application.ears. <br>
  +*  The deployment is done by determining the file type of the given url.
  +*  The file must be a valid zip file and must contain either a META-INF/ejb-jar.xml
  +*  or META-INF/application.xml or a WEB-INF/web.xml file.
  +*  Depending on the file type, the whole file (EJBs, WARs)
  +*  or only the relevant packages (EAR) becoming downloaded. <br>
  +*  <i> replacing alternative DDs and validation is not yet implementet! </i>
  +*  The uploaded files are getting passed through to the responsible deployer
  +*  (ContainerFactory for jBoss and EmbededTomcatService for Tomcat).
  +*  The state of deployments is persistent and becomes recovered after shutdown
  +*  or crash.
  +*
  +*   @author <a href="mailto:[EMAIL PROTECTED]">Daniel Schulze</a>
  +*   @version $Revision: 1.4 $
  +*/
   public class J2eeDeployer 
  -   extends ServiceMBeanSupport
  -   implements J2eeDeployerMBean
  +extends ServiceMBeanSupport
  +implements J2eeDeployerMBean
   {
      // Constants -----------------------------------------------------
  -   public String DEPLOYMENT_DIR = "/home/deployment"; // default?
  -   public String CONFIG = "deployments.conf";   
  -
  +   public String DEPLOYMENT_DIR = "/home/deployment"; // default? MUST BE ABSOLUTE 
PATH!!!
  +   public String CONFIG = "deployment.cfg";   
  +   
      // Attributes ----------------------------------------------------
      // my server to lookup for the special deployers
      MBeanServer server;
  -
  +   
      // names of the specials deployers
      ObjectName jarDeployer;
      ObjectName warDeployer;
  -
  +   
      String jarDeployerName;
      String warDeployerName;
  -
  +   
      // The logger for this service
      Log log = new Log(getName());
  -
  +   
      // the deployments
      Hashtable deployments = new Hashtable ();
      
  @@ -84,18 +84,18 @@
      /** only for testing...*/
      public static void main (String[] _args) throws Exception
      {
  -     new J2eeDeployer ("", "EJB:service=ContainerFactory", 
":service=EmbeddedTomcat").deploy (_args[0]);
  +      new J2eeDeployer ("", "EJB:service=ContainerFactory", 
":service=EmbeddedTomcat").deploy (_args[0]);
      }
      
      // Constructors --------------------------------------------------
      /** */
      public J2eeDeployer (String _deployDir, String jarDeployerName, String 
warDeployerName)
      {
  -       DEPLOYMENT_DIR = _deployDir;
  +      DEPLOYMENT_DIR = _deployDir;
         
  -       this.jarDeployerName = jarDeployerName;
  -       this.warDeployerName = warDeployerName;
  -       
  +      this.jarDeployerName = jarDeployerName;
  +      this.warDeployerName = warDeployerName;
  +   
      }
      
      
  @@ -106,57 +106,87 @@
      *   @param _url the url (file or http) to the archiv to deploy
      *   @throws MalformedURLException in case of a malformed url
      *   @throws J2eeDeploymentException if something went wrong...
  +   *   @throws IOException if trouble while file download occurs
      */
  -   public void deploy (String _url) throws MalformedURLException, 
J2eeDeploymentException
  +   public void deploy (String _url) throws MalformedURLException, IOException, 
J2eeDeploymentException
      {
  -     URL url = new URL (_url);
  -     
  -     // undeploy first if it is a redeploy
  -     if (deployments.containsKey (url))
  -        undeploy (_url);
  +      URL url = new URL (_url);
  +      
  +      // undeploy first if it is a redeploy
  +      try
  +      {
  +         undeploy (_url);
  +      }
  +      catch (Exception _e)
  +      {}
         
         // now try to deploy
         log.log ("deploy j2ee application: " + _url);
  -
  -      Deployment d = installApplication (url);
  +      
  +      Deployment d = null;
  +      try
  +      {
  +         d = installApplication (url);
  +         startApplication (d);
  +         
  +         log.log ("j2ee application: " + _url + " is deployed.");
  +      } 
  +      catch (IOException _ioe)
  +      {
  +         uninstallApplication (_url.substring(Math.max (0, _url.lastIndexOf("/"))));
  +         throw _ioe;
  +      }
  +      catch (J2eeDeploymentException _e)
  +      {
  +         if (d != null) // start failed...
  +            stopApplication (d);
            
  -      deployments.put (d.downloadUrl, d);
  -      storeConfig ();
  -      log.log ("j2ee application: " + _url + " is deployed.");
  +         uninstallApplication (_url.substring(Math.max (0, _url.lastIndexOf("/"))));
  +         throw _e;
  +      }
      }
      
      /** Undeploys the given URL (if it is deployed).
  +   *   Actually only the file name is of interest, so it dont has to be 
  +   *   an URL to be undeployed, the file name is ok as well.
      *   @param _url the url to to undeploy 
      *   @throws MalformedURLException in case of a malformed url
      *   @throws J2eeDeploymentException if something went wrong (but should have 
removed all files)
  +   *   @throws IOException if file removement fails
      */
  -   public void undeploy (String _url) throws MalformedURLException, 
J2eeDeploymentException
  +   public void undeploy (String _app) throws IOException, J2eeDeploymentException
      {
  -     URL url = new URL (_url);
  -     
  -      log.log ("undeploy j2ee application: " + _url);
  -     Deployment d = (Deployment)deployments.get (url);
  -     if (d != null)
  -     {
  -         try 
  -         {      
  -            uninstallApplication (d);
  -             } 
  -             catch (J2eeDeploymentException e)
  -             {
  -                     throw e;
  -             } 
  -             finally
  -             {
  -             deployments.remove (url);
  -            storeConfig ();
  -            log.log ("j2ee application: " + _url + " is undeployed.");
  +      String name = _app.substring(Math.max (0, _app.lastIndexOf("/")));
  +      Deployment d = null;
  +      File f = new File (DEPLOYMENT_DIR + File.separator + name);
  +      if (f.exists())
  +      {
  +         try
  +         {
  +            d = loadConfig (name);
  +            stopApplication (d);
  +         }   
  +         catch (IOException _ioe)
  +         {  // thrown by the load config
  +            throw _ioe;
  +         }
  +         catch (J2eeDeploymentException _e)
  +         {
  +            throw _e;               
  +         }
  +         finally 
  +         {
  +            uninstallApplication (name);
            }
  -      } else
  -       log.warning (_url + " is NOT deployed!");
  +      
  +      }
  +      else
  +         throw new J2eeDeploymentException ("The application \""+name+"\" is not 
deployed.");
      }
      
      /** Checks if the given URL is currently deployed or not.
  +   *   Actually only the file name is of interest, so it dont has to be 
  +   *   an URL to be undeployed, the file name is ok as well.
      *   @param _url the url to to check
      *   @return true if _url is deployed
      *   @throws MalformedURLException in case of a malformed url
  @@ -165,51 +195,26 @@
      */
      public boolean isDeployed (String _url) throws MalformedURLException, 
J2eeDeploymentException
      {
  -     URL url = new URL (_url);
  -
  -        boolean deployed = deployments.containsKey (url);
  -        if (deployed)
  -        {
  -         try 
  -         {
  -             // check all modules ...
  -             Deployment d = (Deployment)deployments.get (url);
  -   
  -             // jBoss
  -             Iterator i = d.ejbModules.iterator ();
  -             while (i.hasNext () && deployed)
  -             {
  -                     Deployment.Module m = (Deployment.Module)i.next ();
  -                     // Call the ContainerFactory that is loaded in the JMX server
  -               Object result = server.invoke(jarDeployer, "isDeployed",
  -                                             new Object[] { m.localUrl.toString () 
}, new String[] { "java.lang.String" });
  -               deployed = ((Boolean)result).booleanValue ();
  -            }
  -   
  -             // Tomcat
  -             i = d.webModules.iterator ();
  -             if (i.hasNext () && !warDeployerAvailable ())
  -                throw new J2eeDeploymentException ("the application containes web 
modules but the tomcat service isn't running?!");
  -             
  -             while (i.hasNext () && deployed)
  -             {
  -                     Deployment.Module m = (Deployment.Module)i.next ();
  -               Object result = server.invoke(warDeployer, "isDeployed",
  -                                             new Object[] { m.localUrl.toString () 
}, new String[] { "java.lang.String" });
  -               deployed = ((Boolean)result).booleanValue ();
  -            }
  -                
  -                if (!deployed)       
  -                   throw new J2eeDeploymentException ("The application is not 
correct deployed!");
  -
  +      // get Application name
  +      String name = _url.substring(Math.max (0, _url.lastIndexOf("/")));
  +      Deployment d = null;
  +      File f = new File (DEPLOYMENT_DIR + File.separator + name);
  +      if (f.exists())
  +      {
  +         try
  +         {
  +            return checkApplication (loadConfig (name));         
            } 
  -         catch (MBeanException _mbe) {
  -             throw new J2eeDeploymentException ("error while interacting with 
deployer MBeans... " + _mbe.getTargetException ().getMessage ());
  -         } catch (JMException _jme){
  -             throw new J2eeDeploymentException ("fatal error while interacting with 
deployer MBeans... " + _jme.getMessage ());
  +         catch (IOException e)
  +         {
  +            // no config found
  +            throw new J2eeDeploymentException ("The application \""+name+"\" seems 
to be installed, "+
  +               "but cant read "+CONFIG+" file!? Cant handle...");
            }
  -        }
  -        return deployed;
  +      }
  +      else
  +         return false;
  +   
      }
      
      // ServiceMBeanSupport overrides ---------------------------------
  @@ -217,382 +222,538 @@
      {
         return "J2ee deployer";
      }
  -
  +   
      protected ObjectName getObjectName(MBeanServer server, ObjectName name)
  -      throws javax.management.MalformedObjectNameException
  +   throws javax.management.MalformedObjectNameException
      {
  -     this.server = server;
  +      this.server = server;
         return new ObjectName(OBJECT_NAME);
      }
  -
  +   
      /** */
      protected void initService()
  -      throws Exception
  +   throws Exception
      {
         
         //set the deployment directory
  -       DEPLOYMENT_DIR = new File (DEPLOYMENT_DIR).getCanonicalPath ();
  -
  -       // Save JMX name of the deployers
  +      DEPLOYMENT_DIR = new File (DEPLOYMENT_DIR).getCanonicalPath ();
  +      
  +      // Save JMX name of the deployers
         jarDeployer = new ObjectName(jarDeployerName);
         warDeployer= new ObjectName(warDeployerName);
  -     
  -       // load the configuration
  -       loadConfig ();
  -
  +   
      }
  -
  +   
      /** tries to redeploy all deployments before shutdown.*/
      protected void startService()
  -      throws Exception
  +   throws Exception
      {
         if (!warDeployerAvailable ())
            log.log ("No war deployer found - only EJB deployment available...");
  -
  -
  -      log.log ("trying to redeploy all applications that were running before 
shutdown...");
  -     Iterator it = deployments.values ().iterator ();
  -     while (it.hasNext ())
  -     {
  -             Deployment d = (Deployment) it.next ();
  -             try
  -        {
  -                     deploy (d.downloadUrl.toString ());
  -         }
  -         catch (J2eeDeploymentException _e)
  -         {
  -             log.warning ("unable to redeploy application "+d.name+" -> delete 
it.");
  -         }
  -     }
  -      log.log ("deployment state recovered.");
  +      
  +      /*
  +      // this doesnt work properly ?!
  +      // when something was deployed on server stop and on server restart
  +      // it becomes restarted (the same files) the ContainerFactory has a
  +      // strange problem in handling its copys of the jar files in the tmp/deploy 
dir... 
  +      
  +      log.log ("trying to start all applications that were running before 
service.stop ()...");
  +      
  +      File f = new File (DEPLOYMENT_DIR);
  +      File[] files =  f.listFiles();
  +      int count = 0;
  +      for (int i = 0, l = files.length; i<l; ++i)
  +      {
  +      if (files[i].isDirectory ())
  +      {
  +      // lets give it a try...
  +      Deployment d = null;
  +      try
  +      {
  +      d = loadConfig (files[i].getName());
  +      }
  +      catch (IOException _io)
  +      {
  +      continue;
  +      }
  +      
  +      // ok, then lets start it
  +      log.log ("starting application: " + d.name);
  +      try
  +      {
  +      startApplication (d);
  +      log.log ("application: " + d.name + " is started.");
  +      ++count;
  +      } 
  +      catch (J2eeDeploymentException _e)
  +      {
  +      stopApplication (d);
  +      uninstallApplication (files[i].getName());
  +      
  +      _e.printStackTrace();
  +      }
  +      }
  +      }
  +      log.log ("started "+count+" applications.");
  +      */
      }
  -
  -
  -   /** */
  +   
  +   
  +   /** undeploys all deployments */
      protected void stopService()
      {
  -/*      // Uncomment method body to get redeploy only on server crash
  -      log.log ("undeploy all applications...");
  -     Iterator it = deployments.values ().iterator ();
  -     while (it.hasNext ())
  -     {
  -             Deployment d = (Deployment) it.next ();
  -             try
  -        {
  -                     undeploy (d.downloadUrl.toString ());
  -         }
  -         catch (J2eeDeploymentException _e)
  +      log.log ("undeploying all applications.");
  +      
  +      File f = new File (DEPLOYMENT_DIR);
  +      File[] files =  f.listFiles();
  +      int count = 0;
  +      for (int i = 0, l = files.length; i<l; ++i)
  +      {
  +         if (files[i].isDirectory ())
            {
  -             log.warning ("hmmm... "+d.name+" dont wants to become undeployed?!");
  +            try
  +            {
  +               Deployment d = loadConfig (files[i].getName ());
  +               stopApplication (d);
  +            }   
  +            catch (IOException _ioe)
  +            {  // thrown by the load config
  +               //throw _ioe; 
  +               log.exception(_ioe);
  +            }
  +            catch (J2eeDeploymentException _e)
  +            {
  +               //throw _e;               
  +               log.exception(_e);
  +            }
  +            finally 
  +            {
  +               try {
  +                  uninstallApplication (files[i].getName ());
  +               } catch (IOException _ioe)
  +               {log.exception(_ioe);}
  +            }
  +            ++count;
            }
  -     }
  -     storeConfig ();
  -      log.log ("all applications undeployed.");
  -*/   }
  -    
  +      }
  +      log.log ("undeployed "+count+" applications.");
  +   }
  +   
      // Private -------------------------------------------------------
  -
  -   /** Deploys all packages of the given deployment. <br>
  -   *   This means download the needed packages do some validation and/or
  -   *   other things and pass the references to the special deployers.
  +   
  +   /** determines deployment type and installs (downloads) all packages needed 
  +   *   by the given deployment of the given deployment. <br>
  +   *   This means download the needed packages do some validation...
      *   <i> Validation and do some other things is not yet implemented </i>
      *   @param _d the deployment (= a J2ee application or module)
  +   *   @throws IOException if the download fails
  +   *   @throws J2eeDeploymentException if the given package is somehow inconsistent
      */
  -   Deployment installApplication (URL _downloadUrl) throws J2eeDeploymentException
  +   Deployment installApplication (URL _downloadUrl) throws IOException, 
J2eeDeploymentException
      {
         // determine the file type by trying to access one of the possible
         // deployment descriptors...
         Element root = null;
         String[] files = {"META-INF/ejb-jar.xml", "META-INF/application.xml", 
"WEB-INF/web.xml"};
  +      boolean directory = false;
         for (int i = 0; i < files.length && root == null; ++i)
         {
  -             try 
  -             {
  +         // try it as if it is a jar file
  +         try {
               root = XmlFileLoader.getDocument (new URL ("jar:" + 
_downloadUrl.toString () + "!/" + files[i])).getDocumentElement ();
  -             } catch (Exception _e) {}
  -      }
  +         } catch (Exception _e) {}
   
  +         // try it, as if it is a directory
  +         // but this cant be handled right now
  +         // try {
  +         //    root = XmlFileLoader.getDocument (new URL (_downloadUrl.toString () 
+ "/" + files[i])).getDocumentElement ();
  +         //    directory = true;
  +         // } catch (Exception _e) {}
  +      }
  +      
  +      if (root == null)
  +         // no descriptor was found...
  +      throw new J2eeDeploymentException ("No valid deployment descriptor was found 
within this URL: "+
  +         _downloadUrl.toString ()+
  +         "\nMake sure it points to a valid j2ee package 
(ejb.jar/web.war/app.ear)!");
  +      
         // create a Deployment
         Deployment d = new Deployment ();
  +      URL currentDownload = null; // to give more precize info in error case which 
download failed
         try {
  -         // if we found a deployment descriptor
  -         if (root != null)
  -         {         
  -            d.name = d.getAppName (_downloadUrl);
  -            d.downloadUrl = _downloadUrl;
  -            StringBuffer sb = new StringBuffer ("file:");
  -            sb.append (DEPLOYMENT_DIR);
  -            sb.append ("/");
  -            // sb.append (d.getAdminCtx (d.downloadUrl));
  -            // sb.append ("/");
  -            sb.append (d.name);
  -            d.localUrl = new URL (sb.toString ()); // should never throw an URLEx
  -
  -             log.log ("create application " + d.name);
  -
  -
  -            // do the app type dependent stuff...
  -             if ("ejb-jar".equals (root.getTagName ()))
  -             {
  -                     // its a EJB.jar... just take the package
  -                     Deployment.Module m = d.newModule ();
  -               // localUrl: file:<download_dir> / <adminCtx> / <appName> / <appName>
  -               m.downloadUrl = d.downloadUrl;
  -               m.name = d.getFileName (m.downloadUrl);
  -               m.localUrl = new URL (d.localUrl.toString () + "/" + m.name);// 
should never throw an URLEx
  -
  -             log.log ("downloading module " + m.name);
  -               URLWizzard.download (m.downloadUrl, m.localUrl);
  -               d.ejbModules.add (m);                 
  -             } 
  -             else if ("web-app".equals (root.getTagName ()))
  -             {
  -                     // its a WAR.jar... just take the package
  -                     if (!warDeployerAvailable ())
  -                        throw new J2eeDeploymentException ("No war deployer 
available!");
  -                     
  -                     Deployment.Module m = d.newModule ();
  -               m.downloadUrl = d.downloadUrl;
  -               m.name = d.getFileName (m.downloadUrl);
  -               m.localUrl = new URL (d.localUrl.toString () + "/" + m.name);// 
should never throw an URLEx
  -               m.webContext = d.getWebContext (m.downloadUrl);
  -
  -             log.log ("downloading module " + m.name);
  -               URLWizzard.downloadAndInflate (m.downloadUrl, m.localUrl);
  -               d.webModules.add (m);                 
  -             } 
  -             else if ("application".equals (root.getTagName ()))
  -            {
  -                     // its a EAR.jar... hmmm, its a little more
  -                     // create a MetaData object...
  -               J2eeApplicationMetaData app;
  -                     app = new J2eeApplicationMetaData (root);
  -      
  -               // currently we only take care for the modules
  -               // no security stuff, no alternative DDs 
  -               // no dependency and integrity checking... !!!
  -                     J2eeModuleMetaData mod;
  -                     Iterator it = app.getModules ();
  -                     while (it.hasNext ())
  -                {
  -                             mod = (J2eeModuleMetaData) it.next ();
  -                             // iterate the ear modules
  -                  if (mod.isEjb () || mod.isWeb ())
  -                  {
  -                     // a EJB or WEB package...
  -                     Deployment.Module m = d.newModule ();
  -                     try 
  -                     {
  -                        m.downloadUrl = new URL ("jar:" + _downloadUrl.toString () 
+ "!/" + mod.getFileName ());// chould throw an URLEx
  -                        m.name = d.getFileName (m.downloadUrl);
  -                        m.localUrl = new URL (d.localUrl.toString () + "/" + 
m.name);// chould throw an URLEx
  -                     } catch (MalformedURLException _mue) { 
  -                        throw new J2eeDeploymentException ("syntax error in module 
section in application DD : " + _mue.getMessage ());
  -                     }
  -                     
  -                     if (mod.isWeb ())
  -                     {
  -                             if (!warDeployerAvailable ())
  -                                throw new J2eeDeploymentException ("No war deployer 
available!");
  -
  -                        m.webContext = mod.getWebContext ();
  -                        if (m.webContext == null)
  -                           m.webContext = d.getWebContext (m.downloadUrl);
  +         d.name = Deployment.getAppName (_downloadUrl);
  +         String localUrl = "file:"+DEPLOYMENT_DIR+"/"+d.name;
            
  -                             log.log ("downloading and inflating module " + m.name);
  -                        URLWizzard.downloadAndInflate (m.downloadUrl, m.localUrl);
  -                        d.webModules.add (m);                
  -                     }
  -                     else
  -                     {
  -                             log.log ("downloading module " + m.name);
  -                        URLWizzard.download (m.downloadUrl, m.localUrl);
  -                        d.ejbModules.add (m);
  -                     }
  -
  -                     // here the code for checking and DD replacment...
  -                     // ...
  -                     } 
  -               }
  -                  // other packages we dont care about (currently)
  -             }
  -             
  -                     // set the context classloader for this application
  -                     createContextClassLoader(d);
  -                     
  -                     // redirect all modules to the responsible deployer
  -                     Deployment.Module m = null;
  -                             
  -                     try
  +         log.log ("create application " + d.name);
  +         
  +         
  +         // do the app type dependent stuff...
  +         if ("ejb-jar".equals (root.getTagName ()))
  +         {
  +            // its a EJB.jar... just take the package
  +            Deployment.Module m = d.newModule ();
  +            // localUrl: file:<download_dir> / <appName> / <appName>
  +            m.name = Deployment.getFileName (_downloadUrl);
  +            m.localUrl = new URL (localUrl+"/"+m.name);// should never throw an 
URLEx
  +            
  +            log.log ("downloading module " + m.name);
  +            currentDownload = _downloadUrl;
  +            URLWizzard.download (currentDownload, m.localUrl);
  +            d.ejbModules.add (m);                    
  +         } 
  +         else if ("web-app".equals (root.getTagName ()))
  +         {
  +            // its a WAR.jar... just take the package
  +            if (!warDeployerAvailable ())
  +               throw new J2eeDeploymentException ("No war container available!");
  +            
  +            Deployment.Module m = d.newModule ();
  +            m.name = Deployment.getFileName (_downloadUrl);
  +            m.localUrl = new URL (localUrl+"/"+m.name);// should never throw an 
URLEx
  +            m.webContext = Deployment.getWebContext (_downloadUrl);
  +            
  +            log.log ("downloading and inflating module " + m.name);
  +            currentDownload = _downloadUrl;
  +            URLWizzard.downloadAndInflate (currentDownload, m.localUrl);
  +            d.webModules.add (m);                    
  +         } 
  +         else if ("application".equals (root.getTagName ()))
  +         {
  +            // its a EAR.jar... hmmm, its a little more
  +            // create a MetaData object...
  +            J2eeApplicationMetaData app;
  +            app = new J2eeApplicationMetaData (root);
  +            
  +            // currently we only take care for the modules
  +            // no security stuff, no alternative DDs 
  +            // no dependency and integrity checking... !!!
  +            J2eeModuleMetaData mod;
  +            Iterator it = app.getModules ();
  +            while (it.hasNext ())
               {
  -                     // jBoss
  -               Iterator it = d.ejbModules.iterator ();
  -               while (it.hasNext ())
  +               mod = (J2eeModuleMetaData) it.next ();
  +               // iterate the ear modules
  +               if (mod.isEjb () || mod.isWeb ())
                  {
  -                     m = (Deployment.Module)it.next ();
  -                     log.log ("deploying module " + m.name);
  -                     // Call the ContainerFactory that is loaded in the JMX server
  -                  server.invoke(jarDeployer, "deploy",
  -                                  new Object[] { m.localUrl.toString () }, new 
String[] { "java.lang.String" });
  -               }
  -      
  -                     
  -                     // Tomcat
  -               it = d.webModules.iterator ();
  -               while (it.hasNext ())
  -               {
  -                     m = (Deployment.Module)it.next ();
  -                     log.log ("deploying module " + m.name);
  -                     // Call the TomcatDeployer that is loaded in the JMX server
  -                  server.invoke(warDeployer, "deploy",
  -                                  new Object[] { m.webContext, m.localUrl.toString 
()}, new String[] { "java.lang.String", "java.lang.String" });
  -                     
  -             }
  +                  
  +                  // common stuff
  +                  Deployment.Module m = d.newModule ();
  +                  URL downloadUrl;
  +                  try 
  +                  {
  +                     currentDownload = new URL ("jar:" + _downloadUrl.toString () + 
"!"+
  +                        (mod.getFileName ().startsWith ("/") ? "" : "/")+
  +                        mod.getFileName ());// chould throw an URLEx
  +                     m.name = Deployment.getFileName (currentDownload);
  +                     m.localUrl = new URL (localUrl + "/" + m.name);// should not 
throw an URLEx
  +                  } 
  +                  catch (MalformedURLException _mue) 
  +                  { 
  +                     throw new J2eeDeploymentException ("syntax error in module 
section in application DD (module uri: " + mod.getFileName()+")");
  +                  }
  +                  
  +                  // web specific
  +                  if (mod.isWeb ())
  +                  {
  +                     if (!warDeployerAvailable ())
  +                        throw new J2eeDeploymentException ("Application containes 
.war file. No war container available!");
  +                     
  +                     m.webContext = mod.getWebContext ();
  +                     if (m.webContext == null)
  +                        m.webContext = Deployment.getWebContext (currentDownload);
  +                     
  +                     log.log ("downloading and inflating module " + m.name);
  +                     URLWizzard.downloadAndInflate (currentDownload, m.localUrl);
  +                     d.webModules.add (m);                   
  +                  }
  +                  // ejb specific
  +                  else 
  +                  {
  +                     log.log ("downloading module " + m.name);
  +                     URLWizzard.download (currentDownload, m.localUrl);
  +                     d.ejbModules.add (m);
  +                  }
  +                  
  +                  // here the code for checking and DD replacment...
  +                  // ...
  +               } 
               }
  -            catch (MBeanException _mbe) {
  -                     log.log ("deploying failed!");
  -                        uninstallApplication (d);
  -             throw new J2eeDeploymentException ("error while interacting with 
deployer MBeans... " + _mbe.getTargetException ().getMessage ());
  -            } catch (JMException _jme){
  -                     log.log ("deploying failed!");
  -                        uninstallApplication (d);
  -             throw new J2eeDeploymentException ("fatal error while interacting with 
deployer MBeans... " + _jme.getMessage ());
  -            }                        
  -            
  -
  -            return d;
  +            // other packages we dont care about (currently)
            }
  -
  -             } catch (IOException _ioe) {
  -                uninstallApplication (d);
  -                throw new J2eeDeploymentException ("Error in downloading module: " 
+ _ioe.getMessage ());
  -             } catch (DeploymentException _de) { 
  -                uninstallApplication (d);
  -                throw new J2eeDeploymentException ("Unable scan .ear file: " + 
_de.getMessage ());
  -             } catch (Exception _e) { 
  -                _e.printStackTrace ();
  -                uninstallApplication (d);
  -                throw new J2eeDeploymentException ("FATAL ERROR!");
  +         // storing the deployment
  +         currentDownload = null;
  +         FileOutputStream fout = new FileOutputStream (localUrl.substring (5, 
localUrl.length ()) + File.separator + CONFIG);
  +         ObjectOutputStream out = new ObjectOutputStream (fout);
  +         out.writeObject (d);
  +         out.flush ();
  +         out.close ();
  +         
  +         return d;
  +      } 
  +      catch (IOException _ioe) //happens on downloading modules 
  +      {
  +         if (currentDownload == null)
  +            throw new IOException ("Error in saving config file "+CONFIG +": 
"+_ioe.getMessage ());
  +         else
  +            throw new IOException ("Error in downloading 
"+currentDownload.toString() +
  +            ": "+_ioe.getMessage ());
  +      }
  +      catch (DeploymentException _de) // happens on creating 
J2eeApplicationMetaData 
  +      { 
  +         throw new J2eeDeploymentException ("Error in scanning .ear file deploymnet 
descriptor: " + _de.getMessage ());
  +      } 
  +      catch (Exception _e) // when something goes really wrong (runtime exception) 
  +      { 
  +         _e.printStackTrace ();
  +         throw new J2eeDeploymentException ("FATAL ERROR! "+_e.toString ());
         }                      
  -      throw new J2eeDeploymentException (_downloadUrl.toString ()+" points not to a 
valid j2ee application (app.ear/ejb.jar/web.war)!");
      }
  -
  -   void uninstallApplication (Deployment _d) throws J2eeDeploymentException
  +   
  +   /** Deletes the file tree of  the specified application. <br>
  +   *   @param _name the directory (DEPLOYMENT_DIR/<_name> to remove recursivly
  +   *   @throws IOException if something goes wrong
  +   */
  +   void uninstallApplication (String _name) throws IOException
      {
  -     log.log ("destroying application " + _d.name);
  -      String error = "";
  -      Iterator it = _d.ejbModules.iterator ();
  -      while (it.hasNext ())
  -      {
  -             Deployment.Module m = (Deployment.Module)it.next ();
  -             log.log ("uninstalling module " + m.name);
  -             
  -             //jBoss
  -         try
  +      log.log ("destroying application " + _name);
  +      URL url = null;
  +      try 
  +      {
  +         url = new URL ("file:"+DEPLOYMENT_DIR+"/"+_name);
  +         URLWizzard.deleteTree (url);      
  +         log.log ("file tree "+url.toString ()+" deleted.");
  +      } catch (MalformedURLException _mfe) { // should never happen
  +      } catch (IOException _ioe) {
  +         throw _ioe;
  +      }
  +   }
  +   
  +   /** Starts the successful downloaded deployment. <br>
  +   *   Means the modules are deployed by the responsible container deployer
  +   *   @param _d the deployment to start
  +   *   @throws J2eeDeploymentException if an error occures for one of these
  +   *           modules
  +   */
  +   private void startApplication (Deployment _d) throws J2eeDeploymentException
  +   {
  +      // set the context classloader for this application
  +      createContextClassLoader(_d);
  +      
  +      // redirect all modules to the responsible deployers
  +      Deployment.Module m = null;
  +      String message;
  +      try
  +      {
  +         // jBoss
  +         Iterator it = _d.ejbModules.iterator ();
  +         while (it.hasNext ())
            {
  -                     // Call the ContainerFactory that is loaded in the JMX server
  -            server.invoke(jarDeployer, "undeploy",
  -                            new Object[] { m.localUrl.toString () }, new String[] { 
"java.lang.String" });
  +            m = (Deployment.Module)it.next ();
  +            log.log ("starting module " + m.name);
  +            // Call the ContainerFactory that is loaded in the JMX server
  +            server.invoke(jarDeployer, "deploy",
  +               new Object[] { m.localUrl.toString () }, new String[] { 
"java.lang.String" });
            }
  -         catch (MBeanException _mbe) {
  -             log.warning ("unable to undeploy module " + m.name + ": " + 
_mbe.getTargetException ().getMessage ());
  -         } catch (JMException _jme){
  -             error += "[fatal error while interacting with deployer MBeans... " + 
_jme.getMessage () + "]";
  -                     }
  +         
  +         
  +         // Tomcat
  +         it = _d.webModules.iterator ();
  +         while (it.hasNext ())
  +         {
  +            m = (Deployment.Module)it.next ();
  +            log.log ("starting module " + m.name);
  +            // Call the TomcatDeployer that is loaded in the JMX server
  +            server.invoke(warDeployer, "deploy",
  +               new Object[] { m.webContext, m.localUrl.toString ()}, new String[] { 
"java.lang.String", "java.lang.String" });
  +         }
         }
  -      it = _d.webModules.iterator ();
  -      while (it.hasNext ())
  +      catch (MBeanException _mbe) {
  +         log.error ("starting "+m.name+" failed!");
  +         throw new J2eeDeploymentException ("error while starting "+m.name+": " + 
_mbe.getTargetException ().getMessage ());
  +      } catch (JMException _jme){
  +         log.error ("starting failed!");
  +         throw new J2eeDeploymentException ("fatal error while interacting with 
deployer MBeans... " + _jme.getMessage ());
  +      }                      
  +   }
  +   
  +   /** Stops a running deployment. <br>
  +   *   Means the modules are undeployed by the responsible container deployer
  +   *   @param _d the deployment to stop
  +   *   @throws J2eeDeploymentException if an error occures for one of these
  +   *           modules
  +   */
  +   private void stopApplication (Deployment _d) throws J2eeDeploymentException
  +   {
  +      StringBuffer error = new StringBuffer ();
  +      ObjectName[] container = new ObjectName[] {jarDeployer, warDeployer};
  +      Iterator modules[] = new Iterator[] {_d.ejbModules.iterator 
(),_d.webModules.iterator ()};
  +      for (int i = 0; i < container.length; ++i)
         {
  -             Deployment.Module m = (Deployment.Module)it.next ();
  -             log.log ("uninstalling module " + m.name);
  -
  -             // tomcat
  -                     if (warDeployerAvailable ())
  -                     {
  +         if (container[i] == null && modules[i].hasNext ())
  +         {
  +            // in case we are not running with tomcat
  +            // should only happen for tomcat (i=1)
  +            log.warning("cannot find .war container");
  +            continue;
  +         }         
  +         
  +         while (modules[i].hasNext ())
  +         {
  +            Deployment.Module m = (Deployment.Module)modules[i].next ();
               try
  +            {
  +               // Call the ContainerFactory/EmbededTomcat that is loaded in the JMX 
server
  +               Object result = server.invoke(container[i], "isDeployed",
  +                  new Object[] { m.localUrl.toString () }, new String[] { 
"java.lang.String" });
  +               if (((Boolean)result).booleanValue ())
  +               {
  +                  
  +                  log.log ("stopping module " + m.name);
  +                  server.invoke(container[i], "undeploy",
  +                     new Object[] { m.localUrl.toString () }, new String[] { 
"java.lang.String" });
  +               }
  +               else
  +                  log.log ("module " + m.name+" is not running");
  +            
  +            }
  +            catch (MBeanException _mbe) 
  +            {
  +               log.error ("unable to stop module " + m.name + ": " + 
_mbe.getTargetException ().getMessage ());
  +               error.append("unable to stop module " + m.name + ": " + 
_mbe.getTargetException ().getMessage ());
  +               error.append ("/n");
  +            } 
  +            catch (JMException _jme)
               {
  -               server.invoke(warDeployer, "undeploy",
  -                               new Object[] { m.localUrl.toString () }, new 
String[] { "java.lang.String" });
  +               log.error ("unable to stop module " + m.name + ": " + 
_jme.getMessage ());
  +               error.append("unable to stop module " + m.name + ": fatal error 
while calling "+container[i]+": " + _jme.getMessage ());
  +               error.append ("/n");
               }
  -            catch (MBeanException _mbe) {
  -             log.warning ("unable to undeploy module " + m.name + ": " + 
_mbe.getTargetException ().getMessage ());
  -            } catch (JMException _jme){
  -             error += "[fatal error while interacting with deployer MBeans... " + 
_jme.getMessage () + "]";
  -                     }
            }
  -      }      
  -      
  -      try 
  -      {
  -         URLWizzard.deleteTree (_d.localUrl);      
  -             log.log ("file tree "+_d.localUrl.toString ()+" deleted.");
  -      } catch (IOException _ioe) {
  -                throw new J2eeDeploymentException ("Error in removing local 
application files ("+_d.localUrl+"): " + _ioe.getMessage ());
         }
  -
  -      if (!error.equals (""))
  -                throw new J2eeDeploymentException (error);
  +      if (!error.toString ().equals (""))
  +         // there was at least one error...
  +      throw new J2eeDeploymentException ("error(s) on stopping application 
"+_d.name+":\n"+error.toString ());
      }
  -
  -
  -
  -   private void loadConfig ()
  +   
  +   /** Checks the Deplyment if it is correctly deployed.
  +   *   @param app to check
  +   *   @throws J2eeDeploymentException if some inconsistency in the deployment is
  +   *           detected
  +   */
  +   private boolean checkApplication (Deployment _d) throws J2eeDeploymentException
      {
  -      try {
  -     ObjectInputStream in = new ObjectInputStream (new FileInputStream 
(DEPLOYMENT_DIR + File.separator + CONFIG));
  -     deployments = (Hashtable)in.readObject ();
  -     in.close ();
  -      } catch (Exception e)
  +      boolean result = false;
  +      int count = 0;
  +      int others = 0;
  +      
  +      ObjectName[] container = new ObjectName[] {jarDeployer, warDeployer};
  +      Iterator modules[] = new Iterator[] {_d.ejbModules.iterator 
(),_d.webModules.iterator ()};
  +      for (int i = 0; i < container.length; ++i)
         {
  -             log.log ("no config file found...");
  +         if (container[i] == null && modules[i].hasNext ())
  +         {
  +            // in case we are not running with tomcat
  +            // should only happen for tomcat (i=1)
  +            log.warning("cannot find .war container");
  +            continue;
  +         }         
  +         
  +         while (modules[i].hasNext ())
  +         {
  +            Deployment.Module m = (Deployment.Module)modules[i].next ();
  +            Object o = null;
  +            try
  +            {
  +               
  +               log.log ("checking module " + m.name);
  +               // Call the ContainerFactory/EmbededTomcat that is loaded in the JMX 
server
  +               o = server.invoke(container[i], "isDeployed",
  +                  new Object[] { m.localUrl.toString () }, new String[] { 
"java.lang.String" });
  +               
  +            }
  +            catch (MBeanException _mbe) 
  +            {
  +               log.error ("error while checking module " + m.name + ": " + 
_mbe.getTargetException ().getMessage ());
  +            } 
  +            catch (JMException _jme)
  +            {
  +               log.error ("fatal error while checking module " + m.name + ": " + 
_jme.getMessage ());
  +            }
  +
  +            if (o == null) // had an exception
  +               ++others;
  +            else if (count++ == 0) // first module -> set state
  +               result = ((Boolean)o).booleanValue ();
  +            else if (result != ((Boolean)o).booleanValue ()) // only if differs 
from state
  +               ++others;
  +         }
         }
  +      if (others > 0)
  +         // there was at least one error...
  +      throw new J2eeDeploymentException ("application "+_d.name+" is NOT correct 
deployed! ("+
  +         (result ? count-others : others)+
  +         " modules are running "+
  +         (result ? others : count-others)+
  +         " are not)");
  +      return result;
      }
  -
  -   private void storeConfig ()
  -   {
  -      try {
  -     ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream 
(DEPLOYMENT_DIR + File.separator + CONFIG));
  -     out.writeObject (deployments);
  -     out.close ();
  -      } catch (IOException e)
  +   
  +   
  +   /** Loads the serialized CONFIG file for the specified app. <br>
  +   *   loads DEPLOYMENT_DIR / _app / CONFIG
  +   *   @param _app the name of the app
  +   *   @throws IOException if an error occures
  +   */
  +   private Deployment loadConfig (String _app) throws IOException
  +   {          
  +      Deployment d = null;
  +      try
         {
  -             log.warning ("storing config failed...");
  +         ObjectInputStream in = new ObjectInputStream (
  +            new FileInputStream (DEPLOYMENT_DIR + File.separator + _app + 
File.separator + CONFIG));
  +         d = (Deployment)in.readObject ();
  +         in.close ();
         }
  +      catch (ClassNotFoundException _snfe){} // should never happen...
  +      
  +      return d;
      }
  -
  +   
  +   /** tests if the web container deployer is available
  +   */
      private boolean warDeployerAvailable ()
      {
         return server.isRegistered (warDeployer);
      }
  -
  -
  -     /**
  -      * creates an application class loader for this deployment
  -      * this class loader will be shared between jboss and tomcat via the 
contextclassloader
  -      */
  -     private void createContextClassLoader(Deployment deployment) {
  -             
  -             // find all the urls to add
  -             ArrayList urls = new ArrayList();
  -             
  -             // ejb applications
  -             Iterator iterator = deployment.ejbModules.iterator();
  -             while (iterator.hasNext()) {
  -                     // add the local url to the classloader
  -                     urls.add(((Deployment.Module)iterator.next()).localUrl);
  -             }
  -             
  -             // TODO? web applications
  -             // add WEB-INF/classes and WEB-INF/lib/* from webModules.localUrl
  -             
  -             // actually create the class loader. 
  -             // Keep the current context class loader as a parent (jboss and tomcat 
classes are in it)
  -             URL[] urlArray = (URL[])urls.toArray(new URL[urls.size()]);
  -             ClassLoader parent = Thread.currentThread().getContextClassLoader();
  -             
  -             URLClassLoader appCl = new URLClassLoader(urlArray, parent);
  -             
  -             // set it as the context class loader for the deployment thread
  -             Thread.currentThread().setContextClassLoader(appCl);
  -     }
  +   
  +   
  +   /**
  +   * creates an application class loader for this deployment
  +   * this class loader will be shared between jboss and tomcat via the 
contextclassloader
  +   */
  +   private void createContextClassLoader(Deployment deployment) {
  +      
  +      // find all the urls to add
  +      ArrayList urls = new ArrayList();
  +      
  +      // ejb applications
  +      Iterator iterator = deployment.ejbModules.iterator();
  +      while (iterator.hasNext()) {
  +         // add the local url to the classloader
  +         urls.add(((Deployment.Module)iterator.next()).localUrl);
  +      }
  +      
  +      // TODO? web applications
  +      // add WEB-INF/classes and WEB-INF/lib/* from webModules.localUrl
  +      
  +      // actually create the class loader. 
  +      // Keep the current context class loader as a parent (jboss and tomcat 
classes are in it)
  +      URL[] urlArray = (URL[])urls.toArray(new URL[urls.size()]);
  +      ClassLoader parent = Thread.currentThread().getContextClassLoader();
  +      
  +      URLClassLoader appCl = new URLClassLoader(urlArray, parent);
  +      
  +      // set it as the context class loader for the deployment thread
  +      Thread.currentThread().setContextClassLoader(appCl);
  +   }
   }
  
  
  
  1.2       +5 -3      jboss/src/main/org/jboss/deployment/J2eeDeployerMBean.java
  
  Index: J2eeDeployerMBean.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jboss/src/main/org/jboss/deployment/J2eeDeployerMBean.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- J2eeDeployerMBean.java    2000/10/03 00:08:15     1.1
  +++ J2eeDeployerMBean.java    2000/10/27 19:10:27     1.2
  @@ -7,12 +7,14 @@
   package org.jboss.deployment;
   
   import java.net.MalformedURLException;
  +import java.io.IOException;
  +
   import org.jboss.util.ServiceMBean;
   
   /**
    *   @see 
    *   @author Daniel Schulze ([EMAIL PROTECTED])
  - *   @version $Revision: 1.1 $
  + *   @version $Revision: 1.2 $
    */
   public interface J2eeDeployerMBean
        extends ServiceMBean
  @@ -21,9 +23,9 @@
      public static final String OBJECT_NAME = "J2EE:service=J2eeDeployer";
       
      // Public --------------------------------------------------------
  -   public void deploy (String url) throws MalformedURLException, 
J2eeDeploymentException;
  +   public void deploy (String url) throws MalformedURLException, IOException, 
J2eeDeploymentException;
   
  -   public void undeploy (String url) throws MalformedURLException, 
J2eeDeploymentException;
  +   public void undeploy (String url) throws MalformedURLException, IOException, 
J2eeDeploymentException;
   
      public boolean isDeployed (String url) throws MalformedURLException, 
J2eeDeploymentException;
   }
  
  
  
  1.2       +5 -28     jboss/src/main/org/jboss/deployment/Deployment.java
  
  Index: Deployment.java
  ===================================================================
  RCS file: /products/cvs/ejboss/jboss/src/main/org/jboss/deployment/Deployment.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Deployment.java   2000/10/03 00:08:15     1.1
  +++ Deployment.java   2000/10/27 19:10:27     1.2
  @@ -13,6 +13,9 @@
   import java.util.Vector;
   
   /** Represents a J2EE application or module (EJB.jar, Web.war or App.ear). <br>
  +*
  +*  @author <a href="mailto:[EMAIL PROTECTED]">Daniel Schulze</a>
  +*  @version $Revision: 1.2 $
   */
   public class Deployment
      implements java.io.Serializable
  @@ -20,7 +23,7 @@
        // the applications name
      protected String name;
      // the url from which this app is downloaded and under which it is deployed 
  -   protected URL downloadUrl;
  +   //protected URL downloadUrl;
      // the local position of the apps root directory
      protected URL localUrl;
      
  @@ -43,32 +46,6 @@
        return new Module ();
      }
      
  -     
  -
  -
  -   /** Returns the admin context of this url.
  -   *   @param the download URL
  -   *   @return the administrator context as path fragment
  -   */      
  -   public static String getAdminCtx (URL _url)
  -   {
  -      StringBuffer sb = new StringBuffer ();
  -     if (_url.getProtocol ().equals ("file"))
  -     {
  -             String s = _url.getFile ();
  -             sb.append (s.substring (1, Math.max (0, s.lastIndexOf ("/"))));
  -     }
  -     else
  -      {
  -             sb.append (_url.getHost ());
  -             if (_url.getPort () != -1)
  -             {
  -             sb.append ("/");
  -             sb.append (_url.getPort ());
  -         }
  -      }
  -      return sb.toString ();
  -   }
   
      /** the name of the application.
      *   @param the download URL
  @@ -131,7 +108,7 @@
        String name;
        // the url from which it is downloaded
        // (in case of ear module like: jar:<app.downloadUrl>!/<module>
  -     URL downloadUrl;
  +     //URL downloadUrl;
        // the local url under which it is deployed by the special deployer
        URL localUrl;
        // the web root context in case of war file
  
  
  

Reply via email to