Search in sources :

Example 1 with CustomObjectInputStream

use of org.apache.catalina.util.CustomObjectInputStream in project tomcat by apache.

the class StoreBase method getObjectInputStream.

// --------------------------------------------------------- Protected Methods
/**
     * Create the object input stream to use to read a session from the store.
     * Sub-classes <b>must</b> have set the thread context class loader before
     * calling this method.
     *
     * @param is The input stream provided by the sub-class that will provide
     *           the data for a session
     *
     * @return An appropriately configured ObjectInputStream from which the
     *         session can be read.
     *
     * @throws IOException if a problem occurs creating the ObjectInputStream
     */
protected ObjectInputStream getObjectInputStream(InputStream is) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(is);
    CustomObjectInputStream ois;
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (manager instanceof ManagerBase) {
        ManagerBase managerBase = (ManagerBase) manager;
        ois = new CustomObjectInputStream(bis, classLoader, manager.getContext().getLogger(), managerBase.getSessionAttributeValueClassNamePattern(), managerBase.getWarnOnSessionAttributeFilterFailure());
    } else {
        ois = new CustomObjectInputStream(bis, classLoader);
    }
    return ois;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) CustomObjectInputStream(org.apache.catalina.util.CustomObjectInputStream)

Example 2 with CustomObjectInputStream

use of org.apache.catalina.util.CustomObjectInputStream in project tomcat by apache.

the class StandardManager method doLoad.

/**
     * Load any currently active sessions that were previously unloaded
     * to the appropriate persistence mechanism, if any.  If persistence is not
     * supported, this method returns without doing anything.
     *
     * @exception ClassNotFoundException if a serialized class cannot be
     *  found during the reload
     * @exception IOException if an input/output error occurs
     */
protected void doLoad() throws ClassNotFoundException, IOException {
    if (log.isDebugEnabled()) {
        log.debug("Start: Loading persisted sessions");
    }
    // Initialize our internal data structures
    sessions.clear();
    // Open an input stream to the specified pathname, if any
    File file = file();
    if (file == null) {
        return;
    }
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("standardManager.loading", pathname));
    }
    Loader loader = null;
    ClassLoader classLoader = null;
    Log logger = null;
    try (FileInputStream fis = new FileInputStream(file.getAbsolutePath());
        BufferedInputStream bis = new BufferedInputStream(fis)) {
        Context c = getContext();
        loader = c.getLoader();
        logger = c.getLogger();
        if (loader != null) {
            classLoader = loader.getClassLoader();
        }
        if (classLoader == null) {
            classLoader = getClass().getClassLoader();
        }
        // Load the previously unloaded active sessions
        synchronized (sessions) {
            try (ObjectInputStream ois = new CustomObjectInputStream(bis, classLoader, logger, getSessionAttributeValueClassNamePattern(), getWarnOnSessionAttributeFilterFailure())) {
                Integer count = (Integer) ois.readObject();
                int n = count.intValue();
                if (log.isDebugEnabled())
                    log.debug("Loading " + n + " persisted sessions");
                for (int i = 0; i < n; i++) {
                    StandardSession session = getNewSession();
                    session.readObjectData(ois);
                    session.setManager(this);
                    sessions.put(session.getIdInternal(), session);
                    session.activate();
                    if (!session.isValidInternal()) {
                        // If session is already invalid,
                        // expire session to prevent memory leak.
                        session.setValid(true);
                        session.expire();
                    }
                    sessionCounter++;
                }
            } finally {
                // Delete the persistent storage file
                if (file.exists()) {
                    file.delete();
                }
            }
        }
    } catch (FileNotFoundException e) {
        if (log.isDebugEnabled()) {
            log.debug("No persisted data file found");
        }
        return;
    }
    if (log.isDebugEnabled()) {
        log.debug("Finish: Loading persisted sessions");
    }
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) Log(org.apache.juli.logging.Log) FileNotFoundException(java.io.FileNotFoundException) Loader(org.apache.catalina.Loader) CustomObjectInputStream(org.apache.catalina.util.CustomObjectInputStream) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) ObjectInputStream(java.io.ObjectInputStream) CustomObjectInputStream(org.apache.catalina.util.CustomObjectInputStream)

Example 3 with CustomObjectInputStream

