You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2013/04/25 00:06:22 UTC

svn commit: r1471724 - in /commons/sandbox/weaver/trunk: ant/lib/src/main/java/org/apache/commons/weaver/ant/ maven-plugin/src/main/java/org/apache/commons/weaver/maven/ processor/src/main/java/org/apache/commons/weaver/ processor/src/test/java/org/apa...

Author: mbenson
Date: Wed Apr 24 22:06:06 2013
New Revision: 1471724

URL: http://svn.apache.org/r1471724
Log:
no singleton for Weaver; the stateful nature of the class's properties left us vulnerable to synchronization issues, so rather just create a new instance with its config in place; also add some debugging and null guarding to the maven plugin's AbstractWeaveMojo

Modified:
    commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/WeaveTask.java
    commons/sandbox/weaver/trunk/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractWeaveMojo.java
    commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java
    commons/sandbox/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaveProcessorTest.java

Modified: commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/WeaveTask.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/WeaveTask.java?rev=1471724&r1=1471723&r2=1471724&view=diff
==============================================================================
--- commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/WeaveTask.java (original)
+++ commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/WeaveTask.java Wed Apr 24 22:06:06 2013
@@ -23,9 +23,7 @@ import java.util.Properties;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.weaver.WeaveProcessor;
-
 import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.DynamicElement;
 import org.apache.tools.ant.Task;
 import org.apache.tools.ant.types.EnumeratedAttribute;
 import org.apache.tools.ant.types.Path;
