/** * Save any currently active sessions in the appropriate persistence mechanism, if any. If * persistence is not supported, this method returns without doing anything. * * @exception IOException if an input/output error occurs */ public void unload() throws IOException { if (debug >= 1) log("Unloading persisted sessions"); // Open an output stream to the specified pathname, if any File file = file(); if (file == null) return; if (debug >= 1) log(sm.getString("standardManager.unloading", pathname)); FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(file.getAbsolutePath()); oos = new ObjectOutputStream(new BufferedOutputStream(fos)); } catch (IOException e) { log(sm.getString("standardManager.unloading.ioe", e), e); if (oos != null) { try { oos.close(); } catch (IOException f) {; } oos = null; } throw e; } // Write the number of active sessions, followed by the details ArrayList list = new ArrayList(); synchronized (sessions) { if (debug >= 1) log("Unloading " + sessions.size() + " sessions"); try { oos.writeObject(new Integer(sessions.size())); Iterator elements = sessions.values().iterator(); while (elements.hasNext()) { StandardSession session = (StandardSession) elements.next(); list.add(session); session.passivate(); session.writeObjectData(oos); } } catch (IOException e) { log(sm.getString("standardManager.unloading.ioe", e), e); if (oos != null) { try { oos.close(); } catch (IOException f) {; } oos = null; } throw e; } } // Flush and close the output stream try { oos.flush(); oos.close(); oos = null; } catch (IOException e) { if (oos != null) { try { oos.close(); } catch (IOException f) {; } oos = null; } throw e; } // Expire all the sessions we just wrote if (debug >= 1) log("Expiring " + list.size() + " persisted sessions"); Iterator expires = list.iterator(); while (expires.hasNext()) { StandardSession session = (StandardSession) expires.next(); try { session.expire(false); } catch (Throwable t) {; } } if (debug >= 1) log("Unloading complete"); }