1 23 24 29 30 package com.sun.enterprise.web; 31 32 import java.io.File ; 33 import java.util.ArrayList ; 34 import java.util.StringTokenizer ; 35 import java.util.logging.Logger ; 36 import java.util.logging.Level ; 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.deployment.runtime.web.SessionManager; 43 import com.sun.enterprise.deployment.runtime.web.StoreProperties; 46 import com.sun.enterprise.deployment.runtime.web.WebProperty; 49 import com.sun.enterprise.web.session.PersistenceType; 52 54 60 import com.sun.enterprise.config.serverbeans.ElementProperty; 61 63 65 public abstract class BasePersistenceStrategyBuilder implements PersistenceStrategyBuilder { 66 67 68 public BasePersistenceStrategyBuilder(Logger logger) { 69 _logger = logger; 70 } 71 72 73 public BasePersistenceStrategyBuilder() { 74 } 75 76 public void initializePersistenceStrategy(Context ctx, SessionManager smBean ) { 77 78 this.setDefaultParams(ctx, smBean ); 82 83 this.readInstanceLevelParams(); 88 89 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 106 sessionFilename = DEFAULT_SESSION_FILENAME; 108 109 storeReapInterval = DEFAULT_REAP_INTERVAL; 111 112 directory = ((StandardContext) ctx).getWorkDir(); 113 } 114 115 116 public void readInstanceLevelParams() { 117 ServerConfigLookup lookup = new ServerConfigLookup(); 119 com.sun.enterprise.config.serverbeans.SessionManager smBean = 120 lookup.getInstanceSessionManager(); 122 123 if (smBean != null) { 124 127 131 com.sun.enterprise.config.serverbeans.ManagerProperties mgrBean = 132 smBean.getManagerProperties(); 134 if (mgrBean != null) { 135 String reapIntervalInSecondsString = mgrBean.getReapIntervalInSeconds(); 137 if (reapIntervalInSecondsString != null) { 138 try { 139 reapInterval = Integer.parseInt(reapIntervalInSecondsString); 140 _logger.finest(" mgr reapInterval set = " + reapInterval); 141 } catch (NumberFormatException e) { 142 } 144 } else { 145 if(_logger.isLoggable(Level.FINEST)) { 146 _logger.finest("no instance level value set for mgr reapInterval"); 147 } 148 152 } 153 String maxSessionsString = mgrBean.getMaxSessions(); 155 if (maxSessionsString != null) { 156 try { 157 maxSessions = Integer.parseInt(maxSessionsString); 158 _logger.finest("maxSessions set = " + maxSessions); 159 } catch (NumberFormatException e) { 160 } 162 } else { 163 if(_logger.isLoggable(Level.FINEST)) { 164 _logger.finest("no instance level value set for maxSessions"); 165 } 166 170 } 171 String sessionFilenameString = mgrBean.getSessionFileName(); 173 if (sessionFilenameString != null) { 174 sessionFilename = sessionFilenameString; 175 _logger.finest("sessionFilename set = " + sessionFilename); 176 } 177 178 com.sun.enterprise.config.serverbeans.ElementProperty[] props = 180 mgrBean.getElementProperty(); 181 for (int i = 0; i < props.length; i++) { 182 String name = props[i].getAttributeValue("name"); 183 String value = props[i].getAttributeValue("value"); 184 if (name.equalsIgnoreCase("maxIdleBackupSeconds")) { 186 try { 187 maxIdleBackup = Integer.parseInt(value); 188 } catch (NumberFormatException e) { 189 } 191 } 192 } 193 } 194 195 com.sun.enterprise.config.serverbeans.StoreProperties storeBean = 196 smBean.getStoreProperties(); 198 199 if (storeBean != null) { 200 String reapIntervalInSecondsString = storeBean.getReapIntervalInSeconds(); 202 if (reapIntervalInSecondsString != null) { 203 try { 204 storeReapInterval = Integer.parseInt(reapIntervalInSecondsString); 205 _logger.finest("storeReapInterval set = " + storeReapInterval); 206 } catch (NumberFormatException e) { 207 } 209 } 210 String directoryString = storeBean.getDirectory(); 212 if (directoryString != null) { 213 directory = directoryString; 214 _logger.finest("directory set = " + directoryString); 215 } 216 } 217 } 218 219 com.sun.enterprise.config.serverbeans.SessionProperties spBean = 221 lookup.getInstanceSessionProperties(); 222 223 if (spBean != null) { 224 String timeoutSecondsString = spBean.getTimeoutInSeconds(); 226 if (timeoutSecondsString != null) { 227 try { 228 sessionMaxInactiveInterval = Integer.parseInt(timeoutSecondsString); 229 _logger.finest("sessionMaxInactiveInterval set = " + sessionMaxInactiveInterval); 230 } catch (NumberFormatException e) { 231 } 233 } else { 234 if(_logger.isLoggable(Level.FINEST)) { 235 _logger.finest("no instance level value set for sessionMaxInactiveInterval"); 236 } 237 241 } 242 } 243 244 } 245 246 public void readWebAppParams(Context ctx, SessionManager smBean ) { 247 248 if (smBean != null) { 249 252 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(WebProperty.NAME); 263 String value = props[i].getAttributeValue(WebProperty.VALUE); 264 if (name.equalsIgnoreCase("reapIntervalSeconds")) { 265 try { 266 reapInterval = Integer.parseInt(value); 267 } catch (NumberFormatException e) { 268 } 270 } else if (name.equalsIgnoreCase("maxSessions")) { 271 try { 272 maxSessions = Integer.parseInt(value); 273 } catch (NumberFormatException e) { 274 } 276 } else if (name.equalsIgnoreCase("maxIdleBackupSeconds")) { 277 try { 278 maxIdleBackup = Integer.parseInt(value); 279 } catch (NumberFormatException e) { 280 } 282 } else if (name.equalsIgnoreCase("sessionFilename")) { 283 sessionFilename = value; 284 } else if (name.equalsIgnoreCase("persistenceFrequency")) { 285 _persistenceFrequency = value; 286 } else { 287 Object [] 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(WebProperty.NAME); 300 String value = props[i].getAttributeValue(WebProperty.VALUE); 301 if (name.equalsIgnoreCase("reapIntervalSeconds")) { 302 try { 303 storeReapInterval = Integer.parseInt(value); 304 } catch (NumberFormatException e) { 305 } 307 } else if (name.equalsIgnoreCase("directory")) { 308 directory = value; 309 } else if (name.equalsIgnoreCase("persistenceScope")) { 310 _persistenceScope = value; 311 } else { 312 Object [] params = { name }; 313 _logger.log(Level.INFO, "webcontainer.notYet", params); 314 } 315 } 316 } 317 } 318 } 319 320 protected String prependContextPathTo(String str, Context ctx) { 321 if(str == null) 322 return str; 323 String filePart = getFilePartOf(str); 324 if(filePart == null || filePart.equals("")) { 325 return null; 326 } 327 String strippedContextPath = stripNonAlphaNumericsExceptUnderscore(ctx.getPath()); 328 String 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 result = null; 336 if(lastSlashIdx == -1) { 337 result = modifiedFilePart; 338 } else { 339 String firstPart = str.substring(0, lastSlashIdx); 340 result = firstPart + File.separator + modifiedFilePart; 341 } 342 return result; 344 } 345 346 protected String getFilePartOf(String str) { 347 if(str == null) 348 return str; 349 int lastSlashIdx = str.lastIndexOf(File.separator); 350 String result = null; 351 if(lastSlashIdx == -1) { 352 result = cleanFileParts(str); 353 } else { 354 result = cleanFileParts(str.substring(lastSlashIdx + 1, str.length())); 355 } 356 return result; 358 } 359 360 private String cleanFileParts(String fileString) { 361 String fileMainPart = getFileMainPart(fileString); 362 String 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 getFileMainPart(String fileString) { 374 ArrayList results = new ArrayList (); 375 StringTokenizer st = new StringTokenizer (fileString, "."); 376 while (st.hasMoreTokens()) { 377 results.add(st.nextToken()); 378 } 379 if(results.size() > 0) { 380 return stripNonAlphaNumericsExceptUnderscore((String )results.get(0)); 381 } else { 382 return null; 383 } 384 } 385 386 private String getFileSuffixPart(String fileString) { 387 ArrayList results = new ArrayList (); 388 StringTokenizer st = new StringTokenizer (fileString, "."); 389 while (st.hasMoreTokens()) { 390 results.add(st.nextToken()); 391 } 392 if(results.size() > 1) { 393 return stripNonAlphaNumericsExceptUnderscore((String )results.get(1)); 394 } else { 395 return null; 396 } 397 } 398 399 404 protected String stripNonAlphas(String inputString) { 405 StringBuffer sb = new StringBuffer (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 420 protected String stripNonAlphaNumericsExceptUnderscore(String inputString) { 421 StringBuffer sb = new StringBuffer (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 436 protected String stripNonAlphaNumerics(String inputString) { 437 StringBuffer sb = new StringBuffer (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 logger) { 448 _logger = logger; 449 } 450 451 public String getPersistenceFrequency() { 452 return _persistenceFrequency; 453 } 454 455 public void setPersistenceFrequency(String persistenceFrequency) { 456 _persistenceFrequency = persistenceFrequency; 457 } 458 459 public String getPersistenceScope() { 460 return _persistenceScope; 461 } 462 463 public void setPersistenceScope(String persistenceScope) { 464 _persistenceScope = persistenceScope; 465 } 466 467 470 protected Logger _logger = null; 471 472 protected String directory = null; 473 protected String DEFAULT_SESSION_FILENAME = "SESSIONS.ser"; 474 protected String sessionFilename = DEFAULT_SESSION_FILENAME; 475 protected String _persistenceFrequency = null; 476 protected String _persistenceScope = null; 477 protected int maxSessions = -1; 478 protected final int DEFAULT_REAP_INTERVAL = 60; protected int reapInterval = DEFAULT_REAP_INTERVAL; 480 protected int storeReapInterval = DEFAULT_REAP_INTERVAL; 481 protected final int DEFAULT_MAX_IDLE_BACKUP = -1; protected int maxIdleBackup = DEFAULT_MAX_IDLE_BACKUP; 483 protected final int DEFAULT_SESSION_TIMEOUT = 1800; protected int sessionMaxInactiveInterval = DEFAULT_SESSION_TIMEOUT; 485 protected static final String JSF_HA_ENABLED = "com.sun.appserver.enableHighAvailability"; 487 488 } 489 | Popular Tags |