You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by si...@apache.org on 2011/12/26 22:04:52 UTC

svn commit: r1224812 - in /cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli: CliCommand.java CliOptions.java Main.java

Author: simonetripodi
Date: Mon Dec 26 21:04:52 2011
New Revision: 1224812

URL: http://svn.apache.org/viewvc?rev=1224812&view=rev
Log:
applies a (simplified) command pattern instead of getting and analyzing parsed options

Added:
    cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/CliCommand.java   (contents, props changed)
      - copied, changed from r1224808, cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/CliOptions.java
Removed:
    cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/CliOptions.java
Modified:
    cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/Main.java

Copied: cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/CliCommand.java (from r1224808, cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/CliOptions.java)
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/CliCommand.java?p2=cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/CliCommand.java&p1=cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/CliOptions.java&r1=1224808&r2=1224812&rev=1224812&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/CliOptions.java (original)
+++ cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/CliCommand.java Mon Dec 26 21:04:52 2011
@@ -18,14 +18,29 @@
  */
 package org.apache.cocoon.cli;
 
+import static java.lang.String.format;
+
 import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
+import java.util.Properties;
+
+import org.apache.cocoon.pipeline.Pipeline;
+import org.apache.cocoon.sax.SAXPipelineComponent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.joran.JoranConfigurator;
+import ch.qos.logback.core.joran.spi.JoranException;
 
 import com.beust.jcommander.Parameter;
 import com.beust.jcommander.converters.FileConverter;
 
-public final class CliOptions
+public final class CliCommand
 {
 
     @Parameter( names = { "-h", "--help" }, description = "Display help information." )
@@ -51,29 +66,235 @@ public final class CliOptions
         return printHelp;
     }
 
