KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > env > SessionArrayValue


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.env;
31
32 import com.caucho.quercus.QuercusModuleException;
33 import com.caucho.quercus.lib.UnserializeReader;
34 import com.caucho.util.CacheListener;
35
36 import java.io.IOException JavaDoc;
37 import java.io.ObjectInputStream JavaDoc;
38 import java.io.ObjectOutputStream JavaDoc;
39 import java.io.Serializable JavaDoc;
40 import java.util.IdentityHashMap JavaDoc;
41 import java.util.Map JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 /**
46  * Represents the $_SESSION
47  */

48 public class SessionArrayValue extends ArrayValueWrapper
49   implements CacheListener, Serializable JavaDoc
50 {
51   static protected final Logger JavaDoc log
52     = Logger.getLogger(SessionArrayValue.class.getName());
53
54   private String JavaDoc _id;
55
56   private int _useCount;
57
58   protected long _accessTime;
59   private long _maxInactiveInterval;
60
61   private boolean _isValid;
62
63   public SessionArrayValue(String JavaDoc id, long now,
64                            long maxInactiveInterval)
65   {
66     this(id, now, maxInactiveInterval, new ArrayValueImpl());
67   }
68   
69   public SessionArrayValue(String JavaDoc id, long now,
70                            long maxInactiveInterval, ArrayValue array)
71   {
72     super(array);
73     
74     _id = id;
75     _accessTime = now;
76     _maxInactiveInterval = maxInactiveInterval;
77   }
78
79   /**
80    * Returns the session id.
81    */

82   public String JavaDoc getId()
83   {
84     return _id;
85   }
86   
87   /**
88    * Converts to an object.
89    */

90   public Object JavaDoc toObject()
91   {
92     return null;
93   }
94
95   /**
96    * Copy for serialization
97    */

98   public Value copy(Env env, IdentityHashMap JavaDoc<Value,Value> map)
99   {
100     long accessTime = _accessTime;
101
102     SessionArrayValue copy =
103       new SessionArrayValue(_id, accessTime, _maxInactiveInterval,
104                             (ArrayValue) getArray().copy(env, map));
105
106     return copy;
107   }
108
109   /**
110    * Encoding for serialization.
111    */

112   public String JavaDoc encode()
113   {
114     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
115     ArrayValue array = getArray();
116
117     synchronized (array) {
118       for (Map.Entry JavaDoc<Value,Value> entry : array.entrySet()) {
119         sb.append(entry.getKey().toString());
120         sb.append("|");
121         entry.getValue().serialize(sb);
122       }
123     }
124
125     return sb.toString();
126   }
127
128   /**
129    * Decodes encoded values, adding them to this object.
130    */

131   public boolean decode(Env env, String JavaDoc encoded)
132   {
133     ArrayValue array = getArray();
134
135     try {
136       UnserializeReader is = new UnserializeReader(encoded);
137
138       StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
139
140       synchronized (array) {
141         while (true) {
142           int ch;
143
144           sb.setLength(0);
145
146           while ((ch = is.read()) > 0 && ch != '|') {
147             sb.append((char) ch);
148           }
149
150           if (sb.length() == 0)
151             return true;
152
153           String JavaDoc key = sb.toString();
154
155           array.put(new StringValueImpl(key), is.unserialize(env));
156         }
157       }
158     } catch (IOException JavaDoc e) {
159       throw new QuercusModuleException(e);
160     }
161   }
162
163   public synchronized boolean inUse()
164   {
165     return _useCount > 0;
166   }
167
168   public synchronized void addUse()
169   {
170     _useCount++;
171   }
172
173   public boolean load()
174   {
175     return true;
176   }
177
178   /**
179    * Saves the object to the output stream.
180    */

181   public void store(ObjectOutputStream JavaDoc out)
182     throws IOException JavaDoc
183   {
184     out.writeObject(encode());
185   }
186
187   public void load(Env env, ObjectInputStream JavaDoc in)
188     throws IOException JavaDoc
189   {
190     try {
191       String JavaDoc encoded = in.readObject().toString();
192
193       decode(env, encoded);
194     } catch (ClassNotFoundException JavaDoc e) {
195       log.log(Level.WARNING, "Can't deserialize session", e);
196     }
197   }
198
199   /**
200    * Cleaning up session stuff at the end of a request.
201    *
202    * <p>If the session data has changed and we have persistent sessions,
203    * save the session. However, if save-on-shutdown is true, only save
204    * on a server shutdown.
205    */

206   public void finish()
207   {
208     int count;
209
210     synchronized (this) {
211       count = --_useCount;
212     }
213
214     if (count > 0)
215       return;
216
217     if (count < 0)
218       throw new IllegalStateException JavaDoc();
219
220     store();
221   }
222
223   /**
224    * Store on shutdown.
225    */

226   public void storeOnShutdown()
227   {
228     store();
229   }
230
231   protected void store()
232   {
233   }
234
235   public long getMaxInactiveInterval()
236   {
237     return _maxInactiveInterval;
238   }
239
240   /*
241   public void setClusterObject(ClusterObject clusterObject)
242   {
243     _clusterObject = clusterObject;
244   }
245   */

246
247   public void reset(long now)
248   {
249     setValid(true);
250     setAccess(now);
251     clear();
252   }
253
254   public synchronized long getAccessTime()
255   {
256     return _accessTime;
257   }
258   
259   public synchronized void setAccess(long now)
260   {
261     _accessTime = now;
262   }
263
264   public synchronized boolean isValid()
265   {
266     return _isValid;
267   }
268
269   public synchronized void setValid(boolean isValid)
270   {
271     _isValid = isValid;
272   }
273
274   /**
275    * Invalidates the session.
276    */

277   public void invalidate()
278   {
279     if (! _isValid)
280       throw new IllegalStateException JavaDoc(L.l("Can't call invalidate() when session is no longer valid."));
281
282     try {
283       remove();
284
285       clear();
286     } finally {
287       _isValid = false;
288     }
289   }
290
291   protected void remove()
292   {
293   }
294   
295   public boolean isEmpty()
296   {
297     return getSize() == 0;
298   }
299
300   /**
301    * Callback when the session is removed from the session cache, generally
302    * because the session cache is full.
303    */

304   public void removeEvent()
305   {
306     // XXX: logic doesn't make sense
307

308     /*
309     boolean isValid = _isValid;
310
311     if (log.isLoggable(Level.FINE)) {
312       log.fine("remove session " + _id);
313     }
314
315     long now = Alarm.getCurrentTime();
316
317     ClusterObject clusterObject = _clusterObject;
318
319     if (_isValid && clusterObject != null) {
320       try {
321         clusterObject.update();
322         clusterObject.store(this);
323       } catch (Throwable e) {
324         log.log(Level.WARNING, "Can't serialize session", e);
325       }
326     }
327     */

328   }
329   
330   //
331
// Java serialization code
332
//
333

334   private Object JavaDoc writeReplace()
335   {
336     return new ArrayValueImpl(this);
337   }
338 }
339
Popular Tags