Author: struberg
Date: Thu Aug 30 20:08:05 2012
New Revision: 1379114

URL: http://svn.apache.org/viewvc?rev=1379114&view=rev
Log:
MSHARED-236 import 2 small ALv2 licensed helper classes written by krosenvold

Added:
    
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/AbstractStreamHandler.java
   (with props)
    
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineCallable.java
   (with props)
Modified:
    
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java

Added: 
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/AbstractStreamHandler.java
URL: 
http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/AbstractStreamHandler.java?rev=1379114&view=auto
==============================================================================
--- 
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/AbstractStreamHandler.java
 (added)
+++ 
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/AbstractStreamHandler.java
 Thu Aug 30 20:08:05 2012
@@ -0,0 +1,57 @@
+package org.apache.maven.shared.utils.cli;
+
+/*
+ * Licensed 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.
+ */
+
+/**
+ * @author <a href="mailto:[email protected]";>Kristian Rosenvold</a>
+ */
+public class AbstractStreamHandler
+    extends Thread
+{
+    private boolean done;
+
+    private volatile boolean disabled;
+
+    public boolean isDone()
+    {
+        return done;
+    }
+
+    public synchronized void waitUntilDone()
+        throws InterruptedException
+    {
+        while ( !isDone() )
+        {
+            wait();
+        }
+    }
+
+
+    protected boolean isDisabled()
+    {
+        return disabled;
+    }
+
+    public void disable()
+    {
+        disabled = true;
+    }
+
+    public void setDone()
+    {
+        done = true;
+    }
+
+}

Propchange: 
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/AbstractStreamHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineCallable.java
URL: 
http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineCallable.java?rev=1379114&view=auto
==============================================================================
--- 
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineCallable.java
 (added)
+++ 
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineCallable.java
 Thu Aug 30 20:08:05 2012
@@ -0,0 +1,26 @@
+package org.apache.maven.shared.utils.cli;
+
+/*
+ * Licensed 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.concurrent.Callable;
+
+/**
+ * Callable wrapper that exposes the proper exeception type to the client.
+ * @author Kristian Rosenvold
+ */
+public interface CommandLineCallable extends Callable<Integer>
+{
+    public Integer call() throws CommandLineException;
+}

Propchange: 
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineCallable.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java
URL: 
http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java?rev=1379114&r1=1379113&r2=1379114&view=diff
==============================================================================
--- 
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java
 (original)
+++ 
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java
 Thu Aug 30 20:08:05 2012
@@ -35,6 +35,7 @@ import java.io.InputStreamReader;
 import java.io.Reader;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.security.AccessControlException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
@@ -174,76 +175,97 @@ public abstract class CommandLineUtils
 
         final ProcessHook processHook = new ProcessHook( p );
 
