commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1516279 - in /commons/sandbox/weaver/trunk/ant: lib/src/main/java/org/apache/commons/weaver/ant/ lib/src/main/resources/org/apache/commons/weaver/ant/ test/
Date Wed, 21 Aug 2013 19:58:21 GMT
Author: mbenson
Date: Wed Aug 21 19:58:21 2013
New Revision: 1516279

URL: http://svn.apache.org/r1516279
Log:
refactor antlib to extract weaver settings into a standalone data type, eliminate duplication,
and simplify configuration at the task level

Added:
    commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/AbstractWeaverTask.java
  (with props)
    commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/WeaverSettings.java
  (with props)
Modified:
    commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/CleanTask.java
    commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/InlineProperties.java
    commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/WeaveTask.java
    commons/sandbox/weaver/trunk/ant/lib/src/main/resources/org/apache/commons/weaver/ant/antlib.xml
    commons/sandbox/weaver/trunk/ant/test/pom.xml

Added: commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/AbstractWeaverTask.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/AbstractWeaverTask.java?rev=1516279&view=auto
==============================================================================
--- commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/AbstractWeaverTask.java
(added)
+++ commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/AbstractWeaverTask.java
Wed Aug 21 19:58:21 2013
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+package org.apache.commons.weaver.ant;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.Task;
+import org.apache.tools.ant.types.Reference;
+
+/**
+ * Abstract weaver Ant task. Manages settings for filesystem-based weaving.
+ */
+public abstract class AbstractWeaverTask extends Task {
+    private WeaverSettings settings;
+
+    protected AbstractWeaverTask(Project project) {
+        super();
+        setProject(project);
+    }
+
+    public void add(WeaverSettings settings) {
+        if (this.settings != null) {
+            throw new BuildException("settings already specified");
+        }
+        this.settings = settings;
+    }
+
+    public WeaverSettings getSettings() {
+        return settings;
+    }
+
+    public void setSettingsRef(String refid) {
+        final WeaverSettings settings = new WeaverSettings(getProject());
+        settings.setRefid(new Reference(getProject(), refid));
+        add(settings);
+    }
+
+}
\ No newline at end of file

Propchange: commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/AbstractWeaverTask.java
------------------------------------------------------------------------------
    svn:executable = *

Modified: commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/CleanTask.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/CleanTask.java?rev=1516279&r1=1516278&r2=1516279&view=diff
==============================================================================
--- commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/CleanTask.java
(original)
+++ commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/CleanTask.java
Wed Aug 21 19:58:21 2013
@@ -18,122 +18,28 @@
  */
 package org.apache.commons.weaver.ant;
 
-import java.io.File;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-
-import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.Validate;
 import org.apache.commons.weaver.CleanProcessor;
 import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.Task;
-import org.apache.tools.ant.types.EnumeratedAttribute;
-import org.apache.tools.ant.types.Path;
-import org.apache.tools.ant.types.PropertySet;
-import org.apache.tools.ant.types.PropertySet.BuiltinPropertySetName;
-import org.apache.tools.ant.types.Reference;
+import org.apache.tools.ant.Project;
 
 /**
  * Clean Ant task.
  */
