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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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", "Tomcat", 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 setValue(String value) {
  81. this.value = value;
  82. }
  83. public String getValue() {
  84. return value;
  85. }
  86. public void setFile(File file) {
  87. this.file = file;
  88. }
  89. public File getFile() {
  90. return file;
  91. }
  92. public void setRefid(Reference ref) {
  93. this.ref = ref;
  94. }
  95. public Reference getRefid() {
  96. return ref;
  97. }
  98. public void setResource(String resource) {
  99. this.resource = resource;
  100. }
  101. public String getResource() {
  102. return resource;
  103. }
  104. public void setUserProperty(boolean userProperty) {
  105. this.userProperty = userProperty;
  106. }
  107. public String toString() {
  108. return value == null ? "" : value;
  109. }
  110. public void execute() throws BuildException {
  111. try {
  112. if ((name != null) && (value != null)) {
  113. addProperty(name, value);
  114. }
  115. if (file != null) loadFile(file);
  116. if (resource != null) loadResource(resource);
  117. if ((name != null) && (ref != null)) {
  118. Object obj = ref.getReferencedObject(getProject());
  119. if (obj != null) {
  120. addProperty(name, obj.toString());
  121. }
  122. }
  123. } catch (Exception e) {
  124. throw new BuildException(e, location);
  125. }
  126. }
  127. protected void loadFile (File file) throws BuildException {
  128. Properties props = new Properties();
  129. log("Loading " + file.getAbsolutePath(), Project.MSG_VERBOSE);
  130. try {
  131. if (file.exists()) {
  132. FileInputStream fis = new FileInputStream(file);
  133. try {
  134. props.load(fis);
  135. } finally {
  136. if (fis != null) {
  137. fis.close();
  138. }
  139. }
  140. addProperties(props);
  141. } else {
  142. log("Unable to find " + file.getAbsolutePath(),
  143. Project.MSG_VERBOSE);
  144. }
  145. } catch(Exception ex) {
  146. throw new BuildException(ex.getMessage(), ex, location);
  147. }
  148. }
  149. protected void loadResource( String name ) {
  150. Properties props = new Properties();
  151. log("Resource Loading " + name, Project.MSG_VERBOSE);
  152. try {
  153. InputStream is = this.getClass().getResourceAsStream(name);
  154. if (is != null) {
  155. props.load(is);
  156. addProperties(props);
  157. }
  158. } catch (Exception ex) {
  159. ex.printStackTrace();
  160. }
  161. }
  162. protected void addProperties(Properties props) {
  163. resolveAllProperties(props);
  164. Enumeration e = props.keys();
  165. while (e.hasMoreElements()) {
  166. String name = (String) e.nextElement();
  167. String value = (String) props.getProperty(name);
  168. String v = ProjectHelper.replaceProperties(value, project.getProperties());
  169. addProperty(name, value);
  170. }
  171. }
  172. protected void addProperty(String n, String v) {
  173. if( userProperty ) {
  174. if (project.getUserProperty(n) == null) {
  175. project.setUserProperty(n, v);
  176. } else {
  177. log("Override ignored for " + n, Project.MSG_VERBOSE);
  178. }
  179. } else {
  180. if (project.getProperty(n) == null) {
  181. project.setProperty(n, v);
  182. } else {
  183. log("Override ignored for " + n, Project.MSG_VERBOSE);
  184. }
  185. }
  186. }
  187. private void resolveAllProperties(Hashtable props) {
  188. Hashtable unresolvableProperties = new Hashtable();
  189. for (Enumeration e = props.keys(); e.hasMoreElements(); ) {
  190. String name = (String) e.nextElement();
  191. String value = (String) props.get(name);
  192. boolean resolved = false;
  193. while (!resolved) {
  194. Vector propsInValue = new Vector();
  195. // assume it will be resolved
  196. resolved = true;
  197. boolean unresolvable = false;
  198. if (extractProperties(value, propsInValue)) {
  199. for (int i=0; i < propsInValue.size(); i++) {
  200. String elem = (String) propsInValue.elementAt(i);
  201. if (elem.equals(name) || unresolvableProperties.containsKey(elem)) {
  202. // we won't try further resolving elements with circular
  203. // property dependencies or dependencies on unresolvable elements
  204. unresolvable = true;
  205. break;
  206. }
  207. if (project.getProperties().containsKey(elem) ||
  208. props.containsKey(elem)) {
  209. resolved = false;
  210. }
  211. }
  212. }
  213. if (unresolvable) {
  214. unresolvableProperties.put(name, value);
  215. resolved = true;
  216. }
  217. if (!resolved) {
  218. value = ProjectHelper.replaceProperties(value,
  219. project.getProperties());
  220. value = ProjectHelper.replaceProperties(value, props);
  221. props.put(name, value);
  222. }
  223. }
  224. }
  225. }
  226. private boolean extractProperties(String source, Vector properties) {
  227. // This is an abreviated version of
  228. // ProjectHelper.replaceProperties method
  229. int i=0;
  230. int prev=0;
  231. int pos;
  232. while( (pos=source.indexOf( "$", prev )) >= 0 ) {
  233. if( pos == (source.length() - 1)) {
  234. prev = pos + 1;
  235. } else if (source.charAt( pos + 1 ) != '{' ) {
  236. prev=pos+2;
  237. } else {
  238. int endName=source.indexOf( '}', pos );
  239. String n=source.substring( pos+2, endName );
  240. properties.addElement(n);
  241. prev=endName+1;
  242. }
  243. }
  244. return (properties.size() > 0);
  245. }
  246. }