1 23 package com.sun.enterprise.repository; 24 25 import java.util.*; 26 import java.io.*; 27 import com.sun.enterprise.util.FileUtil; 28 import com.sun.enterprise.util.Utility; 29 import java.util.logging.*; 31 import com.sun.logging.*; 32 34 39 public class ConfigurationImpl implements Configuration { 40 private static Logger _logger=null; 42 static{ 43 _logger=LogDomains.getLogger(LogDomains.ROOT_LOGGER); 44 } 45 private static String OBJECT_STORE_DIR = "repository" + File.separator + 47 Utility.getLocalHost() +File.separator + "objects" 48 +File.separator; 49 50 54 55 private static final String OBJECT_FILE_EXT = ".ser"; 56 private Hashtable table; 57 private Repository defaultRepository; 58 59 63 public ConfigurationImpl() { 64 70 71 table = new Hashtable(); 72 defaultRepository = getRepository("default"); 73 76 } 77 78 private String getIndex(String key) { 79 int index = key.indexOf("."); 81 82 if(index < 0) 83 return "default"; 84 85 return key.substring(0, index); 86 } 87 88 private String getEffectiveKey(String key) { 89 int index = key.indexOf("."); 91 92 if(index < 0) 93 return key; 94 95 return key.substring(index + 1); 96 } 97 98 private Repository getRepository(String repName) { 99 Repository rep = (Repository) table.get(repName); 100 if(rep == null) { 101 rep = new Repository(repName); 102 if(rep.getName().equals(repName)) { 103 table.put(repName, rep); 104 } else 105 rep = defaultRepository; 106 } 107 108 return rep; 109 } 110 111 143 144 148 public String getProperty(String key) 149 throws java.rmi.RemoteException 150 { 151 String index = getIndex(key); 152 String newKey = getEffectiveKey(key); 153 154 Repository rep = getRepository(index); 155 156 String val = null; 157 158 if(rep.getName().equals(index)){ 159 val = rep.find(newKey); 160 } else { 161 val = rep.find(key); 162 } 163 164 165 return val; 166 } 167 168 171 public void setProperty(String key, String value) 172 throws java.rmi.RemoteException 173 { 174 String index = getIndex(key); 175 String newKey = getEffectiveKey(key); 176 Repository rep = getRepository(index); 177 178 if(rep.getName().equals(index)){ 179 rep.add(newKey, value); 180 } else { 181 rep.add(key, value); 182 } 183 } 184 185 188 public void removeProperty(String key) 189 throws java.rmi.RemoteException 190 { 191 String index = getIndex(key); 192 String newKey = getEffectiveKey(key); 193 Repository rep = getRepository(index); 194 195 if(rep.getName().equals(index)){ 196 rep.remove(newKey); 197 } else { 198 rep.remove(key); 199 } 200 } 201 202 203 public Object getObject(String key) 204 throws java.rmi.RemoteException 205 { 206 String fname = getProperty(key); 207 Object obj = null; 208 209 if(fname != null) { 211 try{ 212 FileInputStream fstream = new FileInputStream(fname); 213 ObjectInputStream objstream = 214 new ObjectInputStream(fstream); 215 obj = objstream.readObject(); 216 fstream.close(); 217 }catch(Exception e){ 218 _logger.log(Level.SEVERE,"enterprise.file_exception",e); 221 } 223 } 224 225 return obj; 226 } 227 228 231 public void setObject(String key, Object obj) 232 throws java.rmi.RemoteException 233 { 234 String className = obj.getClass().getName(); 235 String instanceId = String.valueOf(obj.hashCode()); 236 String fname = OBJECT_STORE_DIR + className + instanceId + 237 OBJECT_FILE_EXT; 238 239 241 try{ 242 String absFileName = FileUtil.getAbsolutePath(fname); 243 FileOutputStream fstream = new FileOutputStream(absFileName); 244 ObjectOutputStream objstream = new ObjectOutputStream(fstream); 245 objstream.writeObject(obj); 246 objstream.flush(); 247 fstream.close(); 248 setProperty(key, absFileName); 249 }catch(Exception e){ 250 _logger.log(Level.SEVERE,"enterprise.file_exception",e); 253 } 255 } 256 257 public void removeObject(String key) 258 throws java.rmi.RemoteException 259 { 260 String fname = getProperty(key); 261 262 if(fname != null) { 264 try{ 265 File file = new File(fname); 266 if(file.exists()) 267 file.delete(); 268 removeProperty(key); 269 }catch(Exception e){ 270 _logger.log(Level.SEVERE,"enterprise.file_exception",e); 273 } 275 } 276 } 277 278 282 public String [] getKeys(String index) throws java.rmi.RemoteException 283 { 284 Repository rep = getRepository(index); 285 286 return rep.keys(); 287 } 288 } 289 | Popular Tags |