You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefBase.java 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2003-2005 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. getDelegate().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 getDelegate().getClasspath();
  50. }
  51. /**
  52. * @return the reverse loader attribute of the classpath delegate.
  53. */
  54. public boolean isReverseLoader() {
  55. return getDelegate().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 getDelegate().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 getDelegate().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. getDelegate().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 getDelegate().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. getDelegate().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. getDelegate().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 && cpDelegate == null) {
  116. return getAntlibClassLoader();
  117. }
  118. if (createdLoader == null) {
  119. createdLoader = getDelegate().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. super.init();
  134. }
  135. private ClasspathUtils.Delegate getDelegate() {
  136. if (cpDelegate == null) {
  137. cpDelegate = ClasspathUtils.getDelegate(this);
  138. }
  139. return cpDelegate;
  140. }
  141. }