KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > catalina > container > OfbizStore


1 /*
2  * $Id: OfbizStore.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  */

5 package org.ofbiz.catalina.container;
6
7 import java.io.IOException JavaDoc;
8 import java.io.ObjectInputStream JavaDoc;
9 import java.io.BufferedInputStream JavaDoc;
10 import java.io.ByteArrayInputStream JavaDoc;
11 import java.io.ByteArrayOutputStream JavaDoc;
12 import java.io.ObjectOutputStream JavaDoc;
13 import java.io.BufferedOutputStream JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.ofbiz.entity.GenericDelegator;
18 import org.ofbiz.entity.GenericEntityException;
19 import org.ofbiz.entity.GenericValue;
20 import org.ofbiz.base.util.UtilMisc;
21 import org.ofbiz.base.util.Debug;
22
23 import org.apache.catalina.session.StoreBase;
24 import org.apache.catalina.session.StandardSession;
25 import org.apache.catalina.Store;
26 import org.apache.catalina.Session;
27 import org.apache.catalina.Loader;
28 import org.apache.catalina.Container;
29 import org.apache.catalina.util.CustomObjectInputStream;
30
31 /**
32  *
33  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
34  * @version $Rev: 5462 $
35  * @since Jul 4, 2004
36  */

37 public class OfbizStore extends StoreBase implements Store {
38
39     public static final String JavaDoc module = OfbizStore.class.getName();
40     public static final String JavaDoc entityName = "CatalinaSession";
41
42     protected static String JavaDoc info = "OfbizStore/1.0";
43     protected static String JavaDoc storeName = "OfbizStore";
44
45     protected GenericDelegator delegator = null;
46
47     public OfbizStore(GenericDelegator delegator) {
48         this.delegator = delegator;
49     }
50
51     public String JavaDoc getInfo() {
52         return info;
53     }
54
55     public String JavaDoc getStoreName() {
56         return storeName;
57     }
58
59     public int getSize() throws IOException JavaDoc {
60         long count = 0;
61         try {
62             count = delegator.findCountByAnd(entityName, null);
63         } catch (GenericEntityException e) {
64             throw new IOException JavaDoc(e.getMessage());
65         }
66
67         return (int) count;
68     }
69
70     public String JavaDoc[] keys() throws IOException JavaDoc {
71         List JavaDoc sessions = null;
72         try {
73             sessions = delegator.findAll(entityName);
74         } catch (GenericEntityException e) {
75             throw new IOException JavaDoc(e.getMessage());
76         }
77
78         if (sessions == null) {
79             return new String JavaDoc[0];
80         } else {
81             String JavaDoc[] ids = new String JavaDoc[sessions.size()];
82             Iterator JavaDoc i = sessions.iterator();
83             int loc = 0;
84             while (i.hasNext()) {
85                 GenericValue value = (GenericValue) i.next();
86                 ids[loc] = value.getString("sessionId");
87             }
88
89             return ids;
90         }
91     }
92
93     public Session load(String JavaDoc id) throws ClassNotFoundException JavaDoc, IOException JavaDoc {
94         StandardSession _session = null;
95         GenericValue sessionValue = null;
96         try {
97             sessionValue = delegator.findByPrimaryKey(entityName, UtilMisc.toMap("sessionId", id));
98         } catch (GenericEntityException e) {
99             throw new IOException JavaDoc(e.getMessage());
100         }
101
102         if (sessionValue != null) {
103             byte[] bytes = sessionValue.getBytes("sessionInfo");
104             if (bytes != null) {
105                 BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(new ByteArrayInputStream JavaDoc(bytes));
106
107                 Container container = manager.getContainer();
108                 ClassLoader JavaDoc classLoader = null;
109                 Loader loader = null;
110
111                 if (container != null) {
112                     loader = container.getLoader();
113                 }
114                 if (loader != null) {
115                     classLoader = loader.getClassLoader();
116                 }
117
118                 ObjectInputStream JavaDoc ois = null;
119                 if (classLoader != null) {
120                     ois = new CustomObjectInputStream(bis, classLoader);
121                 } else {
122                     ois = new ObjectInputStream JavaDoc(bis);
123                 }
124
125                 //Debug.logInfo("Loading Session Store [" + id + "]", module);
126
_session = (StandardSession) manager.createEmptySession();
127                 _session.readObjectData(ois);
128                 _session.setManager(manager);
129             }
130         }
131
132         return _session;
133     }
134
135     public void remove(String JavaDoc id) throws IOException JavaDoc {
136         try {
137             delegator.removeByAnd(entityName, UtilMisc.toMap("sessionId", id));
138         } catch (GenericEntityException e) {
139             throw new IOException JavaDoc(e.getMessage());
140         }
141     }
142
143     public void clear() throws IOException JavaDoc {
144         try {
145             delegator.removeByAnd(entityName, null);
146         } catch (GenericEntityException e) {
147             throw new IOException JavaDoc(e.getMessage());
148         }
149     }
150
151     public void save(Session session) throws IOException JavaDoc {
152         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
153         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(new BufferedOutputStream JavaDoc(bos));
154
155         ((StandardSession) session).writeObjectData(oos);
156         oos.close();
157         oos = null;
158
159         byte[] obs = bos.toByteArray();
160         int size = obs.length;
161
162         GenericValue sessionValue = delegator.makeValue(entityName, null);
163         sessionValue.setBytes("sessionInfo", obs);
164         sessionValue.set("sessionId", session.getId());
165         sessionValue.set("sessionSize", new Long JavaDoc(size));
166         sessionValue.set("isValid", session.isValid() ? "Y" : "N");
167         sessionValue.set("maxIdle", new Long JavaDoc(session.getMaxInactiveInterval()));
168         sessionValue.set("lastAccessed", new Long JavaDoc(session.getLastAccessedTime()));
169
170         try {
171             delegator.createOrStore(sessionValue);
172         } catch (GenericEntityException e) {
173             throw new IOException JavaDoc(e.getMessage());
174         }
175
176         Debug.logInfo("Persisted session [" + session.getId() + "]", module);
177     }
178 }
179
Popular Tags