KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > cluster > session > DeltaRequest


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.catalina.cluster.session;
19
20 /**
21  * This class is used to track the series of actions that happens when
22  * a request is executed. These actions will then translate into invokations of methods
23  * on the actual session.
24  * This class is NOT thread safe. One DeltaRequest per session
25  * @author <a HREF="mailto:fhanik@apache.org">Filip Hanik</a>
26  * @version 1.0
27  */

28
29 import java.util.LinkedList JavaDoc;
30 import java.io.Externalizable JavaDoc;
31 import java.security.Principal JavaDoc;
32 import org.apache.catalina.realm.GenericPrincipal;
33
34
35 public class DeltaRequest implements Externalizable JavaDoc {
36
37     public static org.apache.commons.logging.Log log =
38         org.apache.commons.logging.LogFactory.getLog( DeltaRequest.class );
39
40     public static final int TYPE_ATTRIBUTE = 0;
41     public static final int TYPE_PRINCIPAL = 1;
42     public static final int TYPE_ISNEW = 2;
43     public static final int TYPE_MAXINTERVAL = 3;
44
45     public static final int ACTION_SET = 0;
46     public static final int ACTION_REMOVE = 1;
47
48     public static final String JavaDoc NAME_PRINCIPAL = "__SET__PRINCIPAL__";
49     public static final String JavaDoc NAME_MAXINTERVAL = "__SET__MAXINTERVAL__";
50     public static final String JavaDoc NAME_ISNEW = "__SET__ISNEW__";
51
52     private String JavaDoc sessionId;
53     private LinkedList JavaDoc actions = new LinkedList JavaDoc();
54     private LinkedList JavaDoc actionPool = new LinkedList JavaDoc();
55     
56     private boolean recordAllActions = false;
57
58     public DeltaRequest() {
59         
60     }
61     
62     public DeltaRequest(String JavaDoc sessionId, boolean recordAllActions) {
63         this.recordAllActions=recordAllActions;
64         if(sessionId != null)
65             setSessionId(sessionId);
66     }
67
68
69     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
70         int action = (value==null)?ACTION_REMOVE:ACTION_SET;
71         addAction(TYPE_ATTRIBUTE,action,name,value);
72     }
73
74     public void removeAttribute(String JavaDoc name) {
75         int action = ACTION_REMOVE;
76         addAction(TYPE_ATTRIBUTE,action,name,null);
77     }
78
79     public void setMaxInactiveInterval(int interval) {
80         int action = ACTION_SET;
81         addAction(TYPE_MAXINTERVAL,action,NAME_MAXINTERVAL,new Integer JavaDoc(interval));
82     }
83
84     public void setPrincipal(Principal JavaDoc p) {
85         int action = (p==null)?ACTION_REMOVE:ACTION_SET;
86         SerializablePrincipal sp = null;
87         if ( p != null ) {
88             sp = SerializablePrincipal.createPrincipal((GenericPrincipal)p);
89         }
90         addAction(TYPE_PRINCIPAL,action,NAME_PRINCIPAL,sp);
91     }
92
93     public void setNew(boolean n) {
94         int action = ACTION_SET;
95         addAction(TYPE_ISNEW,action,NAME_ISNEW,new Boolean JavaDoc(n));
96     }
97
98     protected synchronized void addAction(int type,
99                              int action,
100                              String JavaDoc name,
101                              Object JavaDoc value) {
102         AttributeInfo info = null;
103         if ( this.actionPool.size() > 0 ) {
104             try {
105                 info = (AttributeInfo) actionPool.removeFirst();
106             }catch ( Exception JavaDoc x ) {
107                 log.error("Unable to remove element:",x);
108                 info = new AttributeInfo(type, action, name, value);
109             }
110             info.init(type,action,name,value);
111         } else {
112             info = new AttributeInfo(type, action, name, value);
113         }
114         //if we have already done something to this attribute, make sure
115
//we don't send multiple actions across the wire
116
if ( !recordAllActions) {
117             try {
118                 actions.remove(info);
119             } catch (java.util.NoSuchElementException JavaDoc x) {
120                 //do nothing, we wanted to remove it anyway
121
}
122         }
123         //add the action
124
actions.addLast(info);
125     }
126     
127     public void execute(DeltaSession session) {
128         execute(session,true);
129     }
130
131     public synchronized void execute(DeltaSession session, boolean notifyListeners) {
132         if ( !this.sessionId.equals( session.getId() ) )
133             throw new java.lang.IllegalArgumentException JavaDoc("Session id mismatch, not executing the delta request");
134         session.access();
135         for ( int i=0; i<actions.size(); i++ ) {
136             AttributeInfo info = (AttributeInfo)actions.get(i);
137             switch ( info.getType() ) {
138                 case TYPE_ATTRIBUTE: {
139                     if ( info.getAction() == ACTION_SET ) {
140                         session.setAttribute(info.getName(), info.getValue(),notifyListeners,false);
141                     } else
142                         session.removeAttribute(info.getName(),notifyListeners,false);
143                     break;
144                 }//case
145
case TYPE_ISNEW: {
146                     session.setNew(((Boolean JavaDoc)info.getValue()).booleanValue(),false);
147                     break;
148                 }//case
149
case TYPE_MAXINTERVAL: {
150                     session.setMaxInactiveInterval(((Integer JavaDoc)info.getValue()).intValue(),false);
151                     break;
152                 }//case
153
case TYPE_PRINCIPAL: {
154                     Principal JavaDoc p = null;
155                     if ( info.getAction() == ACTION_SET ) {
156                         SerializablePrincipal sp = (SerializablePrincipal)info.getValue();
157                         p = (Principal JavaDoc)sp.getPrincipal(session.getManager().getContainer().getRealm());
158                     }
159                     session.setPrincipal(p,false);
160                     break;
161                 }//case
162
default : throw new java.lang.IllegalArgumentException JavaDoc("Invalid attribute info type="+info);
163             }//switch
164
}//for
165
session.endAccess();
166     }
167
168     public synchronized void reset() {
169         while ( actions.size() > 0 ) {
170             try {
171                 AttributeInfo info = (AttributeInfo) actions.removeFirst();
172                 info.recycle();
173                 actionPool.addLast(info);
174             }catch ( Exception JavaDoc x ) {
175                 log.error("Unable to remove element",x);
176             }
177         }
178         actions.clear();
179     }
180     
181     public String JavaDoc getSessionId() {
182         return sessionId;
183     }
184     public void setSessionId(String JavaDoc sessionId) {
185         this.sessionId = sessionId;
186         if ( sessionId == null ) {
187             new Exception JavaDoc("Session Id is null for setSessionId").fillInStackTrace().printStackTrace();
188         }
189     }
190     public int getSize() {
191         return actions.size();
192     }
193     
194     public synchronized void clear() {
195         actions.clear();
196         actionPool.clear();
197     }
198     
199     public synchronized void readExternal(java.io.ObjectInput JavaDoc in) throws java.io.IOException JavaDoc,
200         java.lang.ClassNotFoundException JavaDoc {
201         //sessionId - String
202
//recordAll - boolean
203
//size - int
204
//AttributeInfo - in an array
205
reset();
206         sessionId = in.readUTF();
207         recordAllActions = in.readBoolean();
208         int cnt = in.readInt();
209         if (actions == null)
210             actions = new LinkedList JavaDoc();
211         else
212             actions.clear();
213         for (int i = 0; i < cnt; i++) {
214             AttributeInfo info = null;
215             if (this.actionPool.size() > 0) {
216                 try {
217                     info = (AttributeInfo) actionPool.removeFirst();
218                 } catch ( Exception JavaDoc x ) {
219                     log.error("Unable to remove element",x);
220                     info = new AttributeInfo(-1,-1,null,null);
221                 }
222             }
223             else {
224                 info = new AttributeInfo(-1,-1,null,null);
225             }
226             info.readExternal(in);
227             actions.addLast(info);
228         }//for
229
}
230         
231
232
233     public synchronized void writeExternal(java.io.ObjectOutput JavaDoc out ) throws java.io.IOException JavaDoc {
234         //sessionId - String
235
//recordAll - boolean
236
//size - int
237
//AttributeInfo - in an array
238
out.writeUTF(getSessionId());
239         out.writeBoolean(recordAllActions);
240         out.writeInt(getSize());
241         for ( int i=0; i<getSize(); i++ ) {
242             AttributeInfo info = (AttributeInfo)actions.get(i);
243             info.writeExternal(out);
244         }
245     }
246     
247     private static class AttributeInfo implements java.io.Externalizable JavaDoc {
248         private String JavaDoc name = null;
249         private Object JavaDoc value = null;
250         private int action;
251         private int type;
252
253         public AttributeInfo() {}
254
255         public AttributeInfo(int type,
256                              int action,
257                              String JavaDoc name,
258                              Object JavaDoc value) {
259             super();
260             init(type,action,name,value);
261         }
262
263         public void init(int type,
264                          int action,
265                          String JavaDoc name,
266                          Object JavaDoc value) {
267             this.name = name;
268             this.value = value;
269             this.action = action;
270             this.type = type;
271         }
272
273         public int getType() {
274             return type;
275         }
276
277         public int getAction() {
278             return action;
279         }
280
281         public Object JavaDoc getValue() {
282             return value;
283         }
284         public int hashCode() {
285             return name.hashCode();
286         }
287
288         public String JavaDoc getName() {
289             return name;
290         }
291         
292         public void recycle() {
293             name = null;
294             value = null;
295             type=-1;
296             action=-1;
297         }
298
299         public boolean equals(Object JavaDoc o) {
300             if ( ! (o instanceof AttributeInfo ) ) return false;
301             AttributeInfo other = (AttributeInfo)o;
302             return other.getName().equals(this.getName());
303         }
304         
305         public synchronized void readExternal(java.io.ObjectInput JavaDoc in ) throws java.io.IOException JavaDoc,
306             java.lang.ClassNotFoundException JavaDoc {
307             //type - int
308
//action - int
309
//name - String
310
//value - object
311
type = in.readInt();
312             action = in.readInt();
313             name = in.readUTF();
314             value = in.readObject();
315         }
316
317         public synchronized void writeExternal(java.io.ObjectOutput JavaDoc out) throws java.io.
318
JavaDoc            IOException {
319             //type - int
320
//action - int
321
//name - String
322
//value - object
323
out.writeInt(getType());
324             out.writeInt(getAction());
325             out.writeUTF(getName());
326             out.writeObject(getValue());
327         }
328         
329         public String JavaDoc toString() {
330             StringBuffer JavaDoc buf = new StringBuffer JavaDoc("AttributeInfo[type=");
331             buf.append(getType()).append(", action=").append(getAction());
332             buf.append(", name=").append(getName()).append(", value=").append(getValue());
333             buf.append(", addr=").append(super.toString()).append("]");
334             return buf.toString();
335         }
336
337     }
338
339 }
340
Popular Tags