remm 2002/12/10 04:39:18 Modified: catalina/src/share/org/apache/catalina/cluster ReplicatedSession.java ReplicationStream.java SerializablePrincipal.java SessionMessage.java Log: - Code cleanup part 2. Revision Changes Path 1.2 +143 -143 jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/cluster/ReplicatedSession.java Index: ReplicatedSession.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/cluster/ReplicatedSession.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ReplicatedSession.java 5 Dec 2002 12:25:09 -0000 1.1 +++ ReplicatedSession.java 10 Dec 2002 12:39:18 -0000 1.2 @@ -72,65 +72,81 @@ import org.apache.catalina.SessionListener; import org.apache.catalina.realm.GenericPrincipal; import org.apache.catalina.session.StandardSession; +import org.apache.catalina.util.StringManager; /** * Title: Tomcat Session Replication for Tomcat 4.0 <BR> * Description: A very simple straight forward implementation of * session replication of servers in a cluster.<BR> * This session replication is implemented "live". By live - * I mean, when a session attribute is added into a session on Node A - * a message is broadcasted to other messages and setAttribute is called on the replicated - * sessions.<BR> + * I mean, when a session attribute is added into a session on + * Node A a message is broadcasted to other messages + * and setAttribute is called on the replicated sessions.<BR> * A full description of this implementation can be found under - * <href="http://www.filip.net/tomcat/">Filip's Tomcat Page</a><BR> + * <href="http://www.filip.net/tomcat/">Filip's Tomcat + * Page</a><BR> * - * Copyright: See apache license - * Company: www.filip.net - * @author <a href="mailto:[EMAIL PROTECTED]">Filip Hanik</a> - * @version 1.0 for TC 4.0 * Description:<BR> - * The ReplicatedSession class is a simple extension of the StandardSession class - * It overrides a few methods (setAttribute, removeAttribute, expire, access) and has - * hooks into the JGManager to broadcast and receive events from the cluster.<BR> - * This class inherits the readObjectData and writeObject data methods from the StandardSession - * and does not contain any serializable elements in addition to the inherited ones from the StandardSession + * The ReplicatedSession class is a simple extension of the StandardSession + * class. It overrides a few methods (setAttribute, removeAttribute, expire, + * access) and has hooks into the JGManager to broadcast and receive events + * from the cluster.<BR> + * This class inherits the readObjectData and writeObject data methods from + * the StandardSession and does not contain any serializable elements + * in addition to the inherited ones from the StandardSession. * + * @author Filip Hanik */ public class ReplicatedSession extends StandardSession { - - private transient Manager mManager = null; - + + + // ----------------------------------------------------- Instance Variables + + + /** + * The string manager for this package. + */ + protected StringManager sm = StringManager.getManager(Constants.Package); + + + private transient Manager manager = null; + + + // ------------------------------------------------------------ Constructor + + public ReplicatedSession(Manager manager) { super(manager); - mManager = manager; + this.manager = manager; } - + + + // --------------------------------------------------------- Public Methods + + /** * Update the accessed time information for this session. This method * should be called by the context when a request comes in for a particular * session, even if the application does not reference it. + * * @param notify - if true the other cluster nodes will be notified */ public void access(boolean notify) { super.access(); - //notify javagroups that session has been accessed - if ( notify ) - { + // Notify javagroups that session has been accessed + if (notify) { SessionMessage msg = new SessionMessage - (mManager.getContainer().getName(), + (manager.getContainer().getName(), SessionMessage.EVT_SESSION_ACCESSED, - null, - this.getId(), - null, - null, - null); + null, this.getId(), null, null, null); sendMessage(msg); } - //log("Access has been called in the session"); + } + /** * Update the accessed time information for this session. This method * should be called by the context when a request comes in for a particular @@ -139,13 +155,12 @@ public void access() { access(true); } - - - + /** * Perform the internal processing required to invalidate this session, * without triggering an exception if the session has already expired. + * * @param notify - the inherited notify from StandardSession * @param jgnotify - if true other nodes in the cluster will be notified */ @@ -153,22 +168,19 @@ String id = getId(); super.expire(); - //notify javagroups about the expiration - if ( jgnotify ) - { - int event = notify ? SessionMessage.EVT_SESSION_EXPIRED_WNOTIFY : SessionMessage.EVT_SESSION_EXPIRED_WONOTIFY; - SessionMessage msg = - new SessionMessage(mManager.getContainer().getName(), event, - null, - id, - null, - null, - null); + // Notify javagroups about the expiration + if (jgnotify) { + int event = notify ? SessionMessage.EVT_SESSION_EXPIRED_WNOTIFY + : SessionMessage.EVT_SESSION_EXPIRED_WONOTIFY; + SessionMessage msg = new SessionMessage + (manager.getContainer().getName(), event, + null, id, null, null, null); sendMessage(msg); } - log("Expire called on session with jgnotify="+jgnotify+ " and notify="+notify); - }//expire - + + } + + /** * Perform the internal processing required to invalidate this session, * without triggering an exception if the session has already expired. @@ -176,16 +188,17 @@ public void expire() { expire(true,true); } - - /** + + + /** * Perform the internal processing required to invalidate this session, * without triggering an exception if the session has already expired. */ public void expire(boolean notify) { expire(notify,true); } - - + + /** * Remove the object bound with the specified name from this session. If * the session does not have an object bound with this name, this method @@ -202,31 +215,31 @@ * @exception IllegalStateException if this method is called on an * invalidated session */ - public void removeAttribute(String name, boolean notify, boolean jgnotify) { + public void removeAttribute(String name, boolean notify, + boolean jgnotify) { + super.removeAttribute(name); - if ( jgnotify ) - { - SessionMessage msg = - new SessionMessage(mManager.getContainer().getName(), notify?SessionMessage.EVT_ATTRIBUTE_REMOVED_WNOTIFY:SessionMessage.EVT_ATTRIBUTE_REMOVED_WONOTIFY, - null, - getId(), - name, - null, - null); + if (jgnotify) { + SessionMessage msg = new SessionMessage + (manager.getContainer().getName(), + notify ? SessionMessage.EVT_ATTRIBUTE_REMOVED_WNOTIFY + : SessionMessage.EVT_ATTRIBUTE_REMOVED_WONOTIFY, + null, getId(), name, null, null); sendMessage(msg); } - + } - + + /** - * see parent description, + * See parent description, * plus we also notify other nodes in the cluster */ public void removeAttribute(String name, boolean notify) { removeAttribute(name,notify,true); } - - + + /** * Bind an object to this session, using the specified name. If an object * of the same name is already bound to this session, the object is @@ -246,46 +259,43 @@ * @excpetion IllegalArgumentException if the value is not serializable */ public void setAttribute(String name, Object value, boolean notify) { - if (!(value instanceof java.io.Serializable)) - { - throw new java.lang.IllegalArgumentException("Attribute["+name+"] is not serializable."); - } + + if (!(value instanceof java.io.Serializable)) { + throw new IllegalArgumentException + (sm.getString("clusterSession.attributeNotSerializable", + name)); + } super.setAttribute(name,value); - //notify javagroups - if ( notify) - { - SessionMessage msg = - new SessionMessage(mManager.getContainer().getName(), SessionMessage.EVT_ATTRIBUTE_ADDED, - null, - getId(), - name, - value, - null); + // Notify javagroups + if (notify) { + SessionMessage msg = new SessionMessage + (manager.getContainer().getName(), + SessionMessage.EVT_ATTRIBUTE_ADDED, + null, getId(), name, value, null); sendMessage(msg); } + } - + + /** * Sets an attribute and notifies the other nodes in the cluster */ - public void setAttribute(String name, Object value) - { - setAttribute(name,value,true); + public void setAttribute(String name, Object value) { + setAttribute(name, value, true); } - - + /** * Sets the manager for this session * @param mgr - the servers JGManager */ - public void setManager(JGManager mgr) - { - mManager = mgr; + public void setManager(JGManager mgr) { + manager = mgr; super.setManager(mgr); } - - + + /** * Set the authenticated Principal that is associated with this Session. * This provides an <code>Authenticator</code> with a means to cache a @@ -296,27 +306,24 @@ * @param jgnotify notify the other nodes in the cluster? (true/false) */ public void setPrincipal(Principal principal) { - setPrincipal(principal,true); + setPrincipal(principal, true); } - + + public void setPrincipal(Principal principal, boolean jgnotify) { - //log("setPrincipal called in the ReplicatedSession"); super.setPrincipal(principal); - if ( jgnotify) - { - //log("Sending message"); - SessionMessage msg = - new SessionMessage(mManager.getContainer().getName(), SessionMessage.EVT_SET_USER_PRINCIPAL, - null, - getId(), - null, - null, - SerializablePrincipal.createPrincipal((GenericPrincipal)principal)); + if (jgnotify) { + SessionMessage msg = new SessionMessage + (manager.getContainer().getName(), + SessionMessage.EVT_SET_USER_PRINCIPAL, + null, getId(), null, null, + SerializablePrincipal.createPrincipal + ((GenericPrincipal) principal)); sendMessage(msg); } - } - - + } + + /** * Remove any object bound to the specified name in the internal notes * for this session. @@ -325,25 +332,22 @@ */ public void removeNote(String name, boolean jgnotify) { super.removeNote(name); - if ( jgnotify) - { - SessionMessage msg = - new SessionMessage(mManager.getContainer().getName(), SessionMessage.EVT_REMOVE_SESSION_NOTE, - null, - getId(), - name, - null, - null); + if (jgnotify) { + SessionMessage msg = new SessionMessage + (manager.getContainer().getName(), + SessionMessage.EVT_REMOVE_SESSION_NOTE, + null, getId(), name, null, null); sendMessage(msg); } } + + public void removeNote(String name) { - //disable replication of notes - removeNote(name,false); - //removeNote(name,true); + // Disable replication of notes + removeNote(name, false); } - - + + /** * Bind an object to a specified name in the internal notes associated * with this session, replacing any existing binding for this name. @@ -354,39 +358,35 @@ public void setNote(String name, Object value, boolean jgnotify) { super.setNote(name,value); - if ( jgnotify ) - { - SessionMessage msg = - new SessionMessage(mManager.getContainer().getName(), SessionMessage.EVT_SET_SESSION_NOTE, - null, - getId(), - name, - value, - null); + if (jgnotify) { + SessionMessage msg = new SessionMessage + (manager.getContainer().getName(), + SessionMessage.EVT_SET_SESSION_NOTE, + null, getId(), name, value, null); sendMessage(msg); } } + + public void setNote(String name, Object value) { //for now disable replication of notes - setNote(name,value,false); - //setNote(name,value,true); + setNote(name, value, false); } + // -------------------------------------------------------- Private Methods + + /** - * Uses the manager to send a message to the other nodes + * Uses the manager to send a message to the other nodes. */ - private void sendMessage(SessionMessage msg ) - { - if ( this.mManager != null && this.mManager instanceof JGManager ) - { - JGManager transport = (JGManager) mManager; + private void sendMessage(SessionMessage msg) { + if ((this.manager != null) && (this.manager instanceof JGManager)) { + JGManager transport = (JGManager) manager; transport.sendSessionEvent(msg); - } - else - { - log("Not sending session event through javagroups - invalid manager="+mManager); + } else { + log(sm.getString("clusterSession.invalidManager")); } } 1.2 +19 -17 jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/cluster/ReplicationStream.java Index: ReplicationStream.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/cluster/ReplicationStream.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ReplicationStream.java 5 Dec 2002 12:25:09 -0000 1.1 +++ ReplicationStream.java 10 Dec 2002 12:39:18 -0000 1.2 @@ -74,9 +74,9 @@ * class loader for this web application. This allows classes defined only * with the web application to be found correctly. * - * @@author Craig R. McClanahan - * @@author Bip Thelin - * @@version $Revision$, $Date$ + * @author Craig R. McClanahan + * @author Bip Thelin + * @version $Revision$, $Date$ */ public final class ReplicationStream @@ -88,13 +88,14 @@ */ private ClassLoader classLoader = null; + /** * Construct a new instance of CustomObjectInputStream * - * @@param stream The input stream we will read from - * @@param classLoader The class loader used to instantiate objects + * @param stream The input stream we will read from + * @param classLoader The class loader used to instantiate objects * - * @@exception IOException if an input/output error occurs + * @exception IOException if an input/output error occurs */ public ReplicationStream(InputStream stream, ClassLoader classLoader) @@ -102,26 +103,27 @@ super(stream); this.classLoader = classLoader; + } + /** * Load the local class equivalent of the specified stream class * description, by using the class loader assigned to this Context. * - * @@param classDesc Class description from the input stream + * @param classDesc Class description from the input stream * - * @@exception ClassNotFoundException if this class cannot be found - * @@exception IOException if an input/output error occurs + * @exception ClassNotFoundException if this class cannot be found + * @exception IOException if an input/output error occurs */ public Class resolveClass(ObjectStreamClass classDesc) throws ClassNotFoundException, IOException { - try - { + try { return (classLoader.loadClass(classDesc.getName())); - } - catch ( Exception x ) - { + } catch (Exception x) { return getClass().getClassLoader().loadClass(classDesc.getName()); } } + + } 1.2 +31 -27 jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/cluster/SerializablePrincipal.java Index: SerializablePrincipal.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/cluster/SerializablePrincipal.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- SerializablePrincipal.java 5 Dec 2002 12:25:09 -0000 1.1 +++ SerializablePrincipal.java 10 Dec 2002 12:39:18 -0000 1.2 @@ -76,8 +76,9 @@ /** * Generic implementation of <strong>java.security.Principal</strong> that * is available for use by <code>Realm</code> implementations. - * The GenericPrincipal does NOT implement serializable and I didn't want to change that implementation - * hence I implemented this one instead. + * The GenericPrincipal does NOT implement serializable and I didn't want + * to change that implementation hence I implemented this one instead. + * * @author Filip Hanik * @version $Revision$ $Date$ */ @@ -87,10 +88,12 @@ // ----------------------------------------------------------- Constructors - public SerializablePrincipal() - { + + public SerializablePrincipal() { super(); } + + /** * Construct a new Principal, associated with the specified Realm, for the * specified username and password. @@ -100,9 +103,7 @@ * @param password Credentials used to authenticate this user */ public SerializablePrincipal(Realm realm, String name, String password) { - this(realm, name, password, null); - } @@ -117,7 +118,7 @@ * @param roles List of roles (must be Strings) possessed by this user */ public SerializablePrincipal(Realm realm, String name, String password, - List roles) { + List roles) { super(); this.realm = realm; @@ -171,8 +172,6 @@ } - - /** * The set of roles associated with this user. */ @@ -186,8 +185,6 @@ // --------------------------------------------------------- Public Methods - - /** * Return a String representation of this object, which exposes only * information that should be public. @@ -200,19 +197,26 @@ return (sb.toString()); } - - public static SerializablePrincipal createPrincipal(GenericPrincipal principal) - { - if ( principal==null) return null; - return new SerializablePrincipal(principal.getRealm(), - principal.getName(), - principal.getPassword(), - principal.getRoles()!=null?Arrays.asList(principal.getRoles()):null); + + + public static SerializablePrincipal createPrincipal + (GenericPrincipal principal) { + + if (principal == null) + return null; + + return new SerializablePrincipal + (principal.getRealm(), principal.getName(), + principal.getPassword(), + principal.getRoles() != null + ? Arrays.asList(principal.getRoles()) : null); } - - public GenericPrincipal getPrincipal( Realm realm ) - { - return new GenericPrincipal(realm,name,password,getRoles()!=null?Arrays.asList(getRoles()):null); + + + public GenericPrincipal getPrincipal(Realm realm) { + return new GenericPrincipal(realm, name, password, getRoles() != null + ? Arrays.asList(getRoles()) + : null); } 1.2 +108 -67 jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/cluster/SessionMessage.java Index: SessionMessage.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/cluster/SessionMessage.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- SessionMessage.java 5 Dec 2002 12:25:09 -0000 1.1 +++ SessionMessage.java 10 Dec 2002 12:39:18 -0000 1.2 @@ -70,17 +70,13 @@ * Description: A very simple straight forward implementation of * session replication of servers in a cluster.<BR> * This session replication is implemented "live". By live - * I mean, when a session attribute is added into a session on Node A - * a message is broadcasted to other messages and setAttribute is called on the replicated - * sessions.<BR> + * I mean, when a session attribute is added into a session + * on Node A a message is broadcasted to other messages + * and setAttribute is called on the replicated sessions.<BR> * A full description of this implementation can be found under - * <href="http://www.filip.net/tomcat/">Filip's Tomcat Page</a><BR> + * <href="http://www.filip.net/tomcat/">Filip's Tomcat + * Page</a><BR> * - * Copyright: See apache license - * Company: www.filip.net - * @author <a href="mailto:[EMAIL PROTECTED]">Filip Hanik</a> - * @version 1.0 for 4.0 - * * <B>Class Description:</B><BR> * The SessionMessage class is a class that is used when a session has been * created, modified, expired in a Tomcat cluster node.<BR> @@ -100,14 +96,18 @@ * </ul> * * These message are being sent and received from and to the - * InMemoryReplicationManager + * JGManager * + * @author Filip Hanik * @see JGManager */ public class SessionMessage implements java.io.Serializable { + // -------------------------------------------------------------- Constants + + /** * Event type used when a session has been created on a node */ @@ -130,8 +130,8 @@ /** * Event type used when a session has been accessed (ie, last access time - * has been updated. This is used so that the replicated sessions will not expire - * on the network + * has been updated. This is used so that the replicated sessions will + * not expire on the network */ public static final int EVT_SESSION_ACCESSED = 3; @@ -183,22 +183,30 @@ public static final int EVT_SET_SESSION_NOTE = 11; + // ----------------------------------------------------- Instance Variables + + /** * Private serializable variables to keep the messages state */ private String webapp; - private int mEvtType = -1; - private byte[] mSession; - private String mSessionID; - private String mAttributeName; - private Object mAttributeValue; - private SerializablePrincipal mPrincipal; + private int eventType = -1; + private byte[] session; + private String sessionID; + private String attrName; + private Object attrValue; + private SerializablePrincipal principal; + + + // ------------------------------------------------------------ Constructor /** * Creates a session message. Depending on what event type you want this - * message to represent, you populate the different parameters in the constructor<BR> - * The following rules apply dependent on what event type argument you use:<BR> + * message to represent, you populate the different parameters in + * the constructor<BR> + * The following rules apply dependent on what event type argument + * you use:<BR> * <B>EVT_SESSION_CREATED</B><BR> * The parameters: session, sessionID must be set.<BR> * <B>EVT_SESSION_EXPIRED</B><BR> @@ -224,75 +232,108 @@ * @param attrName - the name of the attribute added/removed * @param attrValue - the value of the attribute added */ - public SessionMessage( String webapp, - int eventtype, - byte[] session, - String sessionID, - String attrName, - Object attrValue, - SerializablePrincipal principal) - { + public SessionMessage(String webapp, + int eventType, + byte[] session, + String sessionID, + String attrName, + Object attrValue, + SerializablePrincipal principal) { this.webapp = webapp; - mEvtType = eventtype; - mSession = session; - mSessionID = sessionID; - mAttributeName = attrName; - mAttributeValue = attrValue; - mPrincipal = principal; + this.eventType = eventType; + this.session = session; + this.sessionID = sessionID; + this.attrName = attrName; + this.attrValue = attrValue; + this.principal = principal; } - + + + // --------------------------------------------------------- Public Methods + + public String getWebapp() { return (this.webapp); } + /** * returns the event type * @return one of the event types EVT_XXXX */ - public int getEventType() { return mEvtType; } + public int getEventType() { + return eventType; + } + /** * @return the serialized data for the session */ - public byte[] getSession() { return mSession;} + public byte[] getSession() { + return session; + } + + /** * @return the session ID for the session */ - public String getSessionID(){ return mSessionID; } + public String getSessionID() { + return sessionID; + } + + /** * @return the name of the attribute */ - public String getAttributeName() { return mAttributeName; } + public String getAttributeName() { + return attrName; + } + + /** * the value of the attribute */ - public Object getAttributeValue() - { - return mAttributeValue; + public Object getAttributeValue() { + return attrValue; } - - public SerializablePrincipal getPrincipal() { return mPrincipal;} - + + + public SerializablePrincipal getPrincipal() { + return principal; + } + + /** * @return the event type in a string representating, useful for debugging */ - public String getEventTypeString() - { - switch (mEvtType) - { - case EVT_SESSION_CREATED : return "SESSION-CREATED"; - case EVT_SESSION_EXPIRED_WNOTIFY : return "SESSION-EXPIRED-WITH-NOTIFY"; - case EVT_SESSION_EXPIRED_WONOTIFY : return "SESSION-EXPIRED-WITHOUT-NOTIFY"; - case EVT_ATTRIBUTE_ADDED : return "SESSION-ATTRIBUTE-ADDED"; - case EVT_ATTRIBUTE_REMOVED_WNOTIFY : return "SESSION-ATTRIBUTE-REMOVED-WITH-NOTIFY"; - case EVT_ATTRIBUTE_REMOVED_WONOTIFY: return "SESSION-ATTRIBUTE-REMOVED-WITHOUT-NOTIFY"; - case EVT_SESSION_ACCESSED : return "SESSION-ACCESSED"; - case EVT_GET_ALL_SESSIONS : return "SESSION-GET-ALL"; - case EVT_SET_SESSION_NOTE: return "SET-SESSION-NOTE"; - case EVT_SET_USER_PRINCIPAL : return "SET-USER-PRINCIPAL"; - case EVT_REMOVE_SESSION_NOTE : return "REMOVE-SESSION-NOTE"; - - default : return "UNKNOWN-EVENT-TYPE"; + public String getEventTypeString() { + switch (eventType) { + case EVT_SESSION_CREATED : + return "SESSION-CREATED"; + case EVT_SESSION_EXPIRED_WNOTIFY : + return "SESSION-EXPIRED-WITH-NOTIFY"; + case EVT_SESSION_EXPIRED_WONOTIFY : + return "SESSION-EXPIRED-WITHOUT-NOTIFY"; + case EVT_ATTRIBUTE_ADDED : + return "SESSION-ATTRIBUTE-ADDED"; + case EVT_ATTRIBUTE_REMOVED_WNOTIFY : + return "SESSION-ATTRIBUTE-REMOVED-WITH-NOTIFY"; + case EVT_ATTRIBUTE_REMOVED_WONOTIFY: + return "SESSION-ATTRIBUTE-REMOVED-WITHOUT-NOTIFY"; + case EVT_SESSION_ACCESSED : + return "SESSION-ACCESSED"; + case EVT_GET_ALL_SESSIONS : + return "SESSION-GET-ALL"; + case EVT_SET_SESSION_NOTE: + return "SET-SESSION-NOTE"; + case EVT_SET_USER_PRINCIPAL : + return "SET-USER-PRINCIPAL"; + case EVT_REMOVE_SESSION_NOTE : + return "REMOVE-SESSION-NOTE"; + default : + return "UNKNOWN-EVENT-TYPE"; } - } -}//SessionMessage + } + + +}
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>