Author: markt
Date: Wed Mar 26 13:09:39 2014
New Revision: 1581820
URL: http://svn.apache.org/r1581820
Log:
More try-with-resources
Modified:
tomcat/trunk/TOMCAT-NEXT.txt
tomcat/trunk/java/org/apache/catalina/session/JDBCStore.java
tomcat/trunk/java/org/apache/catalina/session/LocalStrings.properties
tomcat/trunk/java/org/apache/catalina/session/LocalStrings_es.properties
tomcat/trunk/java/org/apache/catalina/session/LocalStrings_fr.properties
tomcat/trunk/java/org/apache/catalina/session/LocalStrings_ja.properties
tomcat/trunk/java/org/apache/catalina/session/StandardManager.java
Modified: tomcat/trunk/TOMCAT-NEXT.txt
URL:
http://svn.apache.org/viewvc/tomcat/trunk/TOMCAT-NEXT.txt?rev=1581820&r1=1581819&r2=1581820&view=diff
==============================================================================
--- tomcat/trunk/TOMCAT-NEXT.txt (original)
+++ tomcat/trunk/TOMCAT-NEXT.txt Wed Mar 26 13:09:39 2014
@@ -214,7 +214,7 @@ but possibly 7.1.x).
- Use of try with resources
- Started.
- javax.* complete
- - o.a.catalina.[ant to servlets] complete
+ - o.a.catalina.[ant to session] complete
- remainder TODO
- Catching multiple exceptions
- Started
Modified: tomcat/trunk/java/org/apache/catalina/session/JDBCStore.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/session/JDBCStore.java?rev=1581820&r1=1581819&r2=1581820&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/session/JDBCStore.java (original)
+++ tomcat/trunk/java/org/apache/catalina/session/JDBCStore.java Wed Mar 26
13:09:39 2014
@@ -468,7 +468,6 @@ public class JDBCStore extends StoreBase
*/
@Override
public String[] keys() throws IOException {
- ResultSet rst = null;
String keys[] = null;
synchronized (this) {
int numberOfTries = 2;
@@ -487,16 +486,17 @@ public class JDBCStore extends StoreBase
}
preparedKeysSql.setString(1, getName());
- rst = preparedKeysSql.executeQuery();
- ArrayList<String> tmpkeys = new ArrayList<>();
- if (rst != null) {
- while (rst.next()) {
- tmpkeys.add(rst.getString(1));
+ try (ResultSet rst = preparedKeysSql.executeQuery()) {
+ ArrayList<String> tmpkeys = new ArrayList<>();
+ if (rst != null) {
+ while (rst.next()) {
+ tmpkeys.add(rst.getString(1));
+ }
}
+ keys = tmpkeys.toArray(new String[tmpkeys.size()]);
+ // Break out after the finally block
+ numberOfTries = 0;
}
- keys = tmpkeys.toArray(new String[tmpkeys.size()]);
- // Break out after the finally block
- numberOfTries = 0;
} catch (SQLException e) {
manager.getContext().getLogger().error(sm.getString(getStoreName() +
".SQLException", e));
keys = new String[0];
@@ -504,21 +504,12 @@ public class JDBCStore extends StoreBase
if (dbConnection != null)
close(dbConnection);
} finally {
- try {
- if (rst != null) {
- rst.close();
- }
- } catch (SQLException e) {
- // Ignore
- }
-
release(_conn);
}
numberOfTries--;
}
}
-
- return (keys);
+ return keys;
}
/**
@@ -531,7 +522,6 @@ public class JDBCStore extends StoreBase
@Override
public int getSize() throws IOException {
int size = 0;
- ResultSet rst = null;
synchronized (this) {
int numberOfTries = 2;
@@ -551,30 +541,24 @@ public class JDBCStore extends StoreBase
}
preparedSizeSql.setString(1, getName());
- rst = preparedSizeSql.executeQuery();
- if (rst.next()) {
- size = rst.getInt(1);
+ try (ResultSet rst = preparedSizeSql.executeQuery()) {
+ if (rst.next()) {
+ size = rst.getInt(1);
+ }
+ // Break out after the finally block
+ numberOfTries = 0;
}
- // Break out after the finally block
- numberOfTries = 0;
} catch (SQLException e) {
manager.getContext().getLogger().error(sm.getString(getStoreName() +
".SQLException", e));
if (dbConnection != null)
close(dbConnection);
} finally {
- try {
- if (rst != null)
- rst.close();
- } catch (SQLException e) {
- // Ignore
- }
-
release(_conn);
}
numberOfTries--;
}
}
- return (size);
+ return size;
}
/**
@@ -587,9 +571,7 @@ public class JDBCStore extends StoreBase
* @exception IOException if an input/output error occurred
*/
@Override
- public Session load(String id)
- throws ClassNotFoundException, IOException {
- ResultSet rst = null;
+ public Session load(String id) throws ClassNotFoundException, IOException {
StandardSession _session = null;
Loader loader = null;
ClassLoader classLoader = null;
@@ -617,49 +599,43 @@ public class JDBCStore extends StoreBase
preparedLoadSql.setString(1, id);
preparedLoadSql.setString(2, getName());
- rst = preparedLoadSql.executeQuery();
- if (rst.next()) {
- bis = new BufferedInputStream(rst.getBinaryStream(2));
-
- if (context != null) {
- loader = context.getLoader();
- }
- if (loader != null) {
- classLoader = loader.getClassLoader();
- }
- if (classLoader != null) {
-
Thread.currentThread().setContextClassLoader(classLoader);
- ois = new CustomObjectInputStream(bis,
- classLoader);
- } else {
- ois = new ObjectInputStream(bis);
- }
-
- if (manager.getContext().getLogger().isDebugEnabled())
{
-
manager.getContext().getLogger().debug(sm.getString(getStoreName() + ".loading",
- id, sessionTable));
+ try (ResultSet rst = preparedLoadSql.executeQuery()) {
+ if (rst.next()) {
+ bis = new
BufferedInputStream(rst.getBinaryStream(2));
+
+ if (context != null) {
+ loader = context.getLoader();
+ }
+ if (loader != null) {
+ classLoader = loader.getClassLoader();
+ }
+ if (classLoader != null) {
+
Thread.currentThread().setContextClassLoader(classLoader);
+ ois = new CustomObjectInputStream(bis,
+ classLoader);
+ } else {
+ ois = new ObjectInputStream(bis);
+ }
+
+ if
(manager.getContext().getLogger().isDebugEnabled()) {
+
manager.getContext().getLogger().debug(sm.getString(getStoreName() + ".loading",
+ id, sessionTable));
+ }
+
+ _session = (StandardSession)
manager.createEmptySession();
+ _session.readObjectData(ois);
+ _session.setManager(manager);
+ } else if
(manager.getContext().getLogger().isDebugEnabled()) {
+
manager.getContext().getLogger().debug(getStoreName() + ": No persisted data
object found");
}
-
- _session = (StandardSession)
manager.createEmptySession();
- _session.readObjectData(ois);
- _session.setManager(manager);
- } else if
(manager.getContext().getLogger().isDebugEnabled()) {
- manager.getContext().getLogger().debug(getStoreName()
+ ": No persisted data object found");
+ // Break out after the finally block
+ numberOfTries = 0;
}
- // Break out after the finally block
- numberOfTries = 0;
} catch (SQLException e) {
manager.getContext().getLogger().error(sm.getString(getStoreName() +
".SQLException", e));
if (dbConnection != null)
close(dbConnection);
} finally {
- try {
- if (rst != null) {
- rst.close();
- }
- } catch (SQLException e) {
- // Ignore
- }
if (ois != null) {
try {
ois.close();
@@ -787,10 +763,7 @@ public class JDBCStore extends StoreBase
*/
@Override
public void save(Session session) throws IOException {
- ObjectOutputStream oos = null;
ByteArrayOutputStream bos = null;
- ByteArrayInputStream bis = null;
- InputStream in = null;
synchronized (this) {
int numberOfTries = 2;
@@ -807,35 +780,34 @@ public class JDBCStore extends StoreBase
remove(session.getIdInternal(), _conn);
bos = new ByteArrayOutputStream();
- oos = new ObjectOutputStream(new
BufferedOutputStream(bos));
-
- ((StandardSession) session).writeObjectData(oos);
- oos.close();
- oos = null;
+ try (ObjectOutputStream oos =
+ new ObjectOutputStream(new
BufferedOutputStream(bos))) {
+ ((StandardSession) session).writeObjectData(oos);
+ }
byte[] obs = bos.toByteArray();
int size = obs.length;
- bis = new ByteArrayInputStream(obs, 0, size);
- in = new BufferedInputStream(bis, size);
+ try (ByteArrayInputStream bis = new
ByteArrayInputStream(obs, 0, size);
+ InputStream in = new BufferedInputStream(bis,
size)) {
+ if (preparedSaveSql == null) {
+ String saveSql = "INSERT INTO " + sessionTable + "
("
+ + sessionIdCol + ", " + sessionAppCol + ", "
+ + sessionDataCol + ", " + sessionValidCol
+ + ", " + sessionMaxInactiveCol + ", "
+ + sessionLastAccessedCol
+ + ") VALUES (?, ?, ?, ?, ?, ?)";
+ preparedSaveSql = _conn.prepareStatement(saveSql);
+ }
- if (preparedSaveSql == null) {
- String saveSql = "INSERT INTO " + sessionTable + " ("
- + sessionIdCol + ", " + sessionAppCol + ", "
- + sessionDataCol + ", " + sessionValidCol
- + ", " + sessionMaxInactiveCol + ", "
- + sessionLastAccessedCol
- + ") VALUES (?, ?, ?, ?, ?, ?)";
- preparedSaveSql = _conn.prepareStatement(saveSql);
+ preparedSaveSql.setString(1, session.getIdInternal());
+ preparedSaveSql.setString(2, getName());
+ preparedSaveSql.setBinaryStream(3, in, size);
+ preparedSaveSql.setString(4, session.isValid() ? "1" :
"0");
+ preparedSaveSql.setInt(5,
session.getMaxInactiveInterval());
+ preparedSaveSql.setLong(6,
session.getLastAccessedTime());
+ preparedSaveSql.execute();
+ // Break out after the finally block
+ numberOfTries = 0;
}
-
- preparedSaveSql.setString(1, session.getIdInternal());
- preparedSaveSql.setString(2, getName());
- preparedSaveSql.setBinaryStream(3, in, size);
- preparedSaveSql.setString(4, session.isValid() ? "1" :
"0");
- preparedSaveSql.setInt(5,
session.getMaxInactiveInterval());
- preparedSaveSql.setLong(6, session.getLastAccessedTime());
- preparedSaveSql.execute();
- // Break out after the finally block
- numberOfTries = 0;
} catch (SQLException e) {
manager.getContext().getLogger().error(sm.getString(getStoreName() +
".SQLException", e));
if (dbConnection != null)
@@ -843,16 +815,6 @@ public class JDBCStore extends StoreBase
} catch (IOException e) {
// Ignore
} finally {
- if (oos != null) {
- oos.close();
- }
- if (bis != null) {
- bis.close();
- }
- if (in != null) {
- in.close();
- }
-
release(_conn);
}
numberOfTries--;
Modified: tomcat/trunk/java/org/apache/catalina/session/LocalStrings.properties
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/session/LocalStrings.properties?rev=1581820&r1=1581819&r2=1581820&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/session/LocalStrings.properties
(original)
+++ tomcat/trunk/java/org/apache/catalina/session/LocalStrings.properties Wed
Mar 26 13:09:39 2014
@@ -34,8 +34,7 @@ managerBase.container.noop=Managers adde
managerBase.createSession.ise=createSession: Too many active sessions
managerBase.sessionTimeout=Invalid session timeout setting {0}
standardManager.loading=Loading persisted sessions from {0}
-standardManager.loading.cnfe=ClassNotFoundException while loading persisted
sessions: {0}
-standardManager.loading.ioe=IOException while loading persisted sessions: {0}
+standardManager.loading.exception=Exception while loading persisted sessions
standardManager.unloading=Saving persisted sessions to {0}
standardManager.unloading.debug=Unloading persisted sessions
standardManager.unloading.ioe=IOException while saving persisted sessions: {0}
Modified:
tomcat/trunk/java/org/apache/catalina/session/LocalStrings_es.properties
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/session/LocalStrings_es.properties?rev=1581820&r1=1581819&r2=1581820&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/session/LocalStrings_es.properties
(original)
+++ tomcat/trunk/java/org/apache/catalina/session/LocalStrings_es.properties
Wed Mar 26 13:09:39 2014
@@ -27,8 +27,7 @@ JDBCStore.checkConnectionClassNotFoundEx
managerBase.createSession.ise = createSession\: Demasiadas sesiones activas
managerBase.sessionTimeout = Valor inv\u00E1lido de Tiempo Agotado de
sesi\u00F3n {0}
standardManager.loading = Cargando sesiones persistidas desde {0}
-standardManager.loading.cnfe = ClassNotFoundException al cargar sesiones
persistidas\: {0}
-standardManager.loading.ioe = IOException al cargar sesiones persistidas\: {0}
+standardManager.loading.exception = Exception al cargar sesiones persistidas
standardManager.unloading = Salvando sesiones persistidas a {0}
standardManager.unloading.ioe = IOException al salvar sesiones persistidas\:
{0}
standardManager.managerLoad = Excepci\u00F3n cargando sesiones desde
almacenamiento persistente
Modified:
tomcat/trunk/java/org/apache/catalina/session/LocalStrings_fr.properties
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/session/LocalStrings_fr.properties?rev=1581820&r1=1581819&r2=1581820&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/session/LocalStrings_fr.properties
(original)
+++ tomcat/trunk/java/org/apache/catalina/session/LocalStrings_fr.properties
Wed Mar 26 13:09:39 2014
@@ -27,8 +27,7 @@ JDBCStore.checkConnectionClassNotFoundEx
managerBase.createSession.ise="createSession": Trop de sessions actives
managerBase.sessionTimeout=R\u00e9glage du d\u00e9lai d''inactivit\u00e9
(timeout) de session invalide {0}
standardManager.loading=Chargement des sessions qui ont persist\u00e9 depuis
{0}
-standardManager.loading.cnfe="ClassNotFoundException" lors du chargement de
sessions persistantes: {0}
-standardManager.loading.ioe="IOException" lors du chargement de sessions
persistantes: {0}
+standardManager.loading.exception="Exception" lors du chargement de sessions
persistantes
standardManager.unloading=Sauvegarde des sessions ayant persist\u00e9 vers {0}
standardManager.unloading.ioe="IOException" lors de la sauvegarde de sessions
persistantes: {0}
standardManager.managerLoad=Exception au chargement des sessions depuis le
stockage persistant (persistent storage)
Modified:
tomcat/trunk/java/org/apache/catalina/session/LocalStrings_ja.properties
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/session/LocalStrings_ja.properties?rev=1581820&r1=1581819&r2=1581820&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/session/LocalStrings_ja.properties
(original)
+++ tomcat/trunk/java/org/apache/catalina/session/LocalStrings_ja.properties
Wed Mar 26 13:09:39 2014
@@ -28,8 +28,7 @@ JDBCStore.checkConnectionClassNotFoundEx
managerBase.createSession.ise=createSession:
\u30a2\u30af\u30c6\u30a3\u30d6\u30bb\u30c3\u30b7\u30e7\u30f3\u304c\u591a\u3059\u304e\u307e\u3059
managerBase.sessionTimeout=\u7121\u52b9\u306a\u30bb\u30c3\u30b7\u30e7\u30f3\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u8a2d\u5b9a\u3067\u3059
{0}
standardManager.loading={0}
\u304b\u3089\u6301\u7d9a\u3055\u308c\u305f\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u30ed\u30fc\u30c9\u3057\u3066\u3044\u307e\u3059
-standardManager.loading.cnfe=\u6301\u7d9a\u3055\u308c\u305f\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u30ed\u30fc\u30c9\u4e2d\u306bClassNotFoundException\u304c\u767a\u751f\u3057\u307e\u3057\u305f:
{0}
-standardManager.loading.ioe=\u6301\u7d9a\u3055\u308c\u305f\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u30ed\u30fc\u30c9\u4e2d\u306eIOException\u3067\u3059:
{0}
+standardManager.loading.exception=\u6301\u7d9a\u3055\u308c\u305f\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u30ed\u30fc\u30c9\u4e2d\u306bException\u304c\u767a\u751f\u3057\u307e\u3057\u305f
standardManager.unloading=\u6301\u7d9a\u3055\u308c\u305f\u30bb\u30c3\u30b7\u30e7\u30f3\u3092
{0} \u306b\u4fdd\u5b58\u3057\u307e\u3059
standardManager.unloading.ioe=\u6301\u7d9a\u3055\u308c\u305f\u30bb\u30c3\u30b7\u30e7\u30f3\u306e\u4fdd\u5b58\u4e2d\u306eIOException\u3067\u3059:
{0}
standardManager.managerLoad=\u6c38\u7d9a\u8a18\u61b6\u88c5\u7f6e\u304b\u3089\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u30ed\u30fc\u30c9\u4e2d\u306e\u4f8b\u5916\u3067\u3059
Modified: tomcat/trunk/java/org/apache/catalina/session/StandardManager.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/session/StandardManager.java?rev=1581820&r1=1581819&r2=1581820&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/session/StandardManager.java
(original)
+++ tomcat/trunk/java/org/apache/catalina/session/StandardManager.java Wed Mar
26 13:09:39 2014
@@ -204,14 +204,11 @@ public class StandardManager extends Man
return;
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();
@@ -232,20 +229,6 @@ public class StandardManager extends Man
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;
}
@@ -270,21 +253,8 @@ public class StandardManager extends Man
}
sessionCounter++;
}
- } catch (ClassNotFoundException e) {
- log.error(sm.getString("standardManager.loading.cnfe", e), e);
- try {
- ois.close();
- } catch (IOException f) {
- // Ignore
- }
- throw e;
- } catch (IOException e) {
- log.error(sm.getString("standardManager.loading.ioe", e), e);
- try {
- ois.close();
- } catch (IOException f) {
- // Ignore
- }
+ } catch (ClassNotFoundException | IOException e) {
+ log.error(sm.getString("standardManager.loading.exception"),
e);
throw e;
} finally {
// Close the input stream
@@ -339,7 +309,6 @@ public class StandardManager extends Man
*
* @exception IOException if an input/output error occurs
*/
- @SuppressWarnings("null")
protected void doUnload() throws IOException {
if (log.isDebugEnabled())
@@ -356,51 +325,16 @@ public class StandardManager extends Man
return;
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
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)) {
+
+ // Write the number of active sessions, followed by the details
+ synchronized (sessions) {
+ if (log.isDebugEnabled())
+ log.debug("Unloading " + sessions.size() + " sessions");
+
oos.writeObject(new Integer(sessions.size()));
Iterator<Session> elements = sessions.values().iterator();
while (elements.hasNext()) {
@@ -410,26 +344,10 @@ 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
}
+ } catch (IOException e) {
+ log.error(sm.getString("standardManager.unloading.ioe", e), e);
+ throw e;
}
// Expire all the sessions we just wrote
@@ -449,7 +367,6 @@ public class StandardManager extends Man
if (log.isDebugEnabled())
log.debug("Unloading complete");
-
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]