Author: markt
Date: Fri Oct 26 21:22:13 2012
New Revision: 1402667
URL: http://svn.apache.org/viewvc?rev=1402667&view=rev
Log:
Fix resource leak warnings reported by Eclipse
Modified:
tomcat/trunk/java/org/apache/catalina/loader/WebappLoader.java
tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java
tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java
tomcat/trunk/java/org/apache/catalina/startup/ExpandWar.java
tomcat/trunk/java/org/apache/juli/FileHandler.java
Modified: tomcat/trunk/java/org/apache/catalina/loader/WebappLoader.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/loader/WebappLoader.java?rev=1402667&r1=1402666&r2=1402667&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/loader/WebappLoader.java (original)
+++ tomcat/trunk/java/org/apache/catalina/loader/WebappLoader.java Fri Oct 26
21:22:13 2012
@@ -24,7 +24,6 @@ import java.io.FileOutputStream;
import java.io.FilePermission;
import java.io.IOException;
import java.io.InputStream;
-import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
@@ -772,8 +771,7 @@ public class WebappLoader extends Lifecy
}
if (copyJars) {
- if (!copy(jar.getInputStream(),
- new FileOutputStream(destFile))) {
+ if (!copy(jar.getInputStream(),destFile)) {
throw new IOException(
sm.getString("webappLoader.copyFailure"));
}
@@ -894,25 +892,20 @@ public class WebappLoader extends Lifecy
*/
private boolean copyDir(WebResource src, File destDir) {
- try {
- WebResource[] resources =
-
src.getWebResourceRoot().listResources(src.getWebappPath());
- for (WebResource resource : resources) {
- File currentFile = new File(destDir, resource.getName());
- if (resource.isFile()) {
- InputStream is = resource.getInputStream();
- OutputStream os = new FileOutputStream(currentFile);
- if (!copy(is, os))
- return false;
- } else if (resource.isDirectory()) {
- if (!currentFile.isDirectory() && !currentFile.mkdir())
- return false;
- if (!copyDir(resource, currentFile))
- return false;
- }
+ WebResource[] resources =
+ src.getWebResourceRoot().listResources(src.getWebappPath());
+ for (WebResource resource : resources) {
+ File currentFile = new File(destDir, resource.getName());
+ if (resource.isFile()) {
+ InputStream is = resource.getInputStream();
+ if (!copy(is, currentFile))
+ return false;
+ } else if (resource.isDirectory()) {
+ if (!currentFile.isDirectory() && !currentFile.mkdir())
+ return false;
+ if (!copyDir(resource, currentFile))
+ return false;
}
- } catch (IOException e) {
- return false;
}
return true;
@@ -923,9 +916,9 @@ public class WebappLoader extends Lifecy
* Copy a file to the specified temp directory. This is required only
* because Jasper depends on it.
*/
- private boolean copy(InputStream is, OutputStream os) {
+ private boolean copy(InputStream is, File file) {
- try {
+ try (FileOutputStream os = new FileOutputStream(file)){
byte[] buf = new byte[4096];
while (true) {
int len = is.read(buf);
@@ -933,10 +926,14 @@ public class WebappLoader extends Lifecy
break;
os.write(buf, 0, len);
}
- is.close();
- os.close();
} catch (IOException e) {
return false;
+ } finally {
+ try {
+ is.close();
+ } catch (IOException e) {
+ // Ignore
+ }
}
return true;
Modified: tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java?rev=1402667&r1=1402666&r2=1402667&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java
(original)
+++ tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java Fri Oct
26 21:22:13 2012
@@ -499,25 +499,35 @@ public class DefaultServlet
InputStream resourceInputStream = null;
- // Append data specified in ranges to existing content for this
- // resource - create a temp. file on the local filesystem to
- // perform this operation
- // Assume just one range is specified for now
- if (range != null) {
- File contentFile = executePartialPut(req, range, path);
- resourceInputStream = new FileInputStream(contentFile);
- } else {
- resourceInputStream = req.getInputStream();
- }
+ try {
+ // Append data specified in ranges to existing content for this
+ // resource - create a temp. file on the local filesystem to
+ // perform this operation
+ // Assume just one range is specified for now
+ if (range != null) {
+ File contentFile = executePartialPut(req, range, path);
+ resourceInputStream = new FileInputStream(contentFile);
+ } else {
+ resourceInputStream = req.getInputStream();
+ }
- if (resources.write(path, resourceInputStream)) {
- if (resource.exists()) {
- resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
+ if (resources.write(path, resourceInputStream)) {
+ if (resource.exists()) {
+ resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
+ } else {
+ resp.setStatus(HttpServletResponse.SC_CREATED);
+ }
} else {
- resp.setStatus(HttpServletResponse.SC_CREATED);
+ resp.sendError(HttpServletResponse.SC_CONFLICT);
+ }
+ } finally {
+ if (resourceInputStream != null) {
+ try {
+ resourceInputStream.close();
+ } catch (IOException ioe) {
+ // Ignore
+ }
}
- } else {
- resp.sendError(HttpServletResponse.SC_CONFLICT);
}
}
Modified: tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java?rev=1402667&r1=1402666&r2=1402667&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java (original)
+++ tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java Fri Oct 26
21:22:13 2012
@@ -1626,6 +1626,7 @@ public class ContextConfig implements Li
* Identify the application web.xml to be used and obtain an input source
* for it.
*/
+ @SuppressWarnings("resource") // stream is meant to be left open here
protected InputSource getContextWebXmlSource() {
InputStream stream = null;
InputSource source = null;
@@ -1664,6 +1665,13 @@ public class ContextConfig implements Li
if (log.isDebugEnabled()) {
log.debug(sm.getString("contextConfig.applicationMissing") + "
" + context);
}
+ if (stream != null) {
+ try {
+ stream.close();
+ } catch (IOException e) {
+ // Ignore
+ }
+ }
} else {
source = new InputSource(url.toExternalForm());
source.setByteStream(stream);
Modified: tomcat/trunk/java/org/apache/catalina/startup/ExpandWar.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/ExpandWar.java?rev=1402667&r1=1402666&r2=1402667&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/startup/ExpandWar.java (original)
+++ tomcat/trunk/java/org/apache/catalina/startup/ExpandWar.java Fri Oct 26
21:22:13 2012
@@ -152,16 +152,16 @@ public class ExpandWar {
if (input != null) {
try {
input.close();
- } catch (Throwable t) {
- ExceptionUtils.handleThrowable(t);
+ } catch (IOException ioe) {
+ // Ignore
}
input = null;
}
if (jarFile != null) {
try {
jarFile.close();
- } catch (Throwable t) {
- ExceptionUtils.handleThrowable(t);
+ } catch (IOException ioe) {
+ // Ignore
}
jarFile = null;
}
Modified: tomcat/trunk/java/org/apache/juli/FileHandler.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/juli/FileHandler.java?rev=1402667&r1=1402666&r2=1402667&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/juli/FileHandler.java (original)
+++ tomcat/trunk/java/org/apache/juli/FileHandler.java Fri Oct 26 21:22:13 2012
@@ -21,6 +21,7 @@ package org.apache.juli;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
@@ -372,6 +373,8 @@ public class FileHandler
// Open the current log file
writerLock.writeLock().lock();
+ FileOutputStream fos = null;
+ OutputStream os = null;
try {
File pathname = new File(dir.getAbsoluteFile(), prefix
+ (rotatable ? date : "") + suffix);
@@ -383,8 +386,8 @@ public class FileHandler
return;
}
String encoding = getEncoding();
- FileOutputStream fos = new FileOutputStream(pathname, true);
- OutputStream os = bufferSize>0?new
BufferedOutputStream(fos,bufferSize):fos;
+ fos = new FileOutputStream(pathname, true);
+ os = bufferSize>0?new BufferedOutputStream(fos,bufferSize):fos;
writer = new PrintWriter(
(encoding != null) ? new OutputStreamWriter(os, encoding)
: new OutputStreamWriter(os), false);
@@ -392,6 +395,20 @@ public class FileHandler
} catch (Exception e) {
reportError(null, e, ErrorManager.OPEN_FAILURE);
writer = null;
+ if (fos != null) {
+ try {
+ fos.close();
+ } catch (IOException e1) {
+ // Ignore
+ }
+ }
+ if (os != null) {
+ try {
+ os.close();
+ } catch (IOException e1) {
+ // Ignore
+ }
+ }
} finally {
writerLock.writeLock().unlock();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]