You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by rd...@apache.org on 2008/12/13 12:55:54 UTC

svn commit: r726193 - in /james/mpt/trunk: ./ app/ app/src/main/java/org/ app/src/main/java/org/apache/ app/src/main/java/org/apache/james/ app/src/main/java/org/apache/james/mpt/ app/src/main/java/org/apache/james/mpt/app/ main/src/main/java/org/apach...

Author: rdonkin
Date: Sat Dec 13 03:55:53 2008
New Revision: 726193

URL: http://svn.apache.org/viewvc?rev=726193&view=rev
Log:
Basic command line application runs a single script

Added:
    james/mpt/trunk/app/src/main/java/org/
    james/mpt/trunk/app/src/main/java/org/apache/
    james/mpt/trunk/app/src/main/java/org/apache/james/
    james/mpt/trunk/app/src/main/java/org/apache/james/mpt/
    james/mpt/trunk/app/src/main/java/org/apache/james/mpt/app/
    james/mpt/trunk/app/src/main/java/org/apache/james/mpt/app/Main.java
    james/mpt/trunk/app/src/main/java/org/apache/james/mpt/app/RunScript.java
Modified:
    james/mpt/trunk/app/pom.xml
    james/mpt/trunk/main/src/main/java/org/apache/james/mpt/SystemLoggingMonitor.java
    james/mpt/trunk/pom.xml

Modified: james/mpt/trunk/app/pom.xml
URL: http://svn.apache.org/viewvc/james/mpt/trunk/app/pom.xml?rev=726193&r1=726192&r2=726193&view=diff
==============================================================================
--- james/mpt/trunk/app/pom.xml (original)
+++ james/mpt/trunk/app/pom.xml Sat Dec 13 03:55:53 2008
@@ -53,7 +53,11 @@
       <groupId>jmock</groupId>
       <artifactId>jmock</artifactId>
       <scope>test</scope>
-    </dependency>   
+    </dependency> 
+    <dependency>
+      <groupId>commons-cli</groupId>
+      <artifactId>commons-cli</artifactId>
+    </dependency>
     <dependency>
         <groupId>${project.groupId}</groupId>
         <artifactId>apache-james-mtp</artifactId>
@@ -67,6 +71,23 @@
 
   <build>
     <plugins>
+      <!-- Uber Jar -->
+      <plugin>
+        <artifactId>maven-dependency-plugin</artifactId>
+        <executions>
+          <execution>
+            <phase>compile</phase>
+            <goals>
+              <goal>unpack-dependencies</goal>
+            </goals>
+            <configuration>
+              <includeScope>runtime</includeScope>
+              <outputDirectory>${project.build.outputDirectory}</outputDirectory>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jar-plugin</artifactId>
@@ -74,6 +95,7 @@
         <configuration>
         <archive>
             <manifestEntries>
+              <Main-Class>org.apache.james.mpt.app.Main</Main-Class>
               <Specification-Title>Apache James MPT Application</Specification-Title>
               <Specification-Version>${pom.version}</Specification-Version>
               <Specification-Vendor>The Apache Software Foundation</Specification-Vendor>

