001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/session/DistributedManager.java,v 1.5 2002/01/03 08:52:57 remm Exp $
003: * $Revision: 1.5 $
004: * $Date: 2002/01/03 08:52:57 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.session;
065:
066: import java.io.InputStream;
067: import java.io.OutputStream;
068: import java.io.BufferedInputStream;
069: import java.io.BufferedOutputStream;
070: import java.io.ByteArrayInputStream;
071: import java.io.ByteArrayOutputStream;
072: import java.io.IOException;
073: import java.io.InputStream;
074: import java.io.ObjectInputStream;
075: import java.io.ObjectOutputStream;
076: import java.io.ObjectStreamClass;
077:
078: import org.apache.catalina.Cluster;
079: import org.apache.catalina.Container;
080: import org.apache.catalina.LifecycleException;
081: import org.apache.catalina.Loader;
082: import org.apache.catalina.Session;
083: import org.apache.catalina.cluster.ClusterSender;
084: import org.apache.catalina.cluster.ClusterReceiver;
085: import org.apache.catalina.cluster.ReplicationWrapper;
086: import org.apache.catalina.util.CustomObjectInputStream;
087:
088: /**
089: * This manager is responsible for in memory replication of
090: * Sessions across a defined Cluster. It could also utilize a
091: * Store to make Sessions persistence.
092: *
093: * @author Bip Thelin
094: * @version $Revision: 1.5 $, $Date: 2002/01/03 08:52:57 $
095: */
096:
097: public final class DistributedManager extends PersistentManagerBase {
098:
099: // ----------------------------------------------------- Instance Variables
100:
101: /**
102: * The descriptive information about this implementation.
103: */
104: private static final String info = "DistributedManager/1.0";
105:
106: /**
107: * The descriptive name of this Manager implementation (for logging).
108: */
109: protected static String name = "DistributedManager";
110:
111: /**
112: * Our ClusterSender, used when replicating sessions
113: */
114: private ClusterSender clusterSender = null;
115:
116: /**
117: * Our ClusterReceiver
118: */
119: private ClusterReceiver clusterReceiver = null;
120:
121: // ------------------------------------------------------------- Properties
122:
123: /**
124: * Return descriptive information about this Manager implementation and
125: * the corresponding version number, in the format
126: * <code><description>/<version></code>.
127: */
128: public String getInfo() {
129: return (this .info);
130: }
131:
132: /**
133: * Return the descriptive short name of this Manager implementation.
134: */
135: public String getName() {
136: return (this .name);
137: }
138:
139: // --------------------------------------------------------- Public Methods
140:
141: /**
142: * Create a Session and replicate it in our Cluster
143: *
144: * @return The newly created Session
145: */
146: public Session createSession() {
147: Session session = super .createSession();
148: ObjectOutputStream oos = null;
149: ByteArrayOutputStream bos = null;
150: ByteArrayInputStream bis = null;
151:
152: try {
153: bos = new ByteArrayOutputStream();
154: oos = new ObjectOutputStream(new BufferedOutputStream(bos));
155:
156: ((StandardSession) session).writeObjectData(oos);
157: oos.close();
158:
159: byte[] obs = bos.toByteArray();
160: clusterSender.send(obs);
161:
162: if (debug > 0)
163: log("Replicating Session: " + session.getId());
164: } catch (IOException e) {
165: log("An error occurred when replicating Session: "
166: + session.getId());
167: }
168:
169: return (session);
170: }
171:
172: /**
173: * Start this manager
174: *
175: * @exception LifecycleException if an error occurs
176: */
177: public void start() throws LifecycleException {
178: Container container = getContainer();
179: Cluster cluster = null;
180:
181: if (container != null)
182: cluster = container.getCluster();
183:
184: if (cluster != null) {
185: this .clusterSender = cluster.getClusterSender(getName());
186: this .clusterReceiver = cluster
187: .getClusterReceiver(getName());
188: }
189:
190: super .start();
191: }
192:
193: /**
194: * Called from our background thread to process new received Sessions
195: *
196: */
197: public void processClusterReceiver() {
198: Object[] objs = clusterReceiver.getObjects();
199: StandardSession _session = null;
200: ByteArrayInputStream bis = null;
201: Loader loader = null;
202: ClassLoader classLoader = null;
203: ObjectInputStream ois = null;
204: byte[] buf = new byte[5000];
205: ReplicationWrapper repObj = null;
206:
207: for (int i = 0; i < objs.length; i++) {
208: try {
209: bis = new ByteArrayInputStream(buf);
210: repObj = (ReplicationWrapper) objs[i];
211: buf = repObj.getDataStream();
212: bis = new ByteArrayInputStream(buf, 0, buf.length);
213:
214: if (container != null)
215: loader = container.getLoader();
216:
217: if (loader != null)
218: classLoader = loader.getClassLoader();
219:
220: if (classLoader != null)
221: ois = new CustomObjectInputStream(bis, classLoader);
222: else
223: ois = new ObjectInputStream(bis);
224:
225: _session = (StandardSession) super .createSession();
226: _session.readObjectData(ois);
227: _session.setManager(this );
228:
229: if (debug > 0)
230: log("Loading replicated session: "
231: + _session.getId());
232: } catch (IOException e) {
233: log("Error occurred when trying to read replicated session: "
234: + e.toString());
235: } catch (ClassNotFoundException e) {
236: log("Error occurred when trying to read replicated session: "
237: + e.toString());
238: } finally {
239: if (ois != null) {
240: try {
241: ois.close();
242: bis = null;
243: } catch (IOException e) {
244: ;
245: }
246: }
247: }
248: }
249: }
250:
251: /**
252: * The background thread that checks for session timeouts and shutdown.
253: */
254: public void run() {
255: // Loop until the termination semaphore is set
256: while (!threadDone) {
257: threadSleep();
258: processClusterReceiver();
259: processExpires();
260: processPersistenceChecks();
261: }
262: }
263: }
|