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 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import org.apache.tools.ant.AntClassLoader;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.Project;
  22. import org.apache.tools.ant.types.Path;
  23. import org.apache.tools.ant.types.Reference;
  24. import org.apache.tools.ant.util.ClasspathUtils;
  25. /**
  26. * Base class for Definitions 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. * Check if classpath attributes have been set.
  36. * (to be called before getCpDelegate() is used.
  37. * @return true if cpDelegate has been created.
  38. */
  39. protected boolean hasCpDelegate() {
  40. return cpDelegate != null;
  41. }
  42. /**
  43. * @param reverseLoader if true a delegated loader will take precedence over
  44. * the parent
  45. * @deprecated since 1.6.x.
  46. * stop using this attribute
  47. * @ant.attribute ignore="true"
  48. */
  49. public void setReverseLoader(boolean reverseLoader) {
  50. getDelegate().setReverseLoader(reverseLoader);
  51. log("The reverseloader attribute is DEPRECATED. It will be removed",
  52. Project.MSG_WARN);
  53. }
  54. /**
  55. * @return the classpath for this definition
  56. */
  57. public Path getClasspath() {
  58. return getDelegate().getClasspath();
  59. }
  60. /**
  61. * @return the reverse loader attribute of the classpath delegate.
  62. */
  63. public boolean isReverseLoader() {
  64. return getDelegate().isReverseLoader();
  65. }
  66. /**
  67. * Returns the loader id of the class path Delegate.
  68. * @return the loader id
  69. */
  70. public String getLoaderId() {
  71. return getDelegate().getClassLoadId();
  72. }
  73. /**
  74. * Returns the class path id of the class path delegate.
  75. * @return the class path id
  76. */
  77. public String getClasspathId() {
  78. return getDelegate().getClassLoadId();
  79. }
  80. /**
  81. * Set the classpath to be used when searching for component being defined.
  82. *
  83. * @param classpath an Ant Path object containing the classpath.
  84. */
  85. public void setClasspath(Path classpath) {
  86. getDelegate().setClasspath(classpath);
  87. }
  88. /**
  89. * Create the classpath to be used when searching for component being
  90. * defined.
  91. * @return the classpath of the this definition
  92. */
  93. public Path createClasspath() {
  94. return getDelegate().createClasspath();
  95. }
  96. /**
  97. * Set a reference to a classpath to use when loading the files.
  98. * To actually share the same loader, set loaderref as well
  99. * @param r the reference to the classpath
  100. */
  101. public void setClasspathRef(Reference r) {
  102. getDelegate().setClasspathref(r);
  103. }
  104. /**
  105. * Use the reference to locate the loader. If the loader is not
  106. * found, the specified classpath will be used and registered
  107. * with the specified name.
  108. *
  109. * This allows multiple taskdef/typedef to use the same class loader,
  110. * so they can be used together, eliminating the need to
  111. * put them in the CLASSPATH.
  112. *
  113. * @param r the reference to locate the loader.
  114. * @since Ant 1.5
  115. */
  116. public void setLoaderRef(Reference r) {
  117. getDelegate().setLoaderRef(r);
  118. }
  119. /**
  120. * create a classloader for this definition
  121. * @return the classloader from the cpDelegate
  122. */
  123. protected ClassLoader createLoader() {
  124. if (getAntlibClassLoader() != null && cpDelegate == null) {
  125. return getAntlibClassLoader();
  126. }
  127. if (createdLoader == null) {
  128. createdLoader = getDelegate().getClassLoader();
  129. // need to load Task via system classloader or the new
  130. // task we want to define will never be a Task but always
  131. // be wrapped into a TaskAdapter.
  132. ((AntClassLoader) createdLoader)
  133. .addSystemPackageRoot("org.apache.tools.ant");
  134. }
  135. return createdLoader;
  136. }
  137. /**
  138. * @see org.apache.tools.ant.Task#init()
  139. * @throws BuildException on error.
  140. * @since Ant 1.6
  141. */
  142. public void init() throws BuildException {
  143. super.init();
  144. }
  145. private ClasspathUtils.Delegate getDelegate() {
  146. if (cpDelegate == null) {
  147. cpDelegate = ClasspathUtils.getDelegate(this);
  148. }
  149. return cpDelegate;
  150. }
  151. }