geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ammul...@apache.org
Subject svn commit: r332215 - in /geronimo/trunk/modules: deploy-jsr88/src/java/org/apache/geronimo/deployment/plugin/jmx/ deploy-tool/src/java/org/apache/geronimo/deployment/cli/
Date Thu, 10 Nov 2005 03:29:13 GMT
Author: ammulder
Date: Wed Nov  9 19:29:07 2005
New Revision: 332215

URL: http://svn.apache.org/viewcvs?rev=332215&view=rev
Log:
Auto-detect a module's ModuleID and use that to make the ModuleID argument
  optional for "redeploy".  GERONIMO-1120 and GERONIMO-804.
A couple minor cleanups.

Modified:
    geronimo/trunk/modules/deploy-jsr88/src/java/org/apache/geronimo/deployment/plugin/jmx/JMXDeploymentManager.java
    geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/AbstractCommand.java
    geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/CommandRedeploy.java
    geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/CommandUndeploy.java
    geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/DeployUtils.java
    geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/ServerConnection.java

Modified: geronimo/trunk/modules/deploy-jsr88/src/java/org/apache/geronimo/deployment/plugin/jmx/JMXDeploymentManager.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/deploy-jsr88/src/java/org/apache/geronimo/deployment/plugin/jmx/JMXDeploymentManager.java?rev=332215&r1=332214&r2=332215&view=diff
==============================================================================
--- geronimo/trunk/modules/deploy-jsr88/src/java/org/apache/geronimo/deployment/plugin/jmx/JMXDeploymentManager.java
(original)
+++ geronimo/trunk/modules/deploy-jsr88/src/java/org/apache/geronimo/deployment/plugin/jmx/JMXDeploymentManager.java
Wed Nov  9 19:29:07 2005
@@ -282,8 +282,7 @@
         } else if(dObj.getType().equals(ModuleType.RAR)) {
             return new RARConfigurer().createConfiguration(dObj);
         } else if(dObj.getType().equals(ModuleType.WAR)) {
-            return new WARConfigurer().createConfiguration(dObj); // this is jetty
-            // todo: Tomcat WARConfigurer
+            return new WARConfigurer().createConfiguration(dObj);
         }
         throw new InvalidModuleException("Not supported");
     }

Modified: geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/AbstractCommand.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/AbstractCommand.java?rev=332215&r1=332214&r2=332215&view=diff
==============================================================================
--- geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/AbstractCommand.java
(original)
+++ geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/AbstractCommand.java
Wed Nov  9 19:29:07 2005
@@ -26,6 +26,7 @@
 import javax.enterprise.deploy.spi.Target;
 import javax.enterprise.deploy.spi.DeploymentManager;
 import java.io.PrintWriter;
+import java.io.OutputStreamWriter;
 import java.util.List;
 import java.util.Set;
 import java.util.HashSet;
@@ -43,6 +44,7 @@
     private String group;
     private String helpArgumentList;
     private String helpText;
+    private PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
 
     public AbstractCommand(String command, String group, String helpArgumentList, String
helpText) {
         this.command = command;
@@ -71,6 +73,15 @@
         return false;
     }
 
+    public void setOut(PrintWriter out) {
+        this.out = out;
+    }
+
+    protected void emit(String message) {
+        out.println(DeployUtils.reformat(message,4,72));
+        out.flush();
+    }
+
     /**
      * Busy-waits until the provided <code>ProgressObject</code>
      * indicates that it's no longer running.
@@ -80,13 +91,13 @@
      * trace.
      * @param po a <code>ProgressObject</code> value
      */
