1 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 import com.sun.enterprise.admin.util.SOMLocalStringsManager; 32 33 public class InitConfFileBean 34 { 35 public final static String INITCONF_SECURITY_ATTRIBUTE = "Security"; 36 public final static String INITCONF_VALUE_ON = "on"; 37 public final static String 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 mag_file; 46 47 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 instanceName, boolean bBackupFile) throws IOException 62 { 63 InstanceEnvironment environ = new InstanceEnvironment(instanceName); 64 String confFile; 65 confFile = environ.getInitFilePath(); 66 initialize(confFile); 67 } 68 69 70 private void initialize(String 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 fileName) throws IOException 84 { 85 File inputFile = new File(fileName) ; 86 BufferedReader in = new BufferedReader( new FileReader(inputFile)); 87 String 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 msg = localizedStrMgr.getString( "admin.common.wrong_filename_format" ); 96 throw new IOException( msg ); 97 } 98 String 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 (position)); 113 position++; 114 } 115 else 116 { 117 String 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 (position)); 149 init_position++; 150 } 151 152 } 153 } 154 155 } 156 157 public void writeConfig(String 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 temp_key =(String ) e.nextElement(); 170 String temp_val = ( String ) temp_store.get(temp_key) ; 171 String 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 temp_key =(String ) e.nextElement(); 190 String temp_val = (String ) temp_store.get(temp_key) ; 191 String temp_value= temp_key+" "+temp_val; 192 193 if (temp_val != null && !temp_val.trim().equals("")) 194 out.println(temp_value); 195 } 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 varName ) 209 { 210 int indexHash = -1; 211 212 Object o = index.get(varName); 213 if (o != null) 214 indexHash = (( Integer )(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 (in_size)); 224 in_size++; 225 } 226 } 227 private synchronized void syncIndex(String 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 (storage.size() - 1)); 243 } 244 break; 245 } 246 } 247 public String get_mag_var( String varName ) 248 { 249 int iterIndex = this.searchIndex(varName); 250 String retVal= ""; 251 if (iterIndex != -1) 252 synchronized(storage) 253 { 254 retVal =( String ) (( Hashtable) (storage.get(iterIndex))).get(varName); 255 } 256 return retVal; 257 } 258 public void set_mag_var( String varName , String 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 [] args ) throws IOException 279 { 280 286 } 287 288 public String isSelected(String varName, String val) 289 { 290 String 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 |