KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > sessionEnhydra > persistent > PersistentSessionDO


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: PersistentSessionDO.java,v 1.2 2005/03/24 10:51:20 slobodan Exp $
23  */

24
25 package com.lutris.appserver.server.sessionEnhydra.persistent;
26
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.ObjectInputStream JavaDoc;
32 import java.io.ObjectOutputStream JavaDoc;
33 import java.io.ObjectStreamClass JavaDoc;
34 import java.io.StreamCorruptedException JavaDoc;
35 import java.math.BigDecimal JavaDoc;
36 import java.sql.PreparedStatement JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.sql.Types JavaDoc;
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 /**
46  * Database interface for persistent session.
47  *
48  * @version $Revision: 1.2 $
49  * @author Kyle Clark
50  */

51 public class PersistentSessionDO implements Transaction {
52
53     /**
54      * The session represented by this object.
55      */

56     private PersistentSession session;
57
58     /**
59      * Holds the transaction status.
60      */

61     private boolean success = false;
62
63     /**
64      * Holds the transaction row count.
65      */

66     private int rowCount = 0;
67
68     /**
69      * @param session the session for which this data object
70      * will provide a database interface.
71      */

72     protected PersistentSessionDO(PersistentSession session) {
73         this.session = session;
74     }
75
76     /**
77      * @param rs
78      * Result set from which to instantiate the session.
79      * @param loader
80      * The class loader to use when reconstructing the session
81      * from serialized data.
82      * @exception SQLException
83      * If a database connection error occurs.
84      * @exception IOException
85      * If the serialized session cannot be re-instantiated from the
86      * persistent data.
87      * @exception ClassNotFoundException
88      * If a class associated with the serialized session cannot
89      * be found.
90      */

91     public PersistentSessionDO(ResultSet JavaDoc rs, ClassLoader JavaDoc loader)
92         throws SQLException JavaDoc, IOException JavaDoc, ClassNotFoundException JavaDoc {
93         InputStream JavaDoc in = rs.getBinaryStream("data");
94         LoaderObjectInputStream objIn =
95             new LoaderObjectInputStream(in, loader);
96         session = (PersistentSession)objIn.readObject();
97         objIn.close();
98     }
99
100     /**
101      * Inserts the session into the database.
102      *
103      * @param conn Database connection.
104      * @exception java.sql.SQLException If a database access error
105      * occurs or if the session data could not be serialized.
106      */

107     public void executeInsert(DBConnection conn)
108         throws SQLException JavaDoc {
109         if (session == null) {
110             return;
111         }
112         String JavaDoc sql = "insert into " + PersistentSessionHome.dbTableName
113             + " values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
114         PreparedStatement JavaDoc 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 JavaDoc((double)session.getTimeCreated()));
123         stmt.setBigDecimal(param++, new BigDecimal JavaDoc((double)session.getTimeLastUsed()));
124         stmt.setBigDecimal(param++, new BigDecimal JavaDoc((double)session.getTimeExpires()));
125         stmt.setBigDecimal(param++, new BigDecimal JavaDoc((double)session.getMaxIdleTime()));
126         stmt.setBigDecimal(param++, new BigDecimal JavaDoc((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 JavaDoc in = getBinaryInputStream();
135             stmt.setBinaryStream(param++, in, in.available());
136             rowCount = conn.executeUpdate(stmt, sql);
137             in.close();
138         } catch (IOException JavaDoc e) {
139             throw new SQLException JavaDoc(e.getMessage());
140         }
141     }
142
143     /**
144      * If this object's <code>executeInsert</code> method was
145      * called then <code>finalizeInsert</code> is called with
146      * the status of the database transaction. This method
147      * allows the data object to perform any post processing
148      * if the transaction succeeded or failed.
149      *
150      * @param success true if the transaction succeeded
151      * and this object was successfully inserted into the database.
152      */

153     public void finalizeInsert(boolean success) {
154         this.success = success;
155         if (!success) {
156             this.rowCount = 0;
157         }
158     }
159
160     /**
161      * Method to update contents of object in database.
162      *
163      * @param conn Database connection.
164      * @exception java.sql.SQLException If a database access error
165      * occurs.
166      */

167     public void executeUpdate(DBConnection conn)
168         throws SQLException JavaDoc {
169         String JavaDoc sql =
170             "update " + PersistentSessionHome.dbTableName
171             + " set isNew = ?, timeCreated = ?, timeLastUsed = ?, "
172             + " timeExpires = ?, maxIdleTime = ?, maxNoUserIdleTime = ?, "
173             + " userName = ?, data = ? where sessionKey = ?";
174         PreparedStatement JavaDoc 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 JavaDoc((double)session.getTimeCreated()));
182         stmt.setBigDecimal(param++, new BigDecimal JavaDoc((double)session.getTimeLastUsed()));
183         stmt.setBigDecimal(param++, new BigDecimal JavaDoc((double)session.getTimeExpires()));
184         stmt.setBigDecimal(param++, new BigDecimal JavaDoc((double)session.getMaxIdleTime()));
185         stmt.setBigDecimal(param++, new BigDecimal JavaDoc((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 JavaDoc 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 JavaDoc e) {
199             throw new SQLException JavaDoc(e.getMessage());
200         }
201     }
202
203     /**
204      * If this object's <code>executeUpdate</code> method was
205      * called then <code>finalizeUpdate</code> is called with
206      * the status of the database transaction.
207      * For instance the data object may want to
208      * increment its version number once it has successfully
209      * been commited to the database.
210      *
211      * @param success true if the transaction succeeded
212      * and this object was successfully updated in the database.
213      */

214     public void finalizeUpdate(boolean success) {
215         this.success = success;
216         if (!success) {
217             this.rowCount = 0;
218         }
219     }
220
221     /**
222      * Method to delete an object from the database.
223      *
224      * @param conn Database connection.
225      * @exception java.sql.SQLException If a database access error
226      * occurs.
227      */

228     public void executeDelete(DBConnection conn)
229         throws SQLException JavaDoc {
230         String JavaDoc sql = "delete from " + PersistentSessionHome.dbTableName
231             + " where sessionKey = ?";
232         PreparedStatement JavaDoc stmt = conn.prepareStatement(sql);
233         stmt.setString(1, session.getSessionKey());
234         rowCount = conn.executeUpdate(stmt, sql);
235     }
236
237     /**
238      * If this object's <code>executeDelete</code> method was
239      * called then <code>finalizeDelete</code> is called with
240      * the status of the database transaction.
241      *
242      * @param success true if the transaction succeeded
243      * and this object was successfully deleted from the
244      * database.
245      */

246     public void finalizeDelete(boolean success) {
247         this.success = success;
248         if (!success) {
249             this.rowCount = 0;
250         }
251     }
252
253     /**
254      * Returns an input stream containing the serialized
255      * session data.
256      *
257      * @return the input stream.
258      */

259     private InputStream JavaDoc getBinaryInputStream() throws IOException JavaDoc {
260         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
261         ObjectOutputStream JavaDoc objOut = new ObjectOutputStream JavaDoc(out);
262         objOut.writeObject(session);
263         objOut.flush();
264         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
265         objOut.close();
266         return in;
267     }
268
269     /**
270      * Returns the session for which this data object is
271      * acting as a data base interface.
272      *
273      * @return the session
274      */

275     PersistentSession getSession() {
276         return session;
277     }
278
279     /**
280      * Returns the transaction status.
281      *
282      * @return the transaction status.
283      */

284     boolean getTransactionStatus() {
285         return success;
286     }
287
288     /**
289      * Returns the transaction count - i.e. the number
290      * of rows affected in the transaction.
291      */

292     int getTransactionRowCount() {
293         return rowCount;
294     }
295 }
296
297
298 // Internal class that can load objects with the loader we specify.
299
class LoaderObjectInputStream extends ObjectInputStream JavaDoc {
300
301     private ClassLoader JavaDoc loader;
302
303     public LoaderObjectInputStream(InputStream JavaDoc in, ClassLoader JavaDoc loader)
304     throws IOException JavaDoc, StreamCorruptedException JavaDoc {
305         super(in);
306         this.loader = loader;
307     }
308
309     /**
310      * Subclasses may implement this method to allow classes to be
311      * fetched from an alternate source.
312      *
313      * The corresponding method in ObjectOutputStream is
314      * annotateClass. This method will be invoked only once for each
315      * unique class in the stream. This method can be implemented by
316      * subclasses to use an alternate loading mechanism but must
317      * return a Class object. Once returned, the serialVersionUID of the
318      * class is compared to the serialVersionUID of the serialized class.
319      * If there is a mismatch, the deserialization fails and an exception
320      * is raised. <p>
321      *
322      * By default the class name is resolved relative to the class
323      * that called readObject. <p>
324      *
325      * @exception ClassNotFoundException If class of
326      * a serialized object cannot be found.
327      * @since JDK1.1
328      */

329     protected Class JavaDoc resolveClass(ObjectStreamClass JavaDoc v)
330     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
331         if (loader != null) {
332             return loader.loadClass(v.getName());
333         } else {
334             return super.resolveClass(v);
335         }
336     }
337
338 }
339
Popular Tags