001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.core.stateful;
017:
018: import org.apache.openejb.spi.Serializer;
019:
020: import java.io.File;
021: import java.io.RandomAccessFile;
022: import java.util.Enumeration;
023: import java.util.Hashtable;
024: import java.util.Properties;
025:
026: // optimization: replace HashTable with HashMap (vc no debug hashmap)
027:
028: public class RAFPassivater implements PassivationStrategy {
029:
030: int fileID = 0;
031: Hashtable masterTable = new Hashtable();
032:
033: static class Pointer {
034: int fileid;
035: long filepointer;
036: int bytesize;
037:
038: public Pointer(int file, long pointer, int bytecount) {
039: fileid = file;
040: filepointer = pointer;
041: bytesize = bytecount;
042: }
043: }
044:
045: public void init(Properties props)
046: throws org.apache.openejb.SystemException {
047: }
048:
049: public synchronized void passivate(Hashtable stateTable)
050: throws org.apache.openejb.SystemException {
051: try {
052: fileID++;
053:
054: RandomAccessFile ras = new RandomAccessFile(System
055: .getProperty("java.io.tmpdir", File.separator
056: + "tmp")
057: + File.separator + "passivation" + fileID + ".ser",
058: "rw");
059: Enumeration enumeration = stateTable.keys();
060: Pointer lastPointer = null;
061: while (enumeration.hasMoreElements()) {
062: Object id = enumeration.nextElement();
063: Object obj = stateTable.get(id);
064: byte[] bytes = Serializer.serialize(obj);
065: long filepointer = ras.getFilePointer();
066:
067: if (lastPointer == null)
068: lastPointer = new Pointer(fileID, filepointer,
069: (int) (filepointer));
070: else
071: lastPointer = new Pointer(
072: fileID,
073: filepointer,
074: (int) (filepointer - lastPointer.filepointer));
075:
076: masterTable.put(id, lastPointer);
077: ras.write(bytes);
078: }
079: ras.close();
080: } catch (Exception e) {
081: throw new org.apache.openejb.SystemException(e);
082: }
083: }
084:
085: public synchronized Object activate(Object primaryKey)
086: throws org.apache.openejb.SystemException {
087:
088: Pointer pointer = (Pointer) masterTable.get(primaryKey);
089: if (pointer == null)
090: return null;
091:
092: try {
093: RandomAccessFile ras = new RandomAccessFile(System
094: .getProperty("java.io.tmpdir", File.separator
095: + "tmp")
096: + File.separator
097: + "passivation"
098: + pointer.fileid
099: + ".ser", "r");
100: byte[] bytes = new byte[(int) pointer.bytesize];
101: ras.seek(pointer.filepointer);
102: ras.readFully(bytes);
103: ras.close();
104: return Serializer.deserialize(bytes);
105: } catch (Exception e) {
106: throw new org.apache.openejb.SystemException(e);
107: }
108:
109: }
110:
111: }
|