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 org.apache.tools.ant.AntClassLoader;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Project;
  21. import org.apache.tools.ant.types.Path;
  22. import org.apache.tools.ant.types.Reference;
  23. import org.apache.tools.ant.util.ClasspathUtils;
  24. /**
  25. * Base class for Definitions
  26. * handling uri and class loading.
  27. * (This was part of Definer)
  28. *
  29. * @since Ant 1.6
  30. */
  31. public abstract class DefBase extends AntlibDefinition {
  32. private ClassLoader createdLoader;
  33. private ClasspathUtils.Delegate cpDelegate;
  34. /**
  35. * @param reverseLoader if true a delegated loader will take precedence over
  36. * the parent
  37. * @deprecated stop using this attribute
  38. * @ant.attribute ignore="true"
  39. */
  40. public void setReverseLoader(boolean reverseLoader) {
  41. this.cpDelegate.setReverseLoader(reverseLoader);
  42. log("The reverseloader attribute is DEPRECATED. It will be removed",
  43. Project.MSG_WARN);
  44. }
  45. /**
  46. * @return the classpath for this definition
  47. */
  48. public Path getClasspath() {
  49. return cpDelegate.getClasspath();
  50. }
  51. /**
  52. * @return the reverse loader attribute of the classpath delegate.
  53. */
  54. public boolean isReverseLoader() {
  55. return cpDelegate.isReverseLoader();
  56. }
  57. /**
  58. * Returns the loader id of the class path Delegate.
  59. * @return the loader id
  60. */
  61. public String getLoaderId() {
  62. return cpDelegate.getClassLoadId();
  63. }
  64. /**
  65. * Returns the class path id of the class path delegate.
  66. * @return the class path id
  67. */
  68. public String getClasspathId() {
  69. return cpDelegate.getClassLoadId();
  70. }
  71. /**
  72. * Set the classpath to be used when searching for component being defined
  73. *
  74. * @param classpath an Ant Path object containing the classpath.
  75. */
  76. public void setClasspath(Path classpath) {
  77. this.cpDelegate.setClasspath(classpath);
  78. }
  79. /**
  80. * Create the classpath to be used when searching for component being
  81. * defined
  82. * @return the classpath of the this definition
  83. */
  84. public Path createClasspath() {
  85. return this.cpDelegate.createClasspath();
  86. }
  87. /**
  88. * reference to a classpath to use when loading the files.
  89. * To actually share the same loader, set loaderref as well
  90. * @param r the reference to the classpath
  91. */
  92. public void setClasspathRef(Reference r) {
  93. this.cpDelegate.setClasspathref(r);
  94. }
  95. /**
  96. * Use the reference to locate the loader. If the loader is not
  97. * found, taskdef will use the specified classpath and register it
  98. * with the specified name.
  99. *
  100. * This allow multiple taskdef/typedef to use the same class loader,
  101. * so they can be used together. It eliminate the need to
  102. * put them in the CLASSPATH.
  103. *
  104. * @param r the reference to locate the loader.
  105. * @since Ant 1.5
  106. */
  107. public void setLoaderRef(Reference r) {
  108. this.cpDelegate.setLoaderRef(r);
  109. }
  110. /**
  111. * create a classloader for this definition
  112. * @return the classloader from the cpDelegate
  113. */
  114. protected ClassLoader createLoader() {
  115. if (getAntlibClassLoader() != null) {
  116. return getAntlibClassLoader();
  117. }
  118. if (createdLoader == null) {
  119. createdLoader = this.cpDelegate.getClassLoader();
  120. // need to load Task via system classloader or the new
  121. // task we want to define will never be a Task but always
  122. // be wrapped into a TaskAdapter.
  123. ((AntClassLoader) createdLoader)
  124. .addSystemPackageRoot("org.apache.tools.ant");
  125. }
  126. return createdLoader;
  127. }
  128. /**
  129. * @see org.apache.tools.ant.Task#init()
  130. * @since Ant 1.6
  131. */
  132. public void init() throws BuildException {
  133. this.cpDelegate = ClasspathUtils.getDelegate(this);
  134. super.init();
  135. }
  136. }