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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * Copyright 2001-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 java.io.File;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.net.URL;
  22. import java.util.Map;
  23. import java.util.HashMap;
  24. import java.util.Enumeration;
  25. import java.util.Locale;
  26. import java.util.NoSuchElementException;
  27. import java.util.Properties;
  28. import org.apache.tools.ant.AntTypeDefinition;
  29. import org.apache.tools.ant.ComponentHelper;
  30. import org.apache.tools.ant.BuildException;
  31. import org.apache.tools.ant.Project;
  32. import org.apache.tools.ant.ProjectHelper;
  33. import org.apache.tools.ant.types.EnumeratedAttribute;
  34. /**
  35. * Base class for Taskdef and Typedef - handles all
  36. * the attributes for Typedef. The uri and class
  37. * handling is handled by DefBase
  38. *
  39. * @since Ant 1.4
  40. */
  41. public abstract class Definer extends DefBase {
  42. private static class ResourceStack extends ThreadLocal {
  43. public Object initialValue() {
  44. return new HashMap();
  45. }
  46. Map getStack() {
  47. return (Map) get();
  48. }
  49. }
  50. private static ResourceStack resourceStack = new ResourceStack();
  51. private String name;
  52. private String classname;
  53. private File file;
  54. private String resource;
  55. private int format = Format.PROPERTIES;
  56. private boolean definerSet = false;
  57. private int onError = OnError.FAIL;
  58. private String adapter;
  59. private String adaptTo;
  60. private Class adapterClass;
  61. private Class adaptToClass;
  62. /**
  63. * Enumerated type for onError attribute
  64. *
  65. * @see EnumeratedAttribute
  66. */
  67. public static class OnError extends EnumeratedAttribute {
  68. /** Enumerated values */
  69. public static final int FAIL = 0, REPORT = 1, IGNORE = 2, FAIL_ALL = 3;
  70. /**
  71. * Constructor
  72. */
  73. public OnError() {
  74. super();
  75. }
  76. /**
  77. * Constructor using a string.
  78. * @param value the value of the attribute
  79. */
  80. public OnError(String value) {
  81. setValue(value);
  82. }
  83. /**
  84. * get the values
  85. * @return an array of the allowed values for this attribute.
  86. */
  87. public String[] getValues() {
  88. return new String[] {"fail", "report", "ignore", "failall"};
  89. }
  90. }
  91. /**
  92. * Enumerated type for format attribute
  93. *
  94. * @see EnumeratedAttribute
  95. */
  96. public static class Format extends EnumeratedAttribute {
  97. /** Enumerated values */
  98. public static final int PROPERTIES = 0, XML = 1;
  99. /**
  100. * get the values
  101. * @return an array of the allowed values for this attribute.
  102. */
  103. public String[] getValues() {
  104. return new String[] {"properties", "xml"};
  105. }
  106. }
  107. /**
  108. * What to do if there is an error in loading the class.
  109. * <dl>
  110. * <li>error - throw build exception</li>
  111. * <li>report - output at warning level</li>
  112. * <li>ignore - output at debug level</li>
  113. * </dl>
  114. *
  115. * @param onError an <code>OnError</code> value
  116. */
  117. public void setOnError(OnError onError) {
  118. this.onError = onError.getIndex();
  119. }
  120. /**
  121. * Sets the format of the file or resource
  122. * @param format the enumerated value - xml or properties
  123. */
  124. public void setFormat(Format format) {
  125. this.format = format.getIndex();
  126. }
  127. /**
  128. * @return the name for this definition
  129. */
  130. public String getName() {
  131. return name;
  132. }
  133. /**
  134. * @return the file containing definitions
  135. */
  136. public File getFile() {
  137. return file;
  138. }
  139. /**
  140. * @return the resource containing definitions
  141. */
  142. public String getResource() {
  143. return resource;
  144. }
  145. /**
  146. * Run the definition.
  147. *
  148. * @exception BuildException if an error occurs
  149. */
  150. public void execute() throws BuildException {
  151. ClassLoader al = createLoader();
  152. if (!definerSet) {
  153. throw new BuildException(
  154. "name, file or resource attribute of "
  155. + getTaskName() + " is undefined", getLocation());
  156. }
  157. if (name != null) {
  158. if (classname == null) {
  159. throw new BuildException(
  160. "classname attribute of " + getTaskName() + " element "
  161. + "is undefined", getLocation());
  162. }
  163. addDefinition(al, name, classname);
  164. } else {
  165. if (classname != null) {
  166. String msg = "You must not specify classname "
  167. + "together with file or resource.";
  168. throw new BuildException(msg, getLocation());
  169. }
  170. Enumeration/*<URL>*/ urls = null;
  171. if (file != null) {
  172. final URL url = fileToURL();
  173. if (url == null) {
  174. return;
  175. }
  176. urls = new Enumeration() {
  177. private boolean more = true;
  178. public boolean hasMoreElements() {
  179. return more;
  180. }
  181. public Object nextElement() throws NoSuchElementException {
  182. if (more) {
  183. more = false;
  184. return url;
  185. } else {
  186. throw new NoSuchElementException();
  187. }
  188. }
  189. };
  190. } else {
  191. urls = resourceToURLs(al);
  192. }
  193. while (urls.hasMoreElements()) {
  194. URL url = (URL) urls.nextElement();
  195. int format = this.format;
  196. if (url.toString().toLowerCase(Locale.US).endsWith(".xml")) {
  197. format = Format.XML;
  198. }
  199. if (format == Format.PROPERTIES) {
  200. loadProperties(al, url);
  201. break;
  202. } else {
  203. if (resourceStack.getStack().get(url) != null) {
  204. log("Warning: Recursive loading of " + url
  205. + " ignored"
  206. + " at " + getLocation()
  207. + " originally loaded at "
  208. + resourceStack.getStack().get(url),
  209. Project.MSG_WARN);
  210. } else {
  211. try {
  212. resourceStack.getStack().put(url, getLocation());
  213. loadAntlib(al, url);
  214. } finally {
  215. resourceStack.getStack().remove(url);
  216. }
  217. }
  218. }
  219. }
  220. }
  221. }
  222. private URL fileToURL() {
  223. String message = null;
  224. if (!(file.exists())) {
  225. message = "File " + file + " does not exist";
  226. }
  227. if (message == null && !(file.isFile())) {
  228. message = "File " + file + " is not a file";
  229. }
  230. try {
  231. if (message == null) {
  232. return file.toURL();
  233. }
  234. } catch (Exception ex) {
  235. message =
  236. "File " + file + " cannot use as URL: "
  237. + ex.toString();
  238. }
  239. // Here if there is an error
  240. switch (onError) {
  241. case OnError.FAIL_ALL:
  242. throw new BuildException(message);
  243. case OnError.FAIL:
  244. // Fall Through
  245. case OnError.REPORT:
  246. log(message, Project.MSG_WARN);
  247. break;
  248. case OnError.IGNORE:
  249. // Fall Through
  250. default:
  251. // Ignore the problem
  252. break;
  253. }
  254. return null;
  255. }
  256. private Enumeration/*<URL>*/ resourceToURLs(ClassLoader classLoader) {
  257. Enumeration ret;
  258. try {
  259. ret = classLoader.getResources(resource);
  260. } catch (IOException e) {
  261. throw new BuildException(
  262. "Could not fetch resources named " + resource,
  263. e, getLocation());
  264. }
  265. if (!ret.hasMoreElements()) {
  266. String message = "Could not load definitions from resource "
  267. + resource + ". It could not be found.";
  268. switch (onError) {
  269. case OnError.FAIL_ALL:
  270. throw new BuildException(message);
  271. case OnError.FAIL:
  272. case OnError.REPORT:
  273. log(message, Project.MSG_WARN);
  274. break;
  275. case OnError.IGNORE:
  276. // Fall Through
  277. default:
  278. // Ignore the problem
  279. break;
  280. }
  281. }
  282. return ret;
  283. }
  284. /**
  285. * Load type definitions as properties from a url.
  286. *
  287. * @param al the classloader to use
  288. * @param url the url to get the definitions from
  289. */
  290. protected void loadProperties(ClassLoader al, URL url) {
  291. InputStream is = null;
  292. try {
  293. is = url.openStream();
  294. if (is == null) {
  295. log("Could not load definitions from " + url,
  296. Project.MSG_WARN);
  297. return;
  298. }
  299. Properties props = new Properties();
  300. props.load(is);
  301. Enumeration keys = props.keys();
  302. while (keys.hasMoreElements()) {
  303. name = ((String) keys.nextElement());
  304. classname = props.getProperty(name);
  305. addDefinition(al, name, classname);
  306. }
  307. } catch (IOException ex) {
  308. throw new BuildException(ex, getLocation());
  309. } finally {
  310. if (is != null) {
  311. try {
  312. is.close();
  313. } catch (IOException e) {
  314. // ignore
  315. }
  316. }
  317. }
  318. }
  319. /**
  320. * Load an antlib from a url.
  321. *
  322. * @param classLoader the classloader to use.
  323. * @param url the url to load the definitions from.
  324. */
  325. private void loadAntlib(ClassLoader classLoader, URL url) {
  326. try {
  327. Antlib antlib = Antlib.createAntlib(getProject(), url, getURI());
  328. antlib.setClassLoader(classLoader);
  329. antlib.setURI(getURI());
  330. antlib.perform();
  331. } catch (BuildException ex) {
  332. throw ProjectHelper.addLocationToBuildException(
  333. ex, getLocation());
  334. }
  335. }
  336. /**
  337. * Name of the property file to load
  338. * ant name/classname pairs from.
  339. * @param file the file
  340. */
  341. public void setFile(File file) {
  342. if (definerSet) {
  343. tooManyDefinitions();
  344. }
  345. definerSet = true;
  346. this.file = file;
  347. }
  348. /**
  349. * Name of the property resource to load
  350. * ant name/classname pairs from.
  351. * @param res the resource to use
  352. */
  353. public void setResource(String res) {
  354. if (definerSet) {
  355. tooManyDefinitions();
  356. }
  357. definerSet = true;
  358. this.resource = res;
  359. }
  360. /**
  361. * Name of the definition
  362. * @param name the name of the definition
  363. */
  364. public void setName(String name) {
  365. if (definerSet) {
  366. tooManyDefinitions();
  367. }
  368. definerSet = true;
  369. this.name = name;
  370. }
  371. /**
  372. * Returns the classname of the object we are defining.
  373. * May be <code>null</code>.
  374. * @return the class name
  375. */
  376. public String getClassname() {
  377. return classname;
  378. }
  379. /**
  380. * The full class name of the object being defined.
  381. * Required, unless file or resource have
  382. * been specified.
  383. * @param classname the name of the class
  384. */
  385. public void setClassname(String classname) {
  386. this.classname = classname;
  387. }
  388. /**
  389. * Set the class name of the adapter class.
  390. * An adapter class is used to proxy the
  391. * definition class. It is used if the
  392. * definition class is not assignable to
  393. * the adaptto class, or if the adaptto
  394. * class is not present.
  395. *
  396. * @param adapter the name of the adapter class
  397. */
  398. public void setAdapter(String adapter) {
  399. this.adapter = adapter;
  400. }
  401. /**
  402. * Set the adapter class.
  403. *
  404. * @param adapterClass the class to use to adapt the definition class
  405. */
  406. protected void setAdapterClass(Class adapterClass) {
  407. this.adapterClass = adapterClass;
  408. }
  409. /**
  410. * Set the classname of the class that the definition
  411. * must be compatible with, either directly or
  412. * by use of the adapter class.
  413. *
  414. * @param adaptTo the name of the adaptto class
  415. */
  416. public void setAdaptTo(String adaptTo) {
  417. this.adaptTo = adaptTo;
  418. }
  419. /**
  420. * Set the class for adaptToClass, to be
  421. * used by derived classes, used instead of
  422. * the adaptTo attribute.
  423. *
  424. * @param adaptToClass the class for adapto.
  425. */
  426. protected void setAdaptToClass(Class adaptToClass) {
  427. this.adaptToClass = adaptToClass;
  428. }
  429. /**
  430. * Add a definition using the attributes of Definer
  431. *
  432. * @param al the ClassLoader to use
  433. * @param name the name of the definition
  434. * @param classname the classname of the definition
  435. * @exception BuildException if an error occurs
  436. */
  437. protected void addDefinition(ClassLoader al, String name, String classname)
  438. throws BuildException {
  439. Class cl = null;
  440. try {
  441. try {
  442. name = ProjectHelper.genComponentName(getURI(), name);
  443. if (onError != OnError.IGNORE) {
  444. cl = Class.forName(classname, true, al);
  445. }
  446. if (adapter != null) {
  447. adapterClass = Class.forName(adapter, true, al);
  448. }
  449. if (adaptTo != null) {
  450. adaptToClass = Class.forName(adaptTo, true, al);
  451. }
  452. AntTypeDefinition def = new AntTypeDefinition();
  453. def.setName(name);
  454. def.setClassName(classname);
  455. def.setClass(cl);
  456. def.setAdapterClass(adapterClass);
  457. def.setAdaptToClass(adaptToClass);
  458. def.setClassLoader(al);
  459. if (cl != null) {
  460. def.checkClass(getProject());
  461. }
  462. ComponentHelper.getComponentHelper(getProject())
  463. .addDataTypeDefinition(def);
  464. } catch (ClassNotFoundException cnfe) {
  465. String msg = getTaskName() + " class " + classname
  466. + " cannot be found";
  467. throw new BuildException(msg, cnfe, getLocation());
  468. } catch (NoClassDefFoundError ncdfe) {
  469. String msg = getTaskName() + " A class needed by class "
  470. + classname + " cannot be found: " + ncdfe.getMessage();
  471. throw new BuildException(msg, ncdfe, getLocation());
  472. }
  473. } catch (BuildException ex) {
  474. switch (onError) {
  475. case OnError.FAIL_ALL:
  476. case OnError.FAIL:
  477. throw ex;
  478. case OnError.REPORT:
  479. log(ex.getLocation() + "Warning: " + ex.getMessage(),
  480. Project.MSG_WARN);
  481. break;
  482. default:
  483. log(ex.getLocation() + ex.getMessage(),
  484. Project.MSG_DEBUG);
  485. }
  486. }
  487. }
  488. private void tooManyDefinitions() {
  489. throw new BuildException(
  490. "Only one of the attributes name,file,resource"
  491. + " can be set", getLocation());
  492. }
  493. }