You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by cr...@apache.org on 2002/03/18 19:56:21 UTC
cvs commit: jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/session FileStore.java
craigmcc 02/03/18 10:56:21
Modified: catalina/src/share/org/apache/catalina/session
FileStore.java
Log:
Refactor FileStore to clean up the code, and to create the storage directory
if it does not already exist.
PR: Bugzilla #7171
Submitted by: Peter Rossbach <pr at webapp.de>
Revision Changes Path
1.7 +99 -80 jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/session/FileStore.java
Index: FileStore.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/session/FileStore.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- FileStore.java 22 Jul 2001 20:25:12 -0000 1.6
+++ FileStore.java 18 Mar 2002 18:56:21 -0000 1.7
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/session/FileStore.java,v 1.6 2001/07/22 20:25:12 pier Exp $
- * $Revision: 1.6 $
- * $Date: 2001/07/22 20:25:12 $
+ * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/session/FileStore.java,v 1.7 2002/03/18 18:56:21 craigmcc Exp $
+ * $Revision: 1.7 $
+ * $Date: 2002/03/18 18:56:21 $
*
* ====================================================================
*
@@ -77,9 +77,7 @@
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.Serializable;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.Vector;
+import java.util.ArrayList;
import javax.servlet.ServletContext;
import org.apache.catalina.Context;
import org.apache.catalina.Globals;
@@ -96,7 +94,7 @@
* saved are still subject to being expired based on inactivity.
*
* @author Craig R. McClanahan
- * @version $Revision: 1.6 $ $Date: 2001/07/22 20:25:12 $
+ * @version $Revision: 1.7 $ $Date: 2002/03/18 18:56:21 $
*/
public final class FileStore
@@ -117,7 +115,8 @@
/**
* The pathname of the directory in which Sessions are stored.
- * Relative to the temp directory for the web application.
+ * This may be an absolute pathname, or a relative path that is
+ * resolved against the temporary work directory for this application.
*/
private String directory = ".";
@@ -206,15 +205,20 @@
*/
public int getSize() throws IOException {
- String[] files = getDirectoryFile().list();
+ // Acquire the list of files in our storage directory
+ File file = directory();
+ if (file == null) {
+ return (0);
+ }
+ String files[] = file.list();
// Figure out which files are sessions
int keycount = 0;
for (int i = 0; i < files.length; i++) {
- if (files[i].endsWith(FILE_EXT))
+ if (files[i].endsWith(FILE_EXT)) {
keycount++;
+ }
}
-
return (keycount);
}
@@ -222,6 +226,23 @@
// --------------------------------------------------------- Public Methods
+
+ /**
+ * Remove all of the Sessions in this Store.
+ *
+ * @exception IOException if an input/output error occurs
+ */
+ public void clear()
+ throws IOException {
+
+ String[] keys = keys();
+ for (int i = 0; i < keys.length; i++) {
+ remove(keys[i]);
+ }
+
+ }
+
+
/**
* Return an array containing the session identifiers of all Sessions
* currently saved in this Store. If there are no such Sessions, a
@@ -231,30 +252,22 @@
*/
public String[] keys() throws IOException {
- String[] files = getDirectoryFile().list();
-
- // Figure out which files contain sessions
- int keycount = 0;
- for (int i = 0; i < files.length; i++) {
- if (files[i].endsWith(FILE_EXT))
- keycount++;
- else
- files[i] = null;
+ // Acquire the list of files in our storage directory
+ File file = directory();
+ if (file == null) {
+ return (new String[0]);
}
+ String files[] = file.list();
- // Get keys from relevant filenames.
- String[] keys = new String[keycount];
- if (keycount > 0) {
- keycount = 0;
- for (int i = 0; i < files.length; i++) {
- if (files[i] != null) {
- keys[keycount] = files[i].substring (0, files[i].lastIndexOf('.'));
- keycount++;
- }
+ // Build and return the list of session identifiers
+ ArrayList list = new ArrayList();
+ int n = FILE_EXT.length();
+ for (int i = 0; i < files.length; i++) {
+ if (files[i].endsWith(FILE_EXT)) {
+ list.add(files[i].substring(0, files[i].length() - n));
}
}
-
- return (keys);
+ return ((String[]) list.toArray(new String[list.size()]));
}
@@ -274,11 +287,13 @@
// Open an input stream to the specified pathname, if any
File file = file(id);
- if (file == null)
+ if (file == null) {
return (null);
- if (debug >= 1)
+ }
+ if (debug >= 1) {
log(sm.getString(getStoreName()+".loading",
id, file.getAbsolutePath()));
+ }
FileInputStream fis = null;
ObjectInputStream ois = null;
@@ -313,7 +328,8 @@
}
try {
- StandardSession session = (StandardSession) manager.createSession();
+ StandardSession session =
+ (StandardSession) manager.createSession();
session.readObjectData(ois);
session.setManager(manager);
return (session);
@@ -341,29 +357,15 @@
*/
public void remove(String id) throws IOException {
- // Open an input stream to the specified pathname, if any
File file = file(id);
- if (file == null)
+ if (file == null) {
return;
- if (debug >= 1)
+ }
+ if (debug >= 1) {
log(sm.getString(getStoreName()+".removing",
id, file.getAbsolutePath()));
- file.delete();
- }
-
-
- /**
- * Remove all of the Sessions in this Store.
- *
- * @exception IOException if an input/output error occurs
- */
- public void clear()
- throws IOException {
-
- String[] keys = keys();
- for (int i = 0; i < keys.length; i++) {
- remove(keys[i]);
}
+ file.delete();
}
@@ -380,11 +382,13 @@
// Open an output stream to the specified pathname, if any
File file = file(session.getId());
- if (file == null)
+ if (file == null) {
return;
- if (debug >= 1)
+ }
+ if (debug >= 1) {
log(sm.getString(getStoreName()+".saving",
session.getId(), file.getAbsolutePath()));
+ }
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
@@ -412,47 +416,62 @@
// -------------------------------------------------------- Private Methods
+
/**
* Return a File object representing the pathname to our
- * session persistence file, if any.
- *
- * @param id The ID of the Session to be retrieved. This is
- * used in the file naming.
+ * session persistence directory, if any. The directory will be
+ * created if it does not already exist.
*/
- private File file(String id) {
+ private File directory() {
- if (directory == null)
+ if (this.directory == null) {
return (null);
-
- String pathname = directory + "/" + id + FILE_EXT;
- File file = new File(pathname);
- if (!file.isAbsolute()) {
- File tempdir = getDirectoryFile();
- if (tempdir != null)
- file = new File(tempdir, pathname);
}
- return (file);
-
- }
-
- /**
- * Return a File object for the directory property.
- */
- private File getDirectoryFile() {
-
- if (directoryFile == null) {
+ if (this.directoryFile != null) {
+ // NOTE: Race condition is harmless, so do not synchronize
+ return (this.directoryFile);
+ }
+ File file = new File(this.directory);
+ if (!file.isAbsolute()) {
Container container = manager.getContainer();
if (container instanceof Context) {
ServletContext servletContext =
((Context) container).getServletContext();
- directoryFile = (File)
+ File work = (File)
servletContext.getAttribute(Globals.WORK_DIR_ATTR);
+ file = new File(work, this.directory);
} else {
- throw new IllegalArgumentException("directory not set, I can't work with this Container");
+ throw new IllegalArgumentException
+ ("Parent Container is not a Context");
}
}
+ if (!file.exists() || !file.isDirectory()) {
+ file.delete();
+ file.mkdirs();
+ }
+ this.directoryFile = file;
+ return (file);
+
+ }
- return directoryFile;
+
+ /**
+ * Return a File object representing the pathname to our
+ * session persistence file, if any.
+ *
+ * @param id The ID of the Session to be retrieved. This is
+ * used in the file naming.
+ */
+ private File file(String id) {
+
+ if (this.directory == null) {
+ return (null);
+ }
+ String filename = id + FILE_EXT;
+ File file = new File(directory(), filename);
+ return (file);
}
+
+
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>