-    protected static void waitForProgress(PrintWriter out, ProgressObject po) {
+    protected void waitForProgress(PrintWriter out, ProgressObject po) {
         po.addProgressListener(new ProgressListener() {
             String last = null;
             public void handleProgressEvent(ProgressEvent event) {
                 String msg = event.getDeploymentStatus().getMessage();
                 if(last != null && !last.equals(msg)) {
-                    System.out.println(DeployUtils.reformat(last,8,72)); //todo: use the
same writer as DeployTool
+                    emit(last);
                 }
                 last = msg;
             }

Modified: geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/CommandRedeploy.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/CommandRedeploy.java?rev=332215&r1=332214&r2=332215&view=diff
==============================================================================
--- geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/CommandRedeploy.java
(original)
+++ geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/CommandRedeploy.java
Wed Nov  9 19:29:07 2005
@@ -18,9 +18,7 @@
 package org.apache.geronimo.deployment.cli;
 
 import org.apache.geronimo.common.DeploymentException;
-
-import java.io.PrintWriter;
-import java.io.File;
+import java.io.*;
 import java.util.List;
 import java.util.ArrayList;
 import javax.enterprise.deploy.spi.DeploymentManager;
@@ -48,17 +46,20 @@
                 "If more than one TargetModuleID is provided, all TargetModuleIDs " +
                 "must refer to the same module (just running on different targets).\n" +
                 "Regardless of whether the old module was running or not, the new " +
-                "module will be started (if the server is running) or marked to start (if
" +
-                "the server is not running).\n" +
+                "module will be started.\n" +
+                "If no ModuleID or TargetModuleID is specified, and you're deploying to "+
+                "Geronimo, the deployer will attempt to guess the correct ModuleID for "+
+                "you based on the module and/or plan you provided.\n"+
                 "Note: To specify a TargetModuleID, use the form TargetName|ModuleName");
     }
 
     public void execute(PrintWriter out, ServerConnection connection, String[] args) throws
DeploymentException {
+        setOut(out);
         if(!connection.isOnline()) {
             throw new DeploymentException("This command cannot be run unless connecting to
a running server.  Specify --url if server is not running on the default port on localhost.");
         }
-        if(args.length < 2) {
-            throw new DeploymentSyntaxException("Must specify a module or plan (or both)
and one or more module IDs to replace");
+        if(args.length == 0) {
+            throw new DeploymentSyntaxException("Must specify a module or plan (or both)
and optionally module IDs to replace");
         }
         DeploymentManager mgr = connection.getDeploymentManager();
         Target[] allTargets = mgr.getTargets();
@@ -71,9 +72,9 @@
         List modules = new ArrayList();
         File module = null;
         File plan = null;
-        File test = new File(args[0]);
+        File test = new File(args[0]); // Guess whether the first argument is a module or
a plan
         if(!test.exists()) {
-            throw new DeploymentSyntaxException("Must specify a module or plan (or both)
and one or more module IDs to replace");
+            throw new DeploymentSyntaxException("Must specify a module or plan (or both)
and optionally module IDs to replace");
         }
         if(!test.canRead()) {
             throw new DeploymentException("Cannot read file "+test.getAbsolutePath());
@@ -89,24 +90,54 @@
             }
             plan = test;
         }
-        test = new File(args[1]);
-        if(test.exists() && test.canRead()) {
-            if(DeployUtils.isJarFile(test) || test.isDirectory()) {
-                if(module != null) {
-                    throw new DeploymentSyntaxException("Module and plan cannot both be JAR
files or directories!");
+        if(args.length > 1) { // Guess whether the second argument is a module, plan,
ModuleID, or TargetModuleID
+            test = new File(args[1]);
+            if(test.exists() && test.canRead() && !args[1].equals(args[0]))
{
+                if(DeployUtils.isJarFile(test) || test.isDirectory()) {
+                    if(module != null) {
+                        throw new DeploymentSyntaxException("Module and plan cannot both
be JAR files or directories!");
+                    }
+                    module = test;
+                } else {
+                    if(plan != null) {
+                        throw new DeploymentSyntaxException("Module or plan must be a JAR
file or directory!");
+                    }
+                    plan = test;
                 }
-                module = test;
             } else {
+                modules.addAll(identifyTargetModuleIDs(allModules, args[1]));
+            }
+        }
+        for(int i=2; i<args.length; i++) { // Any arguments beyond 2 must be a ModuleID
or TargetModuleID
+            modules.addAll(identifyTargetModuleIDs(allModules, args[i]));
+        }
+        // If we don't have any moduleIDs, try to guess one.
+        if(modules.size() == 0 && connection.isGeronimo()) {
+            emit("No ModuleID or TargetModuleID provided.  Attempting to guess based on the
content of the "+(plan == null ? "archive" : "plan")+".");
+            String moduleId = null;
+            try {
                 if(plan != null) {
-                    throw new DeploymentSyntaxException("Module or plan must be a JAR file
or directory!");
+                    moduleId = DeployUtils.extractModuleIdFromPlan(plan);
+                } else if(module != null) {
+                    moduleId = DeployUtils.extractModuleIdFromArchive(module);
+                    if(moduleId == null) {
+                        int pos = module.getName().lastIndexOf('.');
+                        moduleId = pos > -1 ? module.getName().substring(0, pos) : module.getName();
+                        emit("Unable to locate Geronimo deployment plan in archive.  Calculating
default ModuleID from archive name.");
+                    }
                 }
-                plan = test;
+            } catch (IOException e) {
+                throw new DeploymentException("Unable to read input files: "+e.getMessage());
+            }
+            if(moduleId != null) {
+                emit("Attempting to use ModuleID '"+moduleId+"'");
+                modules.addAll(identifyTargetModuleIDs(allModules, moduleId));
+            } else {
+                emit("Unable to calculate a ModuleID from supplied module and/or plan.");
             }
-        } else {
-            modules.addAll(identifyTargetModuleIDs(allModules, args[1]));
         }
-        for(int i=2; i<args.length; i++) {
-            modules.addAll(identifyTargetModuleIDs(allModules, args[i]));
+        if(modules.size() == 0) { // Either not deploying to Geronimo or unable to identify
modules
+            throw new DeploymentSyntaxException("No ModuleID or TargetModuleID available.
 Nothing to do.  Maybe you should add a ModuleID or TargetModuleID to the command line?");
         }
         if(module != null) {
             module = module.getAbsoluteFile();
@@ -114,6 +145,7 @@
         if(plan != null) {
             plan = plan.getAbsoluteFile();
         }
+        // Now that we've sorted out all the arguments, do the work
         TargetModuleID[] ids = (TargetModuleID[]) modules.toArray(new TargetModuleID[modules.size()]);
         boolean multiple = isMultipleTargets(ids);
         ProgressObject po = mgr.redeploy(ids, module, plan);
@@ -121,11 +153,11 @@
         TargetModuleID[] done = po.getResultTargetModuleIDs();
         for(int i = 0; i < done.length; i++) {
             TargetModuleID id = done[i];
-            out.println(DeployUtils.reformat("Redeployed "+id.getModuleID()+(multiple ? "
on "+id.getTarget().getName() : "")+(id.getWebURL() == null ? "" : " @ "+id.getWebURL()),
4, 72));
+            emit("Redeployed "+id.getModuleID()+(multiple ? " on "+id.getTarget().getName()
: "")+(id.getWebURL() == null ? "" : " @ "+id.getWebURL()));
             if(id.getChildTargetModuleID() != null) {
                 for (int j = 0; j < id.getChildTargetModuleID().length; j++) {
                     TargetModuleID child = id.getChildTargetModuleID()[j];
-                    out.println(DeployUtils.reformat("  `-> "+child.getModuleID()+(child.getWebURL()
== null ? "" : " @ "+child.getWebURL()),4, 72));
+                    emit("  `-> "+child.getModuleID()+(child.getWebURL() == null ? ""
: " @ "+child.getWebURL()));
                 }
             }
         }
@@ -133,4 +165,5 @@
             throw new DeploymentException("Operation failed: "+po.getDeploymentStatus().getMessage());
         }
     }
+
 }

Modified: geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/CommandUndeploy.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/CommandUndeploy.java?rev=332215&r1=332214&r2=332215&view=diff
==============================================================================
--- geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/CommandUndeploy.java
(original)
+++ geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/CommandUndeploy.java
Wed Nov  9 19:29:07 2005
@@ -21,7 +21,6 @@
 import javax.enterprise.deploy.spi.TargetModuleID;
 import javax.enterprise.deploy.spi.DeploymentManager;
 import javax.enterprise.deploy.spi.status.ProgressObject;
-import org.apache.geronimo.common.DeploymentException;
 
 /**
  * the CLI deployer logic to undeploy.

Modified: geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/DeployUtils.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/DeployUtils.java?rev=332215&r1=332214&r2=332215&view=diff
==============================================================================
--- geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/DeployUtils.java
(original)
+++ geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/DeployUtils.java
Wed Nov  9 19:29:07 2005
@@ -18,16 +18,39 @@
 package org.apache.geronimo.deployment.cli;
 
 import java.io.*;
+import java.util.jar.JarFile;
+import java.util.jar.JarEntry;
 
 import org.apache.geronimo.common.DeploymentException;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.Attributes;
+import org.xml.sax.helpers.DefaultHandler;
+
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.ParserConfigurationException;
 
 /**
- *
+ * Various helpers for deployment.
  *
  * @version $Rev: 53762 $ $Date: 2004-10-04 18:54:53 -0400 (Mon, 04 Oct 2004) $
  */
 public class DeployUtils {
+    /**
+     * Split up an output line so it indents at beginning and end (to fit in a
+     * typical terminal) and doesn't break in the middle of a word.
+     * @param source The unformatted String
+     * @param indent The number of characters to indent on the left
+     * @param endCol The maximum width of the entire line in characters,
+     *               including indent (indent 10 with endCol 70 results
+     *               in 60 "usable" characters).
+     * @return
+     */
     public static String reformat(String source, int indent, int endCol) {
+        if(endCol-indent < 10) {
+            throw new IllegalArgumentException("This is ridiculous!");
+        }
         StringBuffer buf = new StringBuffer((int)(source.length()*1.1));
         String prefix = indent == 0 ? "" : buildIndent(indent);
         try {
@@ -74,6 +97,9 @@
         return buf.toString();
     }
 
+    /**
+     * Try to determine whether a file is a JAR File (or, at least, a ZIP file).
+     */
     public static boolean isJarFile(File file) throws DeploymentException {
         if(file.isDirectory()) {
             return false;
@@ -94,8 +120,113 @@
         }
     }
 
-    public static void main(String[] args) {
-        String msg = "/home/ammulder/cvs/geronimo/modules/security/target/geronimo-security-1.0-SNAPSHOT.jar
does not specify a J2EE-DeploymentFactory-Implementation-Class; cannot load driver.";
-        System.out.println(reformat("Error: "+msg, 4, 72));
+    /**
+     * Attempt to calculate the Geronimo ModuleID for a J2EE application
+     * module.
+     *
+     * Given a File representing an archive (which may be a JAR file or a
+     * directory laid out like a JAR file), identify it's J2EE module type
+     * based on which (if any) deployment descriptor is present, and then look
+     * for a Geronimo deployment plan in the usual place, and if one is found,
+     * retrieve the configId from the Geronimo deployment plan.
+     *
+     * @param module A Jar file or directory representing a J2EE module
+     * @return The configId in the Geronimo deployment plan for this module,
+     *         or null if no Geronimo deployment plan was identified.
+     */
+    public static String extractModuleIdFromArchive(File module) throws IOException, DeploymentException
{
+        if(!module.canRead()) {
+            throw new IllegalArgumentException("Not a readable file");
+        }
+        if(module.isDirectory()) {
+            File target = null;
+            if(new File(module, "WEB-INF/web.xml").canRead()) {
+                target = new File(module, "WEB-INF/geronimo-web.xml");
+            } else if(new File(module, "META-INF/application.xml").canRead()) {
+                target = new File(module, "META-INF/geronimo-application.xml");
+            } else if(new File(module, "META-INF/ejb-jar.xml").canRead()) {
+                target = new File(module, "META-INF/openejb-jar.xml");
+            } else if(new File(module, "META-INF/ra.xml").canRead()) {
+                target = new File(module, "META-INF/geronimo-ra.xml");
+            } else if(new File(module, "META-INF/application-client.xml").canRead()) {
+                target = new File(module, "META-INF/geronimo-application-client.xml");
+            }
+            if(target != null && target.canRead()) {
+                Reader in = new BufferedReader(new FileReader(target));
+                return extractModuleIdFromPlan(in);
+            }
+        } else {
+            if(!isJarFile(module)) {
+                throw new IllegalArgumentException(module.getAbsolutePath()+" is neither
a JAR file nor a directory!");
+            }
+            JarFile input = new JarFile(module);
+            //todo: instead of looking for specific file names here, do something generic.
+            //      Perhaps load a DConfigBeanRoot and look for a configId property on the
first child,
+            //      though that would probably be a little heavyweight.
+            try {
+                JarEntry entry = null;
+                if(input.getJarEntry("WEB-INF/web.xml") != null) {
+                    entry = input.getJarEntry("WEB-INF/geronimo-web.xml");
+                } else if(input.getJarEntry("META-INF/application.xml") != null) {
+                    entry = input.getJarEntry("META-INF/geronimo-application.xml");
+                } else if(input.getJarEntry("META-INF/ejb-jar.xml") != null) {
+                    entry = input.getJarEntry("META-INF/openejb-jar.xml");
+                } else if(input.getJarEntry("META-INF/ra.xml") != null) {
+                    entry = input.getJarEntry("META-INF/geronimo-ra.xml");
+                } else if(input.getJarEntry("META-INF/application-client.xml") != null) {
+                    entry = input.getJarEntry("META-INF/geronimo-application-client.xml");
+                }
+                if(entry != null) {
+                    Reader in = new BufferedReader(new InputStreamReader(input.getInputStream(entry)));
+                    return extractModuleIdFromPlan(in);
+                }
+            } finally {
+                input.close();
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Attempt to calculate the Geronimo ModuleID for a Geronimo deployment
+     * plan.
+     *
+     * @param plan A Geronimo deployment plan (which must be an XML file).
+     * @return The configId in the Geronimo deployment plan for this module.
+     */
+    public static String extractModuleIdFromPlan(File plan) throws IOException {
+        if(plan.isDirectory() || !plan.canRead()) {
+            throw new IllegalArgumentException(plan.getAbsolutePath()+" is not a readable
XML file!");
+        }
+        Reader in = new BufferedReader(new FileReader(plan));
+        return extractModuleIdFromPlan(in);
+    }
+
+    private static String extractModuleIdFromPlan(Reader plan) throws IOException {
+        SAXParserFactory factory = SAXParserFactory.newInstance();
+        factory.setNamespaceAware(true);
+        factory.setValidating(false);
+        try {
+            SAXParser parser = factory.newSAXParser();
+            ConfigIdHandler handler = new ConfigIdHandler();
+            parser.parse(new InputSource(plan), handler);
+            return handler.configId;
+        } catch (ParserConfigurationException e) {
+            throw new IOException("Unable to read plan: "+e.getMessage());
+        } catch (SAXException e) {
+            throw new IOException("Unable to read plan: "+e.getMessage());
+        } finally {
+            plan.close();
+        }
+    }
+
+    private static class ConfigIdHandler extends DefaultHandler {
+        private String configId;
+
+        public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
+            if(configId == null) {
+                configId = attributes.getValue("configId");
+            }
+        }
     }
 }

Modified: geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/ServerConnection.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/ServerConnection.java?rev=332215&r1=332214&r2=332215&view=diff
==============================================================================
--- geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/ServerConnection.java
(original)
+++ geronimo/trunk/modules/deploy-tool/src/java/org/apache/geronimo/deployment/cli/ServerConnection.java
Wed Nov  9 19:29:07 2005
@@ -292,6 +292,10 @@
         return manager != null;
     }
 
+    public boolean isGeronimo() {
+        return isOnline() && manager.getClass().getName().startsWith("org.apache.geronimo.");
+    }
+
     public Object invokeOfflineDeployer(Object[] args, String[] argTypes) throws DeploymentException
{
         if(kernel == null) {
             throw new IllegalStateException("Cannot attempt to package when no local kernel
is available");



Mime
View raw message