1 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 ; 12 13 import javax.servlet.http.HttpSessionAttributeListener ; 14 import javax.servlet.http.HttpSessionListener ; 15 16 public class ConfigProperties { 17 18 protected static final String PREFIX = "session."; 19 public static final String ID_LENGTH = PREFIX + "id.length"; 20 public static final String SERVER_ID = PREFIX + "serverid"; 21 public static final String COOKIE_DOMAIN = PREFIX + "cookie.domain"; 22 public static final String COOKIE_COMMENT = PREFIX + "cookie.comment"; 23 public static final String COOKIE_SECURE = PREFIX + "cookie.secure"; 24 public static final String COOKIE_MAX_AGE = PREFIX + "cookie.maxage.seconds"; 25 public static final String COOKIE_NAME = PREFIX + "cookie.name"; 26 public static final String COOKIE_PATH = PREFIX + "cookie.path"; 27 public static final String COOKIE_ENABLED = PREFIX + "cookie.enabled"; 28 public static final String SESSION_TIMEOUT_SECONDS = PREFIX + "maxidle.seconds"; 29 public static final String TRACKING_ENABLED = PREFIX + "tracking.enabled"; 30 public static final String URL_REWRITE_ENABLED = PREFIX + "urlrewrite.enabled"; 31 public static final String ATTRIBUTE_LISTENERS = PREFIX + "attribute.listeners"; 32 public static final String SESSION_LISTENERS = PREFIX + "listeners"; 33 public static final String INVALIDATOR_SLEEP = PREFIX + "invalidator.sleep"; 34 public static final String REQUEST_BENCHES = PREFIX + "request.bench.enabled"; 35 public static final String INVALIDATOR_BENCHES = PREFIX + "invalidator.bench.enabled"; 36 public static final String STUCK_REQUEST_TRACKING = PREFIX + "request.tracking"; 37 public static final String STUCK_REQUEST_THREAD_DUMP = PREFIX + "request.tracking.dump"; 38 public static final String STUCK_REQUEST_INTERVAL = PREFIX + "request.tracking.interval"; 39 public static final String STUCK_REQUEST_THRESHOLD = PREFIX + "request.tracking.threshold"; 40 public static final String DEBUG_SERVER_HOPS = PREFIX + "debug.hops"; 41 public static final String 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 defaultCookieComment = null; 47 protected static final String defaultCookieDomain = null; 48 protected static final int defaultCookieMaxAge = -1; 49 protected static final String defaultCookieName = "JSESSIONID"; 50 protected static final String defaultCookiePath = "/"; 51 protected static final boolean defaultCookieSecure = false; 52 protected static final int defaultIdLength = 20; 53 protected static final String 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 getServerId() { 117 final String wacVal = wac == null ? null : wac.__tc_session_getServerId(); 118 return getStringVal(SERVER_ID, wacVal, defaultServerId); 119 } 120 121 public String getCookieDomain() { 122 final String wacVal = wac == null ? null : wac.__tc_session_getCookieDomain(); 123 return getStringVal(COOKIE_DOMAIN, wacVal, defaultCookieDomain); 124 } 125 126 public String getCookieCoomment() { 127 final String wacVal = wac == null ? null : wac.__tc_session_getCookieComment(); 128 return getStringVal(COOKIE_COMMENT, wacVal, defaultCookieComment); 129 } 130 131 public String getCookieName() { 132 final String wacVal = wac == null ? null : wac.__tc_session_getCookieName(); 133 return getStringVal(COOKIE_NAME, wacVal, defaultCookieName); 134 } 135 136 public String getCookiePath() { 137 final String wacVal = wac == null ? null : wac.__tc_session_getCookiePath(); 138 return getStringVal(COOKIE_PATH, wacVal, defaultCookiePath); 139 } 140 141 public boolean getCookieSecure() { 142 final String wacVal = wac == null ? null : Boolean.toString(wac.__tc_session_getCookieSecure()); 143 final String boolVal = getStringVal(COOKIE_SECURE, wacVal, Boolean.toString(defaultCookieSecure)); 144 return "true".equals(boolVal); 145 } 146 147 public boolean getCookiesEnabled() { 148 final String wacVal = wac == null ? null : Boolean.toString(wac.__tc_session_getCookiesEnabled()); 149 final String boolVal = getStringVal(COOKIE_ENABLED, wacVal, Boolean.toString(defaultCookiesEnabled)); 150 return "true".equals(boolVal); 151 } 152 153 public boolean getSessionTrackingEnabled() { 154 final String wacVal = wac == null ? null : Boolean.toString(wac.__tc_session_getTrackingEnabled()); 155 final String boolVal = getStringVal(TRACKING_ENABLED, wacVal, Boolean.toString(defaultTrackingEnabled)); 156 return "true".equals(boolVal); 157 } 158 159 public boolean getUrlRewritingEnabled() { 160 final String wacVal = wac == null ? null : Boolean.toString(wac.__tc_session_getURLRewritingEnabled()); 161 final String 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 [] getSessionAttributeListeners() { 174 final String list = getProperty(ATTRIBUTE_LISTENERS); 175 if (list == null) return (wac == null) ? null : wac.__tc_session_getHttpSessionAttributeListeners(); 176 final String [] types = list.split(","); 177 if (types == null || types.length == 0) return null; 178 ArrayList listeners = new ArrayList (); 179 for (int i = 0; i < types.length; i++) { 180 HttpSessionAttributeListener l = makeAttributeListener(types[i]); 181 if (l != null) listeners.add(l); 182 } 183 final HttpSessionAttributeListener [] rv = new HttpSessionAttributeListener [listeners.size()]; 184 listeners.toArray(rv); 185 return rv; 186 } 187 188 public HttpSessionListener [] getSessionListeners() { 189 final String list = getProperty(SESSION_LISTENERS); 190 if (list == null) return (wac == null) ? null : wac.__tc_session_getHttpSessionListener(); 191 final String [] types = list.split(","); 192 if (types == null || types.length == 0) return null; 193 ArrayList listeners = new ArrayList (); 194 for (int i = 0; i < types.length; i++) { 195 HttpSessionListener l = makeSessionListener(types[i]); 196 if (l != null) listeners.add(l); 197 } 198 final HttpSessionListener [] rv = new HttpSessionListener [listeners.size()]; 199 listeners.toArray(rv); 200 return rv; 201 } 202 203 protected static HttpSessionAttributeListener makeAttributeListener(String type) { 204 type = type.trim(); 205 HttpSessionAttributeListener rv = null; 206 try { 207 final Class c = Class.forName(type); 208 if (HttpSessionAttributeListener .class.isAssignableFrom(c)) rv = (HttpSessionAttributeListener ) c.newInstance(); 209 } catch (Exception e) { 210 return null; 212 } 213 return rv; 214 } 215 216 protected static HttpSessionListener makeSessionListener(String type) { 217 type = type.trim(); 218 HttpSessionListener rv = null; 219 try { 220 final Class c = Class.forName(type); 221 if (HttpSessionListener .class.isAssignableFrom(c)) rv = (HttpSessionListener ) c.newInstance(); 222 } catch (Exception e) { 223 return null; 225 } 226 return rv; 227 } 228 229 protected String getStringVal(final String propName, final String wacVal, final String defVal) { 230 String 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 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 getProperty(final String propName) { 244 String 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 propName, int defVal) { 252 final String val = props.getProperty(propName, true); 253 if (val == null || val.length() == 0) return defVal; 254 try { 255 return Integer.parseInt(val); 256 } catch (Exception e) { 257 return defVal; 258 } 259 } 260 261 } 262 | Popular Tags |