1 23 24 package com.sun.ejb.base.sfsb; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.io.FileInputStream ; 29 import java.io.FileOutputStream ; 30 import java.io.ObjectInputStream ; 31 import java.io.BufferedInputStream ; 32 import java.io.BufferedOutputStream ; 33 import java.io.ObjectOutputStream ; 34 import java.io.StringWriter ; 35 import java.io.PrintWriter ; 36 37 import java.util.Map ; 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 41 import java.util.logging.*; 42 import com.sun.logging.*; 43 44 import com.sun.ejb.spi.sfsb.SFSBBeanState; 45 import com.sun.ejb.spi.sfsb.SFSBStoreManager; 46 import com.sun.ejb.spi.sfsb.SFSBStoreManagerException; 47 import com.sun.ejb.spi.sfsb.SFSBStoreManagerConstants; 48 import com.sun.ejb.spi.sfsb.SFSBUUIDUtil; 49 50 import com.sun.ejb.spi.monitorable.sfsb.MonitorableSFSBStore; 51 52 import com.sun.appserv.util.cache.CacheListener; 53 import com.sun.ejb.containers.util.cache.PassivatedSessionCache; 54 55 58 59 public abstract class AbstractFileStoreManager 60 implements SFSBStoreManager, MonitorableSFSBStore, 61 CacheListener 62 { 63 protected static Logger _logger = 64 LogDomains.getLogger(LogDomains.EJB_LOGGER); 65 66 protected File baseDir; 67 protected String storeManagerName; 68 69 protected int passivationTimeoutInSeconds; 70 71 private int loadCount; 72 private int loadSuccessCount; 73 private int loadErrorCount; 74 private int storeCount; 75 private int storeSuccessCount; 76 private int storeErrorCount; 77 private int expiredSessionCount; 78 79 private boolean shutdown; 80 private PassivatedSessionCache passivatedSessions; 81 82 private Level TRACE_LEVEL = Level.FINE; 83 private ClassLoader classLoader; 84 85 88 public AbstractFileStoreManager() { 89 } 90 91 92 93 94 95 public void initSessionStore(Map storeEnv) { 96 97 String baseDirName = (String ) storeEnv.get( 98 SFSBStoreManagerConstants.PASSIVATION_DIRECTORY_NAME); 99 if (baseDirName == null) { 100 baseDirName = "."; 101 } 102 baseDir = new File (baseDirName); 103 104 this.storeManagerName = (String ) storeEnv.get( 105 SFSBStoreManagerConstants.STORE_MANAGER_NAME); 106 107 this.classLoader = (ClassLoader ) storeEnv.get( 108 SFSBStoreManagerConstants.CLASS_LOADER); 109 110 try { 111 Integer sessionTimeout = (Integer ) storeEnv.get( 112 SFSBStoreManagerConstants.SESSION_TIMEOUT_IN_SECONDS); 113 passivationTimeoutInSeconds = sessionTimeout.intValue(); 114 } catch (Exception ex) { 115 } 116 117 try { 118 if ((baseDir.mkdirs() == false) && (!baseDir.isDirectory())) { 119 _logger.log(Level.WARNING, "ejb.sfsb_storemgr_mdirs_failed", 120 new Object [] {baseDirName}); 121 } 122 123 passivatedSessions = new PassivatedSessionCache( 124 passivationTimeoutInSeconds * 1000); 125 passivatedSessions.init(8192, null); 126 passivatedSessions.addCacheListener(this); 127 onInitialization(); 128 } catch (Exception ex) { 129 _logger.log(Level.WARNING, "ejb.sfsb_storemgr_init_failed", 130 new Object [] {baseDirName}); 131 _logger.log(Level.WARNING, "ejb.sfsb_storemgr_init_exception", ex); 132 } 133 134 } 135 136 protected void onInitialization() { 137 } 138 139 public SFSBBeanState getState(Object sessionKey) { 140 141 String fileName = sessionKey.toString(); 142 SFSBBeanState beanState = null; 143 144 if(_logger.isLoggable(TRACE_LEVEL)) { 145 _logger.log(TRACE_LEVEL, "[SFSBStore] Attempting to load session: " 146 + sessionKey); 147 } 148 149 if (passivatedSessions.remove(fileName) == null) { 150 if(_logger.isLoggable(TRACE_LEVEL)) { 151 _logger.log(TRACE_LEVEL, "[SFSBStore] Could not find " 152 + "state for session: " + sessionKey); 153 } 154 return null; 155 } 156 157 File file = new File (baseDir, fileName); 158 if (file.exists()) { 159 int dataSize = (int) file.length(); 160 byte[] data = new byte[dataSize]; 161 BufferedInputStream bis = null; 162 FileInputStream fis = null; 163 try { 164 loadCount++; 165 fis = new FileInputStream (file); 166 bis = new BufferedInputStream (fis); 167 int offset = 0; 168 for (int toRead = dataSize; toRead > 0; ) { 169 int count = bis.read(data, offset, toRead); 170 offset += count; 171 toRead -= count; 172 } 173 174 beanState = new SFSBBeanState(sessionKey, -1, false, data); 175 loadSuccessCount++; 176 if(_logger.isLoggable(TRACE_LEVEL)) { 177 _logger.log(TRACE_LEVEL, "[SFSBStore] Successfully Loaded " 178 + "session: " + sessionKey); 179 } 180 } catch (Exception ex) { 181 loadErrorCount++; 182 _logger.log(Level.WARNING, 183 "ejb.sfsb_storemgr_loadstate_failed", 184 new Object [] {fileName}); 185 _logger.log(Level.WARNING, 186 "ejb.sfsb_storemgr_loadstate_exception", ex); 187 remove(sessionKey); 188 } finally { 189 try { 190 bis.close(); 191 } catch (Exception ex) { 192 _logger.log(Level.FINE, "Error while closing buffered input stream", ex); 193 } 194 try { 195 fis.close(); 196 } catch (Exception ex) { 197 _logger.log(Level.FINE, "Error while closing file input stream", ex); 198 } 199 } 200 } else { 201 if(_logger.isLoggable(TRACE_LEVEL)) { 202 _logger.log(TRACE_LEVEL, "[SFSBStore] Could not find passivated " 203 + "file for: " + sessionKey); 204 } 205 } 206 return beanState; 207 } 208 209 public void checkpointSave(SFSBBeanState[] beanStates, 210 boolean transactionFlag) 211 { 212 } 214 215 public void passivateSave(SFSBBeanState beanState) { 216 saveState(beanState, true); 217 } 218 219 public void remove(Object sessionKey) { 220 try { 221 passivatedSessions.remove(sessionKey); 222 removeFile(new File (baseDir, sessionKey.toString())); 223 } catch (Exception ex) { 224 _logger.log(Level.WARNING, 225 "ejb.sfsb_storemgr_removestate_failed", 226 new Object [] {sessionKey.toString()}); 227 _logger.log(Level.WARNING, 228 "ejb.sfsb_storemgr_removestate_exception", ex); 229 } 230 } 231 232 public void removeExpired() { 233 if( shutdown ) { 234 if(_logger.isLoggable(TRACE_LEVEL)) { 235 _logger.log(TRACE_LEVEL, "[SFSBStore] Server is being shutdown hence " 236 + "method cannot be executed" ); 237 } 238 return; 239 } 240 passivatedSessions.trimExpiredEntries(Integer.MAX_VALUE); 241 } 242 243 public void removeAll() { 244 try { 245 String [] fileNames = baseDir.list(); 246 for (int i=0; i<fileNames.length; i++) { 247 remove(fileNames[i]); 248 } 249 250 if (baseDir.delete() == false) { 251 Object [] params = {baseDir.getAbsolutePath()}; 252 _logger.log(Level.WARNING, 253 "ejb.sfsb_storemgr_removedir_failed", params); 254 } 255 } catch (Throwable th) { 256 _logger.log(Level.WARNING, "ejb.sfsb_storemgr_removeall_exception", th); 257 } 258 } 259 260 public void shutdown() { 261 shutdown = true; 262 } 263 264 public MonitorableSFSBStore getMonitorableSFSBStore() { 265 return this; 266 } 267 268 269 270 271 272 public int getCurrentSize() { 273 return passivatedSessions.getEntryCount(); 274 } 275 276 public int getLoadCount() { 277 return loadCount; 278 } 279 280 public int getLoadSuccessCount() { 281 return loadSuccessCount; 282 } 283 284 public int getLoadErrorCount() { 285 return loadErrorCount; 286 } 287 288 public int getPassivationCount() { 289 return storeCount; 290 } 291 292 public int getPassivationSuccessCount() { 293 return storeSuccessCount; 294 } 295 296 public int getPassivationErrorCount() { 297 return storeErrorCount; 298 } 299 300 public int getCheckpointCount() { 301 return storeCount; 302 } 303 304 public int getCheckpointSuccessCount() { 305 return storeSuccessCount; 306 } 307 308 public int getCheckpointErrorCount() { 309 return storeErrorCount; 310 } 311 312 public int getExpiredSessionCount() { 313 return expiredSessionCount; 314 } 315 316 317 318 319 320 321 private void saveState(SFSBBeanState beanState, boolean isPassivated) { 322 323 Object sessionKey = beanState.getId(); 324 String fileName = sessionKey.toString(); 325 326 if(_logger.isLoggable(TRACE_LEVEL)) { 327 _logger.log(TRACE_LEVEL, "[SFSBStore] Attempting to save session: " 328 + sessionKey); 329 } 330 File file = null; 331 332 BufferedOutputStream bos = null; 333 FileOutputStream fos = null; 334 335 try { 336 storeCount++; 337 file = new File (baseDir, fileName); 338 fos = new FileOutputStream (file); 339 bos = new BufferedOutputStream (fos); 340 byte[] data = beanState.getState(); 341 bos.write(data, 0, data.length); 342 343 storeSuccessCount++; 344 if (isPassivated) { 345 passivatedSessions.put(fileName, new Long (beanState.getLastAccess())); 346 if(_logger.isLoggable(TRACE_LEVEL)) { 347 _logger.log(TRACE_LEVEL, "[SFSBStore] Successfully saved session: " 348 + sessionKey); 349 } 350 } 351 } catch (Exception ex) { 352 storeErrorCount++; 353 _logger.log(Level.WARNING, "ejb.sfsb_storemgr_savestate_failed", 354 new Object [] {fileName}); 355 _logger.log(Level.WARNING, "ejb.sfsb_storemgr_savestate_exception", ex); 356 try { removeFile(file); } catch (Exception ex1) {} 357 String errMsg = "Could not save session: " + beanState.getId(); 358 throw new SFSBStoreManagerException(errMsg, ex); 359 } finally { 360 try { 361 if (bos != null) bos.close(); 362 } catch (Exception ex) { 363 _logger.log(Level.FINE, "Error while closing buffered output stream", ex); 364 } 365 try { 366 if (fos != null) fos.close(); 367 } catch (Exception ex) { 368 _logger.log(Level.FINE, "Error while closing file output stream", ex); 369 } 370 } 371 } 372 373 private void removeFile(File file) { 374 final File localFile = file; 375 Boolean temp = (Boolean ) java.security.AccessController.doPrivileged( 376 new java.security.PrivilegedAction () { 377 public java.lang.Object run() { 378 return (new Boolean (localFile.delete())); 379 } 380 } 381 ); 382 boolean success = temp.booleanValue(); 383 if (!success) { 384 _logger.log(Level.WARNING, "ejb.sfsb_storemgr_removestate_failed", 385 new Object [] {file.getName()}); 386 } else { 387 if(_logger.isLoggable(TRACE_LEVEL)) { 388 _logger.log(TRACE_LEVEL, "[SFSBStore] Removed session: " 389 + file.getName()); 390 } 391 } 392 } 393 394 protected void addPassivatedSession(String fileName, long lastAccessTime) { 395 passivatedSessions.add(fileName, new Long (lastAccessTime)); 396 } 397 398 399 400 401 public void trimEvent(Object sessionKey, Object lastAccessedAt) { 402 if(_logger.isLoggable(TRACE_LEVEL)) { 405 _logger.log(TRACE_LEVEL, "[SFSBStore] Removing expired session: " 406 + sessionKey); 407 } 408 remove(sessionKey); 409 expiredSessionCount++; 410 } 411 412 protected ClassLoader getClassLoader() { 413 return this.classLoader; 414 } 415 416 } 417 | Popular Tags |