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/10/20 14:02:02 UTC

git commit: TOMEE-1420 allow to run java code to customize an instance

Repository: tomee
Updated Branches:
  refs/heads/develop 982f16762 -> 96adff22e


TOMEE-1420 allow to run java code to customize an instance


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/96adff22
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/96adff22
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/96adff22

Branch: refs/heads/develop
Commit: 96adff22e42bf24f06c5469be848fa61eda3ec9c
Parents: 982f167
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Mon Oct 20 14:01:41 2014 +0200
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Mon Oct 20 14:01:41 2014 +0200

----------------------------------------------------------------------
 .../openejb/maven/plugin/AbstractTomEEMojo.java | 78 +++++++++++++++++++-
 .../maven/plugin/UpdatableTomEEMojo.java        |  5 +-
 .../maven/plugin/TomEEMavenPluginRule.java      |  9 +++
 3 files changed, 87 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/96adff22/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
----------------------------------------------------------------------
diff --git a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
index d660e0f..8846c73 100644
--- a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
+++ b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
@@ -29,6 +29,7 @@ import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.project.MavenProject;
 import org.apache.maven.settings.Settings;
 import org.apache.openejb.config.RemoteServer;
 import org.apache.openejb.loader.Files;
@@ -40,6 +41,7 @@ import org.apache.tomee.util.QuickServerXmlParser;
 
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
+import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -48,8 +50,13 @@ import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -203,7 +210,13 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
     protected Map<String, String> systemVariables;
 
     @Parameter
-    private List<String> classpaths;
+    protected List<String> classpaths;
+
+    @Parameter
+    protected List<String> customizers;
+
+    @Parameter(defaultValue = "${project}", readonly = true, required = true)
+    protected MavenProject project;
 
     /**
      * forcing nice default for war development (WEB-INF/classes and web resources)
@@ -247,6 +260,9 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
     @Parameter
     protected List<String> apps;
 
+    @Parameter(property = "tomee-plugin.classes", defaultValue = "${project.build.outputDirectory}", readonly = true)
+    protected File classes;
+
     @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.${project.packaging}")
     protected File warFile;
 
@@ -354,11 +370,71 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
             if (!skipCurrentProject) {
                 copyWar();
             }
+
+            if (customizers != null) {
+                final Thread thread = Thread.currentThread();
+                final ClassLoader currentLoader = thread.getContextClassLoader();
+                final ClassLoader tccl = createClassLoader(currentLoader);
+                thread.setContextClassLoader(tccl);
+                try {
+                    // a customizer is a Runnable with or without a constructor taking a File as parameter (catalina base)
+                    // one goal is to avoid coupling as much as possible with this plugin
+                    //
+                    // if really needed we could introduce a Customizer interface but then it has more impacts on your packaging/config
+                    for (final String customizer : customizers) {
+                        try {
+                            final Class<?> clazz = tccl.loadClass(customizer);
+                            try {
+                                final Constructor<?> cons = clazz.getConstructor(File.class);
+                                Runnable.class.cast(cons.newInstance(catalinaBase)).run();
+                            } catch (final NoSuchMethodException e) {
+                                try {
+                                    Runnable.class.cast(clazz.newInstance()).run();
+                                } catch (final Exception e1) {
+                                    throw new MojoExecutionException("can't create customizer: " + currentLoader, e);
+                                }
+                            } catch (final InvocationTargetException | InstantiationException | IllegalAccessException e) {
+                                throw new MojoExecutionException("can't create customizer: " + currentLoader, e);
+                            }
+                        } catch (final ClassNotFoundException e) {
+                            throw new MojoExecutionException("can't find customizer: " + currentLoader, e);
+                        }
+                    }
+                } finally {
+                    try {
+                        if (tccl != null && Closeable.class.isInstance(tccl)) {
+                            Closeable.class.cast(tccl).close();
+                        }
+                    } catch (final IOException e) {
+                        // no-op
+                    }
+                    thread.setContextClassLoader(currentLoader);
+                }
+            }
         }
 
         run();
     }
 
+    private ClassLoader createClassLoader(final ClassLoader parent) {
+        final List<URL> urls = new ArrayList<>();
+        for (final Artifact artifact : (Collection<Artifact>) project.getArtifacts()) {
+            try {
+                urls.add(artifact.getFile().toURI().toURL());
+            } catch (final MalformedURLException e) {
+                getLog().warn("can't use artifact " + artifact.toString());
+            }
+        }
+        if (classes != null && classes.exists()) {
+            try {
+                urls.add(classes.toURI().toURL());
+            } catch (final MalformedURLException e) {
+                getLog().warn("can't use path " + classes.getAbsolutePath());
+            }
+        }
+        return new URLClassLoader(urls.toArray(new URL[urls.size()]), parent);
+    }
+
     protected void fixConfig() {
         if (useOpenEJB) {
             tomeeGroupId = "org.apache.openejb";

http://git-wip-us.apache.org/repos/asf/tomee/blob/96adff22/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/UpdatableTomEEMojo.java
----------------------------------------------------------------------
diff --git a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/UpdatableTomEEMojo.java b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/UpdatableTomEEMojo.java
index 9cf880d..6d751d5 100644
--- a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/UpdatableTomEEMojo.java
+++ b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/UpdatableTomEEMojo.java
@@ -54,9 +54,6 @@ public abstract class UpdatableTomEEMojo extends AbstractTomEEMojo {
     @Parameter
     private List<Synch> synchronizations;
 
-    @Parameter(property = "tomee-plugin.buildDir", defaultValue = "${project.build.directory}", readonly = true)
-    private File buildDir;
-
     @Parameter(property = "tomee-plugin.baseDir", defaultValue = "${project.basedir}", readonly = true)
     private File baseDir;
 
@@ -114,7 +111,7 @@ public abstract class UpdatableTomEEMojo extends AbstractTomEEMojo {
         // defaults values for main synchronization block
         final String destination = destinationName().replaceAll("\\.[jew]ar", "");
         if (synchronization.getBinariesDir() == null) {
-            synchronization.setBinariesDir(new File(buildDir, "classes"));
+            synchronization.setBinariesDir(classes);
         }
         if (synchronization.getResourcesDir() == null) {
             synchronization.setResourcesDir(new File(baseDir, "src/main/webapp"));

http://git-wip-us.apache.org/repos/asf/tomee/blob/96adff22/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/TomEEMavenPluginRule.java
----------------------------------------------------------------------
diff --git a/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/TomEEMavenPluginRule.java b/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/TomEEMavenPluginRule.java
index 4eefb9f..17e589e 100644
--- a/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/TomEEMavenPluginRule.java
+++ b/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/TomEEMavenPluginRule.java
@@ -22,6 +22,7 @@ import org.apache.maven.artifact.repository.DefaultArtifactRepository;
 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 import org.apache.maven.artifact.versioning.VersionRange;
+import org.apache.maven.project.MavenProject;
 import org.apache.maven.settings.Settings;
 import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
 import org.apache.openejb.config.RemoteServer;
@@ -37,8 +38,10 @@ import java.lang.reflect.Field;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
+import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
 
 /**
@@ -154,6 +157,12 @@ public class TomEEMavenPluginRule implements MethodRule {
                 // no-op
             }
         }
+        tomEEMojo.project = new MavenProject() {
+            @Override
+            public Set getArtifacts() {
+                return Collections.emptySet();
+            }
+        };
         if (tomEEMojo.settings == null) {
             tomEEMojo.settings = new Settings();
         }