diff options
author | Mikhail Mazursky | 2013-08-28 03:58:56 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2013-09-09 04:24:31 +0000 |
commit | 767faece5c4692748d1ac8e130b2e4c5faa1ff20 (patch) | |
tree | 787e93c5bf452b69d28d737a04d57cddae0b7ede /jetty-maven-plugin | |
parent | cdd95bb5515f35af090bcbcf6f9af03d5c6762c9 (diff) | |
download | org.eclipse.jetty.project-767faece5c4692748d1ac8e130b2e4c5faa1ff20.tar.gz org.eclipse.jetty.project-767faece5c4692748d1ac8e130b2e4c5faa1ff20.tar.xz org.eclipse.jetty.project-767faece5c4692748d1ac8e130b2e4c5faa1ff20.zip |
[Bug 415999] Fix some of FindBugs warnings
Mostly not closed streams/DB resources are fixed. But also less
important things.
Signed-off-by: Mikhail Mazursky <mikhail.mazursky@gmail.com>
Diffstat (limited to 'jetty-maven-plugin')
6 files changed, 98 insertions, 91 deletions
diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractJettyMojo.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractJettyMojo.java index 50643d50e3..02eeba5d80 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractJettyMojo.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractJettyMojo.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.maven.plugin; import java.io.File; import java.io.FileInputStream; +import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; @@ -758,14 +759,15 @@ public abstract class AbstractJettyMojo extends AbstractMojo public void setSystemPropertiesFile(File file) throws Exception { this.systemPropertiesFile = file; - FileInputStream propFile = new FileInputStream(systemPropertiesFile); Properties properties = new Properties(); - properties.load(propFile); - + try (InputStream propFile = new FileInputStream(systemPropertiesFile)) + { + properties.load(propFile); + } if (this.systemProperties == null ) this.systemProperties = new SystemProperties(); - for (Enumeration keys = properties.keys(); keys.hasMoreElements(); ) + for (Enumeration<?> keys = properties.keys(); keys.hasMoreElements(); ) { String key = (String)keys.nextElement(); if ( ! systemProperties.containsSystemProperty(key) ) @@ -791,10 +793,8 @@ public abstract class AbstractJettyMojo extends AbstractMojo this.systemProperties = systemProperties; else { - Iterator itor = systemProperties.getSystemProperties().iterator(); - while (itor.hasNext()) + for (SystemProperty prop: systemProperties.getSystemProperties()) { - SystemProperty prop = (SystemProperty)itor.next(); this.systemProperties.setSystemProperty(prop); } } diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyRunForkedMojo.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyRunForkedMojo.java index 1e7e9feeef..77d500993b 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyRunForkedMojo.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/JettyRunForkedMojo.java @@ -26,6 +26,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; +import java.io.OutputStream; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Arrays; @@ -504,7 +505,10 @@ public class JettyRunForkedMojo extends AbstractMojo props.put("maven.war.overlay."+(i++), c.toString()); } - props.store(new BufferedOutputStream(new FileOutputStream(propsFile)), "properties for forked webapp"); + try (OutputStream out = new BufferedOutputStream(new FileOutputStream(propsFile))) + { + props.store(out, "properties for forked webapp"); + } return propsFile; } catch (Exception e) @@ -723,18 +727,20 @@ public class JettyRunForkedMojo extends AbstractMojo //child indicates it has finished starting by printing on stdout the token passed to it try { - LineNumberReader reader = new LineNumberReader(new InputStreamReader(forkedProcess.getInputStream())); String line = ""; - int attempts = maxStartupLines; //max lines we'll read trying to get token - while (attempts>0 && line != null) + try (InputStream is = forkedProcess.getInputStream(); + LineNumberReader reader = new LineNumberReader(new InputStreamReader(is))) { - --attempts; - line = reader.readLine(); - if (line != null && line.startsWith(token)) - break; - } + int attempts = maxStartupLines; //max lines we'll read trying to get token + while (attempts>0 && line != null) + { + --attempts; + line = reader.readLine(); + if (line != null && line.startsWith(token)) + break; + } - reader.close(); + } if (line != null && line.trim().equals(token)) PluginLog.getLog().info("Forked process started."); diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/MavenServerConnector.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/MavenServerConnector.java index 54aedb1303..9d343b48af 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/MavenServerConnector.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/MavenServerConnector.java @@ -47,9 +47,9 @@ import org.eclipse.jetty.util.thread.Scheduler; */ public class MavenServerConnector extends AbstractLifeCycle implements Connector { - public static int DEFAULT_PORT = 8080; - public static String DEFAULT_PORT_STR = String.valueOf(DEFAULT_PORT); - public static int DEFAULT_MAX_IDLE_TIME = 30000; + public static final int DEFAULT_PORT = 8080; + public static final String DEFAULT_PORT_STR = String.valueOf(DEFAULT_PORT); + public static final int DEFAULT_MAX_IDLE_TIME = 30000; private Server server; private ServerConnector delegate; @@ -206,7 +206,7 @@ public class MavenServerConnector extends AbstractLifeCycle implements Connector public ConnectionFactory getDefaultConnectionFactory() { checkDelegate(); - return getDefaultConnectionFactory(); + return this.delegate.getDefaultConnectionFactory(); } /** diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/SelectiveJarResource.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/SelectiveJarResource.java index 11c8d479d0..d253ca81a2 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/SelectiveJarResource.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/SelectiveJarResource.java @@ -22,6 +22,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.net.URL; import java.util.Arrays; import java.util.Collections; @@ -145,91 +146,87 @@ public class SelectiveJarResource extends JarResource URL jarFileURL = new URL(urlString.substring(startOfJarUrl, endOfJarUrl)); - InputStream is = jarFileURL.openConnection().getInputStream(); - JarInputStream jin = new JarInputStream(is); - JarEntry entry; - - while((entry=jin.getNextJarEntry())!=null) + try (InputStream is = jarFileURL.openConnection().getInputStream(); + JarInputStream jin = new JarInputStream(is)) { - String entryName = entry.getName(); + JarEntry entry; - LOG.debug("Looking at "+entryName); - String dotCheck = entryName.replace('\\', '/'); - dotCheck = URIUtil.canonicalPath(dotCheck); - if (dotCheck == null) + while((entry=jin.getNextJarEntry())!=null) { - LOG.info("Invalid entry: "+entryName); - continue; - } + String entryName = entry.getName(); + + LOG.debug("Looking at "+entryName); + String dotCheck = entryName.replace('\\', '/'); + dotCheck = URIUtil.canonicalPath(dotCheck); + if (dotCheck == null) + { + LOG.info("Invalid entry: "+entryName); + continue; + } - File file=new File(directory,entryName); + File file=new File(directory,entryName); - if (entry.isDirectory()) - { - if (isIncluded(entryName)) + if (entry.isDirectory()) { - if (!isExcluded(entryName)) + if (isIncluded(entryName)) { - // Make directory - if (!file.exists()) - file.mkdirs(); + if (!isExcluded(entryName)) + { + // Make directory + if (!file.exists()) + file.mkdirs(); + } + else + LOG.debug("{} dir is excluded", entryName); } else - LOG.debug("{} dir is excluded", entryName); + LOG.debug("{} dir is NOT included", entryName); } else - LOG.debug("{} dir is NOT included", entryName); - } - else - { - //entry is a file, is it included? - if (isIncluded(entryName)) { - if (!isExcluded(entryName)) + //entry is a file, is it included? + if (isIncluded(entryName)) { - // make directory (some jars don't list dirs) - File dir = new File(file.getParent()); - if (!dir.exists()) - dir.mkdirs(); - - // Make file - FileOutputStream fout = null; - try - { - fout = new FileOutputStream(file); - IO.copy(jin,fout); - } - finally + if (!isExcluded(entryName)) { - IO.close(fout); + // make directory (some jars don't list dirs) + File dir = new File(file.getParent()); + if (!dir.exists()) + dir.mkdirs(); + + // Make file + try (OutputStream fout = new FileOutputStream(file)) + { + IO.copy(jin,fout); + } + + // touch the file. + if (entry.getTime()>=0) + file.setLastModified(entry.getTime()); } - - // touch the file. - if (entry.getTime()>=0) - file.setLastModified(entry.getTime()); + else + LOG.debug("{} file is excluded", entryName); } else - LOG.debug("{} file is excluded", entryName); + LOG.debug("{} file is NOT included", entryName); } - else - LOG.debug("{} file is NOT included", entryName); } - } - - Manifest manifest = jin.getManifest(); - if (manifest != null) - { - if (isIncluded("META-INF") && !isExcluded("META-INF")) + + Manifest manifest = jin.getManifest(); + if (manifest != null) { - File metaInf = new File (directory, "META-INF"); - metaInf.mkdir(); - File f = new File(metaInf, "MANIFEST.MF"); - FileOutputStream fout = new FileOutputStream(f); - manifest.write(fout); - fout.close(); + if (isIncluded("META-INF") && !isExcluded("META-INF")) + { + File metaInf = new File (directory, "META-INF"); + metaInf.mkdir(); + File f = new File(metaInf, "MANIFEST.MF"); + try (OutputStream fout = new FileOutputStream(f)) + { + manifest.write(fout); + } + } } } - IO.close(jin); } } diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/Starter.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/Starter.java index dcd89597fc..87f7fe61ba 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/Starter.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/Starter.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.maven.plugin; import java.io.File; import java.io.FileInputStream; +import java.io.InputStream; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; @@ -360,7 +361,10 @@ public class Starter { File f = new File(args[++i].trim()); props = new Properties(); - props.load(new FileInputStream(f)); + try (InputStream in = new FileInputStream(f)) + { + props.load(in); + } } //--token diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/SystemProperties.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/SystemProperties.java index 590a32f9ef..19b716dfbf 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/SystemProperties.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/SystemProperties.java @@ -34,12 +34,12 @@ import java.util.Map; */ public class SystemProperties { - Map properties; - boolean force; + private final Map<String, SystemProperty> properties; + private boolean force; public SystemProperties() { - properties = new HashMap(); + properties = new HashMap<>(); } public void setForce (boolean force) @@ -64,7 +64,7 @@ public class SystemProperties public SystemProperty getSystemProperty(String name) { - return (SystemProperty)properties.get(name); + return properties.get(name); } public boolean containsSystemProperty(String name) @@ -72,8 +72,8 @@ public class SystemProperties return properties.containsKey(name); } - public List getSystemProperties () + public List<SystemProperty> getSystemProperties () { - return new ArrayList(properties.values()); + return new ArrayList<>(properties.values()); } } |