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.

Definer.java 8.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2001 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import org.apache.tools.ant.Task;
  56. import org.apache.tools.ant.BuildException;
  57. import org.apache.tools.ant.Project;
  58. import org.apache.tools.ant.AntClassLoader;
  59. import org.apache.tools.ant.types.Path;
  60. import org.apache.tools.ant.types.Reference;
  61. import java.util.Properties;
  62. import java.util.Enumeration;
  63. import java.io.File;
  64. import java.io.InputStream;
  65. import java.io.FileInputStream;
  66. import java.io.IOException;
  67. /**
  68. * Base class for Taskdef and Typedef - does all the classpath
  69. * handling and and class loading.
  70. *
  71. * @author Costin Manolache
  72. * @author <a href="stefan.bodewig@epost.de">Stefan Bodewig</a>
  73. */
  74. public abstract class Definer extends Task {
  75. private String name;
  76. private String value;
  77. private Path classpath;
  78. private File file;
  79. private String resource;
  80. private boolean reverseLoader = false;
  81. public void setReverseLoader(boolean reverseLoader) {
  82. this.reverseLoader = reverseLoader;
  83. log("The reverseloader attribute is DEPRECATED. It will be removed", Project.MSG_WARN);
  84. }
  85. public void setClasspath(Path classpath) {
  86. if (this.classpath == null) {
  87. this.classpath = classpath;
  88. } else {
  89. this.classpath.append(classpath);
  90. }
  91. }
  92. public Path createClasspath() {
  93. if (this.classpath == null) {
  94. this.classpath = new Path(project);
  95. }
  96. return this.classpath.createPath();
  97. }
  98. public void setClasspathRef(Reference r) {
  99. createClasspath().setRefid(r);
  100. }
  101. public void execute() throws BuildException {
  102. AntClassLoader al=createLoader();
  103. if (file==null && resource==null ) {
  104. // simple case - one definition
  105. if ( name==null || value==null ) {
  106. String msg = "name or classname attributes of "
  107. + getTaskName() + " element "
  108. + "are undefined";
  109. throw new BuildException(msg);
  110. }
  111. addDefinition( al, name, value );
  112. } else {
  113. try {
  114. if (name != null || value != null) {
  115. String msg = "You must not specify name or value "
  116. + "together with file or resource.";
  117. throw new BuildException(msg, location);
  118. }
  119. if (file != null && resource != null) {
  120. String msg = "You must not specify both, file and resource.";
  121. throw new BuildException(msg, location);
  122. }
  123. Properties props=new Properties();
  124. InputStream is=null;
  125. if( file != null ) {
  126. log("Loading definitions from file " + file,
  127. Project.MSG_VERBOSE);
  128. is=new FileInputStream( file );
  129. if (is == null) {
  130. log("Could not load definitions from file " + file
  131. + ". It doesn\'t exist.", Project.MSG_WARN);
  132. }
  133. }
  134. if( resource!=null ) {
  135. log("Loading definitions from resource " + resource,
  136. Project.MSG_VERBOSE);
  137. is=al.getResourceAsStream( resource );
  138. if (is == null) {
  139. log("Could not load definitions from resource "
  140. + resource + ". It could not be found.",
  141. Project.MSG_WARN);
  142. }
  143. }
  144. if( is!=null ) {
  145. props.load( is );
  146. Enumeration keys=props.keys();
  147. while( keys.hasMoreElements() ) {
  148. String n=(String)keys.nextElement();
  149. String v=props.getProperty( n );
  150. addDefinition( al, n, v );
  151. }
  152. }
  153. } catch( IOException ex ) {
  154. throw new BuildException(ex, location);
  155. }
  156. }
  157. }
  158. private void addDefinition( ClassLoader al, String name, String value )
  159. throws BuildException {
  160. try {
  161. Class c = al.loadClass(value);
  162. AntClassLoader.initializeClass(c);
  163. addDefinition(name, c);
  164. } catch (ClassNotFoundException cnfe) {
  165. String msg = getTaskName()+" class " + value +
  166. " cannot be found";
  167. throw new BuildException(msg, cnfe, location);
  168. } catch (NoClassDefFoundError ncdfe) {
  169. String msg = getTaskName()+" class " + value +
  170. " cannot be found";
  171. throw new BuildException(msg, ncdfe, location);
  172. }
  173. }
  174. private AntClassLoader createLoader() {
  175. AntClassLoader al = null;
  176. if (classpath != null) {
  177. al = new AntClassLoader(project, classpath, !reverseLoader);
  178. } else {
  179. al = new AntClassLoader(project, Path.systemClasspath, !reverseLoader);
  180. }
  181. // need to load Task via system classloader or the new
  182. // task we want to define will never be a Task but always
  183. // be wrapped into a TaskAdapter.
  184. al.addSystemPackageRoot("org.apache.tools.ant");
  185. return al;
  186. }
  187. public void setFile( File file ) {
  188. this.file=file;
  189. }
  190. public void setResource( String res ) {
  191. this.resource=res;
  192. }
  193. public void setName( String name) {
  194. this.name = name;
  195. }
  196. public String getClassname() {
  197. return value;
  198. }
  199. public void setClassname(String v) {
  200. value = v;
  201. }
  202. protected abstract void addDefinition(String name, Class c);
  203. }