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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2001-2003 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 "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 java.io.File;
  56. import java.io.IOException;
  57. import java.io.InputStream;
  58. import java.net.URL;
  59. import java.util.Enumeration;
  60. import java.util.Properties;
  61. import org.apache.tools.ant.AntTypeDefinition;
  62. import org.apache.tools.ant.AntClassLoader;
  63. import org.apache.tools.ant.ComponentHelper;
  64. import org.apache.tools.ant.BuildException;
  65. import org.apache.tools.ant.Project;
  66. import org.apache.tools.ant.Task;
  67. import org.apache.tools.ant.types.Path;
  68. import org.apache.tools.ant.types.Reference;
  69. import org.apache.tools.ant.util.ClasspathUtils;
  70. import org.apache.tools.ant.types.EnumeratedAttribute;
  71. /**
  72. * Base class for Taskdef and Typedef - does all the classpath
  73. * handling and and class loading.
  74. *
  75. * @author Costin Manolache
  76. * @author Stefan Bodewig
  77. *
  78. * @since Ant 1.4
  79. */
  80. public abstract class Definer extends Task {
  81. private String name;
  82. private String classname;
  83. private File file;
  84. private String resource;
  85. private ClasspathUtils.Delegate cpDelegate;
  86. private boolean definerSet = false;
  87. private ClassLoader internalClassLoader;
  88. private int onError = OnError.FAIL;
  89. private String adapter;
  90. private String adaptTo;
  91. private Class adapterClass;
  92. private Class adaptToClass;
  93. /**
  94. * Enumerated type for onError attribute
  95. *
  96. * @see EnumeratedAttribute
  97. */
  98. public static class OnError extends EnumeratedAttribute {
  99. /** Enumerated values */
  100. public static final int FAIL = 0, REPORT = 1, IGNORE = 2;
  101. /**
  102. * Constructor
  103. */
  104. public OnError() {
  105. super();
  106. }
  107. /**
  108. * Constructor using a string.
  109. * @param value the value of the attribute
  110. */
  111. public OnError(String value) {
  112. setValue(value);
  113. }
  114. /**
  115. * get the values
  116. * @return an array of the allowed values for this attribute.
  117. */
  118. public String[] getValues() {
  119. return new String[] {"fail", "report", "ignore"};
  120. }
  121. }
  122. /**
  123. * What to do if there is an error in loading the class.
  124. * <dl>
  125. * <li>error - throw build exception</li>
  126. * <li>report - output at warning level</li>
  127. * <li>ignore - output at debug level</li>
  128. * </dl>
  129. *
  130. * @param onError an <code>OnError</code> value
  131. */
  132. public void setOnError(OnError onError) {
  133. this.onError = onError.getIndex();
  134. }
  135. /**
  136. * @param reverseLoader if true a delegated loader will take precedence over
  137. * the parent
  138. * @deprecated stop using this attribute
  139. * @ant.attribute ignore="true"
  140. */
  141. public void setReverseLoader(boolean reverseLoader) {
  142. this.cpDelegate.setReverseLoader(reverseLoader);
  143. log("The reverseloader attribute is DEPRECATED. It will be removed",
  144. Project.MSG_WARN);
  145. }
  146. /**
  147. * @return the name for this definition
  148. */
  149. public String getName() {
  150. return name;
  151. }
  152. /**
  153. * @return the class path path for this definition
  154. */
  155. public Path getClasspath() {
  156. return cpDelegate.getClasspath();
  157. }
  158. /**
  159. * @return the file containing definitions
  160. */
  161. public File getFile() {
  162. return file;
  163. }
  164. /**
  165. * @return the resource containing definitions
  166. */
  167. public String getResource() {
  168. return resource;
  169. }
  170. /**
  171. * @return the reverse loader attribute of the classpath delegate.
  172. */
  173. public boolean isReverseLoader() {
  174. return cpDelegate.isReverseLoader();
  175. }
  176. /**
  177. * Returns the loader id of the class path Delegate.
  178. * @return the loader id
  179. */
  180. public String getLoaderId() {
  181. return cpDelegate.getClassLoadId();
  182. }
  183. /**
  184. * Returns the class path id of the class path delegate.
  185. * @return the class path id
  186. */
  187. public String getClasspathId() {
  188. return cpDelegate.getClassLoadId();
  189. }
  190. /**
  191. * Set the classpath to be used when searching for component being defined
  192. *
  193. * @param classpath an Ant Path object containing the classpath.
  194. */
  195. public void setClasspath(Path classpath) {
  196. this.cpDelegate.setClasspath(classpath);
  197. }
  198. /**
  199. * Create the classpath to be used when searching for component being
  200. * defined
  201. * @return the classpath of the this definition
  202. */
  203. public Path createClasspath() {
  204. return this.cpDelegate.createClasspath();
  205. }
  206. /**
  207. * reference to a classpath to use when loading the files.
  208. * To actually share the same loader, set loaderref as well
  209. * @param r the reference to the classpath
  210. */
  211. public void setClasspathRef(Reference r) {
  212. this.cpDelegate.setClasspathref(r);
  213. }
  214. /**
  215. * Use the reference to locate the loader. If the loader is not
  216. * found, taskdef will use the specified classpath and register it
  217. * with the specified name.
  218. *
  219. * This allow multiple taskdef/typedef to use the same class loader,
  220. * so they can be used together. It eliminate the need to
  221. * put them in the CLASSPATH.
  222. *
  223. * @param r the reference to locate the loader.
  224. * @since Ant 1.5
  225. */
  226. public void setLoaderRef(Reference r) {
  227. this.cpDelegate.setLoaderRef(r);
  228. }
  229. /**
  230. * Run the definition.
  231. *
  232. * @exception BuildException if an error occurs
  233. */
  234. public void execute() throws BuildException {
  235. ClassLoader al = createLoader();
  236. if (!definerSet) {
  237. throw new BuildException(
  238. "name, file or resource attribute of "
  239. + getTaskName() + " is undefined", getLocation());
  240. }
  241. if (name != null) {
  242. if (classname == null) {
  243. throw new BuildException(
  244. "classname attribute of " + getTaskName() + " element "
  245. + "is undefined", getLocation());
  246. }
  247. addDefinition(al, name, classname);
  248. } else {
  249. if (classname != null) {
  250. String msg = "You must not specify classname "
  251. + "together with file or resource.";
  252. throw new BuildException(msg, getLocation());
  253. }
  254. URL url = null;
  255. if (file != null) {
  256. url = fileToURL();
  257. }
  258. if (resource != null) {
  259. url = resourceToURL(al);
  260. }
  261. if (url == null) {
  262. return;
  263. }
  264. loadProperties(al, url);
  265. }
  266. }
  267. private URL fileToURL() {
  268. if (!(file.exists())) {
  269. log("File " + file + " does not exist", Project.MSG_WARN);
  270. return null;
  271. }
  272. if (!(file.isFile())) {
  273. log("File " + file + " is not a file", Project.MSG_WARN);
  274. return null;
  275. }
  276. try {
  277. return file.toURL();
  278. } catch (Exception ex) {
  279. log("File " + file + " cannot use as URL: "
  280. + ex.toString(), Project.MSG_WARN);
  281. return null;
  282. }
  283. }
  284. private URL resourceToURL(ClassLoader classLoader) {
  285. URL ret = classLoader.getResource(resource);
  286. if (ret == null) {
  287. if (onError != OnError.IGNORE) {
  288. log("Could not load definitions from resource "
  289. + resource + ". It could not be found.",
  290. Project.MSG_WARN);
  291. }
  292. }
  293. return ret;
  294. }
  295. /**
  296. * Load type definitions as properties from a url.
  297. *
  298. * @param al the classloader to use
  299. * @param url the url to get the definitions from
  300. */
  301. protected void loadProperties(ClassLoader al, URL url) {
  302. InputStream is = null;
  303. try {
  304. is = url.openStream();
  305. if (is == null) {
  306. log("Could not load definitions from " + url,
  307. Project.MSG_WARN);
  308. return;
  309. }
  310. Properties props = new Properties();
  311. props.load(is);
  312. Enumeration keys = props.keys();
  313. while (keys.hasMoreElements()) {
  314. name = ((String) keys.nextElement());
  315. classname = props.getProperty(name);
  316. addDefinition(al, name, classname);
  317. }
  318. } catch (IOException ex) {
  319. throw new BuildException(ex, getLocation());
  320. } finally {
  321. if (is != null) {
  322. try {
  323. is.close();
  324. } catch (IOException e) {
  325. }
  326. }
  327. }
  328. }
  329. /**
  330. * create a classloader for this definition
  331. * @return the classloader from the cpDelegate
  332. */
  333. protected ClassLoader createLoader() {
  334. if (internalClassLoader != null) {
  335. return internalClassLoader;
  336. }
  337. ClassLoader al = this.cpDelegate.getClassLoader();
  338. // need to load Task via system classloader or the new
  339. // task we want to define will never be a Task but always
  340. // be wrapped into a TaskAdapter.
  341. ((AntClassLoader) al).addSystemPackageRoot("org.apache.tools.ant");
  342. return al;
  343. }
  344. /**
  345. * Name of the property file to load
  346. * ant name/classname pairs from.
  347. * @param file the file
  348. */
  349. public void setFile(File file) {
  350. if (definerSet) {
  351. tooManyDefinitions();
  352. }
  353. definerSet = true;
  354. this.file = file;
  355. }
  356. /**
  357. * Name of the property resource to load
  358. * ant name/classname pairs from.
  359. * @param res the resource to use
  360. */
  361. public void setResource(String res) {
  362. if (definerSet) {
  363. tooManyDefinitions();
  364. }
  365. definerSet = true;
  366. this.resource = res;
  367. }
  368. /**
  369. * Name of the definition
  370. * @param name the name of the definition
  371. */
  372. public void setName(String name) {
  373. if (definerSet) {
  374. tooManyDefinitions();
  375. }
  376. definerSet = true;
  377. this.name = name;
  378. }
  379. /**
  380. * Returns the classname of the object we are defining.
  381. * May be <code>null</code>.
  382. * @return the class name
  383. */
  384. public String getClassname() {
  385. return classname;
  386. }
  387. /**
  388. * The full class name of the object being defined.
  389. * Required, unless file or resource have
  390. * been specified.
  391. * @param classname the name of the class
  392. */
  393. public void setClassname(String classname) {
  394. this.classname = classname;
  395. }
  396. /**
  397. * Set the class name of the adapter class.
  398. * An adapter class is used to proxy the
  399. * definition class. It is used if the
  400. * definition class is not assignable to
  401. * the adaptto class, or if the adaptto
  402. * class is not present.
  403. *
  404. * @param adapter the name of the adapter class
  405. */
  406. public void setAdapter(String adapter) {
  407. this.adapter = adapter;
  408. }
  409. /**
  410. * Set the adapter class.
  411. *
  412. * @param adapterClass the class to use to adapt the definition class
  413. */
  414. protected void setAdapterClass(Class adapterClass) {
  415. this.adapterClass = adapterClass;
  416. }
  417. /**
  418. * Set the classname of the class that the definition
  419. * must be compatible with, either directly or
  420. * by use of the adapeter class.
  421. *
  422. * @param adaptTo the name of the adaptto class
  423. */
  424. public void setAdaptTo(String adaptTo) {
  425. this.adaptTo = adaptTo;
  426. }
  427. /**
  428. * Set the class for adaptToClass, to be
  429. * used by derived classes, used instead of
  430. * the adaptTo attribute.
  431. *
  432. * @param adaptToClass the class for adapto.
  433. */
  434. protected void setAdaptToClass(Class adaptToClass) {
  435. this.adaptToClass = adaptToClass;
  436. }
  437. /**
  438. * Set the class loader, overrides the cpDelagate
  439. * classloader.
  440. *
  441. * @param classLoader a <code>ClassLoader</code> value
  442. */
  443. protected void setInternalClassLoader(ClassLoader classLoader) {
  444. this.internalClassLoader = classLoader;
  445. }
  446. /**
  447. * @see org.apache.tools.ant.Task#init()
  448. * @since Ant 1.6
  449. */
  450. public void init() throws BuildException {
  451. this.cpDelegate = ClasspathUtils.getDelegate(this);
  452. super.init();
  453. }
  454. /**
  455. * Add a definition using the attributes of Definer
  456. *
  457. * @param al the ClassLoader to use
  458. * @param name the name of the definition
  459. * @param classname the classname of the definition
  460. * @exception BuildException if an error occurs
  461. */
  462. protected void addDefinition(ClassLoader al, String name, String classname)
  463. throws BuildException {
  464. Class cl = null;
  465. try {
  466. try {
  467. if (onError != OnError.IGNORE) {
  468. cl = al.loadClass(classname);
  469. AntClassLoader.initializeClass(cl);
  470. }
  471. if (adapter != null) {
  472. adapterClass = al.loadClass(adapter);
  473. AntClassLoader.initializeClass(adapterClass);
  474. }
  475. if (adaptTo != null) {
  476. adaptToClass = al.loadClass(adaptTo);
  477. AntClassLoader.initializeClass(adaptToClass);
  478. }
  479. AntTypeDefinition def = new AntTypeDefinition();
  480. def.setName(name);
  481. def.setClassName(classname);
  482. def.setClass(cl);
  483. def.setAdapterClass(adapterClass);
  484. def.setAdaptToClass(adaptToClass);
  485. def.setClassLoader(al);
  486. if (cl != null) {
  487. def.checkClass(getProject());
  488. }
  489. ComponentHelper.getComponentHelper(getProject())
  490. .addDataTypeDefinition(def);
  491. } catch (ClassNotFoundException cnfe) {
  492. String msg = getTaskName() + " class " + classname
  493. + " cannot be found";
  494. throw new BuildException(msg, cnfe, getLocation());
  495. } catch (NoClassDefFoundError ncdfe) {
  496. String msg = getTaskName() + "A class needed by class "
  497. + classname + " cannot be found: " + ncdfe.getMessage();
  498. throw new BuildException(msg, ncdfe, location);
  499. }
  500. } catch (BuildException ex) {
  501. switch (onError) {
  502. case OnError.FAIL:
  503. throw ex;
  504. case OnError.REPORT:
  505. log(ex.getLocation() + "Warning: " + ex.getMessage(),
  506. Project.MSG_WARN);
  507. break;
  508. default:
  509. log(ex.getLocation() + ex.getMessage(),
  510. Project.MSG_DEBUG);
  511. }
  512. }
  513. }
  514. private void tooManyDefinitions() {
  515. throw new BuildException(
  516. "Only one of the attributes name,file,resource"
  517. + " can be set", getLocation());
  518. }
  519. }