1 5 package org.ofbiz.catalina.container; 6 7 import java.io.IOException ; 8 import java.io.ObjectInputStream ; 9 import java.io.BufferedInputStream ; 10 import java.io.ByteArrayInputStream ; 11 import java.io.ByteArrayOutputStream ; 12 import java.io.ObjectOutputStream ; 13 import java.io.BufferedOutputStream ; 14 import java.util.List ; 15 import java.util.Iterator ; 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 37 public class OfbizStore extends StoreBase implements Store { 38 39 public static final String module = OfbizStore.class.getName(); 40 public static final String entityName = "CatalinaSession"; 41 42 protected static String info = "OfbizStore/1.0"; 43 protected static String storeName = "OfbizStore"; 44 45 protected GenericDelegator delegator = null; 46 47 public OfbizStore(GenericDelegator delegator) { 48 this.delegator = delegator; 49 } 50 51 public String getInfo() { 52 return info; 53 } 54 55 public String getStoreName() { 56 return storeName; 57 } 58 59 public int getSize() throws IOException { 60 long count = 0; 61 try { 62 count = delegator.findCountByAnd(entityName, null); 63 } catch (GenericEntityException e) { 64 throw new IOException (e.getMessage()); 65 } 66 67 return (int) count; 68 } 69 70 public String [] keys() throws IOException { 71 List sessions = null; 72 try { 73 sessions = delegator.findAll(entityName); 74 } catch (GenericEntityException e) { 75 throw new IOException (e.getMessage()); 76 } 77 78 if (sessions == null) { 79 return new String [0]; 80 } else { 81 String [] ids = new String [sessions.size()]; 82 Iterator 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 id) throws ClassNotFoundException , IOException { 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 (e.getMessage()); 100 } 101 102 if (sessionValue != null) { 103 byte[] bytes = sessionValue.getBytes("sessionInfo"); 104 if (bytes != null) { 105 BufferedInputStream bis = new BufferedInputStream (new ByteArrayInputStream (bytes)); 106 107 Container container = manager.getContainer(); 108 ClassLoader 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 ois = null; 119 if (classLoader != null) { 120 ois = new CustomObjectInputStream(bis, classLoader); 121 } else { 122 ois = new ObjectInputStream (bis); 123 } 124 125 _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 id) throws IOException { 136 try { 137 delegator.removeByAnd(entityName, UtilMisc.toMap("sessionId", id)); 138 } catch (GenericEntityException e) { 139 throw new IOException (e.getMessage()); 140 } 141 } 142 143 public void clear() throws IOException { 144 try { 145 delegator.removeByAnd(entityName, null); 146 } catch (GenericEntityException e) { 147 throw new IOException (e.getMessage()); 148 } 149 } 150 151 public void save(Session session) throws IOException { 152 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 153 ObjectOutputStream oos = new ObjectOutputStream (new BufferedOutputStream (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 (size)); 166 sessionValue.set("isValid", session.isValid() ? "Y" : "N"); 167 sessionValue.set("maxIdle", new Long (session.getMaxInactiveInterval())); 168 sessionValue.set("lastAccessed", new Long (session.getLastAccessedTime())); 169 170 try { 171 delegator.createOrStore(sessionValue); 172 } catch (GenericEntityException e) { 173 throw new IOException (e.getMessage()); 174 } 175 176 Debug.logInfo("Persisted session [" + session.getId() + "]", module); 177 } 178 } 179 | Popular Tags |