KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > tomcat > session > SessionInternal


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

5 package com.tc.tomcat.session;
6
7 import org.apache.catalina.Manager;
8 import org.apache.catalina.Realm;
9 import org.apache.catalina.Session;
10 import org.apache.catalina.SessionListener;
11 import org.apache.catalina.cluster.session.SerializablePrincipal;
12 import org.apache.catalina.realm.GenericPrincipal;
13
14 import com.terracotta.session.SessionData;
15
16 import java.io.ByteArrayInputStream JavaDoc;
17 import java.io.ByteArrayOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.ObjectInputStream JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21 import java.security.Principal JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import javax.servlet.http.HttpSession JavaDoc;
26
27 public class SessionInternal implements Session {
28
29   private static final String JavaDoc AUTH_TYPE = "tomcatAuthType";
30   private static final String JavaDoc NOTE = "tomcatNote";
31   private static final String JavaDoc TOMCAT_PRINCIPAL = "tomcatPrincipal";
32   private static final String JavaDoc PORTABLE_PRINCIPAL = "tomcatPortablePrincipal";
33
34   private final com.terracotta.session.Session tcSession;
35   private final SessionData sessionData;
36   private final Realm realm;
37
38   public SessionInternal(com.terracotta.session.Session tcSession, Realm realm) {
39     this.tcSession = tcSession;
40     this.sessionData = tcSession.getSessionData();
41     this.realm = realm;
42   }
43
44   public void access() {
45     //
46
}
47
48   public void addSessionListener(SessionListener listener) {
49     //
50
}
51
52   public void endAccess() {
53     //
54
}
55
56   public void expire() {
57     throw new UnsupportedOperationException JavaDoc();
58   }
59
60   public String JavaDoc getAuthType() {
61     return (String JavaDoc) sessionData.getTransientAttribute(AUTH_TYPE);
62   }
63
64   public long getCreationTime() {
65     return tcSession.getCreationTime();
66   }
67
68   public String JavaDoc getId() {
69     return tcSession.getId();
70   }
71
72   public String JavaDoc getIdInternal() {
73     return tcSession.getId();
74   }
75
76   public String JavaDoc getInfo() {
77     return "TerracottaSessionInternal";
78   }
79
80   public long getLastAccessedTime() {
81     return tcSession.getLastAccessedTime();
82   }
83
84   public long getLastAccessedTimeInternal() {
85     throw new UnsupportedOperationException JavaDoc();
86   }
87
88   public Manager getManager() {
89     throw new UnsupportedOperationException JavaDoc();
90   }
91
92   public int getMaxInactiveInterval() {
93     return tcSession.getMaxInactiveInterval();
94   }
95
96   public Object JavaDoc getNote(String JavaDoc name) {
97     return sessionData.getTransientAttribute(makeNoteName(name));
98   }
99
100   public Iterator JavaDoc getNoteNames() {
101     Collection JavaDoc keys = sessionData.getTransientAttributeKeys();
102     for (Iterator JavaDoc i = keys.iterator(); i.hasNext();) {
103       String JavaDoc key = (String JavaDoc) i.next();
104       if (!key.startsWith(NOTE)) {
105         i.remove();
106       }
107     }
108
109     return keys.iterator();
110   }
111
112   public Principal getPrincipal() {
113     Principal p = (Principal) sessionData.getInternalAttribute(PORTABLE_PRINCIPAL);
114     if (p != null) { return p; }
115
116     byte[] principal = (byte[]) sessionData.getInternalAttribute(TOMCAT_PRINCIPAL);
117     if (principal != null) { return deserializeGenericPrincipal(principal); }
118
119     return null;
120   }
121
122   public HttpSession JavaDoc getSession() {
123     return tcSession;
124   }
125
126   public boolean isValid() {
127     return tcSession.isValid();
128   }
129
130   public void recycle() {
131     //
132
}
133
134   public void removeNote(String JavaDoc name) {
135     sessionData.removeTransientAttribute(makeNoteName(name));
136   }
137
138   public void removeSessionListener(SessionListener listener) {
139     //
140
}
141
142   public void setAuthType(String JavaDoc authType) {
143     sessionData.setTransientAttribute(AUTH_TYPE, authType);
144   }
145
146   public void setCreationTime(long time) {
147     throw new UnsupportedOperationException JavaDoc();
148   }
149
150   public void setId(String JavaDoc id) {
151     throw new UnsupportedOperationException JavaDoc();
152   }
153
154   public void setManager(Manager manager) {
155     throw new UnsupportedOperationException JavaDoc();
156   }
157
158   public void setMaxInactiveInterval(int interval) {
159     throw new UnsupportedOperationException JavaDoc();
160   }
161
162   public void setNew(boolean isNew) {
163     throw new UnsupportedOperationException JavaDoc();
164   }
165
166   public void setNote(String JavaDoc name, Object JavaDoc value) {
167     sessionData.setTransientAttribute(makeNoteName(name), value);
168   }
169
170   public void setPrincipal(Principal principal) {
171     if (principal == null) {
172       sessionData.removeInternalAttribute(TOMCAT_PRINCIPAL);
173       sessionData.removeAttribute(PORTABLE_PRINCIPAL);
174     }
175
176     final boolean isTomcatInternalPrincipal = (principal instanceof GenericPrincipal);
177     sessionData.removeAttribute(isTomcatInternalPrincipal ? PORTABLE_PRINCIPAL : TOMCAT_PRINCIPAL);
178
179     if (isTomcatInternalPrincipal) {
180       sessionData.setInternalAttribute(TOMCAT_PRINCIPAL, serializeGenericPrincipal((GenericPrincipal) principal));
181     } else {
182       sessionData.setInternalAttribute(PORTABLE_PRINCIPAL, principal);
183     }
184   }
185
186   private static byte[] serializeGenericPrincipal(GenericPrincipal principal) {
187     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
188
189     try {
190       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
191       SerializablePrincipal.writePrincipal(principal, oos);
192       oos.flush();
193     } catch (IOException JavaDoc e) {
194       throw new RuntimeException JavaDoc("Error serializing principal", e);
195     }
196
197     return baos.toByteArray();
198   }
199
200   private GenericPrincipal deserializeGenericPrincipal(byte[] data) {
201     try {
202       return SerializablePrincipal.readPrincipal(new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(data)), realm);
203     } catch (IOException JavaDoc e) {
204       throw new RuntimeException JavaDoc("Error creating principal", e);
205     }
206   }
207
208   public void setValid(boolean isValid) {
209     throw new UnsupportedOperationException JavaDoc();
210   }
211
212   private static String JavaDoc makeNoteName(String JavaDoc name) {
213     return NOTE + name;
214   }
215
216 }
Popular Tags