KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > common > InitConfFileBean


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.common;
25
26 import java.util.*;
27 import java.io.*;
28 import com.sun.enterprise.instance.InstanceEnvironment;
29
30 // i18n import
31
import com.sun.enterprise.admin.util.SOMLocalStringsManager;
32
33 public class InitConfFileBean
34 {
35     public final static String JavaDoc INITCONF_SECURITY_ATTRIBUTE = "Security";
36     public final static String JavaDoc INITCONF_VALUE_ON = "on";
37     public final static String JavaDoc INITCONF_VALUE_OFF = "off";
38
39     private List storage ;
40     private List init_storage ;
41     private int position;
42     private int init_position;
43     private Hashtable index;
44     private Hashtable init_index;
45     private String JavaDoc mag_file;
46
47     // i18n SOMLocalStringsManager
48
private static SOMLocalStringsManager localizedStrMgr =
49             SOMLocalStringsManager.getManager( InitConfFileBean.class );
50     
51     public InitConfFileBean()
52     {
53         storage = Collections.synchronizedList( new LinkedList());
54         init_storage = Collections.synchronizedList( new LinkedList());
55         index = new Hashtable();
56         init_index = new Hashtable();
57         position=0;
58         init_position = 0;
59     }
60     
61     public void initialise(String JavaDoc instanceName, boolean bBackupFile) throws IOException
62     {
63         InstanceEnvironment environ = new InstanceEnvironment(instanceName);
64         String JavaDoc confFile;
65         confFile = environ.getInitFilePath();
66         initialize(confFile);
67     }
68     
69     
70     private void initialize(String JavaDoc confFile) throws IOException
71     {
72         mag_file = confFile;
73         storage = Collections.synchronizedList( new LinkedList());
74         index = new Hashtable();
75         position=0;
76         init_storage = Collections.synchronizedList( new LinkedList());
77         init_index = new Hashtable();
78         init_position=0;
79         
80         readConfig(confFile);
81     }
82     
83     public void readConfig(String JavaDoc fileName) throws IOException
84     {
85         File inputFile = new File(fileName) ;
86         BufferedReader in = new BufferedReader( new FileReader(inputFile));
87         String JavaDoc inLine = null;
88         int spaceIndex=0;
89         while ( (inLine = in.readLine() )!= null )
90         {
91             if ( inLine.length() > 2 )
92             {
93                 spaceIndex = inLine.indexOf(" ");
94                 if ((spaceIndex == -1) && !(inLine.startsWith("Init"))) {
95                     String JavaDoc msg = localizedStrMgr.getString( "admin.common.wrong_filename_format" );
96                     throw new IOException( msg );
97                 }
98                 String JavaDoc key;
99                 if (!inLine.startsWith("Init"))
100                     key=inLine.substring(0,spaceIndex);
101                 else
102                     key="Init";
103                 
104                 if (!key.equals("Init"))
105                 {
106                     Hashtable temp = new Hashtable();
107                     temp.put(key, inLine.substring(spaceIndex +1 ));
108                     synchronized(storage)
109                     {
110                         storage.add(temp);
111                     }
112                     index.put(key,new Integer JavaDoc(position));
113                     position++;
114                 }
115                 else
116                 {
117                     String JavaDoc tempString = "" ;
118                     if (spaceIndex != -1)
119                         tempString = inLine.substring(spaceIndex + 1);
120                     Hashtable temp = new Hashtable();
121                     while (inLine != null)
122                     {
123                         in.mark(500);
124                         inLine = in.readLine();
125                         if (inLine != null)
126                         {
127                             if ((inLine.startsWith("\t")) ||
128                             (inLine.startsWith(" ")))
129                             {
130                                 if (inLine.startsWith("\t"))
131                                 {
132                                     inLine.replace('\t', ' ');
133                                 }
134                                 tempString += inLine;
135                             }
136                             else
137                             {
138                                 in.reset();
139                                 break;
140                             }
141                         }
142                     }
143                     temp.put(key, tempString);
144                     synchronized(init_storage)
145                     {
146                         init_storage.add(temp);
147                     }
148                     init_index.put(key,new Integer JavaDoc(position));
149                     init_position++;
150                 }
151                 
152             }
153         }
154         
155     }
156     
157     public void writeConfig(String JavaDoc fileName) throws IOException
158     {
159         PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
160         synchronized( storage)
161         {
162             ListIterator storageIter = storage.listIterator(0);
163             while(storageIter.hasNext() )
164             {
165                 Hashtable temp_store = ( Hashtable) (storageIter.next());
166                 Enumeration e = temp_store.keys();
167                 while ( e.hasMoreElements() )
168                 {
169                     String JavaDoc temp_key =(String JavaDoc ) e.nextElement();
170                     String JavaDoc temp_val = ( String JavaDoc ) temp_store.get(temp_key) ;
171                     String JavaDoc temp_value= temp_key+" "+temp_val;
172                     
173                     if (temp_val != null && !temp_val.trim().equals(""))
174                         out.println(temp_value);
175                 }
176                 out.flush();
177             }
178             out.println();
179         }
180         synchronized( init_storage)
181         {
182             ListIterator storageIter = init_storage.listIterator(0);
183             while(storageIter.hasNext() )
184             {
185                 Hashtable temp_store = ( Hashtable) (storageIter.next());
186                 Enumeration e = temp_store.keys();
187                 while ( e.hasMoreElements() )
188                 {
189                     String JavaDoc temp_key =(String JavaDoc ) e.nextElement();
190                     String JavaDoc temp_val = (String JavaDoc ) temp_store.get(temp_key) ;
191                     String JavaDoc temp_value= temp_key+" "+temp_val;
192                     
193                     if (temp_val != null && !temp_val.trim().equals(""))
194                         out.println(temp_value);
195                     //out.println(temp_val);
196
}
197                 out.flush();
198             }
199         }
200         out.close();
201     }
202     
203     public void dump() throws IOException
204     {
205         writeConfig(mag_file);
206     }
207     
208     private int searchIndex( String JavaDoc varName )
209     {
210         int indexHash = -1;
211         
212         Object JavaDoc o = index.get(varName);
213         if (o != null)
214             indexHash = (( Integer JavaDoc)(index.get(varName))).intValue();
215         return indexHash;
216     }
217     private synchronized void remake_index(Hashtable table )
218     {
219         int in_size = 0;
220         Enumeration e = table.keys();
221         while(e.hasMoreElements() )
222         {
223             table.put(e.nextElement(),new Integer JavaDoc(in_size));
224             in_size++;
225         }
226     }
227     private synchronized void syncIndex(String JavaDoc varName , int mode )
228     {
229         switch (mode )
230         {
231             case 0 :
232                 if ( index.containsKey(varName ) )
233                 {
234                     index.remove(varName);
235                     this.remake_index(index);
236                 }
237                 break;
238                 
239             default :
240                 if ( !index.containsKey(varName ) )
241                 {
242                     index.put(varName,new Integer JavaDoc(storage.size() - 1));
243                 }
244                 break;
245         }
246     }
247     public String JavaDoc get_mag_var( String JavaDoc varName )
248     {
249         int iterIndex = this.searchIndex(varName);
250         String JavaDoc retVal= "";
251         if (iterIndex != -1)
252             synchronized(storage)
253             {
254                 retVal =( String JavaDoc ) (( Hashtable) (storage.get(iterIndex))).get(varName);
255             }
256             return retVal;
257     }
258     public void set_mag_var( String JavaDoc varName , String JavaDoc value )
259     {
260         int iterIndex = this.searchIndex(varName);
261         Hashtable temp = new Hashtable();
262         temp.put(varName,value);
263         if (iterIndex != -1)
264         {
265             synchronized(storage)
266             {
267                 storage.set(iterIndex,temp);
268             }
269         }
270         else
271             synchronized(storage)
272             {
273                 storage.add(temp);
274                 this.syncIndex(varName , 1);
275             }
276     }
277     
278     public static void main( String JavaDoc[] args ) throws IOException
279     {
280     /*
281     MagObj testObj = new MagObj();
282     testObj.readConfig("magnus.conf", "sRoot");
283     testObj.set_mag_var("Security","On");
284     testObj.writeConfig("magnus.conf");
285      */

286     }
287     
288     public String JavaDoc isSelected(String JavaDoc varName, String JavaDoc val)
289     {
290         String JavaDoc value = this.get_mag_var(varName);
291         if (value.equals(val))
292             return "SELECTED";
293         else
294             return "";
295     }
296 }
297
298
299
Popular Tags