1 23 package org.enhydra.util; 24 25 import java.io.File ; 26 import java.io.FileNotFoundException ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.OutputStream ; 30 import java.util.Hashtable ; 31 import java.util.Vector ; 32 33 import com.lutris.util.Config; 34 import com.lutris.util.ConfigException; 35 import com.lutris.util.KeywordValueException; 36 import com.lutris.util.KeywordValueTable; 37 38 46 47 public abstract class AbsConfigFile implements ConfigFileInterface{ 48 49 52 protected Config config; 53 54 57 protected Vector order; 58 59 62 protected Hashtable comments; 63 64 67 protected File file = null; 68 69 72 protected JNDIAdapter jndiAdapt = null; 73 76 protected Hashtable jndiParameterNames = null; 77 78 81 public AbsConfigFile () { 82 config = new Config(); 83 order = new Vector (); 84 comments = new Hashtable (); 85 jndiParameterNames = new Hashtable (); 86 } 87 88 93 97 98 105 public AbsConfigFile (File file) throws ConfigException, IOException { 106 this(); 107 108 this.file = file; 111 try { 112 readJndi(); 113 } catch (Exception e){} 114 config.setConfigFile(this); 115 } 116 117 122 public AbsConfigFile(KeywordValueTable kvt) throws ConfigException { 123 config = new Config(kvt); 124 order = new Vector (); 125 comments = new Hashtable (); 126 jndiParameterNames = new Hashtable (); 127 } 128 129 133 134 protected void readJndi() throws ConfigException {} 135 136 140 public Config getConfig() { 141 return config; 142 } 143 144 151 public String getComment(String key) { 152 return (String )comments.get(key); 153 } 154 155 163 public void addEntry(String key, String [] values, String comment) 164 throws KeywordValueException { 165 166 if (!key.equals(TRAILING_COMMENT)) { 168 config.set(key, values); 169 try { 170 if (jndiAdapt != null) { 171 String jndiName = JNDIAdapter.makeContextString(key); 172 String jndiValue = JNDIAdapter.makeStringFromStrings(values); 173 jndiAdapt.set(jndiName+"[]", jndiValue); 174 } 175 } 176 catch (Exception ex){ 177 System.err.println("Error in addEntry method of AbsConfigFile"); 178 } 179 if (!order.contains(key)) { 180 order.addElement(key); 181 } 182 } 183 comments.put(key, comment); 184 } 185 186 194 public void addEntry(String kkey, String value, String comment) 195 throws KeywordValueException { 196 if (!kkey.equals(TRAILING_COMMENT)) { 198 config.set(kkey, value); 199 try { 200 if (jndiAdapt != null) { 201 String jndiName = JNDIAdapter.makeContextString(kkey); 202 jndiAdapt.set(jndiName, value); 203 } 204 } 205 catch (Exception ex){ 206 System.err.println("Error in addEntry method of AbsConfigFile"); 207 } 208 if (!order.contains(kkey)) { 209 order.addElement(kkey); 210 } 211 } 212 comments.put(kkey, comment); 213 } 214 215 220 public void removeEntry(String key) throws KeywordValueException { 221 if (!key.equals(TRAILING_COMMENT)) { 223 config.remove(key); 224 if (jndiAdapt != null) { 225 String jndiName = JNDIAdapter.makeContextString(key); 226 try { 227 jndiAdapt.remove(jndiName); 228 } 229 catch (Exception ex){ 230 System.err.println("Error in removeEntry method of AbsConfigFile 1"); 231 } 232 try { 233 jndiAdapt.remove(jndiName+"[]"); 234 } 235 catch (Exception ex){ 236 System.err.println("Error in removeEntry method of AbsConfigFile 2"); 237 } 238 } 239 240 order.removeElement(key); 241 } 242 comments.remove(key); 243 } 244 245 250 public File getFile() { 251 return file; 252 } 253 254 260 public void setFile(File file) { 261 this.file = file; 262 try { 263 readJndi(); 264 } catch (Exception e){} 265 } 266 267 273 public void write() throws IOException , FileNotFoundException { 274 if (file == null) { 275 throw new FileNotFoundException ("No file associated with this object"); 276 } 277 FileOutputStream out = new FileOutputStream (file); 278 write(out); 279 out.close(); 280 } 281 282 286 public abstract void write(OutputStream outputStream); 287 } | Popular Tags |