1 23 24 package com.sun.ejb.containers.builder; 25 26 import java.lang.reflect.Method ; 27 28 import java.util.logging.Logger ; 29 import java.util.logging.Level ; 30 31 import com.sun.appserv.util.cache.CacheListener; 32 33 import com.sun.ejb.base.container.util.CacheProperties; 34 35 import com.sun.ejb.base.sfsb.initialization.PersistenceStrategyBuilderFactory; 36 37 import com.sun.ejb.base.sfsb.util.CheckpointPolicyImpl; 38 import com.sun.ejb.base.sfsb.util.ScrambledKeyGenerator; 39 import com.sun.ejb.base.sfsb.util.SimpleKeyGenerator; 40 import com.sun.ejb.base.sfsb.util.EJBServerConfigLookup; 41 42 import com.sun.ejb.containers.StatefulSessionContainer; 43 44 import com.sun.ejb.containers.BaseContainer; 45 46 import com.sun.ejb.containers.util.cache.BaseCache; 47 import com.sun.ejb.containers.util.cache.FIFOSessionCache; 48 import com.sun.ejb.containers.util.cache.LruSessionCache; 49 import com.sun.ejb.containers.util.cache.NRUSessionCache; 50 import com.sun.ejb.containers.util.cache.UnBoundedSessionCache; 51 52 import com.sun.enterprise.deployment.EjbDescriptor; 53 import com.sun.enterprise.deployment.EjbSessionDescriptor; 54 import com.sun.enterprise.util.Utility; 55 56 import com.sun.ejb.spi.sfsb.initialization.PersistenceStrategyBuilder; 57 import com.sun.ejb.spi.sfsb.initialization.SFSBContainerInitialization; 58 import com.sun.ejb.spi.sfsb.util.CheckpointPolicy; 59 60 import com.sun.ejb.spi.sfsb.store.SFSBStoreManager; 61 62 import com.sun.enterprise.config.ConfigContext; 63 64 import com.sun.logging.LogDomains; 65 66 80 public class StatefulContainerBuilder 81 extends BaseContainerBuilder 82 { 83 private static final Level TRACE_LEVEL = Level.FINE; 84 85 private StatefulSessionContainer sfsbContainer; 86 87 private SFSBContainerInitialization containerInitialization; 88 89 private CacheProperties cacheProps; 90 91 private EJBServerConfigLookup ejbConfigLookup; 92 93 private LruSessionCache sessionCache; 94 95 private SFSBStoreManager sfsbStoreManager; 96 97 private boolean HAEnabled = false; 98 99 public StatefulContainerBuilder() { 100 super(); 101 } 102 103 public BaseContainer createContainer( 104 EjbDescriptor ejbDescriptor, ClassLoader loader) 105 throws Exception 106 { 107 sfsbContainer = new StatefulSessionContainer(ejbDescriptor, loader); 108 containerInitialization = (SFSBContainerInitialization) sfsbContainer; 109 110 return sfsbContainer; 111 } 112 113 public void buildComponents(ConfigContext dynamicConfigContext) 114 throws Exception 115 { 116 this.ejbConfigLookup = new EJBServerConfigLookup(ejbDescriptor, 117 dynamicConfigContext); 118 this.HAEnabled = 119 ejbConfigLookup.calculateEjbAvailabilityEnabledFromConfig(); 120 121 cacheProps = new CacheProperties(ejbDescriptor); 122 123 buildCheckpointPolicy(this.HAEnabled); 124 buildSFSBUUIDUtil(); 125 126 buildStoreManager(); 128 129 buildCache(); 130 scheduleTimerTasks(sfsbContainer); 131 } 132 133 134 135 136 private final void buildCheckpointPolicy(boolean haEnabled) { 137 containerInitialization.setCheckpointPolicy( 138 new CheckpointPolicyImpl(haEnabled)); 139 } 140 141 private void buildSFSBUUIDUtil() { 142 containerInitialization.setSFSBUUIDUtil(HAEnabled 145 ? new ScrambledKeyGenerator(getIPAddress(), getPort()) 146 : new SimpleKeyGenerator(getIPAddress(), getPort())); 147 } 148 149 private void buildStoreManager() { 150 PersistenceStrategyBuilderFactory builderFactory = 151 new PersistenceStrategyBuilderFactory(); 152 153 String persistenceStoreType = 154 ejbConfigLookup.getPersistenceStoreType(); 155 156 PersistenceStrategyBuilder storeBuilder = 157 builderFactory.createPersistenceStrategyBuilder(persistenceStoreType); 158 159 if (_logger.isLoggable(TRACE_LEVEL)) { 160 _logger.log(TRACE_LEVEL, "++SFSBBuilder:: " 161 + "HAEnabled: " + HAEnabled 162 + "; specifiedStoreType: " + persistenceStoreType 163 + "; builder: " + storeBuilder); 164 } 165 166 storeBuilder.initializePersistenceStrategy( 167 containerInitialization, ejbDescriptor); 168 169 this.sfsbStoreManager = containerInitialization.getSFSBStoreManager(); 170 } 171 172 private void buildCache() { 173 String cacheName = ejbDescriptor.getEjbClassName(); 174 String victimPolicy = cacheProps.getVictimSelectionPolicy(); 175 176 if (cacheProps.getMaxCacheSize() <= 0) { 177 sessionCache = new UnBoundedSessionCache(cacheName, sfsbContainer, 178 cacheProps.getCacheIdleTimeoutInSeconds(), 179 cacheProps.getRemovalTimeoutInSeconds()); 180 } else if ("lru".equalsIgnoreCase(victimPolicy)) { 181 sessionCache = new LruSessionCache(cacheName, sfsbContainer, 182 cacheProps.getCacheIdleTimeoutInSeconds(), 183 cacheProps.getRemovalTimeoutInSeconds()); 184 } else if ("fifo".equalsIgnoreCase(victimPolicy)) { 185 sessionCache = new FIFOSessionCache(cacheName, sfsbContainer, 186 cacheProps.getCacheIdleTimeoutInSeconds(), 187 cacheProps.getRemovalTimeoutInSeconds()); 188 } else { 189 sessionCache = new NRUSessionCache(cacheName, sfsbContainer, 190 cacheProps.getCacheIdleTimeoutInSeconds(), 191 cacheProps.getRemovalTimeoutInSeconds()); 192 } 193 194 195 float ratio = (float) (1.0 * cacheProps.getNumberOfVictimsToSelect() 196 / cacheProps.getMaxCacheSize()); 197 float loadFactor = (float) (1.0 - ratio); 198 if (loadFactor < 0 || loadFactor > 1) { 199 loadFactor = 0.75f; 200 } 201 202 if (cacheProps.getMaxCacheSize() <= 0) { 203 sessionCache.init(16*1024, loadFactor, null); 204 } else { 205 sessionCache.init(cacheProps.getMaxCacheSize(), loadFactor, null); 206 } 207 208 sessionCache.addCacheListener((CacheListener) sfsbContainer); 209 sessionCache.setSessionStore(this.sfsbStoreManager); 210 211 sfsbContainer.setSessionCache(sessionCache); 212 if (cacheProps.getNumberOfVictimsToSelect() > 213 sfsbContainer.MIN_PASSIVATION_BATCH_COUNT) 214 { 215 sfsbContainer.setPassivationBatchCount( 216 cacheProps.getNumberOfVictimsToSelect()); 217 } 218 219 if (_logger.isLoggable(TRACE_LEVEL)) { 220 _logger.log(TRACE_LEVEL, "Created cache [for " 221 + ejbDescriptor.getName() + "] " 222 + cacheProps + "; loadFactor: " 223 + loadFactor 224 + "; storeManager: " + this.sfsbStoreManager); 225 } 226 } 227 228 private void scheduleTimerTasks(StatefulSessionContainer container) { 229 String ejbName = ejbDescriptor.getEjbClassName(); 230 231 if (cacheProps.getCacheIdleTimeoutInSeconds() > 0) { 232 long timeout = cacheProps.getCacheIdleTimeoutInSeconds() * 1000; 233 try { 234 sfsbContainer.invokePeriodically(timeout, timeout, 235 new CachePassivatorTask(ejbName, sessionCache, _logger)); 236 if( _logger.isLoggable(TRACE_LEVEL) ) { 237 _logger.log(TRACE_LEVEL, 238 "[SFSBBuilder]: Added CachePassivator for: " 239 + ejbName + ". To run after " 240 + timeout + " millis..."); 241 } 242 243 } catch (Throwable th) { 244 _logger.log(Level.WARNING, 245 "ejb.sfsb_helper_add_idle_passivatortask_failed", th); 246 } 247 } 248 249 if (cacheProps.getRemovalTimeoutInSeconds() > 0) { 250 long timeout = cacheProps.getRemovalTimeoutInSeconds() * 1000; 251 try { 252 sfsbContainer.invokePeriodically(timeout, timeout, 253 new ExpiredSessionsRemovalTask(ejbName, 254 this.sfsbContainer, _logger)); 255 if (_logger.isLoggable(TRACE_LEVEL)) { 256 _logger.log(TRACE_LEVEL, 257 "[SFSBBuilder]: Added StorePassivator for: " 258 + ejbName + ". To run after " 259 + "after " + timeout + " millis..."); 260 } 261 } catch (Throwable th) { 262 _logger.log(Level.WARNING, 263 "ejb.sfsb_helper_add_remove_passivatortask_failed", th); 264 } 265 } 266 267 } 268 269 } 270 271 class CachePassivatorTask 272 implements Runnable 273 { 274 275 private LruSessionCache cache; 276 private Logger logger; 277 private String name; 278 279 CachePassivatorTask(String name, LruSessionCache cache, Logger logger) { 280 this.name = name; 281 this.cache = cache; 282 this.logger = logger; 283 } 284 285 public void run() { 286 try { 287 cache.trimTimedoutItems(Integer.MAX_VALUE); 288 } catch (Exception ex) { 289 if (logger.isLoggable(Level.WARNING)) { 290 logger.log(Level.WARNING, 291 "ejb.sfsb_helper_remove_idle_beans_failed", ex); 292 } 293 } 294 } 295 } 296 297 class ExpiredSessionsRemovalTask 298 implements Runnable 299 { 300 private StatefulSessionContainer container; 301 private Logger logger; 302 private String name; 303 304 ExpiredSessionsRemovalTask(String name, 305 StatefulSessionContainer container, Logger logger) 306 { 307 this.name = name; 308 this.container = container; 309 this.logger = logger; 310 } 311 312 public void run() { 313 try { 314 container.removeExpiredSessions(); 315 } catch (Exception ex) { 316 if (logger.isLoggable(Level.WARNING)) { 317 logger.log(Level.WARNING, 318 "ejb.sfsb_helper_remove_expired_beans_failed", ex); 319 } 320 } 321 } 322 } 323 324 325 326 327 328 329 330 331 332 333 334 335 | Popular Tags |