You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2014/03/20 23:01:05 UTC

svn commit: r1579779 - in /tomee/tomee/trunk: container/openejb-loader/src/main/java/org/apache/openejb/loader/ maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/ maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plug...

Author: rmannibucau
Date: Thu Mar 20 22:01:05 2014
New Revision: 1579779

URL: http://svn.apache.org/r1579779
Log:
handling redirection even for java 6

Modified:
    tomee/tomee/trunk/container/openejb-loader/src/main/java/org/apache/openejb/loader/Files.java
    tomee/tomee/trunk/container/openejb-loader/src/main/java/org/apache/openejb/loader/IO.java
    tomee/tomee/trunk/container/openejb-loader/src/main/java/org/apache/openejb/loader/Zips.java
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/ExecMojo.java
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java

Modified: tomee/tomee/trunk/container/openejb-loader/src/main/java/org/apache/openejb/loader/Files.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-loader/src/main/java/org/apache/openejb/loader/Files.java?rev=1579779&r1=1579778&r2=1579779&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-loader/src/main/java/org/apache/openejb/loader/Files.java (original)
+++ tomee/tomee/trunk/container/openejb-loader/src/main/java/org/apache/openejb/loader/Files.java Thu Mar 20 22:01:05 2014
@@ -81,12 +81,7 @@ public class Files {
     }
 
     public static List<File> collect(final File dir, final Pattern pattern) {
-        return collect(dir, new FileFilter() {
-            @Override
-            public boolean accept(final File file) {
-                return pattern.matcher(file.getName()).matches();
-            }
-        });
+        return collect(dir, new PatternFileFilter(pattern));
     }
 
 
