1 package org.apache.commons.jelly.tags.ant;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import org.apache.tools.ant.Project;
20 import org.apache.tools.ant.util.JavaEnvUtils;
21
22 import java.util.Enumeration;
23 import java.util.HashMap;
24 import java.util.Hashtable;
25 import java.util.Map;
26 import java.util.Properties;
27
28 /*** Implements the basic {@link org.apache.commons.jelly.tags.ant.PropsHandler} functionality
29 * against an existing map.
30 *
31 * <p>
32 * If extending <code>DefaultPropsHandler</code>, you can
33 * implement <code>setProperty</code>, <code>getProperty</code>,
34 * and <code>getProperties</code> to provide a complete
35 * implementation of <code>PropsHandler</code>.
36 *
37 * @author <a href="mailto:stephenh@chase3000.com">Stephen Haberman</a>
38 * @version $Revision: 155983 $
39 */
40 public class DefaultPropsHandler implements PropsHandler {
41
42 /*** A map of all of the properties. */
43 protected Map properties;
44
45 /*** A history of the properties marked as user properties. */
46 protected Map userProperties = new HashMap();
47
48 /*** A history of the properties makred as inherited properties. */
49 protected Map inheritedProperties = new HashMap();
50
51 /*** Initializes hte object with a blank set of properties.
52 */
53 public DefaultPropsHandler() {
54 this.properties = new HashMap();
55 }
56
57 /*** Initializes the object with a given <code>Map</code>
58 * implementation.
59 *
60 * @param properties The <code>Map</code> to use to store and retrieve properties.
61 */
62 public DefaultPropsHandler(Map properties) {
63 this.properties = properties;
64 }
65
66 /***
67 * @see PropsHandler#setProperty(String, String)
68 */
69 public void setProperty(String key, String value) {
70 this.properties.put(key, value);
71 }
72
73 /***
74 * @see PropsHandler#setUserProperty(String, String)
75 */
76 public void setUserProperty(String key, String value) {
77 this.userProperties.put(key, value);
78 this.setProperty(key, value);
79 }
80
81 /***
82 * @see PropsHandler#setNewProperty(String, String)
83 */
84 public void setNewProperty(String key, String value) {
85 if (this.getProperty(key) == null) {
86 this.setProperty(key, value);
87 }
88 }
89
90 /***
91 * @see PropsHandler#setInheritedProperty(String, String)
92 */
93 public void setInheritedProperty(String key, String value) {
94 this.inheritedProperties.put(key, value);
95 this.setUserProperty(key, value);
96 }
97
98 /***
99 * @see PropsHandler#setPropertyIfUndefinedByUser(String, String)
100 */
101 public void setPropertyIfUndefinedByUser(String key, String value) {
102 if (!this.getUserProperties().contains(key)) {
103 this.setProperty(key, value);
104 }
105 }
106
107 /***
108 * @see PropsHandler#getProperty(String)
109 */
110 public String getProperty(String key) {
111 if (key == null) {
112 return null;
113 }
114 return (String) this.properties.get(key);
115 }
116
117 /***
118 * @see PropsHandler#getUserProperty(String)
119 */
120 public String getUserProperty(String key) {
121 if (key == null) {
122 return null;
123 }
124 return (String) this.userProperties.get(key);
125 }
126
127 /***
128 * @see PropsHandler#getProperties()
129 */
130 public Hashtable getProperties() {
131 return new Hashtable(this.properties);
132 }
133
134 /***
135 * @see PropsHandler#getUserProperties()
136 */
137 public Hashtable getUserProperties() {
138 return new Hashtable(this.userProperties);
139 }
140
141 public Hashtable getInheritedProperties() {
142 return new Hashtable(this.inheritedProperties);
143 }
144
145 /***
146 * @see PropsHandler#copyUserProperties(Project)
147 */
148 public void copyUserProperties(Project other) {
149 Hashtable userProps = this.getUserProperties();
150 Hashtable inheritedProps = this.getInheritedProperties();
151
152 Enumeration e = userProps.keys();
153 while (e.hasMoreElements()) {
154 Object name = e.nextElement();
155 if (inheritedProps.contains(name)) {
156 continue;
157 }
158 Object value = userProps.get(name);
159 other.setUserProperty(name.toString(), value.toString());
160 }
161 }
162
163 /***
164 * @see PropsHandler#copyInheritedProperties(Project)
165 */
166 public void copyInheritedProperties(Project other) {
167 Hashtable inheritedProps = this.getInheritedProperties();
168
169 Enumeration e = inheritedProps.keys();
170 while (e.hasMoreElements()) {
171 String name = e.nextElement().toString();
172 if (other.getUserProperty(name) != null) {
173 continue;
174 }
175 Object value = inheritedProps.get(name);
176 other.setInheritedProperty(name, value.toString());
177 }
178 }
179
180 /***
181 * @see PropsHandler#setSystemProperties
182 */
183 public void setSystemProperties() {
184 Properties systemProps = System.getProperties();
185 Enumeration e = systemProps.keys();
186 while (e.hasMoreElements()) {
187 Object name = e.nextElement();
188 String value = systemProps.get(name).toString();
189 this.setPropertyIfUndefinedByUser(name.toString(), value);
190 }
191 }
192
193 /***
194 * @see PropsHandler#setJavaVersionProperty
195 */
196 public void setJavaVersionProperty() {
197 String javaVersion = JavaEnvUtils.getJavaVersion();
198 this.setPropertyIfUndefinedByUser("ant.java.version", javaVersion);
199 }
200 }