KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > utilities > postinstall > ConfigParams


1 /*
2  * ConfigParams.java
3  *
4  * Created on June 12, 2003, 2:43 PM
5  */

6
7 package com.quikj.application.utilities.postinstall;
8
9 import java.util.*;
10 import java.io.*;
11 /**
12  *
13  * @author amit
14  */

15 public class ConfigParams
16 {
17     private ArrayList list = new ArrayList();
18     
19     /** Holds value of property errorMessage. */
20     private String JavaDoc errorMessage = "";
21     
22     /** Creates a new instance of ConfigParams */
23     public ConfigParams()
24     {
25     }
26     
27     public void put(ConfigElement element)
28     {
29         int index = findIndex(element.getParamName());
30         if (index == -1)
31         {
32             list.add(element);
33         }
34         else
35         {
36             list.set(index, element);
37         }
38     }
39     
40     private int findIndex(String JavaDoc name)
41     {
42         Iterator iter = list.iterator();
43         int i = 0;
44         while (iter.hasNext() == true)
45         {
46             ConfigElement element = (ConfigElement)iter.next();
47             if (element.getParamName().equals(name) == true)
48             {
49                 return i;
50             }
51             i++;
52         }
53         return -1;
54     }
55     
56     public ConfigElement find(String JavaDoc name)
57     {
58         Iterator iter = list.iterator();
59         while (iter.hasNext() == true)
60         {
61             ConfigElement element = (ConfigElement)iter.next();
62             if (element.getParamName().equals(name) == true)
63             {
64                 return element;
65             }
66         }
67         
68         return null;
69     }
70     
71     public void remove(String JavaDoc name)
72     {
73         ConfigElement e = find(name);
74         if (e != null)
75         {
76             list.remove(e);
77         }
78     }
79     
80     
81     public boolean replace(String JavaDoc base, String JavaDoc suffix, ScreenPrinterInterface out)
82     {
83         try
84         {
85             File top = new File(base);
86             if (top.isDirectory() == false)
87             {
88                 errorMessage = "The top-level folder " + base + " is not a folder";
89                 return false;
90             }
91             
92             // get a list of the files
93
ArrayList file_list = new ArrayList();
94             list(top, suffix, file_list);
95             
96             // start the replacement
97
Iterator iter = list.iterator();
98             while (iter.hasNext() == true)
99             {
100                 ConfigElement e = (ConfigElement)iter.next();
101                 String JavaDoc replace_with = e.getReplacePattern();
102                 if (replace_with == null)
103                 {
104                     continue;
105                 }
106                 
107                 ReplaceString rep = new ReplaceString();
108                 rep.setStringToReplace(replace_with);
109                 rep.setStringNewValue(e.getParamValue());
110               
111                 Iterator it = file_list.iterator();
112                 while (it.hasNext() == true)
113                 {
114                     rep.addFile((String JavaDoc)it.next());
115                 }
116                 
117                 rep.replace();
118                 
119                 String JavaDoc error = rep.getErrorMessage();
120                 if (error.length() > 0)
121                 {
122                     out.println("Error replacing values for " + e.getParamName() + ": "
123                     + error);
124                     continue;
125                 }
126                 
127                 out.println("Replaced value for " + e.getParamName() + " in "
128                 + rep.getNumReplaced() + " places");
129             }
130             
131             return true;
132         }
133         catch (Exception JavaDoc ex)
134         {
135             errorMessage = "IO error occured while configuring system files";
136             return false;
137         }
138     }
139     
140     private void list(File base, String JavaDoc suffix, ArrayList list)
141     throws FileNotFoundException, IOException
142     {
143         File[] listing = base.listFiles();
144         for (int i = 0; i < listing.length; i++)
145         {
146             if ((listing[i].getName().equals(".") == true) ||
147             (listing[i].getName().equals("..") == true))
148             {
149                 continue;
150             }
151             
152             if (listing[i].isDirectory() == true)
153             {
154                 list(listing[i], suffix, list);
155                 continue;
156             }
157             
158             // it is a file
159
if (listing[i].getName().endsWith(suffix) == true)
160             {
161                 String JavaDoc orig_abs = listing[i].getAbsolutePath();
162                 String JavaDoc config_abs = orig_abs.substring(0,
163                 (orig_abs.length() - suffix.length()));
164                 // remove the suffix and store in the list
165
list.add(config_abs);
166                 
167                 if ((new File(config_abs)).exists() == true)
168                 {
169                     FileUtils.copy (config_abs, config_abs + ".old");
170                 }
171                 
172                 // copy the content of the orig file to the actual config file
173
FileUtils.copy(orig_abs, config_abs);
174             }
175         }
176     }
177     
178     
179     /** Getter for property errorMessage.
180      * @return Value of property errorMessage.
181      *
182      */

183     public String JavaDoc getErrorMessage()
184     {
185         return this.errorMessage;
186     }
187     
188     /** Getter for property list.
189      * @return Value of property list.
190      *
191      */

192     public java.util.ArrayList JavaDoc getList()
193     {
194         return list;
195     }
196
197     public boolean save(String JavaDoc folder, String JavaDoc filename)
198     {
199         Properties properties = new Properties();
200         Iterator iter = list.iterator();
201         
202         while (iter.hasNext() == true)
203         {
204             ConfigElement e = (ConfigElement)iter.next();
205             
206             int d = e.getDisplay();
207             
208             properties.setProperty(e.getParamName(), e.getParamValue());
209         }
210         
211         // open a file under the ace_home folder and save the data
212

213         try
214         {
215             File file = new File (folder, filename);
216             FileOutputStream fos = new FileOutputStream(file);
217             properties.store(fos, "Ace Operator Install Parameters");
218             fos.close();
219         }
220         catch (IOException ex)
221         {
222             return false;
223         }
224         
225         return true;
226     }
227     
228     public String JavaDoc displayEnteredValues()
229     {
230         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
231         Iterator iter = list.iterator();
232         
233         while (iter.hasNext() == true)
234         {
235             ConfigElement e = (ConfigElement)iter.next();
236             
237             int d = e.getDisplay();
238             
239             if (d != ConfigElement.DISPLAY_NONE)
240             {
241                 buffer.append(e.getParamName() + " : ");
242                 
243                 
244                 if (d == ConfigElement.DISPLAY_ALL)
245                 {
246                     buffer.append(e.getParamValue());
247                 }
248                 else
249                 {
250                     int len = e.getParamValue().length();
251                     for (int i = 0; i < len; i++)
252                     {
253                         buffer.append('*');
254                     }
255                 }
256                 
257                 buffer.append("\n");
258             }
259         }
260         
261         return buffer.toString();
262     }
263 }
264
Popular Tags