Search in sources :

Example 1 with StandardSession

use of org.apache.catalina.session.StandardSession 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)

Example 2 with StandardSession

use of org.apache.catalina.session.StandardSession in project geode by apache.

the class DeltaSessionManager method doUnload.

/**
   * Save any currently active sessions in the appropriate persistence mechanism, if any. If
   * persistence is not supported, this method returns without doing anything.
   *
   * @throws IOException if an input/output error occurs
   */
protected void doUnload() throws IOException {
    QueryService querySvc = sessionCache.getCache().getQueryService();
    Context context = getTheContext();
    if (context == null) {
        return;
    }
    String regionName;
    if (getRegionName().startsWith("/")) {
        regionName = getRegionName();
    } else {
        regionName = "/" + getRegionName();
    }
    Query query = querySvc.newQuery("select s.id from " + regionName + " as s where s.contextName = '" + context.getPath() + "'");
    getLogger().debug("Query: " + query.getQueryString());
    SelectResults results;
    try {
        results = (SelectResults) query.execute();
    } catch (Exception ex) {
        getLogger().error("Unable to perform query during doUnload", ex);
        return;
    }
    if (results.isEmpty()) {
        getLogger().debug("No sessions to unload for context " + context.getPath());
        // nothing to do
        return;
    }
    // Open an output stream to the specified pathname, if any
    File store = sessionStore(context.getPath());
    if (store == null) {
        return;
    }
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("Unloading sessions to " + store.getAbsolutePath());
    }
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    ObjectOutputStream oos = null;
    boolean error = false;
    try {
        fos = new FileOutputStream(store.getAbsolutePath());
        bos = new BufferedOutputStream(fos);
        oos = new ObjectOutputStream(bos);
    } catch (IOException e) {
        error = true;
        getLogger().error("Exception unloading sessions", e);
        throw e;
    } finally {
        if (error) {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException ioe) {
                // Ignore
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException ioe) {
                // Ignore
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException ioe) {
                // Ignore
                }
            }
        }
    }
    ArrayList<DeltaSessionInterface> list = new ArrayList<DeltaSessionInterface>();
    Iterator<String> elements = results.iterator();
    while (elements.hasNext()) {
        String id = elements.next();
        DeltaSessionInterface session = (DeltaSessionInterface) findSession(id);
        if (session != null) {
            list.add(session);
        }
    }
    // Write the number of active sessions, followed by the details
    if (getLogger().isDebugEnabled())
        getLogger().debug("Unloading " + list.size() + " sessions");
    try {
        oos.writeObject(new Integer(list.size()));
        for (DeltaSessionInterface session : list) {
            if (session instanceof StandardSession) {
                StandardSession standardSession = (StandardSession) session;
                standardSession.passivate();
                standardSession.writeObjectData(oos);
            } else {
                // All DeltaSessionInterfaces as of Geode 1.0 should be based on StandardSession
                throw new IOException("Session should be of type StandardSession");
            }
        }
    } catch (IOException e) {
        getLogger().error("Exception unloading sessions", e);
        try {
            oos.close();
        } catch (IOException f) {
        // Ignore
        }
        throw e;
    }
    // Flush and close the output stream
    try {
        oos.flush();
    } finally {
        try {
            oos.close();
        } catch (IOException f) {
        // Ignore
        }
    }
    // Locally destroy the sessions we just wrote
    if (getSessionCache().isClientServer()) {
        for (DeltaSessionInterface session : list) {
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("Locally destroying session " + session.getId());
            }
            getSessionCache().getOperatingRegion().localDestroy(session.getId());
        }
    }
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("Unloading complete");
    }
}
Also used : Context(org.apache.catalina.Context) Query(org.apache.geode.cache.query.Query) StandardSession(org.apache.catalina.session.StandardSession) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Context (org.apache.catalina.Context)2 StandardSession (org.apache.catalina.session.StandardSession)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ArrayList (java.util.ArrayList)1 Loader (org.apache.catalina.Loader)1 CustomObjectInputStream (org.apache.catalina.util.CustomObjectInputStream)1 Region (org.apache.geode.cache.Region)1 Query (org.apache.geode.cache.query.Query)1 QueryService (org.apache.geode.cache.query.QueryService)1 SelectResults (org.apache.geode.cache.query.SelectResults)1