continuum-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From batkin...@apache.org
Subject svn commit: r1656155 - in /continuum/trunk: continuum-api/src/main/java/org/apache/continuum/utils/shell/ continuum-commons/src/main/java/org/apache/continuum/installation/
Date Sat, 31 Jan 2015 03:33:46 GMT
Author: batkinson
Date: Sat Jan 31 03:33:45 2015
New Revision: 1656155

URL: http://svn.apache.org/r1656155
Log:
Pulled output consumers out of shell helper so they can be reused.

Added:
    continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java
    continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/ListOutputConsumer.java
    continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/OutputConsumer.java
Modified:
    continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/DefaultShellCommandHelper.java
    continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/ShellCommandHelper.java
    continuum/trunk/continuum-commons/src/main/java/org/apache/continuum/installation/DefaultInstallationService.java

Modified: continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/DefaultShellCommandHelper.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/DefaultShellCommandHelper.java?rev=1656155&r1=1656154&r2=1656155&view=diff
==============================================================================
--- continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/DefaultShellCommandHelper.java
(original)
+++ continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/DefaultShellCommandHelper.java
Sat Jan 31 03:33:45 2015
@@ -83,9 +83,9 @@ public class DefaultShellCommandHelper
     private static class IOConsumerWrapper
         implements StreamConsumer
     {
-        private IOConsumer userConsumer;
+        private OutputConsumer userConsumer;
 
-        public IOConsumerWrapper( IOConsumer userConsumer )
+        public IOConsumerWrapper( OutputConsumer userConsumer )
         {
             this.userConsumer = userConsumer;
         }
@@ -99,46 +99,8 @@ public class DefaultShellCommandHelper
         }
     }
 
