Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 
 package org.apache.catalina.session;
 
 
 import static org.jboss.web.CatalinaMessages.MESSAGES;
 
 import java.io.File;
 
 
Concrete implementation of the Store interface that utilizes a file per saved Session in a configured directory. Sessions that are saved are still subject to being expired based on inactivity.

Author(s):
Craig R. McClanahan
Version:
$Revision: 1120 $ $Date: 2009-06-22 16:08:27 +0200 (Mon, 22 Jun 2009) $
 
 
 public final class FileStore
     extends StoreBase implements Store {
 
 
     // ----------------------------------------------------- Constants
 

    
The extension to use for serialized session filenames.
 
     private static final String FILE_EXT = ".session";
 
 
     // ----------------------------------------------------- Instance Variables
 

    
The pathname of the directory in which Sessions are stored. This may be an absolute pathname, or a relative path that is resolved against the temporary work directory for this application.
 
     private String directory = ".";


    
A File representing the directory in which Sessions are stored.
 
     private File directoryFile = null;


    
The descriptive information about this implementation.
 
     private static final String info = "FileStore/1.0";

    
Name to register for this Store, used for logging.
 
     private static final String storeName = "fileStore";

    
Name to register for the background thread.
 
     private static final String threadName = "FileStore";
 
 
    // ------------------------------------------------------------- Properties


    
Return the directory path for this Store.
    public String getDirectory() {
        return ();
    }


    
Set the directory path for this Store.

Parameters:
path The new directory path
    public void setDirectory(String path) {
        String oldDirectory = this.;
        this. = path;
        this. = null;
        .firePropertyChange("directory"oldDirectory,
                                   this.);
    }


    
Return descriptive information about this Store implementation and the corresponding version number, in the format <description>/<version>.
    public String getInfo() {
        return ();
    }

    
Return the thread name for this Store.
    public String getThreadName() {
        return();
    }

    
Return the name for this Store, used for logging.
    public String getStoreName() {
        return();
    }


    
Return the number of Sessions present in this Store.

Throws:
java.io.IOException if an input/output error occurs
    public int getSize() throws IOException {
        // 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.lengthi++) {
            if (files[i].endsWith()) {
                keycount++;
            }
        }
        return (keycount);
    }
    // --------------------------------------------------------- Public Methods


    
Remove all of the Sessions in this Store.

Throws:
java.io.IOException if an input/output error occurs
    public void clear()
        throws IOException {
        String[] keys = keys();
        for (int i = 0; i < keys.lengthi++) {
            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 zero-length array is returned.

Throws:
java.io.IOException if an input/output error occurred
    public String[] keys() throws IOException {
        // Acquire the list of files in our storage directory
        File file = directory();
        if (file == null) {
            return (new String[0]);
        }
        String files[] = file.list();
        
        // Bugzilla 32130
        if((files == null) || (files.length < 1)) {
            return (new String[0]);
        }
        // Build and return the list of session identifiers
        ArrayList list = new ArrayList();
        int n = .length();
        for (int i = 0; i < files.lengthi++) {
            if (files[i].endsWith()) {
                list.add(files[i].substring(0, files[i].length() - n));
            }
        }
        return ((String[]) list.toArray(new String[list.size()]));
    }


    
Load and return the Session associated with the specified session identifier from this Store, without removing it. If there is no such stored Session, return null.

Parameters:
id Session identifier of the session to load
Throws:
java.lang.ClassNotFoundException if a deserialization error occurs
java.io.IOException if an input/output error occurs
    public Session load(String id)
        throws ClassNotFoundExceptionIOException {
        // Open an input stream to the specified pathname, if any
        File file = file(id);
        if (file == null) {
            return (null);
        }
        if (! file.exists()) {
            return (null);
        }
        if (.getContainer().getLogger().isDebugEnabled()) {
        }
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        Loader loader = null;
        ClassLoader classLoader = null;
        try {
            fis = new FileInputStream(file.getAbsolutePath());
            BufferedInputStream bis = new BufferedInputStream(fis);
            Container container = .getContainer();
            if (container != null)
                loader = container.getLoader();
            if (loader != null)
                classLoader = loader.getClassLoader();
            if (classLoader != null)
                ois = new CustomObjectInputStream(bisclassLoader);
            else
                ois = new ObjectInputStream(bis);
        } catch (FileNotFoundException e) {
            if (.getContainer().getLogger().isDebugEnabled())
                .getContainer().getLogger().debug(.fileStoreFileNotFound());
            return (null);
        } catch (IOException e) {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException f) {
                    ;
                }
                ois = null;
            }
            throw e;
        }
        try {
            StandardSession session =
                (StandardSession.createEmptySession();
            session.readObjectData(ois);
            session.setManager();
            return (session);
        } finally {
            // Close the input stream
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException f) {
                    ;
                }
            }
        }
    }


    
Remove the Session with the specified session identifier from this Store, if present. If no such Session is present, this method takes no action.

Parameters:
id Session identifier of the Session to be removed
Throws:
java.io.IOException if an input/output error occurs
    public void remove(String idthrows IOException {
        File file = file(id);
        if (file == null) {
            return;
        }
        if (.getContainer().getLogger().isDebugEnabled()) {
        }
        file.delete();
    }


    
Save the specified Session into this Store. Any previously saved information for the associated session identifier is replaced.

Parameters:
session Session to be saved
Throws:
java.io.IOException if an input/output error occurs
    public void save(Session sessionthrows IOException {
        // Open an output stream to the specified pathname, if any
        File file = file(session.getIdInternal());
        if (file == null) {
            return;
        }
        if (.getContainer().getLogger().isDebugEnabled()) {
            .getContainer().getLogger().debug
                (.fileStoreSessionSave(session.getIdInternal(), file.getAbsolutePath()));
        }
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream(file.getAbsolutePath());
            oos = new ObjectOutputStream(new BufferedOutputStream(fos));
        } catch (IOException e) {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException f) {
                    ;
                }
            }
            throw e;
        }
        try {
            ((StandardSession)session).writeObjectData(oos);
        } finally {
            oos.close();
        }
    }
    // -------------------------------------------------------- Private Methods


    
Return a File object representing the pathname to our session persistence directory, if any. The directory will be created if it does not already exist.
    private File directory() {
        if (this. == null) {
            return (null);
        }
        if (this. != null) {
            // NOTE:  Race condition is harmless, so do not synchronize
            return (this.);
        }
        File file = new File(this.);
        if (!file.isAbsolute()) {
            Container container = .getContainer();
            if (container instanceof Context) {
                ServletContext servletContext =
                    ((Contextcontainer).getServletContext();
                File work = (File)
                    servletContext.getAttribute(.);
                file = new File(workthis.);
            } else {
                throw .parentNotContext();
            }
        }
        if (!file.exists() || !file.isDirectory()) {
            file.delete();
            file.mkdirs();
        }
        this. = file;
        return (file);
    }


    
Return a File object representing the pathname to our session persistence file, if any.

Parameters:
id The ID of the Session to be retrieved. This is used in the file naming.
    private File file(String id) {
        if (this. == null) {
            return (null);
        }
        String filename = id + ;
        File file = new File(directory(), filename);
        return (file);
    }
New to GrepCode? Check out our FAQ X