001: // Space4J(TM) - Object Persistence in RAM
002: // Copyright (C) 2003 Sergio Oliveira Junior
003: // This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
004:
005: package org.space4j.passivation;
006:
007: import java.util.*;
008: import java.io.*;
009: import java.nio.channels.*;
010:
011: /**
012: * If you want to make your object passivated, you must distribute it throughout your application as a PassivationObj.<br>
013: * Actually, this is pretty simple. You have an object User. If you put your User in a PassivationMap, you will receive
014: * a PassivationObj whenever you ask for it through the get(key) method. So there won't be any references to User throughout
015: * your applicatoin, but only references to PassivationObj. Whenever you want to gain access to the raal object, you can
016: * use the get() method. If the your Object is in memory, PassivationObj just return it. If it is passivated, PassivationObj
017: * brings it back from disk.<br><br>
018: * <b>Note:</b> <br><br>
019: * - An object can be passivated if and only if it has a unique id returned by its hashCode, like a database primary key.<br><br>
020: * - It is the programmer responsibility not to make strong references to the object itself. For example:<br><br>
021: * public class House {<BR>
022: * User user = null; // WRONG !!!!<BR>
023: * }<BR><BR>
024: * public class House {<BR>
025: * PassivationObj user = null; // RIGHT !!!!<BR>
026: * }<BR><BR>
027: */
028: public class PassivationObj implements Serializable {
029:
030: private final static boolean DEBUG = true;
031:
032: private Object obj = null;
033: private int oid = 0;
034: private Class klass = null;
035: private long lastAccessTime = 0;
036: private boolean passivated = false;
037:
038: /**
039: * Creates a PassivationObj for an object.
040: * @param obj The object to be passivated.
041: */
042: public PassivationObj(Object obj) {
043: this .obj = obj;
044: this .oid = obj.hashCode();
045: this .klass = obj.getClass();
046: setLastAccessTime();
047: }
048:
049: protected String getFilename() {
050: String oid = String.valueOf(this .oid);
051: if (oid.length() == 1) {
052: StringBuffer sb = new StringBuffer("0");
053: sb.append(oid);
054: oid = sb.toString();
055: }
056: String classname = klass.getName();
057: int x = oid.length();
058: String extrapath = oid.substring(x - 2, x);
059: StringBuffer path = new StringBuffer(PassivationMap.DIR);
060: File dir = new File(path.toString());
061: if (!dir.exists())
062: dir.mkdir();
063: path.append("/").append(classname);
064: dir = new File(path.toString());
065: if (!dir.exists())
066: dir.mkdir();
067: path.append("/").append(extrapath);
068: dir = new File(path.toString());
069: if (!dir.exists())
070: dir.mkdir();
071: path.append("/").append(oid);
072: path.append(PassivationMap.PV_PREFIX);
073: return path.toString();
074: }
075:
076: long getLastAccessTime() {
077: return lastAccessTime;
078: }
079:
080: private void setLastAccessTime() {
081: lastAccessTime = System.currentTimeMillis();
082: }
083:
084: /**
085: * Return the object. If the object is passivated it will be activated before it is returned, of course.
086: * @return The object itself.
087: */
088: public synchronized Object get() {
089: setLastAccessTime();
090: if (isPassivated())
091: activate();
092: return obj;
093: }
094:
095: /**
096: * Return the object without activating it. This will be usefull when the system needs to access the object.
097: * In this situation, there is no need to activate the object.
098: * @return The object itself.
099: */
100: public synchronized Object getWithoutActivating() {
101: if (!isPassivated())
102: return this .obj;
103: activate();
104: Object save = this .obj;
105: this .obj = null;
106: passivated = true;
107: return save;
108: }
109:
110: // When the VM brings back this object from serialization, it will call this function.
111: private void readObject(ObjectInputStream stream)
112: throws ClassNotFoundException, IOException {
113: stream.defaultReadObject();
114: if (DEBUG)
115: System.out.println("inside readObject for " + oid);
116: // This will always come with the object from the snapshot
117: // Check if it is passivated...
118: // If it is, write to disk now !!!
119: if (isPassivated() && obj != null) {
120: passivate();
121: } else {
122: setLastAccessTime();
123: }
124: }
125:
126: // Before serializing...
127: private void writeObject(ObjectOutputStream stream)
128: throws IOException {
129: if (DEBUG)
130: System.out.println("inside writeObject for " + oid);
131: if (isPassivated()) {
132: this .obj = getWithoutActivating();
133: }
134: stream.defaultWriteObject();
135: }
136:
137: boolean canPassivate(long n) {
138: if (System.currentTimeMillis() - lastAccessTime > n)
139: return true;
140: return false;
141: }
142:
143: /**
144: * Check if this object is passivated now.
145: * @return true if this object is passivated
146: */
147: public synchronized boolean isPassivated() {
148: return passivated;
149: }
150:
151: synchronized void passivate() {
152: // we need a file lock here for the cluster !!!
153: // synchronized won't help !!!
154: // we can have many separate virtual machines running !!!
155: ObjectOutputStream oos = null;
156: FileLock lock = null;
157: try {
158: File file = new File(getFilename());
159: FileOutputStream fos = new FileOutputStream(file);
160: lock = fos.getChannel().tryLock();
161: if (lock == null) {
162: // somebody is just passivating this right now !!!
163: // consider it passivated...
164: if (DEBUG)
165: System.out.println("Passivation Lock !!!");
166: return;
167: }
168: oos = new ObjectOutputStream(fos);
169: oos.writeObject(obj);
170: if (DEBUG)
171: System.out.println("Object " + obj.toString()
172: + " passivated !!!");
173: passivated = true;
174: this .obj = null; // very important !!!
175: } catch (Exception e) {
176: e.printStackTrace();
177: } finally {
178: try {
179: if (oos != null)
180: oos.close();
181: } catch (Exception e) {
182: }
183: try {
184: if (lock != null)
185: lock.release();
186: } catch (Exception e) {
187: }
188: }
189: }
190:
191: private synchronized void activate() {
192: ObjectInputStream ois = null;
193: try {
194: File file = new File(getFilename());
195: FileInputStream fis = new FileInputStream(file);
196: ois = new ObjectInputStream(fis);
197: Object obj = ois.readObject();
198: if (DEBUG)
199: System.out.println("Object " + obj.toString()
200: + " activated !!!");
201: this .obj = obj;
202: passivated = false;
203: } catch (Exception e) {
204: e.printStackTrace();
205: } finally {
206: try {
207: if (ois != null)
208: ois.close();
209: } catch (Exception e) {
210: }
211: }
212: }
213:
214: public boolean equals(Object obj) {
215: if (obj instanceof PassivationObj) {
216: PassivationObj po = (PassivationObj) obj;
217: if (po.hashCode() == this .hashCode())
218: return true;
219: } else if (klass.isInstance(obj)) {
220: if (obj.hashCode() == this .hashCode())
221: return true;
222: }
223: return false;
224: }
225:
226: public int hashCode() {
227: return oid;
228: }
229:
230: }
|