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 java.io.File;
   20   import java.io.FileInputStream;
   21   import java.io.FileOutputStream;
   22   import java.io.ObjectInputStream;
   23   import java.io.ObjectOutputStream;
   24   import java.util.Map;
   25   import java.util.Properties;
   26   
   27   import org.apache.openejb.SystemException;
   28   import org.apache.openejb.core.EnvProps;
   29   import org.apache.openejb.loader.SystemInstance;
   30   import org.apache.openejb.util.LogCategory;
   31   import org.apache.openejb.util.Logger;
   32   
   33   
   34   public class SimplePassivater implements PassivationStrategy {
   35       private static final Logger logger = Logger.getInstance(LogCategory.OPENEJB, "org.apache.openejb.util.resources");
   36       private File sessionDirectory;
   37   
   38       public SimplePassivater() throws SystemException {
   39           init(null);
   40       }
   41   
   42       public void init(Properties props) throws SystemException {
   43           if (props == null) {
   44               props = new Properties();
   45           }
   46   
   47           String dir = props.getProperty(EnvProps.IM_PASSIVATOR_PATH_PREFIX);
   48   
   49           try {
   50               if (dir != null) {
   51                   sessionDirectory = SystemInstance.get().getBase().getDirectory(dir);
   52               } else {
   53                   sessionDirectory = new File(System.getProperty("java.io.tmpdir", File.separator + "tmp"));
   54               }
   55               logger.info("Using directory " + sessionDirectory + " for stateful session passivation");
   56           } catch (java.io.IOException e) {
   57               throw new SystemException(getClass().getName() + ".init(): can't use directory prefix " + dir + ":" + e, e);
   58           }
   59       }
   60   
   61       public void passivate(Object primaryKey, Object state) throws SystemException {
   62           try {
   63               String filename = primaryKey.toString().replace(':', '=');
   64   
   65               File sessionFile = new File(sessionDirectory, filename);
   66   
   67               logger.info("Passivating to file " + sessionFile);
   68               ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(sessionFile));
   69   
   70               oos.writeObject(state);// passivate just the bean instance
   71               oos.close();
   72               sessionFile.deleteOnExit();
   73           } catch (java.io.NotSerializableException nse) {
   74               logger.error("Passivation failed ", nse);
   75               throw (SystemException) new SystemException("The type " + nse.getMessage() + " is not serializable as mandated by the EJB specification.").initCause(nse);
   76           } catch (Exception t) {
   77               logger.error("Passivation failed ", t);
   78               throw new SystemException(t);
   79           }
   80       }
   81   
   82       public void passivate(Map hash) throws SystemException {
   83           for (Object id : hash.keySet()) {
   84               passivate(id, hash.get(id));
   85           }
   86       }
   87   
   88       public Object activate(Object primaryKey) throws SystemException {
   89           try {
   90               String filename = primaryKey.toString().replace(':', '=');
   91   
   92               File sessionFile = new File(sessionDirectory, filename);
   93   
   94               if (sessionFile.exists()) {
   95                   logger.info("Activating from file " + sessionFile);
   96   
   97                   ObjectInputStream ois = new ObjectInputStream(new FileInputStream(sessionFile));
   98                   Object state = ois.readObject();
   99                   ois.close();
  100                   sessionFile.delete();
  101                   return state;
  102               } else {
  103                   logger.info("Activation failed: file not found " + sessionFile);
  104                   return null;
  105               }
  106           } catch (Exception t) {
  107               logger.info("Activation failed ", t);
  108   
  109               throw new SystemException(t);
  110           }
  111       }
  112   }

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