-public class CleanTask extends Task {
-    private File target;
-    private Path classpath;
-    private String classpathref;
-    private PropertySet propertySet;
-    private InlineProperties inlineProperties;
+public class CleanTask extends AbstractWeaverTask {
+    public CleanTask(Project project) {
+        super(project);
+    }
 
     @Override
     public void execute() throws BuildException {
         try {
-            final CleanProcessor cp = new CleanProcessor(getClassPathEntries(), target, getProperties());
+            final WeaverSettings settings = Validate.notNull(getSettings(), "settings");
+            final CleanProcessor cp =
+                new CleanProcessor(settings.getClasspathEntries(), settings.getTarget(),
settings.getProperties());
             cp.clean();
         } catch (Exception e) {
             throw new BuildException(e);
         }
     }
-
-    protected File getTarget() {
-        return target;
-    }
-
-    public void setTarget(File target) {
-        this.target = target;
-    }
-
-    protected String getClasspathref() {
-        return classpathref;
-    }
-
-    public void setClasspathRef(String classpathref) {
-        this.classpathref = classpathref;
-    }
-
-    protected List<String> getClassPathEntries() {
-        final Path p = new Path(getProject());
-        final Path cp = getClasspath();
-        if (cp != null) {
-            p.add(cp);
-        }
-        p.add(Path.systemClasspath);
-
-        return Arrays.asList(p.list());
-    }
-
-    protected Path getClasspath() {
-        if (classpath == null) {
-            if (getClasspathref() != null) {
-                Path ref = new Path(getProject());
-                ref.setRefid(new Reference(getProject(), getClasspathref()));
-                return ref;
-            }
-        } else if (StringUtils.isNotBlank(getClasspathref())) {
-            throw new BuildException("Only one of classpathref|classpath is permitted.");
-        }
-        return classpath;
-    }
-
-    public void setClasspath(Path classpath) {
-        if (this.classpath != null) {
-            throw new BuildException("classpath already set");
-        }
-        this.classpath = classpath;
-    }
-
-    public InlineProperties createProperties() {
-        if (inlineProperties != null) {
-            throw new BuildException("properties already specified");
-        }
-        inlineProperties = new InlineProperties();
-        return inlineProperties;
-    }
-
-    public PropertySet createPropertySet() {
-        if (propertySet != null) {
-            throw new BuildException("propertyset already specified");
-        }
-        propertySet = new PropertySet();
-        propertySet.setProject(getProject());
-        return propertySet;
-    }
-
-    private Properties getProperties() {
-        if (propertySet == null && inlineProperties == null) {
-            createPropertySet().appendBuiltin(
-                    (BuiltinPropertySetName) EnumeratedAttribute.getInstance(
-                            BuiltinPropertySetName.class, "all"));
-        }
-        final Properties result = new Properties();
-        if (propertySet != null) {
-            result.putAll(propertySet.getProperties());
-        }
-        if (inlineProperties != null) {
-            for (Map.Entry<Object, Object> e : inlineProperties.properties.entrySet())
{
-                result.put(e.getKey(), StringUtils.trim((String) e.getValue()));
-            }
-        }
-        return result;
-    }
-
 }

Modified: commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/InlineProperties.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/InlineProperties.java?rev=1516279&r1=1516278&r2=1516279&view=diff
==============================================================================
--- commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/InlineProperties.java
(original)
+++ commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/InlineProperties.java
Wed Aug 21 19:58:21 2013
@@ -24,7 +24,14 @@ import org.apache.commons.lang3.StringUt
 import org.apache.tools.ant.DynamicElement;
 
 /**
- * Structure to allow the inline specification of weaver properties.
+ * Structure to allow inline specification of properties.
+ * 
+ * Example:
+ * {pre}<foo>foo-value</foo>
+ * <bar>bar-value</bar>
+ * <baz>baz
+ * -nextline-value</baz>
+ * {/pre}
  */
 public class InlineProperties implements DynamicElement {
     /**

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=1516279&r1=1516278&r2=1516279&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 Aug 21 19:58:21 2013
@@ -18,122 +18,28 @@
  */
 package org.apache.commons.weaver.ant;
 
-import java.io.File;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-
-import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.Validate;
 import org.apache.commons.weaver.WeaveProcessor;
 import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.Task;
-import org.apache.tools.ant.types.EnumeratedAttribute;
-import org.apache.tools.ant.types.Path;
-import org.apache.tools.ant.types.PropertySet;
-import org.apache.tools.ant.types.PropertySet.BuiltinPropertySetName;
-import org.apache.tools.ant.types.Reference;
+import org.apache.tools.ant.Project;
 
 /**
  * Weave Ant task.
  */
-public class WeaveTask extends Task {
-    private File target;
-    private Path classpath;
-    private String classpathref;
-    private PropertySet propertySet;
-    private InlineProperties inlineProperties;
+public class WeaveTask extends AbstractWeaverTask {
+    public WeaveTask(Project project) {
+        super(project);
+    }
 
     @Override
     public void execute() throws BuildException {
         try {
-            final WeaveProcessor wp = new WeaveProcessor(getClassPathEntries(), target, getProperties());
+            final WeaverSettings settings = Validate.notNull(getSettings(), "settings");
+            final WeaveProcessor wp =
+                new WeaveProcessor(settings.getClasspathEntries(), settings.getTarget(),
settings.getProperties());
             wp.weave();
         } catch (Exception e) {
             throw new BuildException(e);
         }
     }
-
-    protected File getTarget() {
-        return target;
-    }
-
-    public void setTarget(File target) {
-        this.target = target;
-    }
-
-    protected String getClasspathref() {
-        return classpathref;
-    }
-
-    public void setClasspathRef(String classpathref) {
-        this.classpathref = classpathref;
-    }
-
-    protected List<String> getClassPathEntries() {
-        final Path p = new Path(getProject());
-        final Path cp = getClasspath();
-        if (cp != null) {
-            p.add(cp);
-        }
-        p.add(Path.systemClasspath);
-
-        return Arrays.asList(p.list());
-    }
-
-    protected Path getClasspath() {
-        if (classpath == null) {
-            if (getClasspathref() != null) {
-                Path ref = new Path(getProject());
-                ref.setRefid(new Reference(getProject(), getClasspathref()));
-                return ref;
-            }
-        } else if (StringUtils.isNotBlank(getClasspathref())) {
-            throw new BuildException("Only one of classpathref|classpath is permitted.");
-        }
-        return classpath;
-    }
-
-    public void setClasspath(Path classpath) {
-        if (this.classpath != null) {
-            throw new BuildException("classpath already set");
-        }
-        this.classpath = classpath;
-    }
-
-    public InlineProperties createProperties() {
-        if (inlineProperties != null) {
-            throw new BuildException("properties already specified");
-        }
-        inlineProperties = new InlineProperties();
-        return inlineProperties;
-    }
-
-    public PropertySet createPropertySet() {
-        if (propertySet != null) {
-            throw new BuildException("propertyset already specified");
-        }
-        propertySet = new PropertySet();
-        propertySet.setProject(getProject());
-        return propertySet;
-    }
-
-    private Properties getProperties() {
-        if (propertySet == null && inlineProperties == null) {
-            createPropertySet().appendBuiltin(
-                    (BuiltinPropertySetName) EnumeratedAttribute.getInstance(
-                            BuiltinPropertySetName.class, "all"));
-        }
-        final Properties result = new Properties();
-        if (propertySet != null) {
-            result.putAll(propertySet.getProperties());
-        }
-        if (inlineProperties != null) {
-            for (Map.Entry<Object, Object> e : inlineProperties.properties.entrySet())
{
-                result.put(e.getKey(), StringUtils.trim((String) e.getValue()));
-            }
-        }
-        return result;
-    }
-
 }

Added: commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/WeaverSettings.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/WeaverSettings.java?rev=1516279&view=auto
==============================================================================
--- commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/WeaverSettings.java
(added)
+++ commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/WeaverSettings.java
Wed Aug 21 19:58:21 2013
@@ -0,0 +1,183 @@
+/*
+ * 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.
+ */
+package org.apache.commons.weaver.ant;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.types.DataType;
+import org.apache.tools.ant.types.EnumeratedAttribute;
+import org.apache.tools.ant.types.Path;
+import org.apache.tools.ant.types.PropertySet;
+import org.apache.tools.ant.types.Reference;
+import org.apache.tools.ant.types.PropertySet.BuiltinPropertySetName;
+
+/**
+ * Standalone weaver settings datatype. Handles:
+ * <ul>
+ * <li>{@code target} attribute - {@link File}</li>
+ * <li>{@code classpath} attribute - {@link Path} (incompatible with {@code classpathref})</li>
+ * <li>{@code classpathref} attribute - {@link String} (incompatible with {@code classpath})</li>
+ * <li>nested {@code propertyset} - {@link PropertySet}</li>
+ * <li>nested {@code properties} - {@link InlineProperties}</li>
+ * </ul>
+ * {@code propertyset} and {@code properties} are merged, with the latter taking precedence.
+ */
+public class WeaverSettings extends DataType {
+    private File target;
+    private Path classpath;
+    private String classpathref;
+    private PropertySet propertySet;
+    private InlineProperties inlineProperties;
+
+    public WeaverSettings(Project project) {
+        super();
+        setProject(project);
+    }
+
+    public File getTarget() {
+        if (isReference()) {
+            return getRef().getTarget();
+        }
+        return target;
+    }
+
+    public void setTarget(File target) {
+        if (isReference()) {
+            throw tooManyAttributes();
+        }
+        this.target = target;
+    }
+
+    public String getClasspathref() {
+        if (isReference()) {
+            return getRef().getClasspathref();
+        }
+        return classpathref;
+    }
+
+    public void setClasspathRef(String classpathref) {
+        if (isReference()) {
+            throw tooManyAttributes();
+        }
+        this.classpathref = classpathref;
+    }
+
+    /**
+     * Return the effective classpath (system classpath + configured classpath) as a {@link
List} of {@link String}
+     * filesystem paths.
+     * 
+     * @return List<String>
+     */
+    public List<String> getClasspathEntries() {
+        final Path p = new Path(getProject());
+        final Path cp = getClasspath();
+        if (cp != null) {
+            p.add(cp);
+        }
+        p.add(Path.systemClasspath);
+
+        return Arrays.asList(p.list());
+    }
+
+    public Path getClasspath() {
+        if (isReference()) {
+            return getRef().getClasspath();
+        }
+        if (classpath == null) {
+            if (getClasspathref() != null) {
+                Path ref = new Path(getProject());
+                ref.setRefid(new Reference(getProject(), getClasspathref()));
+                return ref;
+            }
+        } else if (StringUtils.isNotBlank(getClasspathref())) {
+            throw new BuildException("Only one of classpathref|classpath is permitted.");
+        }
+        return classpath;
+    }
+
+    public void setClasspath(Path classpath) {
+        if (isReference()) {
+            throw tooManyAttributes();
+        }
+        if (this.classpath != null) {
+            throw new BuildException("classpath already set");
+        }
+        this.classpath = classpath;
+    }
+
+    public InlineProperties createProperties() {
+        if (isReference()) {
+            throw noChildrenAllowed();
+        }
+        if (inlineProperties != null) {
+            throw new BuildException("properties already specified");
+        }
+        inlineProperties = new InlineProperties();
+        return inlineProperties;
+    }
+
+    public PropertySet createPropertySet() {
+        if (isReference()) {
+            throw noChildrenAllowed();
+        }
+        if (propertySet != null) {
+            throw new BuildException("propertyset already specified");
+        }
+        propertySet = new PropertySet();
+        propertySet.setProject(getProject());
+        return propertySet;
+    }
+
+    /**
+     * Merge nested {@code propertyset} and {@code properties}; latter takes precedence.
+     * 
+     * @return Properties
+     */
+    public Properties getProperties() {
+        if (isReference()) {
+            return getRef().getProperties();
+        }
+        if (propertySet == null && inlineProperties == null) {
+            createPropertySet().appendBuiltin(
+                (BuiltinPropertySetName) EnumeratedAttribute.getInstance(BuiltinPropertySetName.class,
"all"));
+        }
+        final Properties result = new Properties();
+        if (propertySet != null) {
+            result.putAll(propertySet.getProperties());
+        }
+        if (inlineProperties != null) {
+            for (Map.Entry<Object, Object> e : inlineProperties.properties.entrySet())
{
+                result.put(e.getKey(), StringUtils.trim((String) e.getValue()));
+            }
+        }
+        return result;
+    }
+
+    private WeaverSettings getRef() {
+        return (WeaverSettings) getCheckedRef(WeaverSettings.class, "settings");
+    }
+
+}
\ No newline at end of file

Propchange: commons/sandbox/weaver/trunk/ant/lib/src/main/java/org/apache/commons/weaver/ant/WeaverSettings.java
------------------------------------------------------------------------------
    svn:executable = *

Modified: commons/sandbox/weaver/trunk/ant/lib/src/main/resources/org/apache/commons/weaver/ant/antlib.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/ant/lib/src/main/resources/org/apache/commons/weaver/ant/antlib.xml?rev=1516279&r1=1516278&r2=1516279&view=diff
==============================================================================
--- commons/sandbox/weaver/trunk/ant/lib/src/main/resources/org/apache/commons/weaver/ant/antlib.xml
(original)
+++ commons/sandbox/weaver/trunk/ant/lib/src/main/resources/org/apache/commons/weaver/ant/antlib.xml
Wed Aug 21 19:58:21 2013
@@ -18,6 +18,7 @@ specific language governing permissions 
 under the License.
 -->
 <antlib>
+  <typedef name="settings" classname="org.apache.commons.weaver.ant.WeaverSettings" />
   <taskdef name="clean" classname="org.apache.commons.weaver.ant.CleanTask" />
   <taskdef name="weave" classname="org.apache.commons.weaver.ant.WeaveTask" />
 </antlib>

Modified: commons/sandbox/weaver/trunk/ant/test/pom.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/trunk/ant/test/pom.xml?rev=1516279&r1=1516278&r2=1516279&view=diff
==============================================================================
--- commons/sandbox/weaver/trunk/ant/test/pom.xml (original)
+++ commons/sandbox/weaver/trunk/ant/test/pom.xml Wed Aug 21 19:58:21 2013
@@ -153,17 +153,20 @@ under the License.
                       <path id="cp">
                         <fileset dir="${project.build.directory}/compile-classpath" />
                       </path>
-                      <taskdef uri="antlib:org.apache.commons.weaver.ant"
+                      <typedef uri="antlib:org.apache.commons.weaver.ant"
                                resource="org/apache/commons/weaver/ant/antlib.xml"
                                classpathref="cp" />
-                      <cw:weave target="${project.build.outputDirectory}"
-                                  classpathref="cp">
-                          <propertyset>
-                              <propertyref name="privilizer.policy" />
-                          </propertyset>
-                          <properties>
-                              <privilizer.accessLevel>PRIVATE</privilizer.accessLevel>
-                          </properties>
+
+                      <cw:weave>
+                          <cw:settings target="${project.build.outputDirectory}"
+                                       classpathref="cp">
+                              <propertyset>
+                                  <propertyref name="privilizer.policy" />
+                              </propertyset>
+                              <properties>
+                                  <privilizer.accessLevel>PRIVATE</privilizer.accessLevel>
+                              </properties>
+                          </cw:settings>
                       </cw:weave>
                     </sequential>
                   </target>



Mime
View raw message