1 |
|
---|
2 | /*
|
---|
3 | * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
|
---|
4 | *
|
---|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
6 | * you may not use this file except in compliance with the License.
|
---|
7 | * You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 | package ise.antelope.tasks;
|
---|
18 |
|
---|
19 | import java.io.*;
|
---|
20 | import java.lang.reflect.Field;
|
---|
21 | import java.util.*;
|
---|
22 | import org.apache.tools.ant.*;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Unsets a property.
|
---|
26 | *
|
---|
27 | * @author Dale Anson
|
---|
28 | * @version $Revision: 1.1 $
|
---|
29 | * @since Ant 1.6
|
---|
30 | */
|
---|
31 | public class Unset extends Task {
|
---|
32 |
|
---|
33 | private String name = null;
|
---|
34 | private File file = null;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Set the name of the property to unset. Required.
|
---|
38 | *
|
---|
39 | * @param name the name of the property to unset.
|
---|
40 | */
|
---|
41 | public void setName(String name) {
|
---|
42 | this.name = name;
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Sets the file attribute of the Unset object
|
---|
47 | *
|
---|
48 | * @param file The new file value
|
---|
49 | */
|
---|
50 | public void setFile(File file) {
|
---|
51 | this.file = file;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Execute all nested tasks, repeating.
|
---|
56 | *
|
---|
57 | * @exception BuildException Description of the Exception
|
---|
58 | */
|
---|
59 | public void execute() throws BuildException {
|
---|
60 | if (name == null && file == null)
|
---|
61 | throw new BuildException("Must have either the 'name' or 'file' attribute with 'unset'.");
|
---|
62 | if (name != null && name.equals(""))
|
---|
63 | throw new BuildException("Must have non-empty 'name' attribute with 'unset'.");
|
---|
64 |
|
---|
65 | Properties file_props = null;
|
---|
66 | if (file != null) {
|
---|
67 | file_props = new Properties();
|
---|
68 | try {
|
---|
69 | file_props.load(new FileInputStream(file));
|
---|
70 | }
|
---|
71 | catch(FileNotFoundException fnfe) {
|
---|
72 | throw new BuildException("File not found: " + file.toString());
|
---|
73 | }
|
---|
74 | catch (Exception e) {
|
---|
75 | throw new BuildException(e.getMessage());
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | Hashtable properties = null;
|
---|
80 | // Ant 1.5 stores properties in Project, remove project property
|
---|
81 | try {
|
---|
82 | properties = (Hashtable) getValue(getProject(), "properties");
|
---|
83 | removeProperties(properties, name, file_props);
|
---|
84 | }
|
---|
85 | catch (Exception ignored) {
|
---|
86 | // ignore, could be Ant 1.6
|
---|
87 | }
|
---|
88 | // Ant 1.5 stores properties in Project, remove user properties
|
---|
89 | try {
|
---|
90 | properties = (Hashtable) getValue(getProject(), "userProperties");
|
---|
91 | removeProperties(properties, name, file_props);
|
---|
92 | }
|
---|
93 | catch (Exception ignored) {
|
---|
94 | // ignore, could be Ant 1.6
|
---|
95 | }
|
---|
96 |
|
---|
97 | // Ant 1.6 uses a PropertyHelper, can check for it by checking for a
|
---|
98 | // reference to "ant.PropertyHelper"
|
---|
99 | try {
|
---|
100 | Object property_helper = getProject().getReference("ant.PropertyHelper");
|
---|
101 | if (property_helper != null) {
|
---|
102 | // remove project properties
|
---|
103 | try {
|
---|
104 | properties = (Hashtable) getValue(property_helper, "properties");
|
---|
105 | removeProperties(properties, name, file_props);
|
---|
106 | }
|
---|
107 | catch (Exception ignored) {
|
---|
108 | }
|
---|
109 | // remove user properties
|
---|
110 | try {
|
---|
111 | properties = (Hashtable) getValue(property_helper, "userProperties");
|
---|
112 | removeProperties(properties, name, file_props);
|
---|
113 | }
|
---|
114 | catch (Exception ignored) {
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 | catch (Exception ignored) {
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Description of the Method
|
---|
124 | *
|
---|
125 | * @param properties
|
---|
126 | * @param name
|
---|
127 | * @param file_props
|
---|
128 | */
|
---|
129 | private void removeProperties(Hashtable properties, String name, Properties file_props) {
|
---|
130 | if (properties != null) {
|
---|
131 | if (name != null)
|
---|
132 | properties.remove(name);
|
---|
133 | else if (file_props != null) {
|
---|
134 | Enumeration keys = file_props.keys();
|
---|
135 | while (keys.hasMoreElements()) {
|
---|
136 | String key = (String) keys.nextElement();
|
---|
137 | properties.remove(key);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Get a field from a class. Works up through the inheritance chain until
|
---|
145 | * the field is found.
|
---|
146 | *
|
---|
147 | * @param fieldName The field to find
|
---|
148 | * @param someClass
|
---|
149 | * @return The field
|
---|
150 | * @exception NoSuchFieldException Darn, no such field.
|
---|
151 | */
|
---|
152 | private Field getField(Class someClass, String fieldName) throws NoSuchFieldException {
|
---|
153 | if (someClass == null) {
|
---|
154 | throw new NoSuchFieldException("Invalid field : " + fieldName);
|
---|
155 | }
|
---|
156 | try {
|
---|
157 | return someClass.getDeclaredField(fieldName);
|
---|
158 | }
|
---|
159 | catch (NoSuchFieldException e) {
|
---|
160 | return getField(someClass.getSuperclass(), fieldName);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Get a value from a field in an object.
|
---|
167 | *
|
---|
168 | * @param instance the object instance
|
---|
169 | * @param fieldName the name of the field
|
---|
170 | * @return an object representing the value of
|
---|
171 | * the field
|
---|
172 | * @exception IllegalAccessException foiled by the security manager
|
---|
173 | * @exception NoSuchFieldException Darn, no such field.
|
---|
174 | */
|
---|
175 | private Object getValue(Object instance, String fieldName)
|
---|
176 | throws IllegalAccessException, NoSuchFieldException {
|
---|
177 | Field field = getField(instance.getClass(), fieldName);
|
---|
178 | field.setAccessible(true);
|
---|
179 | return field.get(instance);
|
---|
180 | }
|
---|
181 |
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|