onami-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1503750 - in /onami/sandbox/console/src/main: java/org/apache/onami/console/commands/ConsoleRunner.java java/org/apache/onami/console/modules/config/DefaultConfigurationModule.java resources/META-INF/services/com.google.inject.Module
Date Tue, 16 Jul 2013 15:12:24 GMT
Author: simonetripodi
Date: Tue Jul 16 15:12:24 2013
New Revision: 1503750

URL: http://svn.apache.org/r1503750
Log:
better configuration handling, pass configured properties to the injector in order to bind
them in the console application

Modified:
    onami/sandbox/console/src/main/java/org/apache/onami/console/commands/ConsoleRunner.java
    onami/sandbox/console/src/main/java/org/apache/onami/console/modules/config/DefaultConfigurationModule.java
    onami/sandbox/console/src/main/resources/META-INF/services/com.google.inject.Module

Modified: onami/sandbox/console/src/main/java/org/apache/onami/console/commands/ConsoleRunner.java
URL: http://svn.apache.org/viewvc/onami/sandbox/console/src/main/java/org/apache/onami/console/commands/ConsoleRunner.java?rev=1503750&r1=1503749&r2=1503750&view=diff
==============================================================================
--- onami/sandbox/console/src/main/java/org/apache/onami/console/commands/ConsoleRunner.java
(original)
+++ onami/sandbox/console/src/main/java/org/apache/onami/console/commands/ConsoleRunner.java
Tue Jul 16 15:12:24 2013
@@ -20,7 +20,9 @@ package org.apache.onami.console.command
  */
 
 import static com.google.inject.Guice.createInjector;
+import static java.lang.String.format;
 import static java.lang.System.err;
+import static java.lang.System.getProperty;
 import static java.lang.System.setProperty;
 import static org.apache.onami.configuration.OnamiVariablesExpander.expandVariables;
 import static org.apache.onami.spi.modules.GuiceServiceLoader.loadModules;
@@ -31,11 +33,21 @@ import io.airlift.command.Command;
 import io.airlift.command.Help;
 import io.airlift.command.Option;
 import io.airlift.command.ParseArgumentsUnexpectedException;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
 import jline.console.ConsoleReader;
 
+import org.apache.onami.console.descriptor.Console;
 import org.apache.onami.console.descriptor.io.jackson.OnamiConsoleJacksonReader;
 import org.apache.onami.console.descriptor.io.snakeyaml.OnamiConsoleSnakeYamlReader;
 import org.apache.onami.console.descriptor.io.xpp3.OnamiConsoleXpp3Reader;
+import org.apache.onami.console.modules.config.DefaultConfigurationModule;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 import org.slf4j.Logger;
 
 import ch.qos.logback.classic.LoggerContext;
@@ -68,21 +80,26 @@ public final class ConsoleRunner
     @Inject
     private Cli<Runnable> gitParser;
 
