which is a Task and handles setURI and setAntlibClassLoader (from a e-mail by Knut Wannheden) git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@275277 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -70,7 +70,9 @@ import org.apache.tools.ant.UnknownElement; | |||||
| /** | /** | ||||
| * Antlib task. | |||||
| * Antlib task. It does not | |||||
| * occur in an ant build file. It is the root element | |||||
| * an antlib xml file. | |||||
| * | * | ||||
| * @author Peter Reilly | * @author Peter Reilly | ||||
| * | * | ||||
| @@ -179,21 +181,21 @@ public class Antlib extends Task implements TaskContainer { | |||||
| UnknownElement ue = (UnknownElement) i.next(); | UnknownElement ue = (UnknownElement) i.next(); | ||||
| setLocation(ue.getLocation()); | setLocation(ue.getLocation()); | ||||
| ue.maybeConfigure(); | ue.maybeConfigure(); | ||||
| Task t = ue.getTask(); | |||||
| if (t == null) { | |||||
| Object configuredObject = ue.getRealThing(); | |||||
| if (configuredObject == null) { | |||||
| continue; | continue; | ||||
| } | } | ||||
| if (!(t instanceof AntlibInterface)) { | |||||
| if (!(configuredObject instanceof AntlibDefinition)) { | |||||
| throw new BuildException( | throw new BuildException( | ||||
| "Invalid element in antlib " + ue.getTag()); | |||||
| "Invalid task in antlib " + ue.getTag() | |||||
| + " " + configuredObject.getClass() + " does not " | |||||
| + "extend org.apache.tools.ant.taskdefs.AntlibDefinition"); | |||||
| } | } | ||||
| if (t instanceof AntlibInterface) { | |||||
| AntlibInterface d = (AntlibInterface) t; | |||||
| d.setURI(uri); | |||||
| d.setAntlibClassLoader(getClassLoader()); | |||||
| } | |||||
| t.init(); | |||||
| t.execute(); | |||||
| AntlibDefinition def = (AntlibDefinition) configuredObject; | |||||
| def.setURI(uri); | |||||
| def.setAntlibClassLoader(getClassLoader()); | |||||
| def.init(); | |||||
| def.execute(); | |||||
| } | } | ||||
| } | } | ||||
| @@ -54,28 +54,65 @@ | |||||
| package org.apache.tools.ant.taskdefs; | package org.apache.tools.ant.taskdefs; | ||||
| import org.apache.tools.ant.BuildException; | |||||
| import org.apache.tools.ant.ProjectHelper; | |||||
| import org.apache.tools.ant.Task; | |||||
| /** | /** | ||||
| * Interface for tasks that should be informed when | |||||
| * they are loaded in antlib's. | |||||
| * Base class for tasks that that can be used in antlibs. | |||||
| * For handling uri and class loading. | * For handling uri and class loading. | ||||
| * | * | ||||
| * @author Peter Reilly | * @author Peter Reilly | ||||
| * | * | ||||
| * @since Ant 1.6 | * @since Ant 1.6 | ||||
| */ | */ | ||||
| public interface AntlibInterface { | |||||
| public class AntlibDefinition extends Task { | |||||
| private String uri = ""; | |||||
| private ClassLoader antlibClassLoader; | |||||
| /** | /** | ||||
| * The URI for this definition. | * The URI for this definition. | ||||
| * If the URI is "ant:core", the uri will be set to "". (This | |||||
| * is the default uri). | |||||
| * URIs that start with "ant:" and are not | |||||
| * "ant:core" are reserved and are not allowed in this context. | |||||
| * @param uri the namespace URI | * @param uri the namespace URI | ||||
| * @throws BuildException if a reserved URI is used | |||||
| */ | |||||
| public void setURI(String uri) throws BuildException { | |||||
| if (uri.equals(ProjectHelper.ANT_CORE_URI)) { | |||||
| uri = ""; | |||||
| } | |||||
| if (uri.startsWith("ant:")) { | |||||
| throw new BuildException("Attempt to use a reserved URI " + uri); | |||||
| } | |||||
| this.uri = uri; | |||||
| } | |||||
| /** | |||||
| * The URI for this definition. | |||||
| * @return The URI for this defintion. | |||||
| */ | */ | ||||
| void setURI(String uri); | |||||
| public String getURI() { | |||||
| return uri; | |||||
| } | |||||
| /** | /** | ||||
| * Set the class loader of the loading object | * Set the class loader of the loading object | ||||
| * | * | ||||
| * @param classLoader a <code>ClassLoader</code> value | * @param classLoader a <code>ClassLoader</code> value | ||||
| */ | */ | ||||
| void setAntlibClassLoader(ClassLoader classLoader); | |||||
| public void setAntlibClassLoader(ClassLoader classLoader) { | |||||
| this.antlibClassLoader = classLoader; | |||||
| } | |||||
| /** | |||||
| * The current antlib classloader | |||||
| * @return the antlib classloader for the definition, this | |||||
| * is null if the definition is not used in an antlib. | |||||
| */ | |||||
| public ClassLoader getAntlibClassLoader() { | |||||
| return antlibClassLoader; | |||||
| } | |||||
| } | } | ||||
| @@ -57,8 +57,6 @@ package org.apache.tools.ant.taskdefs; | |||||
| import org.apache.tools.ant.AntClassLoader; | import org.apache.tools.ant.AntClassLoader; | ||||
| import org.apache.tools.ant.BuildException; | import org.apache.tools.ant.BuildException; | ||||
| import org.apache.tools.ant.Project; | import org.apache.tools.ant.Project; | ||||
| import org.apache.tools.ant.ProjectHelper; | |||||
| import org.apache.tools.ant.Task; | |||||
| import org.apache.tools.ant.types.Path; | import org.apache.tools.ant.types.Path; | ||||
| import org.apache.tools.ant.types.Reference; | import org.apache.tools.ant.types.Reference; | ||||
| import org.apache.tools.ant.util.ClasspathUtils; | import org.apache.tools.ant.util.ClasspathUtils; | ||||
| @@ -74,49 +72,10 @@ import org.apache.tools.ant.util.ClasspathUtils; | |||||
| * | * | ||||
| * @since Ant 1.6 | * @since Ant 1.6 | ||||
| */ | */ | ||||
| public abstract class DefBase extends Task implements AntlibInterface { | |||||
| private String uri = ""; | |||||
| private ClassLoader internalClassLoader; | |||||
| public abstract class DefBase extends AntlibDefinition { | |||||
| private ClassLoader createdLoader; | private ClassLoader createdLoader; | ||||
| private ClasspathUtils.Delegate cpDelegate; | private ClasspathUtils.Delegate cpDelegate; | ||||
| /** | |||||
| * The URI for this definition. | |||||
| * If the URI is "ant:core", the uri will be set to "". (This | |||||
| * is the default uri). | |||||
| * URIs that start with "ant:" and are not | |||||
| * "ant:core" are reserved and are not allowed in this context. | |||||
| * @param uri the namespace URI | |||||
| * @throws BuildException if a reserved URI is used | |||||
| */ | |||||
| public void setURI(String uri) throws BuildException { | |||||
| if (uri.equals(ProjectHelper.ANT_CORE_URI)) { | |||||
| uri = ""; | |||||
| } | |||||
| if (uri.startsWith("ant:")) { | |||||
| throw new BuildException("Attempt to use a reserved URI " + uri); | |||||
| } | |||||
| this.uri = uri; | |||||
| } | |||||
| /** | |||||
| * @return the namespace uri for this definition | |||||
| */ | |||||
| public String getUri() { | |||||
| return uri; | |||||
| } | |||||
| /** | |||||
| * Set the class loader, overrides the cpDelagate | |||||
| * classloader. | |||||
| * | |||||
| * @param classLoader a <code>ClassLoader</code> value | |||||
| */ | |||||
| public void setAntlibClassLoader(ClassLoader classLoader) { | |||||
| this.internalClassLoader = classLoader; | |||||
| } | |||||
| /** | /** | ||||
| * @param reverseLoader if true a delegated loader will take precedence over | * @param reverseLoader if true a delegated loader will take precedence over | ||||
| * the parent | * the parent | ||||
| @@ -207,8 +166,8 @@ public abstract class DefBase extends Task implements AntlibInterface { | |||||
| * @return the classloader from the cpDelegate | * @return the classloader from the cpDelegate | ||||
| */ | */ | ||||
| protected ClassLoader createLoader() { | protected ClassLoader createLoader() { | ||||
| if (internalClassLoader != null) { | |||||
| return internalClassLoader; | |||||
| if (getAntlibClassLoader() != null) { | |||||
| return getAntlibClassLoader(); | |||||
| } | } | ||||
| if (createdLoader == null) { | if (createdLoader == null) { | ||||
| createdLoader = this.cpDelegate.getClassLoader(); | createdLoader = this.cpDelegate.getClassLoader(); | ||||
| @@ -315,9 +315,9 @@ public abstract class Definer extends DefBase { | |||||
| */ | */ | ||||
| private void loadAntlib(ClassLoader classLoader, URL url) { | private void loadAntlib(ClassLoader classLoader, URL url) { | ||||
| try { | try { | ||||
| Antlib antlib = Antlib.createAntlib(getProject(), url, getUri()); | |||||
| Antlib antlib = Antlib.createAntlib(getProject(), url, getURI()); | |||||
| antlib.setClassLoader(classLoader); | antlib.setClassLoader(classLoader); | ||||
| antlib.setURI(getUri()); | |||||
| antlib.setURI(getURI()); | |||||
| antlib.perform(); | antlib.perform(); | ||||
| } catch (BuildException ex) { | } catch (BuildException ex) { | ||||
| Location exLocation = ex.getLocation(); | Location exLocation = ex.getLocation(); | ||||
| @@ -449,7 +449,7 @@ public abstract class Definer extends DefBase { | |||||
| Class cl = null; | Class cl = null; | ||||
| try { | try { | ||||
| try { | try { | ||||
| name = ProjectHelper.genComponentName(getUri(), name); | |||||
| name = ProjectHelper.genComponentName(getURI(), name); | |||||
| if (onError != OnError.IGNORE) { | if (onError != OnError.IGNORE) { | ||||
| cl = Class.forName(classname, true, al); | cl = Class.forName(classname, true, al); | ||||
| @@ -76,12 +76,11 @@ import org.apache.tools.ant.types.EnumeratedAttribute; | |||||
| * @author Peter Reilly | * @author Peter Reilly | ||||
| * @since Ant 1.6 | * @since Ant 1.6 | ||||
| */ | */ | ||||
| public class MacroDef extends Task implements AntlibInterface, TaskContainer { | |||||
| public class MacroDef extends AntlibDefinition implements TaskContainer { | |||||
| private UnknownElement nestedTask; | private UnknownElement nestedTask; | ||||
| private String name; | private String name; | ||||
| private List attributes = new ArrayList(); | private List attributes = new ArrayList(); | ||||
| private Map elements = new HashMap(); | private Map elements = new HashMap(); | ||||
| private String uri; | |||||
| private int attributeStyle = AttributeStyle.ANT; | private int attributeStyle = AttributeStyle.ANT; | ||||
| /** | /** | ||||
| @@ -92,21 +91,6 @@ public class MacroDef extends Task implements AntlibInterface, TaskContainer { | |||||
| this.name = name; | this.name = name; | ||||
| } | } | ||||
| /** | |||||
| * The URI for this definition. | |||||
| * @param uri the namespace URI | |||||
| * @throws BuildException if uri is not allowed | |||||
| */ | |||||
| public void setURI(String uri) throws BuildException { | |||||
| if (uri.equals(ProjectHelper.ANT_CORE_URI)) { | |||||
| uri = ""; | |||||
| } | |||||
| if (uri.startsWith("ant:")) { | |||||
| throw new BuildException("Attempt to use a reserved URI " + uri); | |||||
| } | |||||
| this.uri = uri; | |||||
| } | |||||
| /** | /** | ||||
| * Enumerated type for attributeStyle attribute | * Enumerated type for attributeStyle attribute | ||||
| * | * | ||||
| @@ -262,7 +246,7 @@ public class MacroDef extends Task implements AntlibInterface, TaskContainer { | |||||
| throw new BuildException("Name not specified"); | throw new BuildException("Name not specified"); | ||||
| } | } | ||||
| name = ProjectHelper.genComponentName(uri, name); | |||||
| name = ProjectHelper.genComponentName(getURI(), name); | |||||
| MyAntTypeDefinition def = new MyAntTypeDefinition(this); | MyAntTypeDefinition def = new MyAntTypeDefinition(this); | ||||
| def.setName(name); | def.setName(name); | ||||
| @@ -439,13 +423,16 @@ public class MacroDef extends Task implements AntlibInterface, TaskContainer { | |||||
| if (!name.equals(other.name)) { | if (!name.equals(other.name)) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| if (uri == null || uri.equals("") | |||||
| || uri.equals(ProjectHelper.ANT_CORE_URI)) { | |||||
| return other.uri == null || other.uri.equals("") | |||||
| || other.uri.equals(ProjectHelper.ANT_CORE_URI); | |||||
| } | |||||
| if (!uri.equals(other.uri)) { | |||||
| return false; | |||||
| if (getURI() == null || getURI().equals("") | |||||
| || getURI().equals(ProjectHelper.ANT_CORE_URI)) { | |||||
| if (!(other.getURI() == null || other.getURI().equals("") | |||||
| || other.getURI().equals(ProjectHelper.ANT_CORE_URI))) { | |||||
| return false; | |||||
| } | |||||
| } else { | |||||
| if (!getURI().equals(other.getURI())) { | |||||
| return false; | |||||
| } | |||||
| } | } | ||||
| if (attributeStyle != other.attributeStyle) { | if (attributeStyle != other.attributeStyle) { | ||||
| @@ -271,7 +271,7 @@ public class ScriptDef extends DefBase { | |||||
| } | } | ||||
| } | } | ||||
| name = ProjectHelper.genComponentName(getUri(), name); | |||||
| name = ProjectHelper.genComponentName(getURI(), name); | |||||
| scriptRepository.put(name, this); | scriptRepository.put(name, this); | ||||
| AntTypeDefinition def = new AntTypeDefinition(); | AntTypeDefinition def = new AntTypeDefinition(); | ||||
| def.setName(name); | def.setName(name); | ||||