001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.terracotta.session.util;
006:
007: import com.tc.logging.TCLogger;
008: import com.tc.object.bytecode.ManagerUtil;
009: import com.tc.properties.TCProperties;
010: import com.terracotta.session.WebAppConfig;
011:
012: import java.util.ArrayList;
013:
014: import javax.servlet.http.HttpSessionAttributeListener;
015: import javax.servlet.http.HttpSessionListener;
016:
017: public class ConfigProperties {
018:
019: protected static final String PREFIX = "session.";
020: public static final String ID_LENGTH = PREFIX + "id.length";
021: public static final String SERVER_ID = PREFIX + "serverid";
022: public static final String COOKIE_DOMAIN = PREFIX + "cookie.domain";
023: public static final String COOKIE_COMMENT = PREFIX
024: + "cookie.comment";
025: public static final String COOKIE_SECURE = PREFIX + "cookie.secure";
026: public static final String COOKIE_MAX_AGE = PREFIX
027: + "cookie.maxage.seconds";
028: public static final String COOKIE_NAME = PREFIX + "cookie.name";
029: public static final String COOKIE_PATH = PREFIX + "cookie.path";
030: public static final String COOKIE_ENABLED = PREFIX
031: + "cookie.enabled";
032: public static final String SESSION_TIMEOUT_SECONDS = PREFIX
033: + "maxidle.seconds";
034: public static final String TRACKING_ENABLED = PREFIX
035: + "tracking.enabled";
036: public static final String URL_REWRITE_ENABLED = PREFIX
037: + "urlrewrite.enabled";
038: public static final String ATTRIBUTE_LISTENERS = PREFIX
039: + "attribute.listeners";
040: public static final String SESSION_LISTENERS = PREFIX + "listeners";
041: public static final String INVALIDATOR_SLEEP = PREFIX
042: + "invalidator.sleep";
043: public static final String REQUEST_BENCHES = PREFIX
044: + "request.bench.enabled";
045: public static final String INVALIDATOR_BENCHES = PREFIX
046: + "invalidator.bench.enabled";
047: public static final String STUCK_REQUEST_TRACKING = PREFIX
048: + "request.tracking";
049: public static final String STUCK_REQUEST_THREAD_DUMP = PREFIX
050: + "request.tracking.dump";
051: public static final String STUCK_REQUEST_INTERVAL = PREFIX
052: + "request.tracking.interval";
053: public static final String STUCK_REQUEST_THRESHOLD = PREFIX
054: + "request.tracking.threshold";
055: public static final String DEBUG_SERVER_HOPS = PREFIX
056: + "debug.hops";
057: public static final String DEBUG_SERVER_HOPS_INTERVAL = PREFIX
058: + "debug.hops.interval";
059: public static final String DEBUG_SESSION_INVALIDATE = PREFIX
060: + "debug.invalidate";
061: public static final String DELIMITER = PREFIX + "delimiter";
062: public static final String DEBUG_SESSIONS = PREFIX
063: + "debug.sessions";
064:
065: public static final boolean defaultCookiesEnabled = true;
066: public static final boolean defaultTrackingEnabled = true;
067: public static final boolean defaultUrlEnabled = true;
068: public static final String defaultCookieComment = null;
069: public static final String defaultCookieDomain = null;
070: public static final int defaultCookieMaxAge = -1;
071: public static final String defaultCookieName = "JSESSIONID";
072: public static final String defaultCookiePath = "/";
073: public static final boolean defaultCookieSecure = false;
074: public static final int defaultIdLength = 20;
075: public static final String defaultServerId = ManagerUtil
076: .getClientID();
077: public static final int defaultSessionTimeout = 30 * 60;
078: public static final String defaultDelimiter = "!";
079:
080: private final WebAppConfig wac;
081: private final TCProperties props;
082: private final ClassLoader loader;
083: private final TCLogger logger;
084:
085: public ConfigProperties(final WebAppConfig wac, ClassLoader loader) {
086: this (wac, ManagerUtil.getTCProperties(), loader, ManagerUtil
087: .getLogger("com.tc.session.ConfigProperties"));
088: }
089:
090: public ConfigProperties(final WebAppConfig wac,
091: final TCProperties props, TCLogger logger) {
092: this (wac, props, ConfigProperties.class.getClassLoader(),
093: logger);
094: }
095:
096: private ConfigProperties(final WebAppConfig wac,
097: final TCProperties props, ClassLoader loader,
098: TCLogger logger) {
099: Assert.pre(props != null);
100:
101: this .wac = wac;
102: this .props = props;
103: this .loader = loader;
104: this .logger = logger;
105: }
106:
107: public int getDebugServerHopsInterval() {
108: return props.getInt(DEBUG_SERVER_HOPS_INTERVAL);
109: }
110:
111: public boolean isDebugSessionInvalidate() {
112: return props.getBoolean(DEBUG_SESSION_INVALIDATE);
113: }
114:
115: public boolean isDebugServerHops() {
116: return props.getBoolean(DEBUG_SERVER_HOPS);
117: }
118:
119: public boolean isRequestTrackingEnabled() {
120: return props.getBoolean(STUCK_REQUEST_TRACKING);
121: }
122:
123: public boolean isDumpThreadsOnStuckRequests() {
124: return props.getBoolean(STUCK_REQUEST_THREAD_DUMP);
125: }
126:
127: public long getRequestTrackerSleepMillis() {
128: return props.getLong(STUCK_REQUEST_INTERVAL);
129: }
130:
131: public long getRequestTrackerStuckThresholdMillis() {
132: return props.getLong(STUCK_REQUEST_THRESHOLD);
133: }
134:
135: public int getSessionIdLength() {
136: String spcVal = getProperty(ID_LENGTH);
137: if (spcVal != null) {
138: return Integer.parseInt(spcVal);
139: }
140:
141: if (wac != null) {
142: return wac.__tc_session_getIdLength();
143: }
144:
145: return defaultIdLength;
146: }
147:
148: public int getCookieMaxAgeSeconds() {
149: String spcVal = getProperty(COOKIE_MAX_AGE);
150: if (spcVal != null) {
151: return Integer.parseInt(spcVal);
152: }
153:
154: if (wac != null) {
155: return wac.__tc_session_getCookieMaxAgeSecs();
156: }
157:
158: return defaultCookieMaxAge;
159: }
160:
161: public int getSessionTimeoutSeconds() {
162: String spcVal = getProperty(SESSION_TIMEOUT_SECONDS);
163: if (spcVal != null) {
164: return Integer.parseInt(spcVal);
165: }
166:
167: if (wac != null) {
168: return wac.__tc_session_getSessionTimeoutSecs();
169: }
170:
171: return defaultSessionTimeout;
172: }
173:
174: public int getInvalidatorSleepSeconds() {
175: return props.getInt(INVALIDATOR_SLEEP);
176: }
177:
178: public String getServerId() {
179: final String wacVal = wac == null ? null : wac
180: .__tc_session_getServerId();
181: return getStringVal(SERVER_ID, wacVal, defaultServerId);
182: }
183:
184: public String getDelimiter() {
185: final String wacVal = wac == null ? null : wac
186: .__tc_session_getSessionDelimiter();
187: return getStringVal(DELIMITER, wacVal, defaultDelimiter);
188: }
189:
190: public String getCookieDomain() {
191: final String wacVal = wac == null ? null : wac
192: .__tc_session_getCookieDomain();
193: return getStringVal(COOKIE_DOMAIN, wacVal, defaultCookieDomain);
194: }
195:
196: public String getCookieCoomment() {
197: final String wacVal = wac == null ? null : wac
198: .__tc_session_getCookieComment();
199: return getStringVal(COOKIE_COMMENT, wacVal,
200: defaultCookieComment);
201: }
202:
203: public String getCookieName() {
204: final String wacVal = wac == null ? null : wac
205: .__tc_session_getCookieName();
206: return getStringVal(COOKIE_NAME, wacVal, defaultCookieName);
207: }
208:
209: public String getCookiePath() {
210: final String wacVal = wac == null ? null : wac
211: .__tc_session_getCookiePath();
212: return getStringVal(COOKIE_PATH, wacVal, null);
213: }
214:
215: public boolean getCookieSecure() {
216: final String wacVal = wac == null ? null : Boolean.toString(wac
217: .__tc_session_getCookieSecure());
218: final String boolVal = getStringVal(COOKIE_SECURE, wacVal,
219: Boolean.toString(defaultCookieSecure));
220: return Boolean.valueOf(boolVal).booleanValue();
221: }
222:
223: public boolean getCookiesEnabled() {
224: final String wacVal = wac == null ? null : Boolean.toString(wac
225: .__tc_session_getCookiesEnabled());
226: final String boolVal = getStringVal(COOKIE_ENABLED, wacVal,
227: Boolean.toString(defaultCookiesEnabled));
228: return Boolean.valueOf(boolVal).booleanValue();
229: }
230:
231: public boolean getSessionTrackingEnabled() {
232: final String wacVal = wac == null ? null : Boolean.toString(wac
233: .__tc_session_getTrackingEnabled());
234: final String boolVal = getStringVal(TRACKING_ENABLED, wacVal,
235: Boolean.toString(defaultTrackingEnabled));
236: return Boolean.valueOf(boolVal).booleanValue();
237: }
238:
239: public boolean getUrlRewritingEnabled() {
240: final String wacVal = wac == null ? null : Boolean.toString(wac
241: .__tc_session_getURLRewritingEnabled());
242: final String boolVal = getStringVal(URL_REWRITE_ENABLED,
243: wacVal, Boolean.toString(defaultUrlEnabled));
244: return Boolean.valueOf(boolVal).booleanValue();
245: }
246:
247: public boolean isDebugSessions() {
248: return props.getBoolean(DEBUG_SESSIONS);
249: }
250:
251: public boolean getRequestLogBenchEnabled() {
252: return props.getBoolean(REQUEST_BENCHES);
253: }
254:
255: public boolean getInvalidatorLogBenchEnabled() {
256: return props.getBoolean(INVALIDATOR_BENCHES);
257: }
258:
259: public HttpSessionAttributeListener[] getSessionAttributeListeners() {
260: final String list = getProperty(ATTRIBUTE_LISTENERS);
261: if (list == null)
262: return (wac == null) ? null : wac
263: .__tc_session_getHttpSessionAttributeListeners();
264: final String[] types = list.split(",");
265: if (types == null || types.length == 0)
266: return null;
267: ArrayList listeners = new ArrayList();
268: for (int i = 0; i < types.length; i++) {
269: HttpSessionAttributeListener l = makeAttributeListener(types[i]);
270: if (l != null)
271: listeners.add(l);
272: }
273: final HttpSessionAttributeListener[] rv = new HttpSessionAttributeListener[listeners
274: .size()];
275: listeners.toArray(rv);
276: return rv;
277: }
278:
279: public HttpSessionListener[] getSessionListeners() {
280: final String list = getProperty(SESSION_LISTENERS);
281: if (list == null)
282: return (wac == null) ? null : wac
283: .__tc_session_getHttpSessionListener();
284: final String[] types = list.split(",");
285: if (types == null || types.length == 0)
286: return null;
287: ArrayList listeners = new ArrayList();
288: for (int i = 0; i < types.length; i++) {
289: HttpSessionListener l = makeSessionListener(types[i]);
290: if (l != null)
291: listeners.add(l);
292: }
293: final HttpSessionListener[] rv = new HttpSessionListener[listeners
294: .size()];
295: listeners.toArray(rv);
296: return rv;
297: }
298:
299: protected HttpSessionAttributeListener makeAttributeListener(
300: String type) {
301: type = type.trim();
302: HttpSessionAttributeListener rv = null;
303: try {
304: final Class c = Class.forName(type, true, loader);
305: if (HttpSessionAttributeListener.class.isAssignableFrom(c))
306: rv = (HttpSessionAttributeListener) c.newInstance();
307: } catch (Exception e) {
308: logger.error("Error instantiaing listener of type " + type,
309: e);
310: return null;
311: }
312: return rv;
313: }
314:
315: protected HttpSessionListener makeSessionListener(String type) {
316: type = type.trim();
317: HttpSessionListener rv = null;
318: try {
319: final Class c = Class.forName(type, true, loader);
320: if (HttpSessionListener.class.isAssignableFrom(c))
321: rv = (HttpSessionListener) c.newInstance();
322: } catch (Exception e) {
323: logger.error("Error instantiaing listener of type " + type,
324: e);
325: return null;
326: }
327: return rv;
328: }
329:
330: protected String getStringVal(final String propName,
331: final String wacVal, final String defVal) {
332: String spcVal = getProperty(propName);
333: if (spcVal != null)
334: return spcVal;
335: if (wacVal != null)
336: return wacVal;
337: return defVal;
338: }
339:
340: protected String getProperty(final String propName) {
341: String rv = props.getProperty(propName, true);
342: if (rv == null)
343: return null;
344: rv = rv.trim();
345: if (rv.length() == 0)
346: return null;
347: else
348: return rv;
349: }
350:
351: }
|