use of org.apache.catalina.util.CustomObjectInputStream in project geode by apache.

the class DeltaSessionManager method doLoad.

/**
   * Load any currently active sessions that were previously unloaded to the appropriate persistence
   * mechanism, if any. If persistence is not supported, this method returns without doing anything.
   *
   * @throws ClassNotFoundException if a serialized class cannot be found during the reload
   * @throws IOException if an input/output error occurs
   */
protected void doLoad() throws ClassNotFoundException, IOException {
    Context context = getTheContext();
    if (context == null) {
        return;
    }
    // Open an input stream to the specified pathname, if any
    File store = sessionStore(context.getPath());
    if (store == null) {
        getLogger().debug("No session store file found");
        return;
    }
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("Loading sessions from " + store.getAbsolutePath());
    }
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    ObjectInputStream ois = null;
    Loader loader = null;
    ClassLoader classLoader = null;
    try {
        fis = new FileInputStream(store.getAbsolutePath());
        bis = new BufferedInputStream(fis);
        if (getTheContext() != null) {
            loader = getTheContext().getLoader();
        }
        if (loader != null) {
            classLoader = loader.getClassLoader();
        }
        if (classLoader != null) {
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("Creating custom object input stream for class loader");
            }
            ois = new CustomObjectInputStream(bis, classLoader);
        } else {
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("Creating standard object input stream");
            }
            ois = new ObjectInputStream(bis);
        }
    } catch (FileNotFoundException e) {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("No persisted data file found");
        }
        return;
    } catch (IOException e) {
        getLogger().error("Exception loading sessions", e);
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException f) {
            // Ignore
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException f) {
            // Ignore
            }
        }
        throw e;
    }
    // Load the previously unloaded active sessions
    try {
        Integer count = (Integer) ois.readObject();
        int n = count.intValue();
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Loading " + n + " persisted sessions");
        }
        for (int i = 0; i < n; i++) {
            StandardSession session = getNewSession();
            session.readObjectData(ois);
            session.setManager(this);
            Region region = getSessionCache().getOperatingRegion();
            DeltaSessionInterface existingSession = (DeltaSessionInterface) region.get(session.getId());
            // Check whether the existing session is newer
            if (existingSession != null && existingSession.getLastAccessedTime() > session.getLastAccessedTime()) {
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("Loaded session " + session.getId() + " is older than cached copy");
                }
                continue;
            }
            // Check whether the new session has already expired
            if (!session.isValid()) {
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("Loaded session " + session.getId() + " is invalid");
                }
                continue;
            }
            getLogger().debug("Loading session " + session.getId());
            session.activate();
            add(session);
        }
    } catch (ClassNotFoundException e) {
        getLogger().error(e);
        try {
            ois.close();
        } catch (IOException f) {
        // Ignore
        }
        throw e;
    } catch (IOException e) {
        getLogger().error(e);
        try {
            ois.close();
        } catch (IOException f) {
        // Ignore
        }
        throw e;
    } finally {
        // Close the input stream
        try {
            ois.close();
        } catch (IOException f) {
        // ignored
        }
        // Delete the persistent storage file
        if (store.exists()) {
            store.delete();
        }
    }
}
Also used : Context(org.apache.catalina.Context) StandardSession(org.apache.catalina.session.StandardSession) FileNotFoundException(java.io.FileNotFoundException) Loader(org.apache.catalina.Loader) IOException(java.io.IOException) CustomObjectInputStream(org.apache.catalina.util.CustomObjectInputStream) FileInputStream(java.io.FileInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BufferedInputStream(java.io.BufferedInputStream) Region(org.apache.geode.cache.Region) File(java.io.File) ObjectInputStream(java.io.ObjectInputStream) CustomObjectInputStream(org.apache.catalina.util.CustomObjectInputStream)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)3 CustomObjectInputStream (org.apache.catalina.util.CustomObjectInputStream)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 ObjectInputStream (java.io.ObjectInputStream)2 Context (org.apache.catalina.Context)2 Loader (org.apache.catalina.Loader)2 IOException (java.io.IOException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ServletContext (javax.servlet.ServletContext)1 StandardSession (org.apache.catalina.session.StandardSession)1 Region (org.apache.geode.cache.Region)1 Log (org.apache.juli.logging.Log)1