1 2 24 25 package com.lutris.appserver.server.sessionEnhydra.persistent; 26 27 import java.io.ByteArrayInputStream ; 28 import java.io.ByteArrayOutputStream ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.ObjectInputStream ; 32 import java.io.ObjectOutputStream ; 33 import java.io.ObjectStreamClass ; 34 import java.io.StreamCorruptedException ; 35 import java.math.BigDecimal ; 36 import java.sql.PreparedStatement ; 37 import java.sql.ResultSet ; 38 import java.sql.SQLException ; 39 import java.sql.Types ; 40 41 import com.lutris.appserver.server.sql.DBConnection; 42 import com.lutris.appserver.server.sql.Transaction; 43 import com.lutris.appserver.server.user.User; 44 45 51 public class PersistentSessionDO implements Transaction { 52 53 56 private PersistentSession session; 57 58 61 private boolean success = false; 62 63 66 private int rowCount = 0; 67 68 72 protected PersistentSessionDO(PersistentSession session) { 73 this.session = session; 74 } 75 76 91 public PersistentSessionDO(ResultSet rs, ClassLoader loader) 92 throws SQLException , IOException , ClassNotFoundException { 93 InputStream in = rs.getBinaryStream("data"); 94 LoaderObjectInputStream objIn = 95 new LoaderObjectInputStream(in, loader); 96 session = (PersistentSession)objIn.readObject(); 97 objIn.close(); 98 } 99 100 107 public void executeInsert(DBConnection conn) 108 throws SQLException { 109 if (session == null) { 110 return; 111 } 112 String sql = "insert into " + PersistentSessionHome.dbTableName 113 + " values (?, ?, ?, ?, ?, ?, ?, ?, ?)"; 114 PreparedStatement stmt = conn.prepareStatement(sql); 115 int param = 1; 116 stmt.setString(param++, session.getSessionKey()); 117 if (session.isNew()) { 118 stmt.setString(param++, "1"); 119 } else { 120 stmt.setString(param++, "0"); 121 } 122 stmt.setBigDecimal(param++, new BigDecimal ((double)session.getTimeCreated())); 123 stmt.setBigDecimal(param++, new BigDecimal ((double)session.getTimeLastUsed())); 124 stmt.setBigDecimal(param++, new BigDecimal ((double)session.getTimeExpires())); 125 stmt.setBigDecimal(param++, new BigDecimal ((double)session.getMaxIdleTime())); 126 stmt.setBigDecimal(param++, new BigDecimal ((double)session.getMaxNoUserIdleTime())); 127 User user = session.getUser(); 128 if (user != null) { 129 stmt.setString(param++, user.getName()); 130 } else { 131 stmt.setNull(param++, Types.VARCHAR); 132 } 133 try { 134 InputStream in = getBinaryInputStream(); 135 stmt.setBinaryStream(param++, in, in.available()); 136 rowCount = conn.executeUpdate(stmt, sql); 137 in.close(); 138 } catch (IOException e) { 139 throw new SQLException (e.getMessage()); 140 } 141 } 142 143 153 public void finalizeInsert(boolean success) { 154 this.success = success; 155 if (!success) { 156 this.rowCount = 0; 157 } 158 } 159 160 167 public void executeUpdate(DBConnection conn) 168 throws SQLException { 169 String sql = 170 "update " + PersistentSessionHome.dbTableName 171 + " set isNew = ?, timeCreated = ?, timeLastUsed = ?, " 172 + " timeExpires = ?, maxIdleTime = ?, maxNoUserIdleTime = ?, " 173 + " userName = ?, data = ? where sessionKey = ?"; 174 PreparedStatement stmt = conn.prepareStatement(sql); 175 int param = 1; 176 if (session.isNew()) { 177 stmt.setString(param++, "1"); 178 } else { 179 stmt.setString(param++, "0"); 180 } 181 stmt.setBigDecimal(param++, new BigDecimal ((double)session.getTimeCreated())); 182 stmt.setBigDecimal(param++, new BigDecimal ((double)session.getTimeLastUsed())); 183 stmt.setBigDecimal(param++, new BigDecimal ((double)session.getTimeExpires())); 184 stmt.setBigDecimal(param++, new BigDecimal ((double)session.getMaxIdleTime())); 185 stmt.setBigDecimal(param++, new BigDecimal ((double)session.getMaxNoUserIdleTime())); 186 User user = session.getUser(); 187 if (user != null) { 188 stmt.setString(param++, user.getName()); 189 } else { 190 stmt.setNull(param++, Types.VARCHAR); 191 } 192 try { 193 InputStream in = getBinaryInputStream(); 194 stmt.setBinaryStream(param++, in, in.available()); 195 stmt.setString(param++, session.getSessionKey()); 196 rowCount = conn.executeUpdate(stmt, sql); 197 in.close(); 198 } catch (IOException e) { 199 throw new SQLException (e.getMessage()); 200 } 201 } 202 203 214 public void finalizeUpdate(boolean success) { 215 this.success = success; 216 if (!success) { 217 this.rowCount = 0; 218 } 219 } 220 221 228 public void executeDelete(DBConnection conn) 229 throws SQLException { 230 String sql = "delete from " + PersistentSessionHome.dbTableName 231 + " where sessionKey = ?"; 232 PreparedStatement stmt = conn.prepareStatement(sql); 233 stmt.setString(1, session.getSessionKey()); 234 rowCount = conn.executeUpdate(stmt, sql); 235 } 236 237 246 public void finalizeDelete(boolean success) { 247 this.success = success; 248 if (!success) { 249 this.rowCount = 0; 250 } 251 } 252 253 259 private InputStream getBinaryInputStream() throws IOException { 260 ByteArrayOutputStream out = new ByteArrayOutputStream (); 261 ObjectOutputStream objOut = new ObjectOutputStream (out); 262 objOut.writeObject(session); 263 objOut.flush(); 264 ByteArrayInputStream in = new ByteArrayInputStream (out.toByteArray()); 265 objOut.close(); 266 return in; 267 } 268 269 275 PersistentSession getSession() { 276 return session; 277 } 278 279 284 boolean getTransactionStatus() { 285 return success; 286 } 287 288 292 int getTransactionRowCount() { 293 return rowCount; 294 } 295 } 296 297 298 class LoaderObjectInputStream extends ObjectInputStream { 300 301 private ClassLoader loader; 302 303 public LoaderObjectInputStream(InputStream in, ClassLoader loader) 304 throws IOException , StreamCorruptedException { 305 super(in); 306 this.loader = loader; 307 } 308 309 329 protected Class resolveClass(ObjectStreamClass v) 330 throws IOException , ClassNotFoundException { 331 if (loader != null) { 332 return loader.loadClass(v.getName()); 333 } else { 334 return super.resolveClass(v); 335 } 336 } 337 338 } 339 | Popular Tags |