e.g. Calendar Search Help
You must enter a value before pressing Search
tomcat

Class: org.apache.catalina.session.FileStore   ©

 OK to copy?
001 /*
002  * Copyright 1999,2004 The Apache Software Foundation.
003  * 
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  * 
008  *      http://www.apache.org/licenses/LICENSE-2.0
009  * 
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 
017 
018 package org.apache.catalina.session;
019 
020 
021 import java.io.BufferedInputStream;
022 import java.io.BufferedOutputStream;
023 import java.io.File;
024 import java.io.FileInputStream;
025 import java.io.FileNotFoundException;
026 import java.io.FileOutputStream;
027 import java.io.IOException;
028 import java.io.ObjectInputStream;
029 import java.io.ObjectOutputStream;
030 import java.util.ArrayList;
031 
032 import javax.servlet.ServletContext;
033 
034 import org.apache.catalina.Container;
035 import org.apache.catalina.Context;
036 import org.apache.catalina.Globals;
037 import org.apache.catalina.Loader;
038 import org.apache.catalina.Session;
039 import org.apache.catalina.Store;
040 import org.apache.catalina.util.CustomObjectInputStream;
041 
042 
043 /**
044  * Concrete implementation of the <b>Store</b> interface that utilizes
045  * a file per saved Session in a configured directory.  Sessions that are
046  * saved are still subject to being expired based on inactivity.
047  *
048  * @author Craig R. McClanahan
049  * @version $Revision: 1.5 $ $Date: 2004/06/23 13:51:36 $
050  */
051 
052 public final class FileStore
053     extends StoreBase implements Store {
054 
055 
056     // ----------------------------------------------------- Constants
057 
058 
059     /**
060      * The extension to use for serialized session filenames.
061      */
062     private static final String FILE_EXT = ".session";
063 
064 
065     // ----------------------------------------------------- Instance Variables
066 
067 
068     /**
069      * The pathname of the directory in which Sessions are stored.
070      * This may be an absolute pathname, or a relative path that is
071      * resolved against the temporary work directory for this application.
072      */
073     private String directory = ".";
074 
075 
076     /**
077      * A File representing the directory in which Sessions are stored.
078      */
079     private File directoryFile = null;
080 
081 
082     /**
083      * The descriptive information about this implementation.
084      */
085     private static final String info = "FileStore/1.0";
086 
087     /**
088      * Name to register for this Store, used for logging.
089      */
090     private static final String storeName = "fileStore";
091 
092     /**
093      * Name to register for the background thread.
094      */
095     private static final String threadName = "FileStore";
096 
097 
098     // ------------------------------------------------------------- Properties
099 
100 
101     /**
102      * Return the directory path for this Store.
103      */
104     public String getDirectory() {
105 
106         return (directory);
107 
108     }
109 
110 
111     /**
112      * Set the directory path for this Store.
113      *
114      * @param path The new directory path
115      */
116     public void setDirectory(String path) {
117 
118         String oldDirectory = this.directory;
119         this.directory = path;
120         this.directoryFile = null;
121         support.firePropertyChange("directory", oldDirectory,
122                                    this.directory);
123 
124     }
125 
126 
127     /**
128      * Return descriptive information about this Store implementation and
129      * the corresponding version number, in the format
130      * <code>&lt;description&gt;/&lt;version&gt;</code>.
131      */
132     public String getInfo() {
133 
134         return (info);
135 
136     }
137 
138     /**
139      * Return the thread name for this Store.
140      */
141     public String getThreadName() {
142         return(threadName);
143     }
144 
145     /**
146      * Return the name for this Store, used for logging.
147      */
148     public String getStoreName() {
149         return(storeName);
150     }
151 
152 
153     /**
154      * Return the number of Sessions present in this Store.
155      *
156      * @exception IOException if an input/output error occurs
157      */
158     public int getSize() throws IOException {
159 
160         // Acquire the list of files in our storage directory
161         File file = directory();
162         if (file == null) {
163             return (0);
164         }
Rate165         String files[] = file.list();
166 
167         // Figure out which files are sessions
168         int keycount = 0;
169         for (int i = 0; i < files.length; i++) {
170             if (files[i].endsWith(FILE_EXT)) {
171                 keycount++;
172             }
173         }
174         return (keycount);
175 
176     }
177 
178 
179     // --------------------------------------------------------- Public Methods
180 
181 
182     /**
183      * Remove all of the Sessions in this Store.
184      *
185      * @exception IOException if an input/output error occurs
186      */
187     public void clear()
188         throws IOException {
189 
190         String[] keys = keys();
191         for (int i = 0; i < keys.length; i++) {
192             remove(keys[i]);
193         }
194 
195     }
196 
197 
198     /**
199      * Return an array containing the session identifiers of all Sessions
200      * currently saved in this Store.  If there are no such Sessions, a
201      * zero-length array is returned.
202      *
203      * @exception IOException if an input/output error occurred
204      */
205     public String[] keys() throws IOException {
206 
207         // Acquire the list of files in our storage directory
208         File file = directory();
209         if (file == null) {
210             return (new String[0]);
211         }
Rate212         String files[] = file.list();
213 
214         // Build and return the list of session identifiers
215         ArrayList list = new ArrayList();
216         int n = FILE_EXT.length();
217         for (int i = 0; i < files.length; i++) {
218             if (files[i].endsWith(FILE_EXT)) {
219                 list.add(files[i].substring(0, files[i].length() - n));
220             }
221         }
222         return ((String[]) list.toArray(new String[list.size()]));
223 
224     }
225 
226 
227     /**
228      * Load and return the Session associated with the specified session
229      * identifier from this Store, without removing it.  If there is no
230      * such stored Session, return <code>null</code>.
231      *
232      * @param id Session identifier of the session to load
233      *
234      * @exception ClassNotFoundException if a deserialization error occurs
235      * @exception IOException if an input/output error occurs
236      */
237     public Session load(String id)
238         throws ClassNotFoundException, IOException {
239 
240         // Open an input stream to the specified pathname, if any
241         File file = file(id);
242         if (file == null) {
243             return (null);
244         }
245 
246         if (! file.exists()) {
247             return (null);
248         }
249         if (manager.getContainer().getLogger().isDebugEnabled()) {
250             manager.getContainer().getLogger().debug(sm.getString(getStoreName()+".loading",
251                              id, file.getAbsolutePath()));
252         }
253 
254         FileInputStream fis = null;
255         ObjectInputStream ois = null;
256         Loader loader = null;
257         ClassLoader classLoader = null;
258         try {
259             fis = new FileInputStream(file.getAbsolutePath());
260             BufferedInputStream bis = new BufferedInputStream(fis);
261             Container container = manager.getContainer();
262             if (container != null)
263                 loader = container.getLoader();
264             if (loader != null)
265                 classLoader = loader.getClassLoader();
266             if (classLoader != null)
267                 ois = new CustomObjectInputStream(bis, classLoader);
268             else
269                 ois = new ObjectInputStream(bis);
270         } catch (FileNotFoundException e) {
271             if (manager.getContainer().getLogger().isDebugEnabled())
272                 manager.getContainer().getLogger().debug("No persisted data file found");
273             return (null);
274         } catch (IOException e) {
275             if (ois != null) {
276                 try {
277                     ois.close();
278                 } catch (IOException f) {
279                     ;
280                 }
281                 ois = null;
282             }
283             throw e;
284         }
285 
286         try {
287             StandardSession session =
288                 (StandardSession) manager.createEmptySession();
289             session.readObjectData(ois);
290             session.setManager(manager);
291             return (session);
292         } finally {
293             // Close the input stream
294             if (ois != null) {
295                 try {
296                     ois.close();
297                 } catch (IOException f) {
298                     ;
299                 }
300             }
301         }
302     }
303 
304 
305     /**
306      * Remove the Session with the specified session identifier from
307      * this Store, if present.  If no such Session is present, this method
308      * takes no action.
309      *
310      * @param id Session identifier of the Session to be removed
311      *
312      * @exception IOException if an input/output error occurs
313      */
314     public void remove(String id) throws IOException {
315 
316         File file = file(id);
317         if (file == null) {
318             return;
319         }
320         if (manager.getContainer().getLogger().isDebugEnabled()) {
321             manager.getContainer().getLogger().debug(sm.getString(getStoreName()+".removing",
322                              id, file.getAbsolutePath()));
323         }
324         file.delete();
325 
326     }
327 
328 
329     /**
330      * Save the specified Session into this Store.  Any previously saved
331      * information for the associated session identifier is replaced.
332      *
333      * @param session Session to be saved
334      *
335      * @exception IOException if an input/output error occurs
336      */
337     public void save(Session session) throws IOException {
338 
339         // Open an output stream to the specified pathname, if any
340         File file = file(session.getId());
341         if (file == null) {
342             return;
343         }
344         if (manager.getContainer().getLogger().isDebugEnabled()) {
345             manager.getContainer().getLogger().debug(sm.getString(getStoreName()+".saving",
346                              session.getId(), file.getAbsolutePath()));
347         }
348         FileOutputStream fos = null;
349         ObjectOutputStream oos = null;
350         try {
351             fos = new FileOutputStream(file.getAbsolutePath());
352             oos = new ObjectOutputStream(new BufferedOutputStream(fos));
353         } catch (IOException e) {
354             if (oos != null) {
355                 try {
356                     oos.close();
357                 } catch (IOException f) {
358                     ;
359                 }
360             }
361             throw e;
362         }
363 
364         try {
365             ((StandardSession)session).writeObjectData(oos);
366         } finally {
367             oos.close();
368         }
369 
370     }
371 
372 
373     // -------------------------------------------------------- Private Methods
374 
375 
376     /**
377      * Return a File object representing the pathname to our
378      * session persistence directory, if any.  The directory will be
379      * created if it does not already exist.
380      */
381     private File directory() {
382 
383         if (this.directory == null) {
384             return (null);
385         }
386         if (this.directoryFile != null) {
387             // NOTE:  Race condition is harmless, so do not synchronize
388             return (this.directoryFile);
389         }
390         File file = new File(this.directory);
391         if (!file.isAbsolute()) {
392             Container container = manager.getContainer();
393             if (container instanceof Context) {
394                 ServletContext servletContext =
395                     ((Context) container).getServletContext();
396                 File work = (File)
397                     servletContext.getAttribute(Globals.WORK_DIR_ATTR);
398                 file = new File(work, this.directory);
399             } else {
400                 throw new IllegalArgumentException
401                     ("Parent Container is not a Context");
402             }
403         }
404         if (!file.exists() || !file.isDirectory()) {
405             file.delete();
406             file.mkdirs();
407         }
408         this.directoryFile = file;
409         return (file);
410 
411     }
412 
413 
414     /**
415      * Return a File object representing the pathname to our
416      * session persistence file, if any.
417      *
418      * @param id The ID of the Session to be retrieved. This is
419      *    used in the file naming.
420      */
421     private File file(String id) {
422 
423         if (this.directory == null) {
424             return (null);
425         }
426         String filename = id + FILE_EXT;
427         File file = new File(directory(), filename);
428         return (file);
429 
430     }
431 
432 
433 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
165 0% of 0
212 0% of 0