-        ShutdownHookUtils.addShutDownHook( processHook );
+        try
+        {
+            Runtime.getRuntime().addShutdownHook( processHook );
+        }
+        catch ( IllegalStateException ignore )
+        {
+        }
+        catch ( AccessControlException ignore )
+        {
+        }
 
         return new CommandLineCallable()
         {
             public Integer call()
                 throws CommandLineException
             {
-        try
-        {
+                try
+                {
                     int returnValue;
                     if ( timeoutInSeconds <= 0 )
-            {
+                    {
                         returnValue = p.waitFor();
                     }
                     else
-                {
+                    {
                         long now = System.currentTimeMillis();
                         long timeoutInMillis = 1000L * timeoutInSeconds;
                         long finish = now + timeoutInMillis;
                         while ( isAlive( p ) && ( System.currentTimeMillis() < 
finish ) )
-                    {
+                        {
                             Thread.sleep( 10 );
-                    }
+                        }
                         if ( isAlive( p ) )
                         {
                             throw new InterruptedException( "Process timeout 
out after " + timeoutInSeconds + " seconds" );
-                }
+                        }
+
                         returnValue = p.exitValue();
-            }
+                    }
 
                     waitForAllPumpers( inputFeeder, outputPumper, errorPumper 
);
 
                     if ( outputPumper.getException() != null )
-            {
-                throw new CommandLineException( "Error inside systemOut 
parser", outputPumper.getException() );
-            }
+                    {
+                        throw new CommandLineException( "Error inside 
systemOut parser", outputPumper.getException() );
+                    }
 
                     if ( errorPumper.getException() != null )
-            {
+                    {
                         throw new CommandLineException( "Error inside 
systemErr parser", errorPumper.getException() );
-            }
+                    }
 
-            return returnValue;
-        }
-        catch ( InterruptedException ex )
-        {
+                    return returnValue;
+                }
+                catch ( InterruptedException ex )
+                {
                     if ( inputFeeder != null )
                     {
                         inputFeeder.disable();
-        }
+                    }
+
                     outputPumper.disable();
                     errorPumper.disable();
                     throw new CommandLineTimeOutException( "Error while 
executing external command, process killed.", ex );
                 }
-        finally
-        {
-                    ShutdownHookUtils.removeShutdownHook( processHook );
+                finally
+                {
+                    try
+                    {
+                        Runtime.getRuntime().removeShutdownHook( processHook );
+                    }
+                    catch ( IllegalStateException ignore )
+                    {
+                        //
+                    }
+                    catch ( AccessControlException ignore )
+                    {
+                    }
 
                     processHook.run();
 
-            if ( inputFeeder != null )
-            {
-                inputFeeder.close();
-            }
+                    if ( inputFeeder != null )
+                    {
+                        inputFeeder.close();
+                    }
 
-            outputPumper.close();
+                    outputPumper.close();
 
-            errorPumper.close();
-        }
-    }
+                    errorPumper.close();
+                }
+            }
         };
     }
 
@@ -318,68 +340,68 @@ public abstract class CommandLineUtils
 
         try
         {
-        Properties envVars = new Properties();
+            Properties envVars = new Properties();
 
-        Runtime r = Runtime.getRuntime();
+            Runtime r = Runtime.getRuntime();
 
-         //If this is windows set the shell to command.com or cmd.exe with 
correct arguments.
+             //If this is windows set the shell to command.com or cmd.exe with 
correct arguments.
             boolean overriddenEncoding = false;
             if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
-        {
-                if ( Os.isFamily( Os.FAMILY_WIN9X ) )
-            {
-                p = r.exec( "command.com /c set" );
-            }
-            else
             {
+                if ( Os.isFamily( Os.FAMILY_WIN9X ) )
+                {
+                    p = r.exec( "command.com /c set" );
+                }
+                else
+                {
                     overriddenEncoding = true;
                     // /U = change stdout encoding to UTF-16LE to avoid 
encoding inconsistency
                     // between command-line/DOS and GUI/Windows, see 
PLXUTILS-124
                     p = r.exec( "cmd.exe /U /c set" );
+                }
+            }
+            else
+            {
+                p = r.exec( "env" );
             }
-        }
-        else
-        {
-            p = r.exec( "env" );
-        }
 
             Reader reader = overriddenEncoding
                 ? new InputStreamReader( p.getInputStream(), UTF_16LE )
                 : new InputStreamReader( p.getInputStream() );
             BufferedReader br = new BufferedReader( reader );
 
-        String line;
-
-        String lastKey = null;
-        String lastVal = null;
+            String line;
 
-        while ( ( line = br.readLine() ) != null )
-        {
-            int idx = line.indexOf( '=' );
+            String lastKey = null;
+            String lastVal = null;
 
-                if ( idx > 0 )
+            while ( ( line = br.readLine() ) != null )
             {
-                lastKey = line.substring( 0, idx );
+                int idx = line.indexOf( '=' );
 
-                if ( !caseSensitive )
+                if ( idx > 0 )
                 {
+                    lastKey = line.substring( 0, idx );
+
+                    if ( !caseSensitive )
+                    {
                         lastKey = lastKey.toUpperCase( Locale.ENGLISH );
-                }
+                    }
 
-                lastVal = line.substring( idx + 1 );
+                    lastVal = line.substring( idx + 1 );
 
-                envVars.setProperty( lastKey, lastVal );
-            }
-            else if ( lastKey != null )
-            {
-                lastVal += "\n" + line;
+                    envVars.setProperty( lastKey, lastVal );
+                }
+                else if ( lastKey != null )
+                {
+                    lastVal += "\n" + line;
 
-                envVars.setProperty( lastKey, lastVal );
+                    envVars.setProperty( lastKey, lastVal );
+                }
             }
-        }
 
-        return envVars;
-    }
+            return envVars;
+        }
         finally
         {
             if ( p != null )
@@ -548,16 +570,16 @@ public abstract class CommandLineUtils
             {
                 throw new CommandLineException( "Can't handle single and 
double quotes in same argument" );
             }
-        else
-        {
+            else
+            {
                 if ( escapeSingleQuotes )
                 {
                     return "\\\'" + argument + "\\\'";
-        }
+                }
                 else if ( wrapExistingQuotes )
                 {
                     return '\'' + argument + '\'';
-    }
+                }
             }
         }
         else if ( argument.contains( "\'" ) )
@@ -592,7 +614,7 @@ public abstract class CommandLineUtils
         if ( ( line == null ) || ( line.length == 0 ) )
         {
             return "";
-    }
+        }
 
         // path containing one or more elements
         final StringBuilder result = new StringBuilder();


Reply via email to