1 45 package org.openejb.core.stateful; 46 47 import java.io.File ; 48 import java.io.FileInputStream ; 49 import java.io.FileOutputStream ; 50 import java.io.ObjectInputStream ; 51 import java.io.ObjectOutputStream ; 52 import java.util.Enumeration ; 53 import java.util.Hashtable ; 54 import java.util.Properties ; 55 56 import org.openejb.core.EnvProps; 57 import org.openejb.util.FileUtils; 58 import org.openejb.loader.SystemInstance; 59 65 public class SimplePassivater implements PassivationStrategy { 66 private File sessionDirectory; 67 final static protected org.apache.log4j.Category logger = org.apache.log4j.Category.getInstance("OpenEJB"); 68 69 public void init(Properties props) throws org.openejb.SystemException{ 70 if (props == null) { 71 props = new Properties (); 72 } 73 74 String dir = props.getProperty(EnvProps.IM_PASSIVATOR_PATH_PREFIX); 75 76 try{ 77 78 if(dir!=null) { 79 sessionDirectory = SystemInstance.get().getBase().getDirectory(dir); 80 }else { 81 sessionDirectory = new File (System.getProperty("java.io.tmpdir", File.separator + "tmp")); 82 } 83 logger.info("Using directory "+sessionDirectory+" for stateful session passivation"); 84 }catch(java.io.IOException e) { 85 throw new org.openejb.SystemException(getClass().getName()+".init(): can't use directory prefix "+dir+":"+e); 86 } 87 } 88 89 public void passivate(Object primaryKey, Object state) 90 throws org.openejb.SystemException{ 91 try{ 92 String filename = primaryKey.toString().replace(':', '=' ); 94 95 File sessionFile = new File ( sessionDirectory, filename); 96 97 logger.info("Passivating to file "+sessionFile); 98 ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream (sessionFile)); 99 100 oos.writeObject(state); oos.close(); 102 sessionFile.deleteOnExit(); 103 } 104 catch(java.io.NotSerializableException nse ) { 105 logger.info("Passivation failed ", nse); 106 throw new org.openejb.SystemException("The type " + nse.getMessage() + " in the bean class " + ((BeanEntry)state).bean.getClass().getName() + " is not serializable as mandated by the EJB specification."); 107 } 108 catch(Exception t){ 109 logger.info("Passivation failed ", t); 110 throw new org.openejb.SystemException(t); 112 } 113 114 } 115 public void passivate(Hashtable hash)throws org.openejb.SystemException{ 116 Enumeration enumeration = hash.keys(); 117 while(enumeration.hasMoreElements()){ 118 Object id = enumeration.nextElement(); 119 passivate(id, hash.get(id)); 120 } 121 } 122 123 130 public Object activate(Object primaryKey) throws org.openejb.SystemException{ 131 132 try{ 133 String filename = primaryKey.toString().replace(':', '=' ); 135 136 File sessionFile = new File ( sessionDirectory, filename); 137 138 if(sessionFile.exists()){ 139 logger.info("Activating from file "+sessionFile); 140 141 ObjectInputStream ois = new ObjectInputStream (new FileInputStream (sessionFile)); 142 Object state = ois.readObject(); 143 ois.close(); 144 sessionFile.delete(); 145 return state; 146 }else{ 147 logger.info("Activation failed: file not found "+sessionFile); 148 return null; 149 } 150 151 }catch(Exception t){ 152 logger.info("Activation failed ", t); 153 throw new org.openejb.SystemException(t); 155 } 156 157 } 158 159 } | Popular Tags |