-    @Inject
-    private Injector injector;
+    public void run()
+    {
+        setupLogging();
 
-    @Inject
-    private OnamiConsoleJacksonReader jacksonReader;
+        // read the console configuration
 
-    @Inject
-    private OnamiConsoleSnakeYamlReader snakeYamlReader;
+        Console consoleConfiguration = readConsoleConfiguration();
 
-    @Inject
-    private OnamiConsoleXpp3Reader xpp3Reader;
+        // setup the injector by discovering modules automatically
 
-    public void run()
-    {
-        setupLogging();
+        Injector injector = createInjector( expandVariables( loadModules() ),
+                                            new DefaultConfigurationModule( consoleConfiguration
) );
+
+        // retrieve components needed to run the console
+
+        injector
+        .getMembersInjector( ConsoleRunner.class )
+        .injectMembers( this );
+
+        // run!
 
         logger.info( "" );
         logger.info( "                           ''~``" );
@@ -99,14 +116,6 @@ public final class ConsoleRunner
         logger.info( "                         \\_)    ) /" );
         logger.info( "                                (_/" );
 
-        // setup the injector by discovering modules automatically
-
-        createInjector( expandVariables( loadModules() ) )
-        .getMembersInjector( ConsoleRunner.class )
-        .injectMembers( this );
-
-        // run!
-
         try
         {
             String line;
@@ -185,4 +194,125 @@ public final class ConsoleRunner
         }
     }
 
+    private Console readConsoleConfiguration()
+    {
+        File userDir = new File( getProperty( "user.dir" ) );
+        File ocrXmlDescriptor = new File( userDir, "ocr.xml" );
+        File ocrJsonDescriptor = new File( userDir, "ocr.json" );
+        File ocrYamlDescriptor = new File( userDir, "ocr.yaml" );
+
+        Console console = null;
+        FileInputStream fis = null;
+
+        if (ocrXmlDescriptor.exists())
+        {
+            logger.info( "Reading XML configuration {}...", ocrXmlDescriptor );
+
+            try
+            {
+                fis = new FileInputStream( ocrXmlDescriptor );
+                console = new OnamiConsoleXpp3Reader().read( fis );
+
+                logger.info( "XML configuration {} read", ocrXmlDescriptor );
+            }
+            catch ( FileNotFoundException e )
+            {
+                // cannot happen
+            }
+            catch ( IOException e )
+            {
+                throw newRuntimeException( "An error occurred while reading %s file: %s",
+                                           ocrXmlDescriptor, e.getMessage() );
+            }
+            catch ( XmlPullParserException e )
+            {
+                throw newRuntimeException( "Configuration file %s does not match the expected
input: %s",
+                                           ocrXmlDescriptor, e.getMessage() );
+            }
+            finally
+            {
+                closeQuietly( fis );
+            }
+        }
+        else if ( ocrJsonDescriptor.exists() )
+        {
+            logger.info( "Reading JSON configuration {}...", ocrJsonDescriptor );
+
+            try
+            {
+                fis = new FileInputStream( ocrJsonDescriptor );
+                console = new OnamiConsoleJacksonReader().read( fis );
+
+                logger.info( "JSON configuration {} read", ocrJsonDescriptor );
+            }
+            catch ( FileNotFoundException e )
+            {
+                // cannot happen
+            }
+            catch ( IOException e )
+            {
+                throw newRuntimeException( "An error occurred while reading %s file: %s",
+                                           ocrJsonDescriptor, e.getMessage() );
+            }
+            finally
+            {
+                closeQuietly( fis );
+            }
+        }
+        else if ( ocrYamlDescriptor.exists() )
+        {
+            logger.info( "Reading YAML configuration {}...", ocrYamlDescriptor );
+
+            try
+            {
+                fis = new FileInputStream( ocrYamlDescriptor );
+                console = new OnamiConsoleSnakeYamlReader().read( fis );
+
+                logger.info( "YAML configuration {} read", ocrYamlDescriptor );
+            }
+            catch ( FileNotFoundException e )
+            {
+                // cannot happen
+            }
+            catch ( IOException e )
+            {
+                throw newRuntimeException( "An error occurred while reading %s file: %s",
+                                           ocrYamlDescriptor, e.getMessage() );
+            }
+            finally
+            {
+                closeQuietly( fis );
+            }
+        }
+        else
+        {
+            logger.info( "No ocr.xml/ocr.json/ocr.yaml configuration file found in {}, creating
default console.",
+                         userDir);
+
+            console = new Console();
+        }
+
+        return console;
+    }
+
+    private static RuntimeException newRuntimeException( String messageFormat, Object...args
)
+    {
+        return new RuntimeException( format( messageFormat, args ) );
+    }
+
+    private static void closeQuietly( Closeable closeable )
+    {
+        if ( closeable != null )
+        {
+            try
+            {
+                closeable.close();
+            }
+            catch ( IOException e )
+            {
+                // swallow it
+            }
+        }
+    }
+
 }

Modified: onami/sandbox/console/src/main/java/org/apache/onami/console/modules/config/DefaultConfigurationModule.java
URL: http://svn.apache.org/viewvc/onami/sandbox/console/src/main/java/org/apache/onami/console/modules/config/DefaultConfigurationModule.java?rev=1503750&r1=1503749&r2=1503750&view=diff
==============================================================================
--- onami/sandbox/console/src/main/java/org/apache/onami/console/modules/config/DefaultConfigurationModule.java
(original)
+++ onami/sandbox/console/src/main/java/org/apache/onami/console/modules/config/DefaultConfigurationModule.java
Tue Jul 16 15:12:24 2013
@@ -25,11 +25,20 @@ import static com.google.inject.name.Nam
 import java.io.File;
 
 import org.apache.onami.configuration.ConfigurationModule;
+import org.apache.onami.console.descriptor.Console;
 
 public final class DefaultConfigurationModule
     extends ConfigurationModule
 {
 
+    private final Console consoleConfiguration;
+
+    public DefaultConfigurationModule( Console consoleConfiguration )
+    {
+        this.consoleConfiguration = consoleConfiguration;
+    }
+
+
     @Override
     protected void bindConfigurations()
     {
@@ -45,6 +54,9 @@ public final class DefaultConfigurationM
         // OCR directories
         bind( File.class ).annotatedWith( named( "ocr.dir" ) ).toProvider( OnamiConsoleRunnerDirProvider.class
).in( SINGLETON );
         bind( File.class ).annotatedWith( named( "ocr.repo.dir" ) ).toProvider( OnamiConsoleRepoDirProvider.class
).in( SINGLETON );
+
+        // configuration
+        bindProperties( consoleConfiguration.getProperties() );
     }
 
 }

Modified: onami/sandbox/console/src/main/resources/META-INF/services/com.google.inject.Module
URL: http://svn.apache.org/viewvc/onami/sandbox/console/src/main/resources/META-INF/services/com.google.inject.Module?rev=1503750&r1=1503749&r2=1503750&view=diff
==============================================================================
--- onami/sandbox/console/src/main/resources/META-INF/services/com.google.inject.Module (original)
+++ onami/sandbox/console/src/main/resources/META-INF/services/com.google.inject.Module Tue
Jul 16 15:12:24 2013
@@ -18,7 +18,6 @@
 # required startup modules
 
 org.apache.onami.console.modules.cli.CliParserModule
-org.apache.onami.console.modules.config.DefaultConfigurationModule
 org.apache.onami.console.modules.console.ConsoleReaderModule
 org.apache.onami.console.modules.descriptor.IoDescriptorsModule
 



Mime
View raw message