Added: james/mpt/trunk/app/src/main/java/org/apache/james/mpt/app/Main.java
URL: http://svn.apache.org/viewvc/james/mpt/trunk/app/src/main/java/org/apache/james/mpt/app/Main.java?rev=726193&view=auto
==============================================================================
--- james/mpt/trunk/app/src/main/java/org/apache/james/mpt/app/Main.java (added)
+++ james/mpt/trunk/app/src/main/java/org/apache/james/mpt/app/Main.java Sat Dec 13 03:55:53 2008
@@ -0,0 +1,144 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mpt.app;
+
+import java.io.File;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.GnuParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.OptionBuilder;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+
+/**
+ * <p>Runs MPT application.</p>
+ * <p>Return values:</p>
+ * <table>
+ * <tr><td>0</td><td>Success</td></tr>
+ * <tr><td>-1</td><td>Illegal Arguments</td></tr>
+ * <tr><td>1</td><td>Script not found</td></tr>
+ * <tr><td>1</td><td>Port not a number</td></tr>
+ * </table>
+ */
+public class Main {
+
+    
+    private static final int FILE_NOT_FOUND = 1;
+    private static final int PORT_NOT_A_NUMBER = 2;
+    
+    private static final char FILE_OPTION = 'f';
+    private static final char PORT_OPTION = 'p';
+    private static final char HOST_OPTION = 'h';
+    private static final char SHABANG_OPTION = 's';
+    private static final char VERBOSE_OPTION = 'v';
+
+    public static final void main(final String[] args) throws Exception {
+        final Options options = buildOptions();
+        
+        try {
+            
+            CommandLineParser parser = new GnuParser();
+            CommandLine cmd = parser.parse(options, args);
+            runCommand(cmd);
+            
+        } catch (ParseException e) {
+            System.out.println(e.getMessage());
+            new HelpFormatter().printHelp( "mpt", options );
+            System.exit(-1);
+        }
+        
+    }
+
+    private static void runCommand(CommandLine cmd) throws Exception {
+        final boolean verbose = Boolean.parseBoolean(cmd.getOptionValue(VERBOSE_OPTION, Boolean.toString(false)));
+        final File file = new File(cmd.getOptionValue(FILE_OPTION));
+        if (file.exists()) {
+            try {
+                final int port = Integer.parseInt(cmd.getOptionValue(PORT_OPTION));    
+                final String host = cmd.getOptionValue(HOST_OPTION, "localhost");
+                final String shabang = cmd.getOptionValue(SHABANG_OPTION, null);
+                RunScript runner = new RunScript(file, port, host, shabang, verbose);
+                runner.run();
+                
+            } catch (NumberFormatException e) {
+                System.out.println("Port must be numeric");
+                System.exit(PORT_NOT_A_NUMBER);
+            }
+        } else {
+            System.out.println("Script not found");
+            System.exit(FILE_NOT_FOUND);
+        }
+    }
+
+    @SuppressWarnings("static-access")
+    private static Options buildOptions() {
+        final Options options = new Options();
+        
+        addRunScriptOptions(options);
+        
+        return options;
+    }
+
+    @SuppressWarnings("static-access")
+    private static void addRunScriptOptions(final Options options) {
+        // -f <file> runs this script
+        options.addOption(OptionBuilder
+                    .withArgName("file")
+                    .hasArg()
+                    .withDescription("run this script")
+                    .withLongOpt("file")
+                    .isRequired()
+                    .create(FILE_OPTION));
+        
+        // -p <port> runs against this port
+        options.addOption(OptionBuilder
+                    .withArgName("port")
+                    .hasArg()
+                    .withDescription("runs against this port")
+                    .withLongOpt("port")
+                    .isRequired()
+                    .create(PORT_OPTION));
+        
+        // -h <host> runs against this host
+        options.addOption(OptionBuilder
+                    .withArgName("host")
+                    .hasArg()
+                    .withDescription("runs against this host (defaults to localhost)")
+                    .withLongOpt("host")
+                    .isRequired(false)
+                    .create(HOST_OPTION));
+        // -s <shabang> sets shabang
+        options.addOption(OptionBuilder
+                    .withArgName("shabang")
+                    .hasArg()
+                    .withDescription("sets shabang (defaults to empty)")
+                    .withLongOpt("shabang")
+                    .isRequired(false)
+                    .create(SHABANG_OPTION));
+        // -v sets logging to verbose
+        options.addOption(OptionBuilder
+                    .withDescription("prints lots of logging")
+                    .withLongOpt("verbose")
+                    .isRequired(false)
+                    .create(VERBOSE_OPTION));
+    }
+}