-    private static class FileIOConsumer
-        implements IOConsumer
-    {
-        private PrintWriter writer;
-
-        public FileIOConsumer( File outputFile )
-        {
-            try
-            {
-                this.writer = new PrintWriter( new FileWriter( outputFile ) );
-            }
-            catch ( IOException e )
-            {
-                log.warn( "failed to create file-based io consumer", e );
-            }
-        }
-
-        public void consume( String line )
-        {
-            if ( writer != null )
-            {
-                writer.println( line );
-            }
-        }
-
-        public void flush()
-        {
-            if ( writer != null )
-                writer.flush();
-        }
-
-        public void close()
-        {
-            if ( writer != null )
-                writer.close();
-        }
-    }
-
     public ExecutionResult executeShellCommand( File workingDirectory, String executable,
String[] arguments,
-                                                IOConsumer io, long idCommand,
+                                                OutputConsumer io, long idCommand,
                                                 Map<String, String> environments )
         throws Exception
     {
@@ -210,7 +172,7 @@ public class DefaultShellCommandHelper
                                                 File output, long idCommand, Map<String,
String> environments )
         throws Exception
     {
-        FileIOConsumer fileConsumer = new FileIOConsumer( output );
+        FileOutputConsumer fileConsumer = new FileOutputConsumer( output );
         try
         {
             return executeShellCommand( workingDirectory, executable, arguments, fileConsumer,
idCommand,

Added: continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java?rev=1656155&view=auto
==============================================================================
--- continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java
(added)
+++ continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java
Sat Jan 31 03:33:45 2015
@@ -0,0 +1,65 @@
+package org.apache.continuum.utils.shell;
+
+/*
+ * 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.
+ */
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+/**
+ * Collects output to a file using a buffered writer. Unlike the list-based consumer, this
should be safer to use when
+ * the output size is expected to be large.
+ */
+public class FileOutputConsumer
+    implements OutputConsumer
+{
+    private PrintWriter writer;
+
+    /**
+     * Creates a output consumer for the given file.
+     *
+     * @param outputFile the file to write the results to
+     * @throws IOException if there is a problem creating a file
+     */
+    public FileOutputConsumer( File outputFile )
+        throws IOException
+    {
+        this.writer = new PrintWriter( new FileWriter( outputFile ) );
+    }
+
+    public void consume( String line )
+    {
+        if ( writer != null )
+            writer.println( line );
+    }
+
+    public void flush()
+    {
+        if ( writer != null )
+            writer.flush();
+    }
+
+    public void close()
+    {
+        if ( writer != null )
+            writer.close();
+    }
+}
\ No newline at end of file

Added: continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/ListOutputConsumer.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/ListOutputConsumer.java?rev=1656155&view=auto
==============================================================================
--- continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/ListOutputConsumer.java
(added)
+++ continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/ListOutputConsumer.java
Sat Jan 31 03:33:45 2015
@@ -0,0 +1,48 @@
+package org.apache.continuum.utils.shell;
+
+/*
+ * 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.
+ */
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Collects output into a list. Since all output is stored in memory simultaneously, it should
not be used when expected
+ * output is large.
+ */
+public class ListOutputConsumer
+    implements OutputConsumer
+{
+    List<String> list;
+
+    public ListOutputConsumer()
+    {
+        list = new ArrayList<String>();
+    }
+
+    public void consume( String line )
+    {
+        list.add( line );
+    }
+
+    public List<String> getList()
+    {
+        return list;
+    }
+}

Added: continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/OutputConsumer.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/OutputConsumer.java?rev=1656155&view=auto
==============================================================================
--- continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/OutputConsumer.java
(added)
+++ continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/OutputConsumer.java
Sat Jan 31 03:33:45 2015
@@ -0,0 +1,29 @@
+package org.apache.continuum.utils.shell;
+
+/*
+ * 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.
+ */
+
+/**
+ * Interface for handling shell output.
+ * Created to avoid dependency on plexus's CommandLineUtils.
+ */
+public interface OutputConsumer
+{
+    void consume( String line );
+}

Modified: continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/ShellCommandHelper.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/ShellCommandHelper.java?rev=1656155&r1=1656154&r2=1656155&view=diff
==============================================================================
--- continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/ShellCommandHelper.java
(original)
+++ continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/ShellCommandHelper.java
Sat Jan 31 03:33:45 2015
@@ -33,18 +33,13 @@ public interface ShellCommandHelper
 {
     String ROLE = ShellCommandHelper.class.getName();
 
-    public static interface IOConsumer
-    {
-        void consume( String line );
-    }
-
     Properties getSystemEnvVars();
 
     ExecutionResult executeShellCommand( File workingDirectory, String executable, String
arguments, File output,
                                          long idCommand, Map<String, String> environments
)
         throws Exception;
 
-    ExecutionResult executeShellCommand( File workingDirectory, String executable, String[]
arguments, IOConsumer io,
+    ExecutionResult executeShellCommand( File workingDirectory, String executable, String[]
arguments, OutputConsumer io,
                                          long idCommand, Map<String, String> environments
)
         throws Exception;
 

Modified: continuum/trunk/continuum-commons/src/main/java/org/apache/continuum/installation/DefaultInstallationService.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-commons/src/main/java/org/apache/continuum/installation/DefaultInstallationService.java?rev=1656155&r1=1656154&r2=1656155&view=diff
==============================================================================
--- continuum/trunk/continuum-commons/src/main/java/org/apache/continuum/installation/DefaultInstallationService.java
(original)
+++ continuum/trunk/continuum-commons/src/main/java/org/apache/continuum/installation/DefaultInstallationService.java
Sat Jan 31 03:33:45 2015
@@ -21,6 +21,8 @@ package org.apache.continuum.installatio
 
 import org.apache.continuum.dao.InstallationDao;
 import org.apache.continuum.utils.shell.ExecutionResult;
+import org.apache.continuum.utils.shell.ListOutputConsumer;
+import org.apache.continuum.utils.shell.OutputConsumer;
 import org.apache.continuum.utils.shell.ShellCommandHelper;
 import org.apache.maven.continuum.execution.ExecutorConfigurator;
 import org.apache.maven.continuum.installation.AlreadyExistsInstallationException;
@@ -340,15 +342,9 @@ public class DefaultInstallationService
     {
 
         String executable = homePath + File.separator + "bin" + File.separator + "java";
-        final List<String> cliOutput = new ArrayList<String>();
+        ListOutputConsumer outputConsumer = new ListOutputConsumer();
         ExecutionResult result = shellCommandHelper.executeShellCommand( null, executable,
new String[] { "-version" },
-                                                                         new ShellCommandHelper.IOConsumer()
-                                                                         {
-                                                                             public void
consume( String line )
-                                                                             {
-                                                                                 cliOutput.add(
line );
-                                                                             }
-                                                                         }, -1, null );
+                                                                         outputConsumer,
-1, null );
         int exitCode = result.getExitCode();
         if ( exitCode != 0 )
         {
@@ -356,7 +352,7 @@ public class DefaultInstallationService
                 String.format( "failed to get java version information, %s returned exit
code %s", executable,
                                exitCode ) );
         }
-        return cliOutput;
+        return outputConsumer.getList();
     }
 
     private Map<String, String> getEnvVars( Profile profile )
@@ -410,19 +406,13 @@ public class DefaultInstallationService
 
         env.putAll( getEnvVars( profile ) );
 
-        final List<String> cliOutput = new ArrayList<String>();
+        ListOutputConsumer outputConsumer = new ListOutputConsumer();
         ExecutionResult result;
         try
         {
-            result = shellCommandHelper.executeShellCommand( null, executable.toString(),
new String[] {
-                                                                 executorConfigurator.getVersionArgument()
},
-                                                             new ShellCommandHelper.IOConsumer()
-                                                             {
-                                                                 public void consume( String
line )
-                                                                 {
-                                                                     cliOutput.add( line
);
-                                                                 }
-                                                             }, -1, null );
+            result = shellCommandHelper.executeShellCommand( null, executable.toString(),
+                                                             new String[] { executorConfigurator.getVersionArgument()
},
+                                                             outputConsumer, -1, null );
         }
         catch ( Exception e )
         {
@@ -437,7 +427,7 @@ public class DefaultInstallationService
                 String.format( "failed to get executor version info, %s returned exit code
%s", executable,
                                exitCode ) );
         }
-        return cliOutput;
+        return outputConsumer.getList();
     }
 
     private boolean alreadyExistInstallationName( Installation installation )



Mime
View raw message