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

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