Author: markt
Date: Thu Jan 21 18:02:10 2016
New Revision: 1726041
URL: http://svn.apache.org/viewvc?rev=1726041&view=rev
Log:
Use try-with-resources to simplify code
Some exception logging removed since it is duplicated in [start|stop]Internal
Modified:
tomcat/tc8.0.x/trunk/java/org/apache/catalina/session/StandardManager.java
Modified:
tomcat/tc8.0.x/trunk/java/org/apache/catalina/session/StandardManager.java
URL:
http://svn.apache.org/viewvc/tomcat/tc8.0.x/trunk/java/org/apache/catalina/session/StandardManager.java?rev=1726041&r1=1726040&r2=1726041&view=diff
==============================================================================
--- tomcat/tc8.0.x/trunk/java/org/apache/catalina/session/StandardManager.java
(original)
+++ tomcat/tc8.0.x/trunk/java/org/apache/catalina/session/StandardManager.java
Thu Jan 21 18:02:10 2016
@@ -201,14 +201,10 @@ public class StandardManager extends Man
if (log.isDebugEnabled()) {
log.debug(sm.getString("standardManager.loading", pathname));
}
- FileInputStream fis = null;
- BufferedInputStream bis = null;
- ObjectInputStream ois = null;
Loader loader = null;
ClassLoader classLoader = null;
- try {
- fis = new FileInputStream(file.getAbsolutePath());
- bis = new BufferedInputStream(fis);
+ try (FileInputStream fis = new FileInputStream(file.getAbsolutePath());
+ BufferedInputStream bis = new BufferedInputStream(fis);) {
Context c = getContext();
if (c != null) {
loader = c.getLoader();
@@ -219,71 +215,40 @@ public class StandardManager extends Man
if (classLoader == null) {
classLoader = getClass().getClassLoader();
}
- ois = new CustomObjectInputStream(bis, classLoader);
+
+ // Load the previously unloaded active sessions
+ synchronized (sessions) {
+ try (ObjectInputStream ois = new CustomObjectInputStream(bis,
classLoader)) {
+ 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;
- } catch (IOException e) {
- log.error(sm.getString("standardManager.loading.ioe", e), 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
- synchronized (sessions) {
- try {
- 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++;
- }
- } catch (ClassNotFoundException e) {
- log.error(sm.getString("standardManager.loading.cnfe", e), e);
- throw e;
- } catch (IOException e) {
- log.error(sm.getString("standardManager.loading.ioe", e), e);
- throw e;
- } finally {
- // Close the input stream
- try {
- ois.close();
- } catch (IOException f) {
- // ignored
- }
-
- // Delete the persistent storage file
- if (file.exists()) {
- file.delete();
- }
- }
}
if (log.isDebugEnabled()) {
@@ -326,7 +291,6 @@ public class StandardManager extends Man
*
* @exception IOException if an input/output error occurs
*/
- @SuppressWarnings("null")
protected void doUnload() throws IOException {
if (log.isDebugEnabled())
@@ -345,52 +309,19 @@ public class StandardManager extends Man
if (log.isDebugEnabled()) {
log.debug(sm.getString("standardManager.unloading", pathname));
}
- FileOutputStream fos = null;
- BufferedOutputStream bos = null;
- ObjectOutputStream oos = null;
- boolean error = false;
- try {
- fos = new FileOutputStream(file.getAbsolutePath());
- bos = new BufferedOutputStream(fos);
- oos = new ObjectOutputStream(bos);
- } catch (IOException e) {
- error = true;
- log.error(sm.getString("standardManager.unloading.ioe", e), 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
- }
- }
- }
- }
- // Write the number of active sessions, followed by the details
+ // Keep a note of sessions that are expired
ArrayList<StandardSession> list = new ArrayList<>();
- synchronized (sessions) {
- if (log.isDebugEnabled()) {
- log.debug("Unloading " + sessions.size() + " sessions");
- }
- try {
- // oos can't be null here
+
+ try (FileOutputStream fos = new
FileOutputStream(file.getAbsolutePath());
+ BufferedOutputStream bos = new BufferedOutputStream(fos);
+ ObjectOutputStream oos = new ObjectOutputStream(bos)) {
+
+ synchronized (sessions) {
+ if (log.isDebugEnabled()) {
+ log.debug("Unloading " + sessions.size() + " sessions");
+ }
+ // Write the number of active sessions, followed by the details
oos.writeObject(Integer.valueOf(sessions.size()));
Iterator<Session> elements = sessions.values().iterator();
while (elements.hasNext()) {
@@ -400,25 +331,6 @@ public class StandardManager extends Man
session.passivate();
session.writeObjectData(oos);
}
- } catch (IOException e) {
- log.error(sm.getString("standardManager.unloading.ioe", e), 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
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]