001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.util;
018:
019: import java.io.File;
020: import java.io.BufferedReader;
021: import java.io.FileNotFoundException;
022: import java.io.FileOutputStream;
023: import java.io.FileReader;
024: import java.io.IOException;
025:
026: import java.util.ArrayList;
027: import java.util.HashMap;
028: import java.util.StringTokenizer;
029:
030: /**
031: * Task to overwrite Properties: used for JRP, TRP and Torque.properties
032: *
033: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
034: * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
035: * @created January 29, 2003
036: * @version $Id: OverwriteProperties.java 516448 2007-03-09 16:25:47Z ate $
037: */
038: public class OverwriteProperties {
039: /** The file to merge properties into */
040: protected File baseProperties;
041: /** The file to pull the properties from */
042: protected File properties;
043: /** The directory to look in for include files */
044: protected File includeRoot;
045:
046: /** Description of the Field */
047: public boolean verbose = false;
048:
049: /** An array of all the properties */
050: protected ArrayList baseArray = new ArrayList(1024);
051: /** An array of all the properties that will be removed */
052: protected ArrayList removeArray = new ArrayList(128);
053: /** Description of the Field */
054: protected HashMap baseMap = new HashMap();
055: /** What to use as a line seperator */
056: protected String lineSeparator = System.getProperty(
057: "line.separator", "\r\n");
058:
059: /**
060: * Sets the file to merge properties into
061: *
062: * @param baseProperties The file path to merge properties into
063: */
064: public void setBaseProperties(File baseProperties) {
065: this .baseProperties = baseProperties;
066: }
067:
068: /**
069: * Sets the file to pull properties from
070: *
071: * @param properties The file path to the pull the merge properties from
072: */
073: public void setProperties(File properties) {
074: this .properties = properties;
075: }
076:
077: /**
078: * Sets the directory to look for includes in.
079: *
080: * @param includeRoot the directory to look in.
081: */
082: public void setIncludeRoot(File includeRoot) {
083: this .includeRoot = includeRoot;
084: }
085:
086: /**
087: * Sets whether to output extra debugging info
088: *
089: * @param verbose The new verbose value
090: */
091: public void setVerbose(boolean verbose) {
092: this .verbose = verbose;
093: }
094:
095: /**
096: * Return the file to merge propertie into
097: *
098: * @return The baseProperties value
099: */
100: public File getBaseProperties() {
101: return baseProperties;
102: }
103:
104: /**
105: * Gets the properties attribute of the OverwriteProperties object
106: *
107: * @return The properties value
108: */
109: public File getProperties() {
110: return properties;
111: }
112:
113: /**
114: * Gets the includeRoot attribute of the OverwriteProperties object
115: *
116: * @return The includeRoot value
117: */
118: public File getIncludeRoot() {
119: return includeRoot;
120: }
121:
122: /**
123: * Gets the verbose attribute of the OverwriteProperties object
124: *
125: * @return The verbose value
126: */
127: public boolean getVerbose() {
128: return verbose;
129: }
130:
131: /**
132: * The main program for the OverwriteProperties class
133: *
134: * @param args The command line arguments
135: * @exception Exception Description of the Exception
136: */
137: public static void main(String[] args) throws Exception {
138: OverwriteProperties overwriteProperties = new OverwriteProperties();
139:
140: try {
141: if (args.length < 3) {
142: System.out
143: .println("Usage: java OverwriteProperties c:/temp/File1.props c:/temp/File2.props c:/include-root/");
144: System.out
145: .println("Usage: File1 will be modified, new parameters from File 2 will be added,");
146: System.out
147: .println("Usage: and same parameters will be updated. The include-root is where include files are found.");
148: throw new Exception(
149: "Incorrect number of arguments supplied");
150: }
151: overwriteProperties.setBaseProperties(new File(args[0]));
152: overwriteProperties.setProperties(new File(args[1]));
153: overwriteProperties.setIncludeRoot(new File(args[2]));
154:
155: overwriteProperties.execute();
156:
157: } catch (FileNotFoundException ex) {
158: System.err.println(ex.getMessage());
159: } catch (IOException ex) {
160: System.err.println(ex.getMessage());
161: } catch (SecurityException ex) {
162: System.err.println(ex.getMessage());
163: }
164: }
165:
166: /** Description of the Method */
167: public void execute() throws FileNotFoundException, IOException,
168: SecurityException {
169:
170: if (verbose) {
171: System.out.println("Merging into file "
172: + getBaseProperties() + " file " + getProperties());
173: }
174:
175: if (!getBaseProperties().exists()) {
176: throw new FileNotFoundException("Could not find file:"
177: + getBaseProperties());
178: }
179:
180: if (!getProperties().exists()) {
181: throw new FileNotFoundException("Could not find file:"
182: + getProperties());
183: }
184:
185: if (!getIncludeRoot().exists()
186: || !getIncludeRoot().isDirectory()) {
187: throw new FileNotFoundException("Could not find directory:"
188: + getIncludeRoot());
189: }
190:
191: BufferedReader reader = new BufferedReader(new FileReader(
192: baseProperties));
193: int index = 0;
194: String key = null;
195: String line = null;
196: while ((line = reader.readLine()) != null) {
197: StringTokenizer tokenizer = new StringTokenizer(line, "=");
198: baseArray.add(index, line);
199: if (verbose) {
200: System.out.println("While reading baseArray[" + index
201: + "] = " + line);
202: }
203: if (tokenizer.countTokens() >= 1 && !line.startsWith("#")
204: && !line.startsWith("include")
205: && !line.startsWith("module.packages")) {
206: key = tokenizer.nextToken().trim();
207: if (key != null && key.length() > 0) {
208: baseMap.put(key, new Integer(index));
209: if (verbose) {
210: System.out.println("baseMap[" + key + ","
211: + index + "]");
212: }
213: }
214: }
215: index++;
216: }
217: reader.close();
218: if (verbose) {
219: System.out.println("\nOverwrite with Delta\n");
220: }
221:
222: readProperties(properties, index);
223:
224: boolean flags[] = removeProperties();
225:
226: baseArray.trimToSize();
227: writeToFile(flags);
228:
229: }
230:
231: /**
232: * Description of the Method
233: *
234: * @param flags Description of the Parameter
235: * @exception FileNotFoundException Description of the Exception
236: * @exception IOException Description of the Exception
237: */
238: public void writeToFile(boolean[] flags)
239: throws FileNotFoundException, IOException {
240: FileOutputStream writer = new FileOutputStream(baseProperties);
241: writer.flush();
242: for (int i = 0; i < baseArray.size(); i++) {
243: if (true == flags[i]) {
244: if (verbose) {
245: System.out.println("Skipping property[" + i
246: + "] = " + baseArray.get(i));
247: }
248: continue;
249: }
250: if (verbose) {
251: System.out.println("Writing property[" + i + "] = "
252: + baseArray.get(i));
253: }
254: writer.write(((String) baseArray.get(i)).getBytes());
255: writer.write(lineSeparator.getBytes());
256: writer.flush();
257: }
258: writer.close();
259:
260: }
261:
262: /**
263: * Description of the Method
264: *
265: * @return Description of the Return Value
266: */
267: public boolean[] removeProperties() {
268:
269: boolean flags[] = new boolean[baseArray.size()];
270:
271: for (int i = 0; i < baseArray.size(); i++) {
272: flags[i] = false;
273: }
274: for (int ix = 0; ix < removeArray.size(); ix++) {
275: String prefix = (String) removeArray.get(ix);
276: for (int iy = 0; iy < baseArray.size(); iy++) {
277: String line = (String) baseArray.get(iy);
278: if (line.startsWith(prefix)) {
279: flags[iy] = true;
280: if (verbose) {
281: System.out
282: .println("flagging removal of property: "
283: + line);
284: }
285: }
286: }
287: }
288: return flags;
289: }
290:
291: /**
292: * Reads in the properties from the specified file
293: *
294: * @param propFile Description of the Parameter
295: * @param index Description of the Parameter
296: * @exception FileNotFoundException Description of the Exception
297: * @exception IOException Description of the Exception
298: */
299: public void readProperties(File propFile, int index)
300: throws FileNotFoundException, IOException {
301: BufferedReader reader = new BufferedReader(new FileReader(
302: propFile));
303: String key = null;
304: String line = null;
305:
306: while ((line = reader.readLine()) != null) {
307: StringTokenizer tokenizer = new StringTokenizer(line, "=");
308:
309: int count = tokenizer.countTokens();
310: if (count == 2 && line.startsWith("include")) {
311: key = tokenizer.nextToken().trim();
312: File includeFile = new File(includeRoot
313: + tokenizer.nextToken().trim());
314: if (verbose) {
315: System.out.println("include File = " + includeFile);
316: }
317: readProperties(includeFile, index);
318: continue;
319: }
320: if (count >= 1 && line.startsWith("module.packages")) {
321: baseArray.add(index, line);
322: if (verbose) {
323: System.out
324: .println("Adding module.package to baseArray["
325: + index + "] = " + line);
326: }
327: index++;
328:
329: key = line.trim();
330: if (baseMap.containsKey(key)) {
331: int ix = ((Integer) baseMap.get(key)).intValue();
332: baseArray.set(ix, line);
333: if (verbose) {
334: System.out.println("Resetting baseArray[" + ix
335: + "] = " + line);
336: }
337: }
338:
339: continue;
340: }
341: if (count >= 1 && line.startsWith("-")) {
342: // remove from base
343:
344: String prefix = line.trim().substring(1);
345: removeArray.add(prefix);
346: if (verbose) {
347: System.out
348: .println("Flagging for removal = " + line);
349: }
350: continue;
351: }
352: if (count >= 1 && !line.startsWith("#")) {
353: key = tokenizer.nextToken().trim();
354: if (key != null && key.length() > 0) {
355: if (baseMap.containsKey(key)) {
356: int ix = ((Integer) baseMap.get(key))
357: .intValue();
358: baseArray.set(ix, line);
359: if (verbose) {
360: System.out.println("Resetting baseArray["
361: + ix + "] = " + line);
362: }
363: } else {
364: baseArray.add(index, line);
365: if (verbose) {
366: System.out
367: .println("Adding new entry to baseArray["
368: + index + "] = " + line);
369: }
370: baseMap.put(key, new Integer(index));
371: if (verbose) {
372: System.out.println("baseMap[" + key + ","
373: + index + "]");
374: }
375: index++;
376: }
377: }
378: }
379:
380: }
381: reader.close();
382:
383: }
384:
385: }
|