Added: james/mpt/trunk/app/src/main/java/org/apache/james/mpt/app/RunScript.java
URL: http://svn.apache.org/viewvc/james/mpt/trunk/app/src/main/java/org/apache/james/mpt/app/RunScript.java?rev=726193&view=auto
==============================================================================
--- james/mpt/trunk/app/src/main/java/org/apache/james/mpt/app/RunScript.java (added)
+++ james/mpt/trunk/app/src/main/java/org/apache/james/mpt/app/RunScript.java Sat Dec 13 03:55:53 2008
@@ -0,0 +1,63 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mpt.app;
+
+import java.io.File;
+import java.io.FileInputStream;
+
+import org.apache.james.mpt.ExternalHostSystem;
+import org.apache.james.mpt.ProtocolSessionBuilder;
+import org.apache.james.mpt.Runner;
+import org.apache.james.mpt.SystemLoggingMonitor;
+
+/**
+ * Runs a single script.
+ */
+class RunScript {
+
+    private final File file;
+    private final int port;
+    private final String host;
+    private final String shabang;
+    private final SystemLoggingMonitor monitor;
+    
+    public RunScript(final File file, final int port, final String host, final String shabang, final boolean verbose) {
+        super();
+        this.file = file;
+        this.port = port;
+        this.host = host;
+        this.shabang = shabang;
+        monitor = new SystemLoggingMonitor(verbose);
+    }
+
+    /**
+     * Runs the script.
+     */
+    public void run() throws Exception {
+       System.out.println("Running " + file + " against " + host + ":"  + port + "...");
+       
+       final ExternalHostSystem host = new ExternalHostSystem(this.host, port, monitor, shabang, null);
+       final ProtocolSessionBuilder builder = new ProtocolSessionBuilder();
+       final Runner runner = new Runner();
+       
+       builder.addProtocolLines(file.getName(), new FileInputStream(file), runner.getTestElements());
+       runner.runSessions(host);
+    }
+}

Modified: james/mpt/trunk/main/src/main/java/org/apache/james/mpt/SystemLoggingMonitor.java
URL: http://svn.apache.org/viewvc/james/mpt/trunk/main/src/main/java/org/apache/james/mpt/SystemLoggingMonitor.java?rev=726193&r1=726192&r2=726193&view=diff
==============================================================================
--- james/mpt/trunk/main/src/main/java/org/apache/james/mpt/SystemLoggingMonitor.java (original)
+++ james/mpt/trunk/main/src/main/java/org/apache/james/mpt/SystemLoggingMonitor.java Sat Dec 13 03:55:53 2008
@@ -26,6 +26,13 @@
 
     private boolean verbose = false;
     
+    public SystemLoggingMonitor() {
+        this(false);
+    }
+    
+    public SystemLoggingMonitor(final boolean verbose) {
+        this.verbose = verbose;
+    }
     
     public boolean isVerbose() {
         return verbose;
@@ -40,11 +47,15 @@
     }
 
     public void debug(char character) {
-        System.out.print(character);
+        if (verbose) {
+            System.out.print(character);
+        }
     }
 
     public void debug(String message) {
-       System.out.println(message);
+        if (verbose) {
+            System.out.println(message);
+        }
     }
 
     

Modified: james/mpt/trunk/pom.xml
URL: http://svn.apache.org/viewvc/james/mpt/trunk/pom.xml?rev=726193&r1=726192&r2=726193&view=diff
==============================================================================
--- james/mpt/trunk/pom.xml (original)
+++ james/mpt/trunk/pom.xml Sat Dec 13 03:55:53 2008
@@ -139,6 +139,14 @@
     </plugins>
   </build>
   <repositories>
+    <!-- enable central that is otherwise disabled by the parent pom. -->
+    <repository>
+      <id>central</id>
+      <url>http://repo1.maven.org/maven2</url>
+      <releases>
+         <enabled>true</enabled>
+      </releases>
+    </repository>
   </repositories>
 
   <mailingLists>
@@ -301,6 +309,11 @@
       <version>1.1</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>commons-cli</groupId>
+      <artifactId>commons-cli</artifactId>
+      <version>1.1</version>
+    </dependency>
    </dependencies>
   </dependencyManagement>
 



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