1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs;
  18. import java.io.IOException;
  19. import java.net.URL;
  20. import java.util.ArrayList;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import org.apache.tools.ant.TaskContainer;
  24. import org.apache.tools.ant.BuildException;
  25. import org.apache.tools.ant.ComponentHelper;
  26. import org.apache.tools.ant.Project;
  27. import org.apache.tools.ant.Task;
  28. import org.apache.tools.ant.helper.ProjectHelper2;
  29. import org.apache.tools.ant.UnknownElement;
  30. /**
  31. * Antlib task. It does not
  32. * occur in an ant build file. It is the root element
  33. * an antlib xml file.
  34. *
  35. *
  36. * @since Ant 1.6
  37. */
  38. public class Antlib extends Task implements TaskContainer {
  39. //
  40. // Static
  41. //
  42. /** The name of this task */
  43. public static final String TAG = "antlib";
  44. /**
  45. * Static method to read an ant lib definition from
  46. * a url.
  47. *
  48. * @param project the current project
  49. * @param antlibUrl the url to read the definitions from
  50. * @param uri the uri that the antlib is to be placed in
  51. * @return the ant lib task
  52. */
  53. public static Antlib createAntlib(Project project, URL antlibUrl,
  54. String uri) {
  55. // Check if we can contact the URL
  56. try {
  57. antlibUrl.openConnection().connect();
  58. } catch (IOException ex) {
  59. throw new BuildException(
  60. "Unable to find " + antlibUrl, ex);
  61. }
  62. ComponentHelper helper =
  63. ComponentHelper.getComponentHelper(project);
  64. helper.enterAntLib(uri);
  65. try {
  66. // Should be safe to parse
  67. ProjectHelper2 parser = new ProjectHelper2();
  68. UnknownElement ue =
  69. parser.parseUnknownElement(project, antlibUrl);
  70. // Check name is "antlib"
  71. if (!(ue.getTag().equals(TAG))) {
  72. throw new BuildException(
  73. "Unexpected tag " + ue.getTag() + " expecting "
  74. + TAG, ue.getLocation());
  75. }
  76. Antlib antlib = new Antlib();
  77. antlib.setProject(project);
  78. antlib.setLocation(ue.getLocation());
  79. antlib.init();
  80. ue.configure(antlib);
  81. return antlib;
  82. } finally {
  83. helper.exitAntLib();
  84. }
  85. }
  86. //
  87. // Instance
  88. //
  89. private ClassLoader classLoader;
  90. private String uri = "";
  91. private List tasks = new ArrayList();
  92. /**
  93. * Set the class loader for this antlib.
  94. * This class loader is used for any tasks that
  95. * derive from Definer.
  96. *
  97. * @param classLoader the class loader
  98. */
  99. protected void setClassLoader(ClassLoader classLoader) {
  100. this.classLoader = classLoader;
  101. }
  102. /**
  103. * Set the URI for this antlib.
  104. * @param uri the namespace uri
  105. */
  106. protected void setURI(String uri) {
  107. this.uri = uri;
  108. }
  109. private ClassLoader getClassLoader() {
  110. if (classLoader == null) {
  111. classLoader = Antlib.class.getClassLoader();
  112. }
  113. return classLoader;
  114. }
  115. /**
  116. * add a task to the list of tasks
  117. *
  118. * @param nestedTask Nested task to execute in antlib
  119. */
  120. public void addTask(Task nestedTask) {
  121. tasks.add(nestedTask);
  122. }
  123. /**
  124. * Execute the nested tasks, setting the classloader for
  125. * any tasks that derive from Definer.
  126. */
  127. public void execute() {
  128. for (Iterator i = tasks.iterator(); i.hasNext();) {
  129. UnknownElement ue = (UnknownElement) i.next();
  130. setLocation(ue.getLocation());
  131. ue.maybeConfigure();
  132. Object configuredObject = ue.getRealThing();
  133. if (configuredObject == null) {
  134. continue;
  135. }
  136. if (!(configuredObject instanceof AntlibDefinition)) {
  137. throw new BuildException(
  138. "Invalid task in antlib " + ue.getTag()
  139. + " " + configuredObject.getClass() + " does not "
  140. + "extend org.apache.tools.ant.taskdefs.AntlibDefinition");
  141. }
  142. AntlibDefinition def = (AntlibDefinition) configuredObject;
  143. def.setURI(uri);
  144. def.setAntlibClassLoader(getClassLoader());
  145. def.init();
  146. def.execute();
  147. }
  148. }
  149. }