Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/util/MergeProperties.java


1   /** 
2    * 
3    * Copyright 2004 Protique Ltd
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   **/
18  
19  package org.activemq.util;
20  
21  /**
22   *
23   * Task to merge the Jmeter properties
24   *
25   */
26  
27  import java.io.File;
28  import java.io.BufferedReader;
29  import java.io.FileNotFoundException;
30  import java.io.FileReader;
31  import java.io.IOException;
32  import java.util.ArrayList;
33  import java.io.FileOutputStream;
34  
35  
36  
37  /**
38   * This class is used by an ant task that will merge the jmeter property files.
39   *
40   */
41  public class MergeProperties {
42  
43       //  The file to merge properties into
44      protected File baseFile;
45      //  The file to pull the properties from
46      protected File mergeFile;
47  
48      public File getBaseFile() {
49          return baseFile;
50      }
51  
52      public void setBaseFile(File baseFile) {
53          this.baseFile = baseFile;
54      }
55  
56      public File getMergeFile() {
57          return mergeFile;
58      }
59  
60      public void setMergeFile(File mergeFile) {
61          this.mergeFile = mergeFile;
62      }
63  
64      public void mergePropertyFiles() throws FileNotFoundException, IOException {
65  
66          if (!getBaseFile().exists()) {
67            throw new FileNotFoundException("Could not find file:" + getBaseFile());
68          }
69  
70          if (!getMergeFile().exists()) {
71            throw new FileNotFoundException("Could not find file:" + getMergeFile());
72          }
73  
74          BufferedReader mergeReader = new BufferedReader(new FileReader(mergeFile));
75          BufferedReader baseReader = new BufferedReader(new FileReader(baseFile));
76  
77          ArrayList baseList = new ArrayList(1024);
78          ArrayList mergeList = new ArrayList(1024);
79  
80          String line = null;
81          while ((line = mergeReader.readLine()) != null) {
82              mergeList.add(line);
83          }
84          while ((line = baseReader.readLine()) != null) {
85              baseList.add(line);
86          }
87  
88          if(previouslyMerged(baseList, mergeList)){
89  
90              for(int i = 0; i < mergeList.size(); i++){
91                  int j = baseList.indexOf(mergeList.get(i));
92  
93                  if(j == -1){
94                      //check if key of mergeList.get(i) is in baseList
95                      String mergeKey = (String) mergeList.get(i);
96  
97                      if(mergeKey.indexOf('=') > -1){
98                          mergeKey = getKey(mergeKey);
99                      }
100 
101                     for(int k=0; k < baseList.size(); k++){
102                         String baseKey = (String) baseList.get(k);
103 
104                         if(baseKey.indexOf('=') > -1){
105                             baseKey = getKey(baseKey);
106                         }
107 
108                         if (mergeKey.equals(baseKey)){
109                             j=k;
110                         }
111                     }
112 
113                     if(j > -1){
114                         baseList.set(j, mergeList.get(i));
115                     } else {
116                         baseList.add(mergeList.get(i));
117                     }
118                 }
119             }
120         } else {
121             baseList.addAll((java.util.Collection) mergeList);
122         }
123         // write baseList to file
124         FileOutputStream writer = new FileOutputStream(baseFile);
125         writer.flush();
126         for (int i = 0; i < baseList.size(); i++)
127         {
128             //System.out.println((String) baseList.get(i));
129             writer.write(((String) baseList.get(i)).getBytes());
130             writer.write(System.getProperty("line.separator", "\r\n").getBytes());
131             writer.flush();
132         }
133 
134         baseReader.close();
135         mergeReader.close();
136         writer.close();
137 
138     }
139 
140     public static void main(String[] args) throws Exception {
141         MergeProperties mergeProperties = new MergeProperties();
142 
143         try
144         {
145             if (args.length < 2)
146             {
147                 System.out.println("Usage: java OverwriteProperties c:/temp/File1.props c:/temp/File2.props");
148                 System.out.println("Usage: File1 will be modified, new parameters from File 2 will be added,");
149                 throw new Exception("Incorrect number of arguments supplied");
150             }
151             mergeProperties.setBaseFile(new File(args[0]));
152             mergeProperties.setMergeFile(new File(args[1]));
153 
154             mergeProperties.mergePropertyFiles();
155 
156         }
157         catch (FileNotFoundException ex)
158         {
159             System.err.println(ex.getMessage());
160         }
161         catch (IOException ex)
162         {
163             System.err.println(ex.getMessage());
164         }
165         catch (SecurityException ex)
166         {
167             System.err.println(ex.getMessage());
168         }
169     }
170 
171     public boolean previouslyMerged(ArrayList base, ArrayList merge) {
172 
173         return (base.contains(merge.get(0)));
174     }
175 
176     public String getKey(String prop) {
177 
178         return (prop.substring(0, prop.indexOf('=')));
179     }
180 
181 }