KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > core > stateful > SimplePassivater


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: SimplePassivater.java 2487 2006-02-22 22:05:03Z dblevins $
44  */

45 package org.openejb.core.stateful;
46
47 import java.io.File JavaDoc;
48 import java.io.FileInputStream JavaDoc;
49 import java.io.FileOutputStream JavaDoc;
50 import java.io.ObjectInputStream JavaDoc;
51 import java.io.ObjectOutputStream JavaDoc;
52 import java.util.Enumeration JavaDoc;
53 import java.util.Hashtable JavaDoc;
54 import java.util.Properties JavaDoc;
55
56 import org.openejb.core.EnvProps;
57 import org.openejb.util.FileUtils;
58 import org.openejb.loader.SystemInstance;
59 /**
60  *
61  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
62  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
63  * @version $Revision: 2487 $ $Date: 2006-02-22 14:05:03 -0800 (Wed, 22 Feb 2006) $
64  */

65 public class SimplePassivater implements PassivationStrategy {
66     private File JavaDoc sessionDirectory;
67     final static protected org.apache.log4j.Category logger = org.apache.log4j.Category.getInstance("OpenEJB");
68
69     public void init(Properties JavaDoc props) throws org.openejb.SystemException{
70         if (props == null) {
71             props = new Properties JavaDoc();
72         }
73
74         String JavaDoc 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 JavaDoc(System.getProperty("java.io.tmpdir", File.separator + "tmp"));
82             }
83             logger.info("Using directory "+sessionDirectory+" for stateful session passivation");
84         }catch(java.io.IOException JavaDoc 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 JavaDoc primaryKey, Object JavaDoc state)
90     throws org.openejb.SystemException{
91         try{
92            // The replace(':','=') ensures the filename is correct under Microsoft Windows OS
93
String JavaDoc filename = primaryKey.toString().replace(':', '=' );
94
95             File JavaDoc sessionFile = new File JavaDoc( sessionDirectory, filename);
96
97             logger.info("Passivating to file "+sessionFile);
98             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc(sessionFile));
99
100             oos.writeObject(state);// passivate just the bean instance
101
oos.close();
102             sessionFile.deleteOnExit();
103     }
104     catch(java.io.NotSerializableException JavaDoc 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 JavaDoc t){
109             logger.info("Passivation failed ", t);
110             // FIXME: More intelligent exception handling needed
111
throw new org.openejb.SystemException(t);
112         }
113
114     }
115     public void passivate(Hashtable JavaDoc hash)throws org.openejb.SystemException{
116         Enumeration JavaDoc enumeration = hash.keys();
117         while(enumeration.hasMoreElements()){
118             Object JavaDoc id = enumeration.nextElement();
119             passivate(id, hash.get(id));
120         }
121     }
122
123     /**
124      *
125      * @param primaryKey
126      * @return object
127      * @exception org.openejb.SystemException
128      * If there is an problem retrieving the instance from the .ser file.
129      */

130     public Object JavaDoc activate(Object JavaDoc primaryKey) throws org.openejb.SystemException{
131
132         try{
133             // The replace(':','=') ensures the filename is correct under Microsoft Windows OS
134
String JavaDoc filename = primaryKey.toString().replace(':', '=' );
135
136             File JavaDoc sessionFile = new File JavaDoc( sessionDirectory, filename);
137
138             if(sessionFile.exists()){
139                 logger.info("Activating from file "+sessionFile);
140
141                 ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(new FileInputStream JavaDoc(sessionFile));
142                 Object JavaDoc 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 JavaDoc t){
152             logger.info("Activation failed ", t);
153             // FIXME: More intelligent exception handling needed
154
throw new org.openejb.SystemException(t);
155         }
156
157     }
158
159 }
Popular Tags