01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.terracotta.session.util;
06:
07: import com.tc.util.Assert;
08:
09: import java.lang.reflect.Field;
10:
11: public class WebsphereIdGenerator extends DefaultIdGenerator {
12:
13: private static final String SessionContextClassName = "com.ibm.ws.webcontainer.httpsession.SessionContext";
14:
15: private final String cacheId = getCacheId();
16: private final String cloneId = getCloneId();
17:
18: private final String tcDelimiter;
19:
20: public WebsphereIdGenerator(int idLength, String serverId,
21: int lockType, final String delimiter) {
22: super (idLength, serverId, lockType, delimiter);
23:
24: String tcDefaultDelimiter = ConfigProperties.defaultDelimiter;
25:
26: // if you're removing this assertion, we can't blindly use ":" below
27: Assert.assertTrue(tcDefaultDelimiter.indexOf(':') < 0);
28: if (delimiter.equals(tcDefaultDelimiter)) {
29: this .tcDelimiter = ":";
30: } else {
31: this .tcDelimiter = tcDefaultDelimiter;
32: }
33:
34: Assert.assertFalse(tcDelimiter.equals(delimiter));
35: }
36:
37: protected String makeExternalId(String key) {
38: // embed the terracotta client id using a different delimiter -- without this we can't detect server hops when WAS
39: // cloneId not set
40: return cacheId + key + tcDelimiter + getServerId()
41: + getDelimiter() + cloneId;
42: }
43:
44: protected int getDLMIndex(String requestedSessionId) {
45: return requestedSessionId.indexOf(tcDelimiter);
46: }
47:
48: private String getCloneId() {
49: try {
50: Class sessionContextClass = this .getClass()
51: .getClassLoader()
52: .loadClass(SessionContextClassName);
53: Field f = sessionContextClass.getDeclaredField("cloneId");
54: f.setAccessible(true);
55: String val = (String) f.get(null);
56: return val;
57: } catch (Exception e) {
58: throw new AssertionError(e);
59: }
60: }
61:
62: // TODO: This method needs to be refactor. Currently, it just get the default cache id from the Websphere Session
63: // Context. For more information, refer to CDV-258.
64: private String getCacheId() {
65: Class sessionContextClass;
66: try {
67: sessionContextClass = this .getClass().getClassLoader()
68: .loadClass(SessionContextClassName);
69: Field f = sessionContextClass
70: .getDeclaredField("defaultCacheId");
71: f.setAccessible(true);
72: String val = (String) f.get(null);
73: return val;
74: } catch (Exception e) {
75: throw new AssertionError(e);
76: }
77: }
78:
79: }
|