KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > containers > builder > StatefulContainerBuilder


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.ejb.containers.builder;
25
26 import java.lang.reflect.Method JavaDoc;
27
28 import java.util.logging.Logger JavaDoc;
29 import java.util.logging.Level JavaDoc;
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 /**
67  * A builder for StatefulSessionContainer. Takes care of
68  * building / initializing the StatefulSessionContainer
69  * with the following classes:
70  * a) Cache (LRU / NRU / FIFO / UnBounded)
71  * b) SFSBStoreManager (Using PersistenceStrategyBuilder)
72  * c) Cache passivation task (if idle-timeout is greater than 0)
73  * d) Passivated sessions removal task (if removal-timeout is greater than 0)
74  * e) CheckpointPolicy (if ha enabled)
75  * f) SFSBUUIDUtil
76  * g) BeanLifecycleManager
77  *
78  * @author Mahesh Kannan
79  */

80 public class StatefulContainerBuilder
81     extends BaseContainerBuilder
82 {
83     private static final Level JavaDoc 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 JavaDoc loader)
105     throws Exception JavaDoc
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 JavaDoc
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     //First build storeManager before Cache is built
127
buildStoreManager();
128
129     buildCache();
130     scheduleTimerTasks(sfsbContainer);
131     }
132
133     /************************* Private Methods *************************/
134     /*******************************************************************/
135
136     private final void buildCheckpointPolicy(boolean haEnabled) {
137     containerInitialization.setCheckpointPolicy(
138         new CheckpointPolicyImpl(haEnabled));
139     }
140
141     private void buildSFSBUUIDUtil() {
142     //Just for debugging purpose, we instantiate
143
// two different key generators
144
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 JavaDoc 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 JavaDoc cacheName = ejbDescriptor.getEjbClassName();
174         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc
273 {
274
275     private LruSessionCache cache;
276     private Logger JavaDoc logger;
277     private String JavaDoc name;
278
279     CachePassivatorTask(String JavaDoc name, LruSessionCache cache, Logger JavaDoc 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 JavaDoc 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 JavaDoc
299 {
300     private StatefulSessionContainer container;
301     private Logger JavaDoc logger;
302     private String JavaDoc name;
303
304     ExpiredSessionsRemovalTask(String JavaDoc name,
305         StatefulSessionContainer container, Logger JavaDoc 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 JavaDoc 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