1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.util;
18
19 import java.io.File;
20 import java.io.BufferedReader;
21 import java.io.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.FileReader;
24 import java.io.IOException;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.StringTokenizer;
29
30 /***
31 * Task to overwrite Properties: used for JRP, TRP and Torque.properties
32 *
33 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
34 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
35 * @created January 29, 2003
36 * @version $Id: OverwriteProperties.java 516448 2007-03-09 16:25:47Z ate $
37 */
38 public class OverwriteProperties
39 {
40 /*** The file to merge properties into */
41 protected File baseProperties;
42 /*** The file to pull the properties from */
43 protected File properties;
44 /*** The directory to look in for include files */
45 protected File includeRoot;
46
47 /*** Description of the Field */
48 public boolean verbose = false;
49
50 /*** An array of all the properties */
51 protected ArrayList baseArray = new ArrayList(1024);
52 /*** An array of all the properties that will be removed */
53 protected ArrayList removeArray = new ArrayList(128);
54 /*** Description of the Field */
55 protected HashMap baseMap = new HashMap();
56 /*** What to use as a line seperator */
57 protected String lineSeparator = System.getProperty("line.separator", "\r\n");
58
59
60 /***
61 * Sets the file to merge properties into
62 *
63 * @param baseProperties The file path to merge properties into
64 */
65 public void setBaseProperties(File baseProperties)
66 {
67 this.baseProperties = baseProperties;
68 }
69
70
71 /***
72 * Sets the file to pull properties from
73 *
74 * @param properties The file path to the pull the merge properties from
75 */
76 public void setProperties(File properties)
77 {
78 this.properties = properties;
79 }
80
81
82 /***
83 * Sets the directory to look for includes in.
84 *
85 * @param includeRoot the directory to look in.
86 */
87 public void setIncludeRoot(File includeRoot)
88 {
89 this.includeRoot = includeRoot;
90 }
91
92
93 /***
94 * Sets whether to output extra debugging info
95 *
96 * @param verbose The new verbose value
97 */
98 public void setVerbose(boolean verbose)
99 {
100 this.verbose = verbose;
101 }
102
103
104 /***
105 * Return the file to merge propertie into
106 *
107 * @return The baseProperties value
108 */
109 public File getBaseProperties()
110 {
111 return baseProperties;
112 }
113
114
115 /***
116 * Gets the properties attribute of the OverwriteProperties object
117 *
118 * @return The properties value
119 */
120 public File getProperties()
121 {
122 return properties;
123 }
124
125
126 /***
127 * Gets the includeRoot attribute of the OverwriteProperties object
128 *
129 * @return The includeRoot value
130 */
131 public File getIncludeRoot()
132 {
133 return includeRoot;
134 }
135
136
137 /***
138 * Gets the verbose attribute of the OverwriteProperties object
139 *
140 * @return The verbose value
141 */
142 public boolean getVerbose()
143 {
144 return verbose;
145 }
146
147
148 /***
149 * The main program for the OverwriteProperties class
150 *
151 * @param args The command line arguments
152 * @exception Exception Description of the Exception
153 */
154 public static void main(String[] args)
155 throws Exception
156 {
157 OverwriteProperties overwriteProperties = new OverwriteProperties();
158
159 try
160 {
161 if (args.length < 3)
162 {
163 System.out.println("Usage: java OverwriteProperties c:/temp/File1.props c:/temp/File2.props c:/include-root/");
164 System.out.println("Usage: File1 will be modified, new parameters from File 2 will be added,");
165 System.out.println(
166 "Usage: and same parameters will be updated. The include-root is where include files are found.");
167 throw new Exception("Incorrect number of arguments supplied");
168 }
169 overwriteProperties.setBaseProperties(new File(args[0]));
170 overwriteProperties.setProperties(new File(args[1]));
171 overwriteProperties.setIncludeRoot(new File(args[2]));
172
173 overwriteProperties.execute();
174
175 }
176 catch (FileNotFoundException ex)
177 {
178 System.err.println(ex.getMessage());
179 }
180 catch (IOException ex)
181 {
182 System.err.println(ex.getMessage());
183 }
184 catch (SecurityException ex)
185 {
186 System.err.println(ex.getMessage());
187 }
188 }
189
190
191 /*** Description of the Method */
192 public void execute() throws FileNotFoundException, IOException, SecurityException
193 {
194
195 if (verbose)
196 {
197 System.out.println("Merging into file " + getBaseProperties() + " file " + getProperties());
198 }
199
200 if (!getBaseProperties().exists())
201 {
202 throw new FileNotFoundException("Could not find file:" + getBaseProperties());
203 }
204
205 if (!getProperties().exists())
206 {
207 throw new FileNotFoundException("Could not find file:" + getProperties());
208 }
209
210 if (!getIncludeRoot().exists() || !getIncludeRoot().isDirectory())
211 {
212 throw new FileNotFoundException("Could not find directory:" + getIncludeRoot());
213 }
214
215 BufferedReader reader = new BufferedReader(new FileReader(baseProperties));
216 int index = 0;
217 String key = null;
218 String line = null;
219 while ((line = reader.readLine()) != null)
220 {
221 StringTokenizer tokenizer = new StringTokenizer(line, "=");
222 baseArray.add(index, line);
223 if (verbose)
224 {
225 System.out.println("While reading baseArray[" + index + "] = " + line);
226 }
227 if (tokenizer.countTokens() >= 1
228 && !line.startsWith("#")
229 && !line.startsWith("include")
230 && !line.startsWith("module.packages"))
231 {
232 key = tokenizer.nextToken().trim();
233 if (key != null && key.length() > 0)
234 {
235 baseMap.put(key, new Integer(index));
236 if (verbose)
237 {
238 System.out.println("baseMap[" + key + "," + index + "]");
239 }
240 }
241 }
242 index++;
243 }
244 reader.close();
245 if (verbose)
246 {
247 System.out.println("\nOverwrite with Delta\n");
248 }
249
250 readProperties(properties, index);
251
252 boolean flags[] = removeProperties();
253
254 baseArray.trimToSize();
255 writeToFile(flags);
256
257
258 }
259
260
261 /***
262 * Description of the Method
263 *
264 * @param flags Description of the Parameter
265 * @exception FileNotFoundException Description of the Exception
266 * @exception IOException Description of the Exception
267 */
268 public void writeToFile(boolean[] flags)
269 throws FileNotFoundException, IOException
270 {
271 FileOutputStream writer = new FileOutputStream(baseProperties);
272 writer.flush();
273 for (int i = 0; i < baseArray.size(); i++)
274 {
275 if (true == flags[i])
276 {
277 if (verbose)
278 {
279 System.out.println("Skipping property[" + i + "] = " + baseArray.get(i));
280 }
281 continue;
282 }
283 if (verbose)
284 {
285 System.out.println("Writing property[" + i + "] = " + baseArray.get(i));
286 }
287 writer.write(((String) baseArray.get(i)).getBytes());
288 writer.write(lineSeparator.getBytes());
289 writer.flush();
290 }
291 writer.close();
292
293 }
294
295
296 /***
297 * Description of the Method
298 *
299 * @return Description of the Return Value
300 */
301 public boolean[] removeProperties()
302 {
303
304 boolean flags[] = new boolean[baseArray.size()];
305
306 for (int i = 0; i < baseArray.size(); i++)
307 {
308 flags[i] = false;
309 }
310 for (int ix = 0; ix < removeArray.size(); ix++)
311 {
312 String prefix = (String) removeArray.get(ix);
313 for (int iy = 0; iy < baseArray.size(); iy++)
314 {
315 String line = (String) baseArray.get(iy);
316 if (line.startsWith(prefix))
317 {
318 flags[iy] = true;
319 if (verbose)
320 {
321 System.out.println("flagging removal of property: " + line);
322 }
323 }
324 }
325 }
326 return flags;
327 }
328
329
330 /***
331 * Reads in the properties from the specified file
332 *
333 * @param propFile Description of the Parameter
334 * @param index Description of the Parameter
335 * @exception FileNotFoundException Description of the Exception
336 * @exception IOException Description of the Exception
337 */
338 public void readProperties(File propFile, int index)
339 throws FileNotFoundException, IOException
340 {
341 BufferedReader reader = new BufferedReader(new FileReader(propFile));
342 String key = null;
343 String line = null;
344
345 while ((line = reader.readLine()) != null)
346 {
347 StringTokenizer tokenizer = new StringTokenizer(line, "=");
348
349 int count = tokenizer.countTokens();
350 if (count == 2 && line.startsWith("include"))
351 {
352 key = tokenizer.nextToken().trim();
353 File includeFile = new File(includeRoot + tokenizer.nextToken().trim());
354 if (verbose)
355 {
356 System.out.println("include File = " + includeFile);
357 }
358 readProperties(includeFile, index);
359 continue;
360 }
361 if (count >= 1 && line.startsWith("module.packages"))
362 {
363 baseArray.add(index, line);
364 if (verbose)
365 {
366 System.out.println("Adding module.package to baseArray[" + index + "] = " + line);
367 }
368 index++;
369
370 key = line.trim();
371 if (baseMap.containsKey(key))
372 {
373 int ix = ((Integer) baseMap.get(key)).intValue();
374 baseArray.set(ix, line);
375 if (verbose)
376 {
377 System.out.println("Resetting baseArray[" + ix + "] = " + line);
378 }
379 }
380
381 continue;
382 }
383 if (count >= 1 && line.startsWith("-"))
384 {
385
386
387 String prefix = line.trim().substring(1);
388 removeArray.add(prefix);
389 if (verbose)
390 {
391 System.out.println("Flagging for removal = " + line);
392 }
393 continue;
394 }
395 if (count >= 1 && !line.startsWith("#"))
396 {
397 key = tokenizer.nextToken().trim();
398 if (key != null && key.length() > 0)
399 {
400 if (baseMap.containsKey(key))
401 {
402 int ix = ((Integer) baseMap.get(key)).intValue();
403 baseArray.set(ix, line);
404 if (verbose)
405 {
406 System.out.println("Resetting baseArray[" + ix + "] = " + line);
407 }
408 }
409 else
410 {
411 baseArray.add(index, line);
412 if (verbose)
413 {
414 System.out.println("Adding new entry to baseArray[" + index + "] = " + line);
415 }
416 baseMap.put(key, new Integer(index));
417 if (verbose)
418 {
419 System.out.println("baseMap[" + key + "," + index + "]");
420 }
421 index++;
422 }
423 }
424 }
425
426 }
427 reader.close();
428
429 }
430
431 }