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();
}
}
}
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");
}
}
Aggregations