Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » core » stateful » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.openejb.core.stateful;
   18   
   19   import org.apache.openejb.spi.Serializer;
   20   
   21   import java.io.File;
   22   import java.io.RandomAccessFile;
   23   import java.util.Enumeration;
   24   import java.util.Hashtable;
   25   import java.util.Properties;
   26   import java.util.Map;
   27   import java.util.Iterator;
   28   
   29   // optimization: replace HashTable with HashMap (vc no debug hashmap)
   30   
   31   public class RAFPassivater implements PassivationStrategy {
   32   
   33       int fileID = 0;
   34       Hashtable masterTable = new Hashtable();
   35   
   36       static class Pointer {
   37           int fileid;
   38           long filepointer;
   39           int bytesize;
   40   
   41           public Pointer(int file, long pointer, int bytecount) {
   42               fileid = file;
   43               filepointer = pointer;
   44               bytesize = bytecount;
   45           }
   46       }
   47   
   48       public void init(Properties props) throws org.apache.openejb.SystemException {
   49       }
   50   
   51       public synchronized void passivate(Map stateTable)
   52               throws org.apache.openejb.SystemException {
   53           try {
   54               fileID++;
   55   
   56               RandomAccessFile ras = new RandomAccessFile(System.getProperty("java.io.tmpdir", File.separator + "tmp") + File.separator + "passivation" + fileID + ".ser", "rw");
   57               Iterator iterator = stateTable.keySet().iterator();
   58               Pointer lastPointer = null;
   59               while (iterator.hasNext()) {
   60                   Object id = iterator.next();
   61                   Object obj = stateTable.get(id);
   62                   byte [] bytes = Serializer.serialize(obj);
   63                   long filepointer = ras.getFilePointer();
   64   
   65                   if (lastPointer == null) lastPointer = new Pointer(fileID, filepointer, (int) (filepointer));
   66                   else
   67                       lastPointer = new Pointer(fileID, filepointer, (int) (filepointer - lastPointer.filepointer));
   68   
   69                   masterTable.put(id, lastPointer);
   70                   ras.write(bytes);
   71               }
   72               ras.close();
   73           } catch (Exception e) {
   74               throw new org.apache.openejb.SystemException(e);
   75           }
   76       }
   77   
   78       public synchronized Object activate(Object primaryKey)
   79               throws org.apache.openejb.SystemException {
   80   
   81           Pointer pointer = (Pointer) masterTable.get(primaryKey);
   82           if (pointer == null)
   83               return null;
   84   
   85           try {
   86               RandomAccessFile ras = new RandomAccessFile(System.getProperty("java.io.tmpdir", File.separator + "tmp") + File.separator + "passivation" + pointer.fileid + ".ser", "r");
   87               byte [] bytes = new byte[(int) pointer.bytesize];
   88               ras.seek(pointer.filepointer);
   89               ras.readFully(bytes);
   90               ras.close();
   91               return Serializer.deserialize(bytes);
   92           } catch (Exception e) {
   93               throw new org.apache.openejb.SystemException(e);
   94           }
   95   
   96       }
   97   
   98   }

Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » core » stateful » [javadoc | source]