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.

Property.java 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 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.*;
  56. import org.apache.tools.ant.types.Reference;
  57. import java.io.*;
  58. import java.util.*;
  59. /**
  60. * Will set a Project property. Used to be a hack in ProjectHelper
  61. * Will not override values set by the command line or parent projects.
  62. *
  63. * @author costin@dnt.ro
  64. * @author Sam Ruby <rubys@us.ibm.com>
  65. * @author Glenn McAllister <glennm@ca.ibm.com>
  66. */
  67. public class Property extends Task {
  68. protected String name;
  69. protected String value;
  70. protected File file;
  71. protected String resource;
  72. protected Reference ref = null;
  73. protected boolean userProperty=false; // set read-only properties
  74. public void setName(String name) {
  75. this.name = name;
  76. }
  77. public String getName() {
  78. return name;
  79. }
  80. public void setLocation(File location) {
  81. setValue(location.getAbsolutePath());
  82. }
  83. public void setValue(String value) {
  84. this.value = value;
  85. }
  86. public String getValue() {
  87. return value;
  88. }
  89. public void setFile(File file) {
  90. this.file = file;
  91. }
  92. public File getFile() {
  93. return file;
  94. }
  95. public void setRefid(Reference ref) {
  96. this.ref = ref;
  97. }
  98. public Reference getRefid() {
  99. return ref;
  100. }
  101. public void setResource(String resource) {
  102. this.resource = resource;
  103. }
  104. public String getResource() {
  105. return resource;
  106. }
  107. public void setUserProperty(boolean userProperty) {
  108. this.userProperty = userProperty;
  109. }
  110. public String toString() {
  111. return value == null ? "" : value;
  112. }
  113. public void execute() throws BuildException {
  114. try {
  115. if ((name != null) && (value != null)) {
  116. addProperty(name, value);
  117. }
  118. if (file != null) loadFile(file);
  119. if (resource != null) loadResource(resource);
  120. if ((name != null) && (ref != null)) {
  121. Object obj = ref.getReferencedObject(getProject());
  122. if (obj != null) {
  123. addProperty(name, obj.toString());
  124. }
  125. }
  126. } catch (Exception e) {
  127. throw new BuildException(e, location);
  128. }
  129. }
  130. protected void loadFile (File file) throws BuildException {
  131. Properties props = new Properties();
  132. log("Loading " + file.getAbsolutePath(), Project.MSG_VERBOSE);
  133. try {
  134. if (file.exists()) {
  135. FileInputStream fis = new FileInputStream(file);
  136. try {
  137. props.load(fis);
  138. } finally {
  139. if (fis != null) {
  140. fis.close();
  141. }
  142. }
  143. addProperties(props);
  144. } else {
  145. log("Unable to find " + file.getAbsolutePath(),
  146. Project.MSG_VERBOSE);
  147. }
  148. } catch(Exception ex) {
  149. throw new BuildException(ex.getMessage(), ex, location);
  150. }
  151. }
  152. protected void loadResource( String name ) {
  153. Properties props = new Properties();
  154. log("Resource Loading " + name, Project.MSG_VERBOSE);
  155. try {
  156. ClassLoader cL = this.getClass().getClassLoader();
  157. InputStream is = null;
  158. if (cL == null) {
  159. is = ClassLoader.getSystemResourceAsStream(name);
  160. } else {
  161. is = cL.getResourceAsStream(name);
  162. }
  163. if (is != null) {
  164. props.load(is);
  165. addProperties(props);
  166. } else {
  167. log("Unable to find resource " + name, Project.MSG_WARN);
  168. }
  169. } catch (Exception ex) {
  170. throw new BuildException(ex, location);
  171. }
  172. }
  173. protected void addProperties(Properties props) {
  174. resolveAllProperties(props);
  175. Enumeration e = props.keys();
  176. while (e.hasMoreElements()) {
  177. String name = (String) e.nextElement();
  178. String value = (String) props.getProperty(name);
  179. String v = ProjectHelper.replaceProperties(project, value, project.getProperties());
  180. addProperty(name, value);
  181. }
  182. }
  183. protected void addProperty(String n, String v) {
  184. if( userProperty ) {
  185. if (project.getUserProperty(n) == null) {
  186. project.setUserProperty(n, v);
  187. } else {
  188. log("Override ignored for " + n, Project.MSG_VERBOSE);
  189. }
  190. } else {
  191. if (project.getProperty(n) == null) {
  192. project.setProperty(n, v);
  193. } else {
  194. log("Override ignored for " + n, Project.MSG_VERBOSE);
  195. }
  196. }
  197. }
  198. private void resolveAllProperties(Hashtable props) {
  199. Hashtable unresolvableProperties = new Hashtable();
  200. for (Enumeration e = props.keys(); e.hasMoreElements(); ) {
  201. String name = (String) e.nextElement();
  202. String value = (String) props.get(name);
  203. boolean resolved = false;
  204. while (!resolved) {
  205. Vector propsInValue = new Vector();
  206. // assume it will be resolved
  207. resolved = true;
  208. boolean unresolvable = false;
  209. if (extractProperties(value, propsInValue)) {
  210. for (int i=0; i < propsInValue.size(); i++) {
  211. String elem = (String) propsInValue.elementAt(i);
  212. if (elem.equals(name) || unresolvableProperties.containsKey(elem)) {
  213. // we won't try further resolving elements with circular
  214. // property dependencies or dependencies on unresolvable elements
  215. unresolvable = true;
  216. break;
  217. }
  218. if (project.getProperties().containsKey(elem) ||
  219. props.containsKey(elem)) {
  220. resolved = false;
  221. }
  222. }
  223. }
  224. if (unresolvable) {
  225. unresolvableProperties.put(name, value);
  226. resolved = true;
  227. }
  228. if (!resolved) {
  229. value = ProjectHelper.replaceProperties(project, value,
  230. project.getProperties());
  231. value = ProjectHelper.replaceProperties(project, value, props);
  232. props.put(name, value);
  233. }
  234. }
  235. }
  236. }
  237. private boolean extractProperties(String source, Vector properties) {
  238. // This is an abreviated version of
  239. // ProjectHelper.replaceProperties method
  240. int i=0;
  241. int prev=0;
  242. int pos;
  243. while( (pos=source.indexOf( "$", prev )) >= 0 ) {
  244. if( pos == (source.length() - 1)) {
  245. prev = pos + 1;
  246. } else if (source.charAt( pos + 1 ) != '{' ) {
  247. prev=pos+2;
  248. } else {
  249. int endName=source.indexOf( '}', pos );
  250. String n=source.substring( pos+2, endName );
  251. properties.addElement(n);
  252. prev=endName+1;
  253. }
  254. }
  255. return (properties.size() > 0);
  256. }
  257. }