KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > SessionManagerConfigurationHelper


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 /*
25  * SessionManagerConfigurationHelper.java
26  *
27  * Created on December 19, 2003, 3:48 PM
28  */

29
30 package com.sun.enterprise.web;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.ResourceBundle JavaDoc;
36 import com.sun.logging.LogDomains;
37
38 import com.sun.enterprise.config.ConfigContext;
39 import org.apache.catalina.core.StandardContext;
40 //import com.sun.enterprise.config.serverbeans.WebModule;
41
import com.sun.enterprise.web.session.PersistenceType;
42 import com.sun.enterprise.deployment.runtime.web.SessionManager;
43 import com.sun.enterprise.deployment.runtime.web.ManagerProperties;
44 import com.sun.enterprise.deployment.runtime.web.StoreProperties;
45 import com.sun.enterprise.deployment.runtime.web.WebProperty;
46 import com.sun.enterprise.deployment.WebBundleDescriptor;
47
48 /**
49  *
50  * @author lwhite
51  */

52 public class SessionManagerConfigurationHelper {
53     
54     /** Creates a new instance of SessionManagerConfigurationHelper */
55     public SessionManagerConfigurationHelper(
56         WebModule ctx, SessionManager smBean, WebBundleDescriptor wbd, WebModuleConfig wmInfo) {
57         _ctx = ctx;
58         _smBean = smBean;
59         _wbd = wbd;
60         _wmInfo = wmInfo;
61         _systemApps.add("com_sun_web_ui");
62         _systemApps.add(Constants.DEFAULT_WEB_MODULE_PREFIX + "admingui");
63         _systemApps.add("adminapp");
64         _systemApps.add("admingui");
65         
66         if (_logger == null) {
67             _logger = LogDomains.getLogger(LogDomains.WEB_LOGGER);
68             _rb = _logger.getResourceBundle();
69         }
70     }
71     
72     private boolean isSystemApp(String JavaDoc appName) {
73         return _systemApps.contains(appName);
74     }
75     
76     public void initializeConfiguration() {
77         
78         ConfigContext dynamicConfigContext = null;
79         com.sun.enterprise.config.serverbeans.WebModule wmBean = _wmInfo.getBean();
80         //System.out.println("SessionManagerConfigurationHelper>>initializeConfiguration: wmBean = " + wmBean);
81
if(wmBean != null) {
82             dynamicConfigContext = wmBean.getConfigContext();
83         //System.out.println("SessionManagerConfigurationHelper>>initializeConfiguration: wmBeanOK dynamicConfigContext = " + dynamicConfigContext);
84
}
85         //System.out.println("SessionManagerConfigurationHelper>>initializeConfiguration: wmBeanNotOK dynamicConfigContext = " + dynamicConfigContext);
86

87         boolean isAppDistributable = false;
88         if (_wbd != null)
89             isAppDistributable = _wbd.isDistributable();
90         if(_logger.isLoggable(Level.FINEST)) {
91             _logger.finest("Web App Distributable (" + getApplicationId(_ctx) + "): " + isAppDistributable);
92         }
93         
94         PersistenceType persistence = PersistenceType.MEMORY;
95         String JavaDoc persistenceFrequency = null;
96         String JavaDoc persistenceScope = null;
97         
98         //added code - check global availability-enabled
99
//if availability-enabled set global ha defaults
100
ServerConfigLookup serverConfigLookup = null;
101     /*
102     System.out.println("SessionManagerConfigurationHelper>>dynamicConfigContext = "
103         + dynamicConfigContext);
104      */

105         if(dynamicConfigContext != null) {
106             serverConfigLookup = new ServerConfigLookup(dynamicConfigContext);
107         } else {
108             serverConfigLookup = new ServerConfigLookup();
109         }
110
111         //this change brings takes into account both global and web-container
112
//availability-enabled settings
113
String JavaDoc contextRoot = ((WebModule)_ctx).getContextRoot();
114         String JavaDoc j2eeAppName = ((StandardContext)_ctx).getJ2EEApplication();
115         boolean isAvailabilityEnabled =
116             //serverConfigLookup.getAvailabilityEnabledFromConfig();
117
serverConfigLookup.calculateWebAvailabilityEnabledFromConfig(contextRoot, j2eeAppName);
118         //System.out.println("isAvailabilityEnabled = " + isAvailabilityEnabled);
119
if(_logger.isLoggable(Level.FINEST)) {
120             _logger.finest("AvailabilityGloballyEnabled = " + isAvailabilityEnabled);
121         }
122         if(isAvailabilityEnabled) {
123             //these are the global defaults if nothing is
124
//set at domain.xml or sun-web.xml
125
persistence = PersistenceType.HA;
126             persistenceFrequency = "time-based";
127             persistenceScope = "session";
128         }
129         
130         //added code - if domain.xml default exists, then use that
131
PersistenceType serverDefaultPersistenceType =
132             serverConfigLookup.getPersistenceTypeFromConfig();
133
134         if(serverDefaultPersistenceType != null) {
135         //if(serverDefaultPersistenceType != null && !(serverDefaultPersistenceType.equals(PersistenceType.MEMORY)) ) {
136
persistence = serverDefaultPersistenceType;
137             persistenceFrequency = serverConfigLookup.getPersistenceFrequencyFromConfig();
138             persistenceScope = serverConfigLookup.getPersistenceScopeFromConfig();
139         }
140         String JavaDoc insLevelPersistenceTypeString = null;
141         if(persistence != null) {
142             insLevelPersistenceTypeString = persistence.getType();
143         }
144         if(_logger.isLoggable(Level.FINEST)) {
145             _logger.finest("instance-level persistence-type = " + insLevelPersistenceTypeString);
146             _logger.finest("instance-level persistenceFrequency = " + persistenceFrequency);
147             _logger.finest("instance-level persistenceScope = " + persistenceScope);
148         }
149         
150         String JavaDoc webAppLevelPersistenceFrequency = null;
151         String JavaDoc webAppLevelPersistenceScope = null;
152
153         if (_smBean != null) {
154             // The persistence-type controls what properties of the
155
// session manager can be configured
156
String JavaDoc pType = _smBean.getAttributeValue(SessionManager.PERSISTENCE_TYPE);
157             if(_logger.isLoggable(Level.FINEST)) {
158                 _logger.finest("webAppLevelPersistenceType = " + pType);
159             }
160             //changed code - default back to persistence
161
//persistence = PersistenceType.parseType(pType);
162
persistence = PersistenceType.parseType(pType, persistence);
163
164             webAppLevelPersistenceFrequency = getPersistenceFrequency(_smBean);
165             webAppLevelPersistenceScope = getPersistenceScope(_smBean);
166             if(_logger.isLoggable(Level.FINEST)) {
167                 _logger.finest("webAppLevelPersistenceFrequency = " + webAppLevelPersistenceFrequency);
168                 _logger.finest("webAppLevelPersistenceScope = " + webAppLevelPersistenceScope);
169             }
170         }
171         
172         //use web app level values if they exist (i.e. not null)
173
if(webAppLevelPersistenceFrequency != null) {
174             persistenceFrequency = webAppLevelPersistenceFrequency;
175         }
176         if(webAppLevelPersistenceScope != null) {
177             persistenceScope = webAppLevelPersistenceScope;
178         }
179         if(_logger.isLoggable(Level.FINEST)) {
180             _logger.finest("IN WebContainer>>ConfigureSessionManager after web level check");
181             _logger.finest("AFTER_WEB_PERSISTENCE-TYPE IS = " + persistence.getType());
182             _logger.finest("AFTER_WEB_PERSISTENCE_FREQUENCY IS = " + persistenceFrequency);
183             _logger.finest("AFTER_WEB_PERSISTENCE_SCOPE IS = " + persistenceScope);
184         }
185         
186         //delegate remaining initialization to builder
187
String JavaDoc frequency = null;
188         String JavaDoc scope = null;
189         if( persistence == PersistenceType.MEMORY
190             | persistence == PersistenceType.FILE
191             | persistence == PersistenceType.CUSTOM) {
192             //deliberately leaving frequency & scope null
193
} else {
194             frequency = persistenceFrequency;
195             scope = persistenceScope;
196         }
197
198         //if app is not distributable and non-memory option
199
//is attempted, log error and set back to "memory"
200
if(!isAppDistributable && persistence != PersistenceType.MEMORY) {
201             String JavaDoc wmName = getApplicationId(_ctx);
202             if(_logger.isLoggable(Level.FINEST)) {
203                 _logger.finest("is " + wmName + " a system app: " + isSystemApp(wmName));
204             }
205             //suppress log error msg for default-web-module
206
//log message only if availabilityenabled = true is attempted
207
if (isAvailabilityEnabled && !wmName.equals(Constants.DEFAULT_WEB_MODULE_NAME) && !this.isSystemApp(wmName)) {
208                 //log error
209
Object JavaDoc[] params = { getApplicationId(_ctx), persistence.getType(), frequency, scope };
210                 _logger.log(Level.INFO, "webcontainer.invalidSessionManagerConfig2",
211                             params);
212             }
213             //set back to memory option
214
persistence = PersistenceType.MEMORY;
215             frequency = null;
216             scope = null;
217         }
218         
219         //if availability-enabled is false, reset to "memory"
220
if(!isAvailabilityEnabled) {
221             //set back to memory option
222
persistence = PersistenceType.MEMORY;
223             frequency = null;
224             scope = null;
225         }
226         
227         if(_logger.isLoggable(Level.FINEST)) {
228             _logger.finest("IN WebContainer>>ConfigureSessionManager before builder factory");
229             _logger.finest("FINAL_PERSISTENCE-TYPE IS = " + persistence.getType());
230             _logger.finest("FINAL_PERSISTENCE_FREQUENCY IS = " + frequency);
231             _logger.finest("FINAL_PERSISTENCE_SCOPE IS = " + scope);
232         }
233         
234         _persistence = persistence;
235         _persistenceFrequency = frequency;
236         _persistenceScope = scope;
237         
238     }
239     
240     /**
241      * The application id for this web module
242      * HERCULES:add
243      */

244     public String JavaDoc getApplicationId(WebModule ctx) {
245         com.sun.enterprise.web.WebModule wm =
246             (com.sun.enterprise.web.WebModule)ctx;
247         return wm.getID();
248     }
249     
250     /**
251      * Get the persistence frequency for this web module
252      * (this is the value from sun-web.xml if defined
253      * @param the session manager config bean
254      * HERCULES:add
255      */

256     private String JavaDoc getPersistenceFrequency(SessionManager smBean) {
257         String JavaDoc persistenceFrequency = null;
258         ManagerProperties mgrBean = smBean.getManagerProperties();
259         if ((mgrBean != null) && (mgrBean.sizeWebProperty() > 0)) {
260             WebProperty[] props = mgrBean.getWebProperty();
261             for (int i = 0; i < props.length; i++) {
262                 //String name = props[i].getAttributeValue("name");
263
//String value = props[i].getAttributeValue("value");
264
String JavaDoc name = props[i].getAttributeValue(WebProperty.NAME);
265                 String JavaDoc value = props[i].getAttributeValue(WebProperty.VALUE);
266                 if (name.equalsIgnoreCase("persistenceFrequency")) {
267                     persistenceFrequency = value;
268                 }
269             }
270         }
271         return persistenceFrequency;
272     }
273     
274     /**
275      * Get the persistence scope for this web module
276      * (this is the value from sun-web.xml if defined
277      * @param the session manager config bean
278      * HERCULES:add
279      */

280     private String JavaDoc getPersistenceScope(SessionManager smBean) {
281         String JavaDoc persistenceScope = null;
282         StoreProperties storeBean = smBean.getStoreProperties();
283         if ((storeBean != null) && (storeBean.sizeWebProperty() > 0)) {
284             WebProperty[] props = storeBean.getWebProperty();
285             for (int i = 0; i < props.length; i++) {
286                 //String name = props[i].getAttributeValue("name");
287
//String value = props[i].getAttributeValue("value");
288
String JavaDoc name = props[i].getAttributeValue(WebProperty.NAME);
289                 String JavaDoc value = props[i].getAttributeValue(WebProperty.VALUE);
290                 if (name.equalsIgnoreCase("persistenceScope")) {
291                     persistenceScope = value;
292                 }
293             }
294         }
295         return persistenceScope;
296     }
297     
298     public void checkInitialization() {
299         if(!_initialized) {
300             initializeConfiguration();
301             _initialized = true;
302         }
303     }
304     
305     public PersistenceType getPersistenceType() {
306         checkInitialization();
307         return _persistence;
308     }
309     
310     public String JavaDoc getPersistenceFrequency() {
311         checkInitialization();
312         return _persistenceFrequency;
313     }
314     
315     public String JavaDoc getPersistenceScope() {
316         checkInitialization();
317         return _persistenceScope;
318     }
319     
320     WebModule _ctx = null;
321     SessionManager _smBean = null;
322     WebBundleDescriptor _wbd = null;
323     WebModuleConfig _wmInfo = null;
324     PersistenceType _persistence = PersistenceType.MEMORY;
325     String JavaDoc _persistenceFrequency = null;
326     String JavaDoc _persistenceScope = null;
327     boolean _initialized = false;
328     ArrayList JavaDoc _systemApps = new ArrayList JavaDoc();
329     
330     /**
331      * The logger to use for logging ALL web container related messages.
332      */

333     protected static Logger JavaDoc _logger = null;
334     
335     /**
336      * The resource bundle containing the message strings for _logger.
337      */

338     protected static ResourceBundle JavaDoc _rb = null;
339     
340 }
341
Popular Tags