KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BasePersistenceStrategyBuilder.java
26  *
27  * Created on September 30, 2002, 12:25 PM
28  */

29
30 package com.sun.enterprise.web;
31
32 import java.io.File JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import org.apache.catalina.Context;
38 import org.apache.catalina.core.StandardContext;
39 import com.sun.enterprise.deployment.runtime.web.ManagerProperties;
40 //import com.sun.enterprise.tools.common.dd.webapp.ManagerProperties;
41
//import com.iplanet.ias.tools.common.dd.webapp.ManagerProperties;
42
import com.sun.enterprise.deployment.runtime.web.SessionManager;
43 //import com.sun.enterprise.tools.common.dd.webapp.SessionManager;
44
//import com.iplanet.ias.tools.common.dd.webapp.SessionManager;
45
import com.sun.enterprise.deployment.runtime.web.StoreProperties;
46 //import com.sun.enterprise.tools.common.dd.webapp.StoreProperties;
47
//import com.iplanet.ias.tools.common.dd.webapp.StoreProperties;
48
import com.sun.enterprise.deployment.runtime.web.WebProperty;
49 //import com.sun.enterprise.tools.common.dd.webapp.WebProperty;
50
//import com.iplanet.ias.tools.common.dd.webapp.WebProperty;
51
import com.sun.enterprise.web.session.PersistenceType;
52 //import com.iplanet.ias.web.session.PersistenceType;
53

54 //import com.iplanet.ias.config.ConfigBean;
55
//import com.iplanet.ias.config.ConfigException;
56
//import com.iplanet.ias.config.ConfigContext;
57
//import com.iplanet.ias.server.ApplicationServer;
58
//import com.iplanet.ias.server.ServerContext;
59

60 import com.sun.enterprise.config.serverbeans.ElementProperty;
61 //import com.iplanet.ias.config.serverbeans.ElementProperty;
62

63 //FIXME look at whether reapInterval & storeReapInterval supported now
64

