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.SystemException;
019: import org.apache.openejb.util.LogCategory;
020: import org.apache.openejb.util.Logger;
021: import org.apache.openejb.core.EnvProps;
022: import org.apache.openejb.loader.SystemInstance;
023:
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.FileOutputStream;
027: import java.io.ObjectInputStream;
028: import java.io.ObjectOutputStream;
029: import java.util.Enumeration;
030: import java.util.Hashtable;
031: import java.util.Properties;
032:
033: public class SimplePassivater implements PassivationStrategy {
034: private File sessionDirectory;
035: private static final Logger logger = Logger.getInstance(
036: LogCategory.OPENEJB, "org.apache.openejb.util.resources");
037:
038: public SimplePassivater() throws SystemException {
039: init(null);
040: }
041:
042: public void init(Properties props)
043: throws org.apache.openejb.SystemException {
044: if (props == null) {
045: props = new Properties();
046: }
047:
048: String dir = props
049: .getProperty(EnvProps.IM_PASSIVATOR_PATH_PREFIX);
050:
051: try {
052:
053: if (dir != null) {
054: sessionDirectory = SystemInstance.get().getBase()
055: .getDirectory(dir);
056: } else {
057: sessionDirectory = new File(System.getProperty(
058: "java.io.tmpdir", File.separator + "tmp"));
059: }
060: logger.info("Using directory " + sessionDirectory
061: + " for stateful session passivation");
062: } catch (java.io.IOException e) {
063: throw new org.apache.openejb.SystemException(getClass()
064: .getName()
065: + ".init(): can't use directory prefix "
066: + dir
067: + ":" + e, e);
068: }
069: }
070:
071: public void passivate(Object primaryKey, Object state)
072: throws org.apache.openejb.SystemException {
073: try {
074:
075: String filename = primaryKey.toString().replace(':', '=');
076:
077: File sessionFile = new File(sessionDirectory, filename);
078:
079: logger.info("Passivating to file " + sessionFile);
080: ObjectOutputStream oos = new ObjectOutputStream(
081: new FileOutputStream(sessionFile));
082:
083: oos.writeObject(state);// passivate just the bean instance
084: oos.close();
085: sessionFile.deleteOnExit();
086: } catch (java.io.NotSerializableException nse) {
087: logger.error("Passivation failed ", nse);
088: throw (SystemException) new SystemException(
089: "The type "
090: + nse.getMessage()
091: + " in the bean class "
092: + ((BeanEntry) state).bean.getClass()
093: .getName()
094: + " is not serializable as mandated by the EJB specification.")
095: .initCause(nse);
096: } catch (Exception t) {
097: logger.error("Passivation failed ", t);
098: throw new org.apache.openejb.SystemException(t);
099: }
100:
101: }
102:
103: public void passivate(Hashtable hash)
104: throws org.apache.openejb.SystemException {
105: Enumeration enumeration = hash.keys();
106: while (enumeration.hasMoreElements()) {
107: Object id = enumeration.nextElement();
108: passivate(id, hash.get(id));
109: }
110: }
111:
112: public Object activate(Object primaryKey)
113: throws org.apache.openejb.SystemException {
114:
115: try {
116:
117: String filename = primaryKey.toString().replace(':', '=');
118:
119: File sessionFile = new File(sessionDirectory, filename);
120:
121: if (sessionFile.exists()) {
122: logger.info("Activating from file " + sessionFile);
123:
124: ObjectInputStream ois = new ObjectInputStream(
125: new FileInputStream(sessionFile));
126: Object state = ois.readObject();
127: ois.close();
128: sessionFile.delete();
129: return state;
130: } else {
131: logger.info("Activation failed: file not found "
132: + sessionFile);
133: return null;
134: }
135:
136: } catch (Exception t) {
137: logger.info("Activation failed ", t);
138:
139: throw new org.apache.openejb.SystemException(t);
140: }
141:
142: }
143:
144: }
|