@@ -37,35 +35,6 @@ import org.apache.tools.ant.types.Refere
  * Weave Ant task.
  */
 public class WeaveTask extends Task {
-    /**
-     * Structure to allow the inline specification of weaver properties.
-     */
-    public class InlineProperties implements DynamicElement {
-        /**
-         * Represents a single inline property.
-         */
-        public class InlineProperty {
-            private final String name;
-
-            private InlineProperty(String name) {
-                this.name = name;
-            }
-
-            public void addText(String text) {
-                if (properties.containsKey(name)) {
-                    text = StringUtils.join(properties.getProperty(name), text);
-                }
-                properties.setProperty(name, text);
-            }
-        }
-
-        private final Properties properties = new Properties();
-
-        public InlineProperty createDynamicElement(String name) {
-            return new InlineProperty(name);
-        }
-    } 
-
     private File target;
     private Path classpath;
     private String classpathref;
@@ -75,8 +44,7 @@ public class WeaveTask extends Task {
     @Override
     public void execute() throws BuildException {
         try {
-            WeaveProcessor wp = WeaveProcessor.getInstance();
-            wp.configure(getClassPathEntries(), target, getProperties());
+            final WeaveProcessor wp = new WeaveProcessor(getClassPathEntries(), target, getProperties());
             wp.weave();
         } catch (Exception e) {
             throw new BuildException(e);

Modified: commons/sandbox/weaver/trunk/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractWeaveMojo.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractWeaveMojo.java?rev=1471724&r1=1471723&r2=1471724&view=diff
==============================================================================
--- commons/sandbox/weaver/trunk/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractWeaveMojo.java (original)
+++ commons/sandbox/weaver/trunk/maven-plugin/src/main/java/org/apache/commons/weaver/maven/AbstractWeaveMojo.java Wed Apr 24 22:06:06 2013
@@ -22,7 +22,6 @@ import java.util.Properties;
 import org.apache.commons.weaver.WeaveProcessor;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.plugins.annotations.Parameter;
 
 /**
@@ -42,22 +41,23 @@ public abstract class AbstractWeaveMojo 
 
     @Override
     public void execute() throws MojoExecutionException {
-        Log mojoLog = getLog();
-        JavaLoggingToMojoLoggingRedirector logRedirector = new JavaLoggingToMojoLoggingRedirector(mojoLog);
+        final JavaLoggingToMojoLoggingRedirector logRedirector = new JavaLoggingToMojoLoggingRedirector(getLog());
         logRedirector.activate();
 
+        final List<String> classpath = getClasspath();
+        final File target = getTarget();
+        final Properties config = weaverConfig == null ? new Properties() : weaverConfig;
+
+        getLog().debug(String.format("classpath=%s%ntarget=%s%nconfig=%s", classpath, target, config));
+
         try {
-            WeaveProcessor wp = WeaveProcessor.getInstance();
-            configure(wp);
+            final WeaveProcessor wp = new WeaveProcessor(classpath, target, config);
             wp.weave();
         } catch (Exception e) {
-            throw new MojoExecutionException("weaving failed", e);
+            throw new MojoExecutionException("weaving failed due to " + e.getMessage(), e);
         } finally {
             logRedirector.deactivate();
         }
     }
 
-    protected void configure(WeaveProcessor wp) {
-        wp.configure(getClasspath(), getTarget(), weaverConfig);
-    }
 }

Modified: commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java?rev=1471724&r1=1471723&r2=1471724&view=diff
==============================================================================
--- commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java (original)
+++ commons/sandbox/weaver/trunk/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java Wed Apr 24 22:06:06 2013
@@ -24,11 +24,12 @@ import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.net.URLClassLoader;
 import java.util.ArrayList;
-import java.util.Iterator;
+import java.util.Collections;
 import java.util.List;
 import java.util.Properties;
 import java.util.ServiceLoader;
 
+import org.apache.commons.lang3.Validate;
 import org.apache.commons.weaver.model.ScanResult;
 import org.apache.commons.weaver.model.WeaveInterest;
 import org.apache.commons.weaver.spi.Weaver;
@@ -38,124 +39,112 @@ import org.apache.xbean.finder.Parameter
 import org.apache.xbean.finder.archive.FileArchive;
 
 /**
- * This class picks up all Weaver plugins and process them in one go.
+ * This class discovers and invokes available {@link Weaver} plugins.
  */
 public class WeaveProcessor {
 
-    private static WeaveProcessor instance;
-
-    /**
-     * The classpath which will be used to look up cross references during
-     * weaving.
-     */
-    private List<String> classPath;
-
-    /**
-     * The actual path which gets weaved. All the classes in this path will get
-     * weaved. The weaved classes will replace the original classes.
-     */
-    private File target;
-
     /** List of picked up weaver plugins */
-    private List<Weaver> weavers = new ArrayList<Weaver>();
+    private static List<Weaver> WEAVERS = new ArrayList<Weaver>();
 
-    public static synchronized WeaveProcessor getInstance() {
-        if (instance == null) {
-            instance = new WeaveProcessor();
-            instance.init();
+    static {
+        List<Weaver> weavers = new ArrayList<Weaver>();
+        for (Weaver w : ServiceLoader.load(Weaver.class)) {
+            weavers.add(w);
         }
-        return instance;
+        WEAVERS = Collections.unmodifiableList(weavers);
     }
 
-    private WeaveProcessor() {
-    }
+    /**
+     * The classpath which will be used to look up cross references during weaving.
+     */
+    private final List<String> classpath;
 
-    private void init() {
-        ServiceLoader<Weaver> loader = ServiceLoader.load(Weaver.class);
-        Iterator<Weaver> it = loader.iterator();
+    /**
+     * The actual path to be woven, replacing any affected classes.
+     */
+    private final File target;
 
-        while (it.hasNext()) {
-            weavers.add(it.next());
-        }
-    }
+    /**
+     * Properties for configuring discovered plugin modules.
+     */
+    private final Properties configuration;
 
     /**
-     * Configure all Weavers.
+     * Create a new {@link WeaveProcessor} instance.
      * 
-     * @param classPath
-     *            the classpath to look up cross-references in during weaving
-     * @param target
-     *            the File path where the classes to weave reside
-     * @param config
-     *            additional configuration for all plugins.
+     * @param classpath not {@code null}
+     * @param target not {@code null}
+     * @param configuration not {@code null}
      */
-    public void configure(List<String> classPath, File target, Properties config) {
-        this.classPath = classPath;
-        this.target = target;
-
-        for (Weaver weaver : weavers) {
-            weaver.configure(classPath, target, config);
-        }
+    public WeaveProcessor(List<String> classpath, File target, Properties configuration) {
+        super();
+        this.classpath = Validate.notNull(classpath, "classpath");
+        this.target = Validate.notNull(target, "target");
+        this.configuration = Validate.notNull(configuration, "configuration");
     }
 
     /**
-     * perform the weaving on all specified classpath entries
+     * Weave classes in target directory.
      */
     public void weave() {
-        final ClassLoader classLoader = new URLClassLoader(URLArray.fromPaths(classPath));
+        final ClassLoader classLoader = new URLClassLoader(URLArray.fromPaths(classpath));
         final Finder finder = new Finder(new FileArchive(classLoader, target));
-        for (Weaver weaver : weavers) {
+        for (Weaver weaver : WEAVERS) {
             weave(finder, weaver);
         }
     }
 
-    private void weave(Finder finder, Weaver weaver) {
+    private void weave(final Finder finder, final Weaver weaver) {
+        weaver.configure(classpath, target, configuration);
         final ScanResult result = new ScanResult();
 
         for (WeaveInterest interest : weaver.getScanRequest().getInterests()) {
             switch (interest.target) {
-            case PACKAGE:
-                for (Annotated<Package> pkg : finder.withAnnotations().findAnnotatedPackages(interest.annotationType)) {
-                    result.getWeavable(pkg.get()).addAnnotations(pkg.getAnnotation(interest.annotationType));
-                }
-            case TYPE:
-                for (Annotated<Class<?>> type : finder.withAnnotations().findAnnotatedClasses(interest.annotationType)) {
-                    result.getWeavable(type.get()).addAnnotations(type.getAnnotation(interest.annotationType));
-                }
-                break;
-            case METHOD:
-                for (Annotated<Method> method : finder.withAnnotations().findAnnotatedMethods(interest.annotationType)) {
-                    result.getWeavable(method.get()).addAnnotations(method.getAnnotation(interest.annotationType));
-                }
-                break;
-            case CONSTRUCTOR:
-                for (Annotated<Constructor<?>> cs : finder.withAnnotations().findAnnotatedConstructors(
-                    interest.annotationType)) {
-                    result.getWeavable(cs.get()).addAnnotations(cs.getAnnotation(interest.annotationType));
-                }
-                break;
-            case FIELD:
-                for (Annotated<Field> fld : finder.withAnnotations().findAnnotatedFields(interest.annotationType)) {
-                    result.getWeavable(fld.get()).addAnnotations(fld.getAnnotation(interest.annotationType));
-                }
-                break;
-            case PARAMETER:
-                for (Annotated<Parameter<Method>> parameter : finder.withAnnotations().findAnnotatedMethodParameters(
-                    interest.annotationType)) {
-                    result.getWeavable(parameter.get().getDeclaringExecutable())
-                        .getWeavableParameter(parameter.get().getIndex())
-                        .addAnnotations(parameter.getAnnotation(interest.annotationType));
-                }
-                for (Annotated<Parameter<Constructor<?>>> parameter : finder.withAnnotations()
-                    .findAnnotatedConstructorParameters(interest.annotationType)) {
-                    result.getWeavable(parameter.get().getDeclaringExecutable())
-                        .getWeavableParameter(parameter.get().getIndex())
-                        .addAnnotations(parameter.getAnnotation(interest.annotationType));
-                }
-                break;
-            default:
-                // should we log something?
-                break;
+                case PACKAGE:
+                    for (Annotated<Package> pkg : finder.withAnnotations().findAnnotatedPackages(
+                        interest.annotationType)) {
+                        result.getWeavable(pkg.get()).addAnnotations(pkg.getAnnotation(interest.annotationType));
+                    }
+                case TYPE:
+                    for (Annotated<Class<?>> type : finder.withAnnotations().findAnnotatedClasses(
+                        interest.annotationType)) {
+                        result.getWeavable(type.get()).addAnnotations(type.getAnnotation(interest.annotationType));
+                    }
+                    break;
+                case METHOD:
+                    for (Annotated<Method> method : finder.withAnnotations().findAnnotatedMethods(
+                        interest.annotationType)) {
+                        result.getWeavable(method.get()).addAnnotations(method.getAnnotation(interest.annotationType));
+                    }
+                    break;
+                case CONSTRUCTOR:
+                    for (Annotated<Constructor<?>> cs : finder.withAnnotations().findAnnotatedConstructors(
+                        interest.annotationType)) {
+                        result.getWeavable(cs.get()).addAnnotations(cs.getAnnotation(interest.annotationType));
+                    }
+                    break;
+                case FIELD:
+                    for (Annotated<Field> fld : finder.withAnnotations().findAnnotatedFields(interest.annotationType)) {
+                        result.getWeavable(fld.get()).addAnnotations(fld.getAnnotation(interest.annotationType));
+                    }
+                    break;
+                case PARAMETER:
+                    for (Annotated<Parameter<Method>> parameter : finder.withAnnotations()
+                        .findAnnotatedMethodParameters(interest.annotationType)) {
+                        result.getWeavable(parameter.get().getDeclaringExecutable())
+                            .getWeavableParameter(parameter.get().getIndex())
+                            .addAnnotations(parameter.getAnnotation(interest.annotationType));
+                    }
+                    for (Annotated<Parameter<Constructor<?>>> parameter : finder.withAnnotations()
+                        .findAnnotatedConstructorParameters(interest.annotationType)) {
+                        result.getWeavable(parameter.get().getDeclaringExecutable())
+                            .getWeavableParameter(parameter.get().getIndex())
+                            .addAnnotations(parameter.getAnnotation(interest.annotationType));
+                    }
+                    break;
+                default:
+                    // should we log something?
+                    break;
             }
         }
         weaver.process(result);

Modified: commons/sandbox/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaveProcessorTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaveProcessorTest.java?rev=1471724&r1=1471723&r2=1471724&view=diff
==============================================================================
--- commons/sandbox/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaveProcessorTest.java (original)
+++ commons/sandbox/weaver/trunk/processor/src/test/java/org/apache/commons/weaver/test/WeaveProcessorTest.java Wed Apr 24 22:06:06 2013
@@ -38,12 +38,10 @@ public class WeaveProcessorTest extends 
         addClassForScanning(TestBeanWithMethodAnnotation.class);
         addClassForScanning(TestBeanWithClassAnnotation.class);
 
-        WeaveProcessor wp = WeaveProcessor.getInstance();
-
-        Properties config = new Properties();
+        final Properties config = new Properties();
         config.put("configKey", "configValue");
 
-        wp.configure(getClassPathEntries(), getTargetFolder(), config);
+        WeaveProcessor wp = new WeaveProcessor(getClassPathEntries(), getTargetFolder(), config);
 
         TestWeaver.wovenClasses.clear();
         TestWeaver.wovenMethods.clear();