65 public abstract class BasePersistenceStrategyBuilder implements PersistenceStrategyBuilder {
66
67     /** Creates a new instance of BaseSessionStoreInitializer */
68     public BasePersistenceStrategyBuilder(Logger JavaDoc logger) {
69         _logger = logger;
70     }
71     
72     /** Creates a new instance of BasePersistenceStrategyBuilder */
73     public BasePersistenceStrategyBuilder() {
74     }
75     
76     public void initializePersistenceStrategy(Context ctx, SessionManager smBean ) {
77
78         //this method sets default values
79
//it may be extended in builder subclasses which will have their
80
//own inst vars for additional params
81
this.setDefaultParams(ctx, smBean );
82
83         //this method reads server instance-level parameter values from server.xml.
84
//Any values found here will over-ride defaults
85
//This method may be extended in builder subclasses which will have their
86
//own inst vars for additional params
87
this.readInstanceLevelParams();
88         
89         //this method reads web app parameter values from sun-web.xml
90
//Any values found here will over-ride defaults & instance-level values
91
//This method may be extended in builder subclasses which will have their
92
//own inst vars for additional params
93
this.readWebAppParams(ctx, smBean);
94
95         ctx.setBackgroundProcessorDelay(reapInterval);
96     }
97     
98     public void setDefaultParams(Context ctx, SessionManager smBean ) {
99         
100         reapInterval = DEFAULT_REAP_INTERVAL;
101
102         maxSessions = -1;
103         //take this out
104
//PersistenceType persistence = PersistenceType.MEMORY;
105

106         // Default settings for persistence-type = 'memory'
107
sessionFilename = DEFAULT_SESSION_FILENAME;
108
109         // Default settings for persistence-type = 'file'
110
storeReapInterval = DEFAULT_REAP_INTERVAL;
111
112         directory = ((StandardContext) ctx).getWorkDir();
113     }
114     
115     
116     public void readInstanceLevelParams() {
117       //debug("in readInstanceLevelParams");
118
ServerConfigLookup lookup = new ServerConfigLookup();
119       com.sun.enterprise.config.serverbeans.SessionManager smBean =
120       //com.iplanet.ias.config.serverbeans.SessionManager smBean =
121
lookup.getInstanceSessionManager();
122      
123         if (smBean != null) {
124             // The persistence-type controls what properties of the
125
// session manager can be configured
126

127             //take these 2 lines out
128
//String pType = smBean.getAttributeValue("persistence-type");
129
//persistence = PersistenceType.parseType(pType);
130

131             com.sun.enterprise.config.serverbeans.ManagerProperties mgrBean =
132             //com.iplanet.ias.config.serverbeans.ManagerProperties mgrBean =
133
smBean.getManagerProperties();
134             if (mgrBean != null) {
135                 // manager reap-interval-in-seconds
136
String JavaDoc reapIntervalInSecondsString = mgrBean.getReapIntervalInSeconds();
137                 if (reapIntervalInSecondsString != null) {
138                     try {
139                         reapInterval = Integer.parseInt(reapIntervalInSecondsString);
140                         _logger.finest(" mgr reapInterval set = " + reapInterval);
141                     } catch (NumberFormatException JavaDoc e) {
142                         // XXX need error message
143
}
144                 } else {
145                     if(_logger.isLoggable(Level.FINEST)) {
146                         _logger.finest("no instance level value set for mgr reapInterval");
147                     }
148                     /* not correct message
149                     Object[] params = { "reap-interval-in-seconds" };
150                     _logger.log(Level.INFO, "webcontainer.notYet", params);
151                      */

152                 }
153                 //max-sessions
154
String JavaDoc maxSessionsString = mgrBean.getMaxSessions();
155                 if (maxSessionsString != null) {
156                     try {
157                         maxSessions = Integer.parseInt(maxSessionsString);
158                         _logger.finest("maxSessions set = " + maxSessions);
159                     } catch (NumberFormatException JavaDoc e) {
160                         // XXX need error message
161
}
162                 } else {
163                     if(_logger.isLoggable(Level.FINEST)) {
164                         _logger.finest("no instance level value set for maxSessions");
165                     }
166                     /* not correct message
167                     Object[] params = { "max-sessions" };
168                     _logger.log(Level.INFO, "webcontainer.notYet", params);
169                      */

170                 }
171                 //session-file-name
172
String JavaDoc sessionFilenameString = mgrBean.getSessionFileName();
173                 if (sessionFilenameString != null) {
174                     sessionFilename = sessionFilenameString;
175                     _logger.finest("sessionFilename set = " + sessionFilename);
176                 }
177                 
178                 //now do properties under <manager-properties> element
179
com.sun.enterprise.config.serverbeans.ElementProperty[] props =
180                     mgrBean.getElementProperty();
181                 for (int i = 0; i < props.length; i++) {
182                     String JavaDoc name = props[i].getAttributeValue("name");
183                     String JavaDoc value = props[i].getAttributeValue("value");
184                     //maxIdleBackupSeconds
185
if (name.equalsIgnoreCase("maxIdleBackupSeconds")) {
186                         try {
187                            maxIdleBackup = Integer.parseInt(value);
188                         } catch (NumberFormatException JavaDoc e) {
189                             // XXX need error message
190
}
191                     }
192                 }
193             }
194             
195             com.sun.enterprise.config.serverbeans.StoreProperties storeBean =
196             //com.iplanet.ias.config.serverbeans.StoreProperties storeBean =
197
smBean.getStoreProperties();
198             
199             if (storeBean != null) {
200                 //store reap-interval-in-seconds
201
String JavaDoc reapIntervalInSecondsString = storeBean.getReapIntervalInSeconds();
202                 if (reapIntervalInSecondsString != null) {
203                     try {
204                         storeReapInterval = Integer.parseInt(reapIntervalInSecondsString);
205                         _logger.finest("storeReapInterval set = " + storeReapInterval);
206                     } catch (NumberFormatException JavaDoc e) {
207                         // XXX need error message
208
}
209                 }
210                 //directory
211
String JavaDoc directoryString = storeBean.getDirectory();
212                 if (directoryString != null) {
213                     directory = directoryString;
214                     _logger.finest("directory set = " + directoryString);
215                 }
216             }
217         }
218       
219         //com.iplanet.ias.config.serverbeans.SessionProperties spBean =
220
com.sun.enterprise.config.serverbeans.SessionProperties spBean =
221           lookup.getInstanceSessionProperties();
222         
223         if (spBean != null) {
224             //session timeout-in-seconds
225
String JavaDoc timeoutSecondsString = spBean.getTimeoutInSeconds();
226             if (timeoutSecondsString != null) {
227                 try {
228                     sessionMaxInactiveInterval = Integer.parseInt(timeoutSecondsString);
229                     _logger.finest("sessionMaxInactiveInterval set = " + sessionMaxInactiveInterval);
230                 } catch (NumberFormatException JavaDoc e) {
231                     // XXX need error message
232
}
233             } else {
234                 if(_logger.isLoggable(Level.FINEST)) {
235                     _logger.finest("no instance level value set for sessionMaxInactiveInterval");
236                 }
237                 /* not correct message
238                 Object[] params = { "timeout-in-seconds" };
239                 _logger.log(Level.INFO, "webcontainer.notYet", params);
240                  */

241             }
242         }
243       
244     }
245     
246     public void readWebAppParams(Context ctx, SessionManager smBean ) {
247     
248         if (smBean != null) {
249             // The persistence-type controls what properties of the
250
// session manager can be configured
251

252             //take these 2 lines out
253
//String pType = smBean.getAttributeValue("persistence-type");
254
//persistence = PersistenceType.parseType(pType);
255

256             ManagerProperties mgrBean = smBean.getManagerProperties();
257             if ((mgrBean != null) && (mgrBean.sizeWebProperty() > 0)) {
258                 WebProperty[] props = mgrBean.getWebProperty();
259                 for (int i = 0; i < props.length; i++) {
260                     //String name = props[i].getAttributeValue("name");
261
//String value = props[i].getAttributeValue("value");
262
String JavaDoc name = props[i].getAttributeValue(WebProperty.NAME);
263                     String JavaDoc value = props[i].getAttributeValue(WebProperty.VALUE);
264                     if (name.equalsIgnoreCase("reapIntervalSeconds")) {
265                         try {
266                            reapInterval = Integer.parseInt(value);
267                         } catch (NumberFormatException JavaDoc e) {
268                             // XXX need error message
269
}
270                     } else if (name.equalsIgnoreCase("maxSessions")) {
271                         try {
272                             maxSessions = Integer.parseInt(value);
273                         } catch (NumberFormatException JavaDoc e) {
274                             // XXX need error message
275
}
276                     } else if (name.equalsIgnoreCase("maxIdleBackupSeconds")) {
277                         try {
278                             maxIdleBackup = Integer.parseInt(value);
279                         } catch (NumberFormatException JavaDoc e) {
280                             // XXX need error message
281
}
282                     } else if (name.equalsIgnoreCase("sessionFilename")) {
283                         sessionFilename = value;
284                     } else if (name.equalsIgnoreCase("persistenceFrequency")) {
285                         _persistenceFrequency = value;
286                     } else {
287                         Object JavaDoc[] params = { name };
288                         _logger.log(Level.INFO, "webcontainer.notYet", params);
289                     }
290                 }
291             }
292
293             StoreProperties storeBean = smBean.getStoreProperties();
294             if ((storeBean != null) && (storeBean.sizeWebProperty() > 0)) {
295                 WebProperty[] props = storeBean.getWebProperty();
296                 for (int i = 0; i < props.length; i++) {
297                     //String name = props[i].getAttributeValue("name");
298
//String value = props[i].getAttributeValue("value");
299
String JavaDoc name = props[i].getAttributeValue(WebProperty.NAME);
300                     String JavaDoc value = props[i].getAttributeValue(WebProperty.VALUE);
301                     if (name.equalsIgnoreCase("reapIntervalSeconds")) {
302                         try {
303                            storeReapInterval = Integer.parseInt(value);
304                         } catch (NumberFormatException JavaDoc e) {
305                             // XXX need error message
306
}
307                     } else if (name.equalsIgnoreCase("directory")) {
308                         directory = value;
309                     } else if (name.equalsIgnoreCase("persistenceScope")) {
310                         _persistenceScope = value;
311                     } else {
312                         Object JavaDoc[] params = { name };
313                         _logger.log(Level.INFO, "webcontainer.notYet", params);
314                     }
315                 }
316             }
317         }
318     }
319     
320     protected String JavaDoc prependContextPathTo(String JavaDoc str, Context ctx) {
321         if(str == null)
322             return str;
323         String JavaDoc filePart = getFilePartOf(str);
324         if(filePart == null || filePart.equals("")) {
325             return null;
326         }
327         String JavaDoc strippedContextPath = stripNonAlphaNumericsExceptUnderscore(ctx.getPath());
328         String JavaDoc modifiedFilePart = null;
329         if(strippedContextPath != null && !strippedContextPath.equals("")) {
330             modifiedFilePart = strippedContextPath + "_" + filePart;
331         } else {
332             modifiedFilePart = filePart;
333         }
334         int lastSlashIdx = str.lastIndexOf(File.separator);
335         String JavaDoc result = null;
336         if(lastSlashIdx == -1) {
337             result = modifiedFilePart;
338         } else {
339             String JavaDoc firstPart = str.substring(0, lastSlashIdx);
340             result = firstPart + File.separator + modifiedFilePart;
341         }
342         //System.out.println("BasePersistenceStrategyBuilder>>prependContextPathTo: " + result);
343
return result;
344     }
345     
346     protected String JavaDoc getFilePartOf(String JavaDoc str) {
347         if(str == null)
348             return str;
349         int lastSlashIdx = str.lastIndexOf(File.separator);
350         String JavaDoc result = null;
351         if(lastSlashIdx == -1) {
352             result = cleanFileParts(str);
353         } else {
354             result = cleanFileParts(str.substring(lastSlashIdx + 1, str.length()));
355         }
356         //System.out.println("getFilePartOf result = " + result);
357
return result;
358     }
359     
360     private String JavaDoc cleanFileParts(String JavaDoc fileString) {
361         String JavaDoc fileMainPart = getFileMainPart(fileString);
362         String JavaDoc fileSuffixPart = getFileSuffixPart(fileString);
363         if(fileMainPart == null) {
364             return null;
365         }
366         if(fileSuffixPart == null) {
367             return fileMainPart;
368         } else {
369             return fileMainPart + "." + fileSuffixPart;
370         }
371     }
372     
373     private String JavaDoc getFileMainPart(String JavaDoc fileString) {
374         ArrayList JavaDoc results = new ArrayList JavaDoc();
375         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(fileString, ".");
376         while (st.hasMoreTokens()) {
377             results.add(st.nextToken());
378         }
379         if(results.size() > 0) {
380             return stripNonAlphaNumericsExceptUnderscore((String JavaDoc)results.get(0));
381         } else {
382             return null;
383         }
384     }
385     
386     private String JavaDoc getFileSuffixPart(String JavaDoc fileString) {
387         ArrayList JavaDoc results = new ArrayList JavaDoc();
388         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(fileString, ".");
389         while (st.hasMoreTokens()) {
390             results.add(st.nextToken());
391         }
392         if(results.size() > 1) {
393             return stripNonAlphaNumericsExceptUnderscore((String JavaDoc)results.get(1));
394         } else {
395             return null;
396         }
397     }
398     
399     /**
400      * this method strips out all non-alpha characters
401      *
402      * @param inputString
403      */

404     protected String JavaDoc stripNonAlphas(String JavaDoc inputString) {
405         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(50);
406         for(int i=0; i<inputString.length(); i++) {
407             char nextChar = inputString.charAt(i);
408             if(Character.isLetter(nextChar)) {
409                 sb.append(nextChar);
410             }
411         }
412         return sb.toString();
413     }
414     
415     /**
416      * this method strips out all non-alphanumeric characters
417      *
418      * @param inputString
419      */

420     protected String JavaDoc stripNonAlphaNumericsExceptUnderscore(String JavaDoc inputString) {
421         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(50);
422         for(int i=0; i<inputString.length(); i++) {
423             char nextChar = inputString.charAt(i);
424             if(Character.isLetterOrDigit(nextChar) || "_".equals(String.valueOf(nextChar))) {
425                 sb.append(nextChar);
426             }
427         }
428         return sb.toString();
429     }
430     
431     /**
432      * this method strips out all non-alphanumeric characters
433      *
434      * @param inputString
435      */

436     protected String JavaDoc stripNonAlphaNumerics(String JavaDoc inputString) {
437         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(50);
438         for(int i=0; i<inputString.length(); i++) {
439             char nextChar = inputString.charAt(i);
440             if(Character.isLetterOrDigit(nextChar)) {
441                 sb.append(nextChar);
442             }
443         }
444         return sb.toString();
445     }
446     
447     public void setLogger(Logger JavaDoc logger) {
448         _logger = logger;
449     }
450     
451     public String JavaDoc getPersistenceFrequency() {
452         return _persistenceFrequency;
453     }
454     
455     public void setPersistenceFrequency(String JavaDoc persistenceFrequency) {
456         _persistenceFrequency = persistenceFrequency;
457     }
458     
459     public String JavaDoc getPersistenceScope() {
460         return _persistenceScope;
461     }
462     
463     public void setPersistenceScope(String JavaDoc persistenceScope) {
464         _persistenceScope = persistenceScope;
465     }
466     
467     /**
468      * The logger to use for logging ALL web container related messages.
469      */

470     protected Logger JavaDoc _logger = null;
471     
472     protected String JavaDoc directory = null;
473     protected String JavaDoc DEFAULT_SESSION_FILENAME = "SESSIONS.ser";
474     protected String JavaDoc sessionFilename = DEFAULT_SESSION_FILENAME;
475     protected String JavaDoc _persistenceFrequency = null;
476     protected String JavaDoc _persistenceScope = null;
477     protected int maxSessions = -1;
478     protected final int DEFAULT_REAP_INTERVAL = 60; // 1 minute
479
protected int reapInterval = DEFAULT_REAP_INTERVAL;
480     protected int storeReapInterval = DEFAULT_REAP_INTERVAL;
481     protected final int DEFAULT_MAX_IDLE_BACKUP = -1; // never save
482
protected int maxIdleBackup = DEFAULT_MAX_IDLE_BACKUP;
483     protected final int DEFAULT_SESSION_TIMEOUT = 1800; // 30 minute
484
protected int sessionMaxInactiveInterval = DEFAULT_SESSION_TIMEOUT;
485     //special constant for Java Server Faces
486
protected static final String JavaDoc JSF_HA_ENABLED = "com.sun.appserver.enableHighAvailability";
487     
488 }
489
Popular Tags