@@ -207,12 +202,7 @@ public class Files {
         final ClassLoader loader = Thread.currentThread().getContextClassLoader();
         Thread.currentThread().setContextClassLoader(Files.class.getClassLoader());
         try {
-            final Thread deleteShutdownHook = new Thread() {
-                @Override
-                public void run() {
-                    delete();
-                }
-            };
+            final Thread deleteShutdownHook = new DeleteThread();
             try {
                 Runtime.getRuntime().addShutdownHook(deleteShutdownHook);
             } catch (Throwable e) {
@@ -435,7 +425,7 @@ public class Files {
         return sb.toString();
     }
 
-    private static class NoopOutputStream extends OutputStream {
+    public static class NoopOutputStream extends OutputStream {
         @Override
         public void write(final int b) throws IOException {
             // no-op
@@ -461,4 +451,24 @@ public class Files {
             super(e);
         }
     }
+
+    public static class PatternFileFilter implements FileFilter {
+        private final Pattern pattern;
+
+        public PatternFileFilter(final Pattern pattern) {
+            this.pattern = pattern;
+        }
+
+        @Override
+        public boolean accept(final File file) {
+            return pattern.matcher(file.getName()).matches();
+        }
+    }
+
+    public static class DeleteThread extends Thread {
+        @Override
+        public void run() {
+            delete();
+        }
+    }
 }

Modified: tomee/tomee/trunk/container/openejb-loader/src/main/java/org/apache/openejb/loader/IO.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-loader/src/main/java/org/apache/openejb/loader/IO.java?rev=1579779&r1=1579778&r2=1579779&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-loader/src/main/java/org/apache/openejb/loader/IO.java (original)
+++ tomee/tomee/trunk/container/openejb-loader/src/main/java/org/apache/openejb/loader/IO.java Thu Mar 20 22:01:05 2014
@@ -16,7 +16,25 @@
  */
 package org.apache.openejb.loader;
 
-import java.io.*;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.Flushable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
 import java.net.Proxy;
 import java.net.ProxySelector;
 import java.net.URI;
@@ -32,9 +50,20 @@ import java.util.zip.ZipOutputStream;
 
 /**
  * @version $Revision$ $Date$
+ *
+ * NOTE: CHECK ExecMojo before adding dependency or inner class to it please
  */
 public class IO {
-    private static final int MAX_TIMEOUT = SystemInstance.get().getOptions().get("openejb.io.util.timeout", 5000);
+    private static final int MAX_TIMEOUT;
+    static {
+        int timeout = 5000;
+        try {
+            timeout = SystemInstance.get().getOptions().get("openejb.io.util.timeout", timeout);
+        } catch (final Throwable th) {
+            // no-op: see ExecMojo
+        }
+        MAX_TIMEOUT = timeout;
+    }
 
     public static String readFileAsString(final URI uri) throws IOException {
         final StringBuilder builder = new StringBuilder("");

Modified: tomee/tomee/trunk/container/openejb-loader/src/main/java/org/apache/openejb/loader/Zips.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-loader/src/main/java/org/apache/openejb/loader/Zips.java?rev=1579779&r1=1579778&r2=1579779&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-loader/src/main/java/org/apache/openejb/loader/Zips.java (original)
+++ tomee/tomee/trunk/container/openejb-loader/src/main/java/org/apache/openejb/loader/Zips.java Thu Mar 20 22:01:05 2014
@@ -18,6 +18,7 @@ package org.apache.openejb.loader;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
@@ -37,9 +38,18 @@ public class Zips {
         Files.file(zipFile);
         Files.readable(zipFile);
 
+        final InputStream read = IO.read(zipFile);
+        try {
+            unzip(read, destination, noparent);
+        } finally {
+            IO.close(read);
+        }
+    }
+
+    public static void unzip(InputStream read, File destination, boolean noparent) throws IOException {
         try {
             // Open the ZIP file
-            final ZipInputStream in = IO.unzip(zipFile);
+            final ZipInputStream in = new ZipInputStream(read);
 
             ZipEntry entry;
 
@@ -65,8 +75,8 @@ public class Zips {
 
             in.close();
 
-        } catch (IOException e) {
-            throw new IOException("Unable to unzip " + zipFile.getAbsolutePath(), e);
+        } catch (final IOException e) {
+            throw new IOException("Unable to unzip " + read, e);
         }
     }
 }

Modified: tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/ExecMojo.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/ExecMojo.java?rev=1579779&r1=1579778&r2=1579779&view=diff
==============================================================================
--- tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/ExecMojo.java (original)
+++ tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/ExecMojo.java Thu Mar 20 22:01:05 2014
@@ -25,7 +25,13 @@ import org.apache.maven.plugin.MojoFailu
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
+import org.apache.openejb.loader.Files;
+import org.apache.openejb.loader.IO;
+import org.apache.openejb.loader.JarLocation;
+import org.apache.openejb.loader.LoaderRuntimeException;
+import org.apache.openejb.loader.Zips;
 import org.apache.openejb.maven.plugin.runner.ExecRunner;
+import org.apache.openejb.util.Pipe;
 import org.codehaus.plexus.archiver.jar.Manifest;
 import org.codehaus.plexus.util.IOUtil;
 
@@ -38,6 +44,7 @@ import java.io.StringWriter;
 import java.util.List;
 import java.util.Properties;
 
+import static java.util.Arrays.asList;
 import static org.apache.openejb.loader.Files.mkdirs;
 
 @Mojo(name = "exec", requiresDependencyResolution = ResolutionScope.RUNTIME_PLUS_SYSTEM)
@@ -89,6 +96,7 @@ public class ExecMojo extends BuildTomEE
         config.put("workingDir", runtimeWorkingDir);
         config.put("command", script);
         config.put("catalinaOpts", toString(generateJVMArgs()));
+        config.put("timestamp", Long.toString(System.currentTimeMillis()));
 
         // create an executable jar with main runner and zipFile
         final FileOutputStream fileOutputStream = new FileOutputStream(execFile);
@@ -129,11 +137,19 @@ public class ExecMojo extends BuildTomEE
             os.closeArchiveEntry();
         }
 
-        { // Main
-            final String name = ExecRunner.class.getName().replace('.', '/') + ".class";
-            os.putArchiveEntry(new JarArchiveEntry(name));
-            IOUtils.copy(getClass().getResourceAsStream('/' + name), os);
-            os.closeArchiveEntry();
+        { // Main + utility
+            for (final Class<?> clazz : asList(
+                    ExecRunner.class,
+                    Files.class, Files.PatternFileFilter.class, Files.DeleteThread.class,
+                    Files.FileRuntimeException.class, Files.FileDoesNotExistException.class, Files.NoopOutputStream.class,
+                    LoaderRuntimeException.class,
+                    Pipe.class, IO.class, Zips.class, JarLocation.class
+            )) {
+                final String name = clazz.getName().replace('.', '/') + ".class";
+                os.putArchiveEntry(new JarArchiveEntry(name));
+                IOUtils.copy(getClass().getResourceAsStream('/' + name), os);
+                os.closeArchiveEntry();
+            }
         }
 
         IOUtil.close(os);

Modified: tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java?rev=1579779&r1=1579778&r2=1579779&view=diff
==============================================================================
--- tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java (original)
+++ tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java Thu Mar 20 22:01:05 2014
@@ -16,16 +16,16 @@
  */
 package org.apache.openejb.maven.plugin.runner;
 
+import org.apache.openejb.loader.IO;
+import org.apache.openejb.loader.Zips;
+import org.apache.openejb.util.Pipe;
+
 import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Locale;
 import java.util.Properties;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
 
 import static java.util.Arrays.asList;
 
@@ -48,7 +48,11 @@ public class ExecRunner {
         final String workingDir = config.getProperty("workingDir");
         final InputStream distribIs = contextClassLoader.getResourceAsStream(distrib);
         File distribOutput = new File(workingDir);
-        extract(distribIs, distribOutput);
+        final File timestampFile = new File(distribOutput, "timestamp.txt");
+        if (!timestampFile.exists() || Long.parseLong(IO.slurp(timestampFile)) < Long.parseLong(config.getProperty("timestamp"))) {
+            Zips.unzip(distribIs, distribOutput, false);
+            IO.writeString(timestampFile, config.getProperty("timestamp"));
+        }
 
         final File[] extracted = distribOutput.listFiles();
         if (extracted != null && extracted.length == 1) {
@@ -86,65 +90,24 @@ public class ExecRunner {
             builder.environment().put("CATALINA_OPTS", catalinaOpts);
         }
 
-        builder.redirectError(ProcessBuilder.Redirect.INHERIT);
-        builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
+        boolean redirectOut = false;
+        try { // java >= 7
+            ProcessBuilder.class.getDeclaredMethod("inheritIO").invoke(builder);
+        } catch (final Throwable th){ // java 6
+            redirectOut = true;
+        }
 
         final Process process = builder.start();
+        if (redirectOut) {
+            Pipe.pipe(process);
+        }
+
         process.waitFor();
         System.out.flush();
         System.err.flush();
         System.out.println("Exit status: " + process.exitValue());
     }
 
-    // duplicated to avoid deps, if this class has any dep then it can't be run
-    private static void extract(final InputStream distrib, final File output) throws IOException {
-        mkdirs(output);
-
-        final ZipInputStream in = new ZipInputStream(distrib);
-
-        ZipEntry entry;
-        while ((entry = in.getNextEntry()) != null) {
-            final String path = entry.getName();
-            final File file = new File(output, path);
-
-            if (entry.isDirectory()) {
-                mkdirs(file);
-                continue;
-            }
-
-            mkdirs(file.getParentFile());
-            copy(in, file);
-
-            final long lastModified = entry.getTime();
-            if (lastModified > 0) {
-                file.setLastModified(lastModified);
-            }
-
-        }
-
-        in.close();
-    }
-
-    private static void copy(final ZipInputStream in, final File file) throws IOException {
-        final FileOutputStream to = new FileOutputStream(file);
-        try {
-            final byte[] buffer = new byte[1024];
-            int length;
-            while ((length = in.read(buffer)) != -1) {
-                to.write(buffer, 0, length);
-            }
-            to.flush();
-        } finally {
-            to.close();
-        }
-    }
-
-    private static void mkdirs(File output) {
-        if (!output.exists() && !output.mkdirs()) {
-            throw new IllegalArgumentException("Can't create " + output.getAbsolutePath());
-        }
-    }
-
     private ExecRunner() {
         // no-op
     }