View Javadoc

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