-    public boolean isPrintVersion()
+    public void execute()
     {
-        return printVersion;
-    }
+        if ( printVersion )
+        {
+            Properties properties = new Properties();
+            InputStream input =
+                Main.class.getClassLoader().getResourceAsStream( "META-INF/maven/org.apache.cocoon/cocoon-cli/pom.properties" );
+
+            if ( input != null )
+            {
+                try
+                {
+                    properties.load( input );
+                }
+                catch ( IOException e )
+                {
+                    // ignore, just don't load the properties
+                }
+                finally
+                {
+                    try
+                    {
+                        input.close();
+                    }
+                    catch ( IOException e )
+                    {
+                        // close quietly
+                    }
+                }
+            }
+
+            System.out.printf( "Apache Cocoon 3 %s (%s)%n",
+                    properties.getProperty( "version" ),
+                    properties.getProperty( "build" ));
+            System.out.printf( "Java version: %s, vendor: %s%n",
+                    System.getProperty( "java.version" ),
+                    System.getProperty( "java.vendor" ) );
+            System.out.printf( "Java home: %s%n", System.getProperty( "java.home" ) );
+            System.out.printf( "Default locale: %s_%s, platform encoding: %s%n",
+                    System.getProperty( "user.language" ),
+                    System.getProperty( "user.country" ),
+                    System.getProperty( "sun.jnu.encoding" ) );
+            System.out.printf( "OS name: \"%s\", version: \"%s\", arch: \"%s\", family: \"%s\"%n",
+                    System.getProperty( "os.name" ),
+                    System.getProperty( "os.version" ),
+                    System.getProperty( "os.arch" ),
+                    getOsFamily() );
+        }
+
+        System.out.println( "  _                         _                      _" );
+        System.out.println( " /_) _   _   _ ( _   _     / ` _   _  _   _   _    _)" );
+        System.out.println( "/ / )_) (_( (_  ) ) )_)   (_. (_) (_ (_) (_) ) )   _)" );
+        System.out.println( "    (               (_ " );
+
+        // logging stuff
+        final Logger logger = LoggerFactory.getLogger( Main.class );
+
+        // assume SLF4J is bound to logback in the current environment
+        final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
+
+        try
+        {
+            JoranConfigurator configurator = new JoranConfigurator();
+            configurator.setContext( lc );
+            // the context was probably already configured by default configuration
+            // rules
+            lc.reset();
+            configurator.doConfigure( Main.class.getClassLoader().getResourceAsStream( "logback-config.xml" ) );
+        }
+        catch ( JoranException je )
+        {
+            // StatusPrinter should handle this
+        }
+
+        if ( pipelineDescriptor == null
+                        || !pipelineDescriptor.exists()
+                        || pipelineDescriptor.isDirectory() )
+        {
+            System.err.println( "'c3p.xml' file does not exist!" );
+            System.exit( -1 );
+        }
+
+        logger.info( "" );
+        logger.info( "------------------------------------------------------------------------" );
+        logger.info( "Apache Cocoon 3" );
+        logger.info( "------------------------------------------------------------------------" );
+        logger.info( "" );
+
+        long start = System.currentTimeMillis();
+        int exit = 0;
+
+        Throwable error = null;
+        try
+        {
+            PipelinesRegistry registry = new PipelineDescriptorParser( pipesHelp ).parse( pipelineDescriptor );
+
+            if ( !pipesHelp )
+            {
+                if ( !pipelineIDs.isEmpty() )
+                {
+                    for ( String id : pipelineIDs )
+                    {
+                        logger.info( "Executing pipeline '{}'...", id );
+
+                        Pipeline<SAXPipelineComponent> pipeline = registry.getPipeline( id );
+
+                        if ( pipeline == null )
+                        {
+                            throw new NullPointerException( format( "Pipeline '%s' not found!", id ) );
+                        }
+
+                        pipeline.setup( System.out );
+                        pipeline.execute();
+
+                        logger.info( "Pipeline '{}' executed", id );
+                    }
+                }
+                else
+                {
+                    if ( registry.getDefaultId() == null )
+                    {
+                        throw new NullPointerException( "Default pipeline not specified, no pipeline executed!" );
+                    }
+
+                    logger.info( "Executing default Pipeline '{}'", registry.getDefaultId() );
+
+                    Pipeline<SAXPipelineComponent> pipeline = registry.getDefaultPipeline();
+
+                    if ( pipeline == null )
+                    {
+                        throw new NullPointerException( format( "Default pipeline '%s' not found!", registry.getDefaultId() ) );
+                    }
+
+                    pipeline.setup( System.out );
+                    pipeline.execute();
+
+                    logger.info( "Default pipeline '{}' executed", registry.getDefaultId() );
+                }
+            }
+
+        }
+        catch ( Throwable t )
+        {
+            exit = -1;
+            error = t;
+        }
+        finally
+        {
+            logger.info( "" );
+            logger.info( "------------------------------------------------------------------------" );
+            logger.info( "Apache Cocoon 3 {}", ( exit < 0 ) ? "FAILURE" : "SUCCESS" );
+
+            if ( exit < 0 )
+            {
+                logger.info( "" );
+
+                if ( debug )
+                {
+                    logger.error( "Apache Cocoon 3 terminated with errors", error );
+                }
+                else
+                {
+                    logger.error( "Apache Cocoon 3 terminated with errors: {}", error.getMessage() );
+                }
+
+                logger.info( "" );
+            }
+
+            logger.info( "Total time: {}s", ( ( System.currentTimeMillis() - start ) / 1000 ) );
+            logger.info( "Finished at: {}", new Date() );
+
+            final Runtime runtime = Runtime.getRuntime();
+            final int megaUnit = 1024 * 1024;
+            logger.info( "Final Memory: {}M/{}M", ( runtime.totalMemory() - runtime.freeMemory() ) / megaUnit,
+                         runtime.totalMemory() / megaUnit );
 
-    public boolean isDebug()
-    {
-        return debug;
-    }
+            logger.info( "------------------------------------------------------------------------" );
 
-    public File getPipelineDescriptor()
-    {
-        return pipelineDescriptor;
+            System.exit( exit );
+        }
     }
 
-    public List<String> getPipelineIDs()
+    private static final String getOsFamily()
     {
-        return pipelineIDs;
-    }
+        String osName = System.getProperty( "os.name" ).toLowerCase();
+        String pathSep = System.getProperty( "path.separator" );
 
-    public boolean isPipesHelp()
-    {
-        return pipesHelp;
+        if ( osName.indexOf( "windows" ) != -1 )
+        {
+            return "windows";
+        }
+        else if ( osName.indexOf( "os/2" ) != -1 )
+        {
+            return "os/2";
+        }
+        else if ( osName.indexOf( "z/os" ) != -1 || osName.indexOf( "os/390" ) != -1 )
+        {
+            return "z/os";
+        }
+        else if ( osName.indexOf( "os/400" ) != -1 )
+        {
+            return "os/400";
+        }
+        else if ( pathSep.equals( ";" ) )
+        {
+            return "dos";
+        }
+        else if ( osName.indexOf( "mac" ) != -1 )
+        {
+            if ( osName.endsWith( "x" ) )
+            {
+                return "mac"; // MACOSX
+            }
+            return "unix";
+        }
+        else if ( osName.indexOf( "nonstop_kernel" ) != -1 )
+        {
+            return "tandem";
+        }
+        else if ( osName.indexOf( "openvms" ) != -1 )
+        {
+            return "openvms";
+        }
+        else if ( pathSep.equals( ":" ) )
+        {
+            return "unix";
+        }
+
+        return "undefined";
     }
 
 }

