KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > terracotta > session > util > ConfigProperties


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.terracotta.session.util;
6
7 import com.tc.object.bytecode.ManagerUtil;
8 import com.tc.properties.TCProperties;
9 import com.terracotta.session.WebAppConfig;
10
11 import java.util.ArrayList JavaDoc;
12
13 import javax.servlet.http.HttpSessionAttributeListener JavaDoc;
14 import javax.servlet.http.HttpSessionListener JavaDoc;
15
16 public class ConfigProperties {
17
18   protected static final String JavaDoc PREFIX = "session.";
19   public static final String JavaDoc ID_LENGTH = PREFIX + "id.length";
20   public static final String JavaDoc SERVER_ID = PREFIX + "serverid";
21   public static final String JavaDoc COOKIE_DOMAIN = PREFIX + "cookie.domain";
22   public static final String JavaDoc COOKIE_COMMENT = PREFIX + "cookie.comment";
23   public static final String JavaDoc COOKIE_SECURE = PREFIX + "cookie.secure";
24   public static final String JavaDoc COOKIE_MAX_AGE = PREFIX + "cookie.maxage.seconds";
25   public static final String JavaDoc COOKIE_NAME = PREFIX + "cookie.name";
26   public static final String JavaDoc COOKIE_PATH = PREFIX + "cookie.path";
27   public static final String JavaDoc COOKIE_ENABLED = PREFIX + "cookie.enabled";
28   public static final String JavaDoc SESSION_TIMEOUT_SECONDS = PREFIX + "maxidle.seconds";
29   public static final String JavaDoc TRACKING_ENABLED = PREFIX + "tracking.enabled";
30   public static final String JavaDoc URL_REWRITE_ENABLED = PREFIX + "urlrewrite.enabled";
31   public static final String JavaDoc ATTRIBUTE_LISTENERS = PREFIX + "attribute.listeners";
32   public static final String JavaDoc SESSION_LISTENERS = PREFIX + "listeners";
33   public static final String JavaDoc INVALIDATOR_SLEEP = PREFIX + "invalidator.sleep";
34   public static final String JavaDoc REQUEST_BENCHES = PREFIX + "request.bench.enabled";
35   public static final String JavaDoc INVALIDATOR_BENCHES = PREFIX + "invalidator.bench.enabled";
36   public static final String JavaDoc STUCK_REQUEST_TRACKING = PREFIX + "request.tracking";
37   public static final String JavaDoc STUCK_REQUEST_THREAD_DUMP = PREFIX + "request.tracking.dump";
38   public static final String JavaDoc STUCK_REQUEST_INTERVAL = PREFIX + "request.tracking.interval";
39   public static final String JavaDoc STUCK_REQUEST_THRESHOLD = PREFIX + "request.tracking.threshold";
40   public static final String JavaDoc DEBUG_SERVER_HOPS = PREFIX + "debug.hops";
41   public static final String JavaDoc DEBUG_SERVER_HOPS_INTERVAL = PREFIX + "debug.hops.interval";
42
43   protected static final boolean defaultCookiesEnabled = true;
44   protected static final boolean defaultTrackingEnabled = true;
45   protected static final boolean defaultUrlEnabled = true;
46   protected static final String JavaDoc defaultCookieComment = null;
47   protected static final String JavaDoc defaultCookieDomain = null;
48   protected static final int defaultCookieMaxAge = -1;
49   protected static final String JavaDoc defaultCookieName = "JSESSIONID";
50   protected static final String JavaDoc defaultCookiePath = "/";
51   protected static final boolean defaultCookieSecure = false;
52   protected static final int defaultIdLength = 20;
53   protected static final String JavaDoc defaultServerId = ManagerUtil.getClientID();
54   protected static final int defaultSessionTimeout = 30 * 60;
55
56   private final WebAppConfig wac;
57   private final TCProperties props;
58
59   public ConfigProperties(final WebAppConfig wac) {
60     this(wac, ManagerUtil.getTCProperties());
61   }
62
63   public ConfigProperties(final WebAppConfig wac, final TCProperties props) {
64     Assert.pre(props != null);
65
66     this.wac = wac;
67     this.props = props;
68   }
69
70   public int getDebugServerHopsInterval() {
71     return props.getInt(DEBUG_SERVER_HOPS_INTERVAL);
72   }
73
74   public boolean isDebugServerHops() {
75     return props.getBoolean(DEBUG_SERVER_HOPS);
76   }
77
78   public boolean isRequestTrackingEnabled() {
79     return props.getBoolean(STUCK_REQUEST_TRACKING);
80   }
81
82   public boolean isDumpThreadsOnStuckRequests() {
83     return props.getBoolean(STUCK_REQUEST_THREAD_DUMP);
84   }
85
86   public long getRequestTrackerSleepMillis() {
87     return props.getLong(STUCK_REQUEST_INTERVAL);
88   }
89
90   public long getRequestTrackerStuckThresholdMillis() {
91     return props.getLong(STUCK_REQUEST_THRESHOLD);
92   }
93
94   public int getSessionIdLength() {
95     final int wacVal = (wac == null) ? -1 : wac.__tc_session_getIdLength();
96     final int rv = getIntVal(ID_LENGTH, wacVal, defaultIdLength, -1);
97     return Math.max(1, rv);
98   }
99
100   public int getCookieMaxAgeSeconds() {
101     final int wacVal = (wac == null) ? -2 : wac.__tc_session_getCookieMaxAgeSecs();
102     final int rv = getIntVal(COOKIE_MAX_AGE, wacVal, defaultCookieMaxAge, -2);
103     return Math.max(-1, rv);
104   }
105
106   public int getSessionTimeoutSeconds() {
107     final int wacVal = (wac == null) ? -1 : wac.__tc_session_getSessionTimeoutSecs();
108     final int rv = getIntVal(SESSION_TIMEOUT_SECONDS, wacVal, defaultSessionTimeout, -1);
109     return Math.max(1, rv);
110   }
111
112   public int getInvalidatorSleepSeconds() {
113     return props.getInt(INVALIDATOR_SLEEP);
114   }
115
116   public String JavaDoc getServerId() {
117     final String JavaDoc wacVal = wac == null ? null : wac.__tc_session_getServerId();
118     return getStringVal(SERVER_ID, wacVal, defaultServerId);
119   }
120
121   public String JavaDoc getCookieDomain() {
122     final String JavaDoc wacVal = wac == null ? null : wac.__tc_session_getCookieDomain();
123     return getStringVal(COOKIE_DOMAIN, wacVal, defaultCookieDomain);
124   }
125
126   public String JavaDoc getCookieCoomment() {
127     final String JavaDoc wacVal = wac == null ? null : wac.__tc_session_getCookieComment();
128     return getStringVal(COOKIE_COMMENT, wacVal, defaultCookieComment);
129   }
130
131   public String JavaDoc getCookieName() {
132     final String JavaDoc wacVal = wac == null ? null : wac.__tc_session_getCookieName();
133     return getStringVal(COOKIE_NAME, wacVal, defaultCookieName);
134   }
135
136   public String JavaDoc getCookiePath() {
137     final String JavaDoc wacVal = wac == null ? null : wac.__tc_session_getCookiePath();
138     return getStringVal(COOKIE_PATH, wacVal, defaultCookiePath);
139   }
140
141   public boolean getCookieSecure() {
142     final String JavaDoc wacVal = wac == null ? null : Boolean.toString(wac.__tc_session_getCookieSecure());
143     final String JavaDoc boolVal = getStringVal(COOKIE_SECURE, wacVal, Boolean.toString(defaultCookieSecure));
144     return "true".equals(boolVal);
145   }
146
147   public boolean getCookiesEnabled() {
148     final String JavaDoc wacVal = wac == null ? null : Boolean.toString(wac.__tc_session_getCookiesEnabled());
149     final String JavaDoc boolVal = getStringVal(COOKIE_ENABLED, wacVal, Boolean.toString(defaultCookiesEnabled));
150     return "true".equals(boolVal);
151   }
152
153   public boolean getSessionTrackingEnabled() {
154     final String JavaDoc wacVal = wac == null ? null : Boolean.toString(wac.__tc_session_getTrackingEnabled());
155     final String JavaDoc boolVal = getStringVal(TRACKING_ENABLED, wacVal, Boolean.toString(defaultTrackingEnabled));
156     return "true".equals(boolVal);
157   }
158
159   public boolean getUrlRewritingEnabled() {
160     final String JavaDoc wacVal = wac == null ? null : Boolean.toString(wac.__tc_session_getURLRewritingEnabled());
161     final String JavaDoc boolVal = getStringVal(URL_REWRITE_ENABLED, wacVal, Boolean.toString(defaultUrlEnabled));
162     return "true".equals(boolVal);
163   }
164
165   public boolean getRequestLogBenchEnabled() {
166     return props.getBoolean(REQUEST_BENCHES);
167   }
168
169   public boolean getInvalidatorLogBenchEnabled() {
170     return props.getBoolean(INVALIDATOR_BENCHES);
171   }
172
173   public HttpSessionAttributeListener JavaDoc[] getSessionAttributeListeners() {
174     final String JavaDoc list = getProperty(ATTRIBUTE_LISTENERS);
175     if (list == null) return (wac == null) ? null : wac.__tc_session_getHttpSessionAttributeListeners();
176     final String JavaDoc[] types = list.split(",");
177     if (types == null || types.length == 0) return null;
178     ArrayList JavaDoc listeners = new ArrayList JavaDoc();
179     for (int i = 0; i < types.length; i++) {
180       HttpSessionAttributeListener JavaDoc l = makeAttributeListener(types[i]);
181       if (l != null) listeners.add(l);
182     }
183     final HttpSessionAttributeListener JavaDoc[] rv = new HttpSessionAttributeListener JavaDoc[listeners.size()];
184     listeners.toArray(rv);
185     return rv;
186   }
187
188   public HttpSessionListener JavaDoc[] getSessionListeners() {
189     final String JavaDoc list = getProperty(SESSION_LISTENERS);
190     if (list == null) return (wac == null) ? null : wac.__tc_session_getHttpSessionListener();
191     final String JavaDoc[] types = list.split(",");
192     if (types == null || types.length == 0) return null;
193     ArrayList JavaDoc listeners = new ArrayList JavaDoc();
194     for (int i = 0; i < types.length; i++) {
195       HttpSessionListener JavaDoc l = makeSessionListener(types[i]);
196       if (l != null) listeners.add(l);
197     }
198     final HttpSessionListener JavaDoc[] rv = new HttpSessionListener JavaDoc[listeners.size()];
199     listeners.toArray(rv);
200     return rv;
201   }
202
203   protected static HttpSessionAttributeListener JavaDoc makeAttributeListener(String JavaDoc type) {
204     type = type.trim();
205     HttpSessionAttributeListener JavaDoc rv = null;
206     try {
207       final Class JavaDoc c = Class.forName(type);
208       if (HttpSessionAttributeListener JavaDoc.class.isAssignableFrom(c)) rv = (HttpSessionAttributeListener JavaDoc) c.newInstance();
209     } catch (Exception JavaDoc e) {
210       // TODO: log message here...
211
return null;
212     }
213     return rv;
214   }
215
216   protected static HttpSessionListener JavaDoc makeSessionListener(String JavaDoc type) {
217     type = type.trim();
218     HttpSessionListener JavaDoc rv = null;
219     try {
220       final Class JavaDoc c = Class.forName(type);
221       if (HttpSessionListener JavaDoc.class.isAssignableFrom(c)) rv = (HttpSessionListener JavaDoc) c.newInstance();
222     } catch (Exception JavaDoc e) {
223       // TODO: log message here...
224
return null;
225     }
226     return rv;
227   }
228
229   protected String JavaDoc getStringVal(final String JavaDoc propName, final String JavaDoc wacVal, final String JavaDoc defVal) {
230     String JavaDoc spcVal = getProperty(propName);
231     if (spcVal != null) return spcVal;
232     if (wacVal != null) return wacVal;
233     return defVal;
234   }
235
236   protected int getIntVal(String JavaDoc propName, int wacVal, int defVal, int invalidVal) {
237     final int spcVal = getPropertyInt(propName, invalidVal);
238     if (spcVal != invalidVal) return spcVal;
239     if (wacVal != invalidVal) return wacVal;
240     return defVal;
241   }
242
243   protected String JavaDoc getProperty(final String JavaDoc propName) {
244     String JavaDoc rv = props.getProperty(propName, true);
245     if (rv == null) return null;
246     rv = rv.trim();
247     if (rv.length() == 0) return null;
248     else return rv;
249   }
250
251   protected int getPropertyInt(String JavaDoc propName, int defVal) {
252     final String JavaDoc val = props.getProperty(propName, true);
253     if (val == null || val.length() == 0) return defVal;
254     try {
255       return Integer.parseInt(val);
256     } catch (Exception JavaDoc e) {
257       return defVal;
258     }
259   }
260
261 }
262
Popular Tags