1 23 package com.sun.enterprise.admin.wsmgmt.repository.impl.cache; 24 25 import java.util.List ; 26 import java.util.Map ; 27 import java.util.HashMap ; 28 import java.util.Properties ; 29 import java.io.FileInputStream ; 30 import java.io.FileOutputStream ; 31 import java.io.File ; 32 import java.util.Collection ; 33 import java.util.Iterator ; 34 import java.util.Enumeration ; 35 import com.sun.enterprise.util.SystemPropertyConstants; 36 import com.sun.enterprise.admin.servermgmt.pe.PEFileLayout; 37 38 import java.util.logging.Logger ; 39 import java.util.logging.Level ; 40 import com.sun.logging.LogDomains; 41 42 50 public class CacheMgr { 51 52 57 public static CacheMgr getInstance() { 58 if (_mgr == null) { 59 _mgr = new CacheMgr(); 60 } 61 62 return _mgr; 63 } 64 65 68 private CacheMgr() { 69 load(); 70 } 71 72 77 private String getPropertyFile() { 78 String iRoot = 79 System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY); 80 String file = iRoot + File.separator + PEFileLayout.GENERATED_DIR 81 + File.separator + CACHE_FILE; 82 return file; 83 } 84 85 88 void save() { 89 90 FileOutputStream fos = null; 91 try { 92 Properties pro = new Properties (); 93 94 Collection eValues = _ejbModules.values(); 95 for (Iterator iter=eValues.iterator(); iter.hasNext();) { 96 String ejbMod = (String ) iter.next(); 97 pro.put(ejbMod, EJB); 98 } 99 Collection wValues = _webModules.values(); 100 for (Iterator iter=wValues.iterator(); iter.hasNext();) { 101 String webMod = (String ) iter.next(); 102 pro.put(webMod, WEB); 103 } 104 Collection aValues = _j2eeApplications.values(); 105 for (Iterator iter=aValues.iterator(); iter.hasNext();) { 106 J2eeApplication app = (J2eeApplication) iter.next(); 107 pro.put(app.getName(), app.getPersistentValue()); 108 } 109 110 File file = new File (getPropertyFile()); 111 fos = new FileOutputStream (file); 112 pro.store(fos, ""); 113 114 } catch (Exception e) { 115 _logger.log(Level.FINE, "Error while saving ws-mgmt cache file", e); 116 } finally { 117 if (fos != null) { 118 try { 119 fos.close(); 120 } catch (Exception e) { } 121 } 122 } 123 } 124 125 128 private void load() { 129 130 FileInputStream fis = null; 131 try { 132 File file = new File (getPropertyFile()); 133 134 if (!file.exists()) { 136 return; 137 } 138 139 fis = new FileInputStream (file); 140 Properties pro = new Properties (); 141 pro.load(fis); 142 143 for (Enumeration e = pro.keys(); e.hasMoreElements();) { 144 String key = (String ) e.nextElement(); 145 String val = pro.getProperty(key); 146 147 if (EJB.equals(val)) { 148 _ejbModules.put(key, key); 149 } else if (WEB.equals(val)) { 150 _webModules.put(key, key); 151 } else { 152 J2eeApplication app = new J2eeApplication(key, val); 153 _j2eeApplications.put(key, app); 154 } 155 } 156 } catch (Exception e) { 157 _logger.log(Level.FINE, 158 "Error while loading ws-mgmt cache file", e); 159 } finally { 160 if (fis != null) { 161 try { 162 fis.close(); 163 } catch (Exception e) { } 164 } 165 } 166 } 167 168 175 void addJ2eeApplication(String name, List ejbBundles, List webBundles) { 176 177 J2eeApplication app = new J2eeApplication(name, ejbBundles, webBundles); 178 _j2eeApplications.put(name, app); 179 } 180 181 186 J2eeApplication removeJ2eeApplication(String name) { 187 return (J2eeApplication) _j2eeApplications.remove(name); 188 } 189 190 195 public Map getJ2eeApplications() { 196 return _j2eeApplications; 197 } 198 199 204 public Map getEjbModules() { 205 return _ejbModules; 206 } 207 208 213 void addEjbModule(String name) { 214 _ejbModules.put(name, name); 215 } 216 217 222 String removeEjbModule(String name) { 223 return (String ) _ejbModules.remove(name); 224 } 225 226 231 public Map getWebModules() { 232 return _webModules; 233 } 234 235 240 void addWebModule(String name) { 241 _webModules.put(name, name); 242 } 243 244 249 String removeWebModule(String name) { 250 return (String ) _webModules.remove(name); 251 } 252 253 private static CacheMgr _mgr = null; 255 private Map _j2eeApplications = new HashMap (); 256 private Map _ejbModules = new HashMap (); 257 private Map _webModules = new HashMap (); 258 private static final String EJB = "EJB_MODULE"; 259 private static final String WEB = "WEB_MODULE"; 260 private static final String CACHE_FILE = ".com_sun_appserv_wsindex"; 261 262 private static Logger _logger = Logger.getLogger(LogDomains.ADMIN_LOGGER); 263 } 264 | Popular Tags |