Propchange: cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/CliCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/CliCommand.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/CliCommand.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/Main.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/Main.java?rev=1224812&r1=1224811&r2=1224812&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/Main.java (original)
+++ cocoon/cocoon3/trunk/cocoon-cli/src/main/java/org/apache/cocoon/cli/Main.java Mon Dec 26 21:04:52 2011
@@ -18,23 +18,6 @@
  */
 package org.apache.cocoon.cli;
 
-import static java.lang.String.format;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Date;
-import java.util.Properties;
-
-import org.apache.cocoon.pipeline.Pipeline;
-import org.apache.cocoon.sax.SAXPipelineComponent;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.joran.JoranConfigurator;
-import ch.qos.logback.core.joran.spi.JoranException;
-
 import com.beust.jcommander.JCommander;
 
 public final class Main
@@ -50,248 +33,19 @@ public final class Main
 
     public static void main( String[] args )
     {
-        CliOptions options = new CliOptions();
+        CliCommand command = new CliCommand();
 
-        JCommander jCommander = new JCommander( options );
+        JCommander jCommander = new JCommander( command );
         jCommander.setProgramName( "c3pipe" );
         jCommander.parseWithoutValidation( args );
 
-        if ( options.isPrintHelp() )
+        if ( command.isPrintHelp() )
         {
             jCommander.usage();
             System.exit( -1 );
         }
 
-        if ( options.isPrintVersion() )
-        {
-            Properties properties = new Properties();
-            InputStream input =
-                Main.class.getClassLoader().getResourceAsStream( "META-INF/maven/org.apache.cocoon/cocoon-cli/pom.properties" );
-
-            if ( input != null )
-            {
-                try
-                {
-                    properties.load( input );
-                }
-                catch ( IOException e )
-                {
-                    // ignore, just don't load the properties
-                }
-                finally
-                {
-                    try
-                    {
-                        input.close();
-                    }
-                    catch ( IOException e )
-                    {
-                        // close quietly
-                    }
-                }
-            }
-
-            System.out.printf( "Apache Cocoon 3 %s (%s)%n",
-                    properties.getProperty( "version" ),
-                    properties.getProperty( "build" ));
-            System.out.printf( "Java version: %s, vendor: %s%n",
-                    System.getProperty( "java.version" ),
-                    System.getProperty( "java.vendor" ) );
-            System.out.printf( "Java home: %s%n", System.getProperty( "java.home" ) );
-            System.out.printf( "Default locale: %s_%s, platform encoding: %s%n",
-                    System.getProperty( "user.language" ),
-                    System.getProperty( "user.country" ),
-                    System.getProperty( "sun.jnu.encoding" ) );
-            System.out.printf( "OS name: \"%s\", version: \"%s\", arch: \"%s\", family: \"%s\"%n",
-                    System.getProperty( "os.name" ),
-                    System.getProperty( "os.version" ),
-                    System.getProperty( "os.arch" ),
-                    getOsFamily() );
-
-            System.exit( -1 );
-        }
-
-        System.out.println( "  _                         _                      _" );
-        System.out.println( " /_) _   _   _ ( _   _     / ` _   _  _   _   _    _)" );
-        System.out.println( "/ / )_) (_( (_  ) ) )_)   (_. (_) (_ (_) (_) ) )   _)" );
-        System.out.println( "    (               (_ " );
-
-        // logging stuff
-        final Logger logger = LoggerFactory.getLogger( Main.class );
-
-        // assume SLF4J is bound to logback in the current environment
-        final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
-
-        try
-        {
-            JoranConfigurator configurator = new JoranConfigurator();
-            configurator.setContext( lc );
-            // the context was probably already configured by default configuration
-            // rules
-            lc.reset();
-            configurator.doConfigure( Main.class.getClassLoader().getResourceAsStream( "logback-config.xml" ) );
-        }
-        catch ( JoranException je )
-        {
-            // StatusPrinter should handle this
-        }
-
-        File pipelineDescriptor = options.getPipelineDescriptor();
-        if ( pipelineDescriptor == null
-                        || !pipelineDescriptor.exists()
-                        || pipelineDescriptor.isDirectory() )
-        {
-            System.err.println( "'c3p.xml' file does not exist!" );
-            System.exit( -1 );
-        }
-
-        logger.info( "" );
-        logger.info( "------------------------------------------------------------------------" );
-        logger.info( "Apache Cocoon 3" );
-        logger.info( "------------------------------------------------------------------------" );
-        logger.info( "" );
-
-        long start = System.currentTimeMillis();
-        int exit = 0;
-
-        Throwable error = null;
-        try
-        {
-            PipelinesRegistry registry = new PipelineDescriptorParser( options.isPipesHelp() ).parse( pipelineDescriptor );
-
-            if ( !options.isPipesHelp() )
-            {
-                if ( !options.getPipelineIDs().isEmpty() )
-                {
-                    for ( String id : options.getPipelineIDs() )
-                    {
-                        logger.info( "Executing pipeline '{}'...", id );
-
-                        Pipeline<SAXPipelineComponent> pipeline = registry.getPipeline( id );
-
-                        if ( pipeline == null )
-                        {
-                            throw new NullPointerException( format( "Pipeline '%s' not found!", id ) );
-                        }
-
-                        pipeline.setup( System.out );
-                        pipeline.execute();
-
-                        logger.info( "Pipeline '{}' executed", id );
-                    }
-                }
-                else
-                {
-                    if ( registry.getDefaultId() == null )
-                    {
-                        throw new NullPointerException( "Default pipeline not specified, no pipeline executed!" );
-                    }
-
-                    logger.info( "Executing default Pipeline '{}'", registry.getDefaultId() );
-
-                    Pipeline<SAXPipelineComponent> pipeline = registry.getDefaultPipeline();
-
-                    if ( pipeline == null )
-                    {
-                        throw new NullPointerException( format( "Default pipeline '%s' not found!", registry.getDefaultId() ) );
-                    }
-
-                    pipeline.setup( System.out );
-                    pipeline.execute();
-
-                    logger.info( "Default pipeline '{}' executed", registry.getDefaultId() );
-                }
-            }
-
-        }
-        catch ( Throwable t )
-        {
-            exit = -1;
-            error = t;
-        }
-        finally
-        {
-            logger.info( "" );
-            logger.info( "------------------------------------------------------------------------" );
-            logger.info( "Apache Cocoon 3 {}", ( exit < 0 ) ? "FAILURE" : "SUCCESS" );
-
-            if ( exit < 0 )
-            {
-                logger.info( "" );
-
-                if ( options.isDebug() )
-                {
-                    logger.error( "Apache Cocoon 3 terminated with errors", error );
-                }
-                else
-                {
-                    logger.error( "Apache Cocoon 3 terminated with errors: {}", error.getMessage() );
-                }
-
-                logger.info( "" );
-            }
-
-            logger.info( "Total time: {}s", ( ( System.currentTimeMillis() - start ) / 1000 ) );
-            logger.info( "Finished at: {}", new Date() );
-
-            final Runtime runtime = Runtime.getRuntime();
-            final int megaUnit = 1024 * 1024;
-            logger.info( "Final Memory: {}M/{}M", ( runtime.totalMemory() - runtime.freeMemory() ) / megaUnit,
-                         runtime.totalMemory() / megaUnit );
-
-            logger.info( "------------------------------------------------------------------------" );
-
-            System.exit( exit );
-        }
-    }
-
-    private static final String getOsFamily()
-    {
-        String osName = System.getProperty( "os.name" ).toLowerCase();
-        String pathSep = System.getProperty( "path.separator" );
-
-        if ( osName.indexOf( "windows" ) != -1 )
-        {
-            return "windows";
-        }
-        else if ( osName.indexOf( "os/2" ) != -1 )
-        {
-            return "os/2";
-        }
-        else if ( osName.indexOf( "z/os" ) != -1 || osName.indexOf( "os/390" ) != -1 )
-        {
-            return "z/os";
-        }
-        else if ( osName.indexOf( "os/400" ) != -1 )
-        {
-            return "os/400";
-        }
-        else if ( pathSep.equals( ";" ) )
-        {
-            return "dos";
-        }
-        else if ( osName.indexOf( "mac" ) != -1 )
-        {
-            if ( osName.endsWith( "x" ) )
-            {
-                return "mac"; // MACOSX
-            }
-            return "unix";
-        }
-        else if ( osName.indexOf( "nonstop_kernel" ) != -1 )
-        {
-            return "tandem";
-        }
-        else if ( osName.indexOf( "openvms" ) != -1 )
-        {
-            return "openvms";
-        }
-        else if ( pathSep.equals( ":" ) )
-        {
-            return "unix";
-        }
-
-        return "undefined";
+        command.execute();
     }
 
 }