001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.catalina.container;
019:
020: import java.io.IOException;
021: import java.io.ObjectInputStream;
022: import java.io.BufferedInputStream;
023: import java.io.ByteArrayInputStream;
024: import java.io.ByteArrayOutputStream;
025: import java.io.ObjectOutputStream;
026: import java.io.BufferedOutputStream;
027: import java.util.List;
028: import java.util.Iterator;
029:
030: import org.ofbiz.entity.GenericDelegator;
031: import org.ofbiz.entity.GenericEntityException;
032: import org.ofbiz.entity.GenericValue;
033: import org.ofbiz.base.util.UtilMisc;
034: import org.ofbiz.base.util.Debug;
035:
036: import org.apache.catalina.session.StoreBase;
037: import org.apache.catalina.session.StandardSession;
038: import org.apache.catalina.Store;
039: import org.apache.catalina.Session;
040: import org.apache.catalina.Loader;
041: import org.apache.catalina.Container;
042: import org.apache.catalina.util.CustomObjectInputStream;
043:
044: public class OfbizStore extends StoreBase implements Store {
045:
046: public static final String module = OfbizStore.class.getName();
047: public static final String entityName = "CatalinaSession";
048:
049: protected static String info = "OfbizStore/1.0";
050: protected static String storeName = "OfbizStore";
051:
052: protected GenericDelegator delegator = null;
053:
054: public OfbizStore(GenericDelegator delegator) {
055: this .delegator = delegator;
056: }
057:
058: public String getInfo() {
059: return info;
060: }
061:
062: public String getStoreName() {
063: return storeName;
064: }
065:
066: public int getSize() throws IOException {
067: long count = 0;
068: try {
069: count = delegator.findCountByAnd(entityName, null);
070: } catch (GenericEntityException e) {
071: throw new IOException(e.getMessage());
072: }
073:
074: return (int) count;
075: }
076:
077: public String[] keys() throws IOException {
078: List sessions = null;
079: try {
080: sessions = delegator.findAll(entityName);
081: } catch (GenericEntityException e) {
082: throw new IOException(e.getMessage());
083: }
084:
085: if (sessions == null) {
086: return new String[0];
087: } else {
088: String[] ids = new String[sessions.size()];
089: Iterator i = sessions.iterator();
090: int loc = 0;
091: while (i.hasNext()) {
092: GenericValue value = (GenericValue) i.next();
093: ids[loc] = value.getString("sessionId");
094: }
095:
096: return ids;
097: }
098: }
099:
100: public Session load(String id) throws ClassNotFoundException,
101: IOException {
102: StandardSession _session = null;
103: GenericValue sessionValue = null;
104: try {
105: sessionValue = delegator.findByPrimaryKey(entityName,
106: UtilMisc.toMap("sessionId", id));
107: } catch (GenericEntityException e) {
108: throw new IOException(e.getMessage());
109: }
110:
111: if (sessionValue != null) {
112: byte[] bytes = sessionValue.getBytes("sessionInfo");
113: if (bytes != null) {
114: BufferedInputStream bis = new BufferedInputStream(
115: new ByteArrayInputStream(bytes));
116:
117: Container container = manager.getContainer();
118: ClassLoader classLoader = null;
119: Loader loader = null;
120:
121: if (container != null) {
122: loader = container.getLoader();
123: }
124: if (loader != null) {
125: classLoader = loader.getClassLoader();
126: }
127:
128: ObjectInputStream ois = null;
129: if (classLoader != null) {
130: ois = new CustomObjectInputStream(bis, classLoader);
131: } else {
132: ois = new ObjectInputStream(bis);
133: }
134:
135: //Debug.logInfo("Loading Session Store [" + id + "]", module);
136: _session = (StandardSession) manager
137: .createEmptySession();
138: _session.readObjectData(ois);
139: _session.setManager(manager);
140: }
141: }
142:
143: return _session;
144: }
145:
146: public void remove(String id) throws IOException {
147: try {
148: delegator.removeByAnd(entityName, UtilMisc.toMap(
149: "sessionId", id));
150: } catch (GenericEntityException e) {
151: throw new IOException(e.getMessage());
152: }
153: }
154:
155: public void clear() throws IOException {
156: try {
157: delegator.removeByAnd(entityName, null);
158: } catch (GenericEntityException e) {
159: throw new IOException(e.getMessage());
160: }
161: }
162:
163: public void save(Session session) throws IOException {
164: ByteArrayOutputStream bos = new ByteArrayOutputStream();
165: ObjectOutputStream oos = new ObjectOutputStream(
166: new BufferedOutputStream(bos));
167:
168: ((StandardSession) session).writeObjectData(oos);
169: oos.close();
170: oos = null;
171:
172: byte[] obs = bos.toByteArray();
173: int size = obs.length;
174:
175: GenericValue sessionValue = delegator.makeValue(entityName,
176: null);
177: sessionValue.setBytes("sessionInfo", obs);
178: sessionValue.set("sessionId", session.getId());
179: sessionValue.set("sessionSize", new Long(size));
180: sessionValue.set("isValid", session.isValid() ? "Y" : "N");
181: sessionValue.set("maxIdle", new Long(session
182: .getMaxInactiveInterval()));
183: sessionValue.set("lastAccessed", new Long(session
184: .getLastAccessedTime()));
185:
186: try {
187: delegator.createOrStore(sessionValue);
188: } catch (GenericEntityException e) {
189: throw new IOException(e.getMessage());
190: }
191:
192: Debug.logInfo("Persisted session [" + session.getId() + "]",
193: module);
194: }
195: }
|