1 16 17 18 package org.apache.catalina.cluster.session; 19 20 28 29 import java.util.LinkedList ; 30 import java.io.Externalizable ; 31 import java.security.Principal ; 32 import org.apache.catalina.realm.GenericPrincipal; 33 34 35 public class DeltaRequest implements Externalizable { 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 NAME_PRINCIPAL = "__SET__PRINCIPAL__"; 49 public static final String NAME_MAXINTERVAL = "__SET__MAXINTERVAL__"; 50 public static final String NAME_ISNEW = "__SET__ISNEW__"; 51 52 private String sessionId; 53 private LinkedList actions = new LinkedList (); 54 private LinkedList actionPool = new LinkedList (); 55 56 private boolean recordAllActions = false; 57 58 public DeltaRequest() { 59 60 } 61 62 public DeltaRequest(String sessionId, boolean recordAllActions) { 63 this.recordAllActions=recordAllActions; 64 if(sessionId != null) 65 setSessionId(sessionId); 66 } 67 68 69 public void setAttribute(String name, Object value) { 70 int action = (value==null)?ACTION_REMOVE:ACTION_SET; 71 addAction(TYPE_ATTRIBUTE,action,name,value); 72 } 73 74 public void removeAttribute(String 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 (interval)); 82 } 83 84 public void setPrincipal(Principal 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 (n)); 96 } 97 98 protected synchronized void addAction(int type, 99 int action, 100 String name, 101 Object value) { 102 AttributeInfo info = null; 103 if ( this.actionPool.size() > 0 ) { 104 try { 105 info = (AttributeInfo) actionPool.removeFirst(); 106 }catch ( Exception 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 ( !recordAllActions) { 117 try { 118 actions.remove(info); 119 } catch (java.util.NoSuchElementException x) { 120 } 122 } 123 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 ("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 TYPE_ISNEW: { 146 session.setNew(((Boolean )info.getValue()).booleanValue(),false); 147 break; 148 } case TYPE_MAXINTERVAL: { 150 session.setMaxInactiveInterval(((Integer )info.getValue()).intValue(),false); 151 break; 152 } case TYPE_PRINCIPAL: { 154 Principal p = null; 155 if ( info.getAction() == ACTION_SET ) { 156 SerializablePrincipal sp = (SerializablePrincipal)info.getValue(); 157 p = (Principal )sp.getPrincipal(session.getManager().getContainer().getRealm()); 158 } 159 session.setPrincipal(p,false); 160 break; 161 } default : throw new java.lang.IllegalArgumentException ("Invalid attribute info type="+info); 163 } } 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 x ) { 175 log.error("Unable to remove element",x); 176 } 177 } 178 actions.clear(); 179 } 180 181 public String getSessionId() { 182 return sessionId; 183 } 184 public void setSessionId(String sessionId) { 185 this.sessionId = sessionId; 186 if ( sessionId == null ) { 187 new Exception ("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 in) throws java.io.IOException , 200 java.lang.ClassNotFoundException { 201 reset(); 206 sessionId = in.readUTF(); 207 recordAllActions = in.readBoolean(); 208 int cnt = in.readInt(); 209 if (actions == null) 210 actions = new LinkedList (); 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 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 } } 230 231 232 233 public synchronized void writeExternal(java.io.ObjectOutput out ) throws java.io.IOException { 234 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 { 248 private String name = null; 249 private Object 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 name, 258 Object value) { 259 super(); 260 init(type,action,name,value); 261 } 262 263 public void init(int type, 264 int action, 265 String name, 266 Object 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 getValue() { 282 return value; 283 } 284 public int hashCode() { 285 return name.hashCode(); 286 } 287 288 public String 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 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 in ) throws java.io.IOException , 306 java.lang.ClassNotFoundException { 307 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 out) throws java.io. 318 IOException { 319 out.writeInt(getType()); 324 out.writeInt(getAction()); 325 out.writeUTF(getName()); 326 out.writeObject(getValue()); 327 } 328 329 public String toString() { 330 StringBuffer buf = new StringBuffer ("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 |