001: /*
002: * SerializableCacheManager.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 2000 Sun Microsystems, Inc.
007: *
008: * Sun Public License Notice
009: *
010: * The contents of this file are subject to the Sun Public License Version
011: * 1.0 (the "License"). You may not use this file except in compliance with
012: * the License. A copy of the License is included as the file "license.terms",
013: * and also available at http://www.sun.com/
014: *
015: * The Original Code is from:
016: * Brazil project web application Framework release 1.1.
017: * The Initial Developer of the Original Code is: suhler.
018: * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
019: * All Rights Reserved.
020: *
021: * Contributor(s): suhler.
022: *
023: * Version: 1.3
024: * Created by suhler on 00/11/04
025: * Last modified by suhler on 00/12/08 16:48:12
026: */
027:
028: package sunlabs.brazil.session;
029:
030: import java.io.FileInputStream;
031: import java.io.FileNotFoundException;
032: import java.io.FileOutputStream;
033: import java.io.ObjectInputStream;
034: import java.io.ObjectOutputStream;
035: import java.io.Serializable;
036: import sunlabs.brazil.server.Handler;
037: import sunlabs.brazil.server.Server;
038: import sun.misc.Signal;
039: import sun.misc.SignalHandler;
040:
041: /**
042: * Serializable version of the CacheManager. Saves out the pool of
043: * hashtables upon exit, restoring them on startup.
044: * This provides the same capability as {@link SerialPersist} does for
045: * the default session manager.
046: *
047: * @author Stephen Uhler (stephen.uhler@sun.com)
048: * @version %V% 1.3 SerializableCacheManager.java
049: */
050:
051: public class SerializableCacheManager extends CacheManager implements
052: Handler, Serializable, SignalHandler {
053: private static final String STORE = "store";
054: String store;
055:
056: public boolean init(Server server, String prefix) {
057: super .init(server, prefix);
058: store = server.props.getProperty(prefix + STORE, store);
059:
060: /* Read in the saved pool, if any */
061:
062: ScoredHashtable saved[] = null;
063: try {
064: ObjectInputStream in = new ObjectInputStream(
065: new FileInputStream(store));
066: server.log(Server.LOG_DIAGNOSTIC, prefix,
067: "loading persistent store: " + store);
068: saved = (ScoredHashtable[]) in.readObject();
069: } catch (FileNotFoundException e) {
070: server.log(Server.LOG_DIAGNOSTIC, prefix,
071: "no persistent store: " + store);
072: } catch (Exception e) {
073: server.log(Server.LOG_ERROR, prefix,
074: "Attempting to read store: " + store);
075: }
076: if (saved != null) {
077: int size = 0; // # of saved tables
078: int items = 0; // total sessions
079: for (int i = 0; i < saved.length; i++) {
080: if (saved[i] != null) {
081: size++;
082: items += saved[i].size();
083: }
084: }
085: if (size > maxTables) {
086: maxTables = size;
087: System.out.println(" increasing max tables to "
088: + maxTables);
089: pool = new ScoredHashtable[maxTables];
090: }
091:
092: size = 0;
093: for (int i = 0; i < saved.length; i++) {
094: if (saved[i] != null) {
095: pool[size] = saved[i];
096: size++;
097: }
098: }
099: saved = null;
100: System.out.println("Restored " + items + " sessions in "
101: + size + " tables");
102: }
103: Signal.handle(new Signal("INT"), (SignalHandler) this );
104: return true;
105: }
106:
107: public void handle(Signal sig) {
108: System.out.println("Saving state to: " + store);
109: saveStore();
110: System.exit(0);
111: }
112:
113: void saveStore() {
114: /*
115: pool = (CacheManager.ScoredHashtable [])
116: SerialPersist.cleanForSerialization(pool);
117: */
118:
119: try {
120: ObjectOutputStream out = new ObjectOutputStream(
121: new FileOutputStream(store));
122: out.writeObject(pool);
123: out.close();
124: } catch (Exception e) {
125: e.printStackTrace();
126: }
127: }
128: }
|