IDEMPIERE-455 Discover and fix FindBugs problems / Pattern OS_OPEN_STREAM

This commit is contained in:
Richard Morales 2012-12-11 16:48:34 -05:00
parent db87b54563
commit d9afbe2768
12 changed files with 117 additions and 14 deletions

View File

@ -39,8 +39,10 @@ public class ExtensionPriorityManager {
URL url = location.getURL(); URL url = location.getURL();
File file = new File(url.getPath(), "extensions-priorty.properties"); File file = new File(url.getPath(), "extensions-priorty.properties");
if (file.exists() && file.canRead()) { if (file.exists() && file.canRead()) {
FileInputStream fileInput = null;
try { try {
priorityMap.load(new FileInputStream(file)); fileInput = new FileInputStream(file);
priorityMap.load(fileInput);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
@ -48,6 +50,15 @@ public class ExtensionPriorityManager {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
finally{
if (fileInput != null) {
try {
fileInput.close();
} catch (Exception e2) {}
fileInput = null;
}
}
} }
} }

View File

@ -37,6 +37,7 @@ public class RestletUtil {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> T toObject(Representation entity) { public static <T> T toObject(Representation entity) {
ObjectInputStream ois = null;
try { try {
if (entity instanceof WrapperRepresentation) { if (entity instanceof WrapperRepresentation) {
entity = ((WrapperRepresentation)entity).getWrappedRepresentation(); entity = ((WrapperRepresentation)entity).getWrappedRepresentation();
@ -46,7 +47,7 @@ public class RestletUtil {
return (T) or.getObject(); return (T) or.getObject();
} else if (entity instanceof InputRepresentation) { } else if (entity instanceof InputRepresentation) {
InputRepresentation ir = (InputRepresentation) entity; InputRepresentation ir = (InputRepresentation) entity;
ObjectInputStream ois = new ObjectInputStream(ir.getStream()); ois = new ObjectInputStream(ir.getStream());
return (T) ois.readObject(); return (T) ois.readObject();
} else { } else {
return null; return null;
@ -55,5 +56,13 @@ public class RestletUtil {
log.log(Level.SEVERE, e.getLocalizedMessage(), e); log.log(Level.SEVERE, e.getLocalizedMessage(), e);
return null; return null;
} }
finally{
if (ois != null) {
try {
ois.close();
} catch (Exception e2) {}
ois = null;
}
}
} }
} }

View File

@ -145,6 +145,7 @@ public class CacheHandler {
private void runURLRequest(String cache, String ID) { private void runURLRequest(String cache, String ID) {
String thisURL = null; String thisURL = null;
for(int i=0; i<cacheURLs.length; i++) { for(int i=0; i<cacheURLs.length; i++) {
try { try {
thisURL = "http://" + cacheURLs[i] + "/cache/Service?Cache=" + cache + "&ID=" + ID; thisURL = "http://" + cacheURLs[i] + "/cache/Service?Cache=" + cache + "&ID=" + ID;
URL url = new URL(thisURL); URL url = new URL(thisURL);
@ -153,11 +154,11 @@ public class CacheHandler {
URLConnection urlConn = url.openConnection(thisProxy); URLConnection urlConn = url.openConnection(thisProxy);
urlConn.setUseCaches(false); urlConn.setUseCaches(false);
urlConn.connect(); urlConn.connect();
Reader stream = new java.io.InputStreamReader( Reader stream = null;
urlConn.getInputStream());
StringBuilder srvOutput = new StringBuilder(); StringBuilder srvOutput = new StringBuilder();
try { try {
stream = new java.io.InputStreamReader(urlConn.getInputStream());
int c; int c;
while ( (c=stream.read()) != -1 ) while ( (c=stream.read()) != -1 )
srvOutput.append( (char)c ); srvOutput.append( (char)c );
@ -165,6 +166,14 @@ public class CacheHandler {
} catch (Exception E2) { } catch (Exception E2) {
E2.printStackTrace(); E2.printStackTrace();
} }
finally{
if (stream != null) {
try {
stream.close();
} catch (Exception e) {}
stream = null;
}
}
} catch (IOException E) { } catch (IOException E) {
if (log!=null) if (log!=null)
log.warning("Can't clean cache at:" + thisURL + " be carefull, your deployment server may use invalid or old cache data!"); log.warning("Can't clean cache at:" + thisURL + " be carefull, your deployment server may use invalid or old cache data!");

View File

@ -244,6 +244,14 @@ public class MPasswordRule extends X_AD_PasswordRule {
} catch (Exception e) { } catch (Exception e) {
props = null; props = null;
} }
finally{
if (in != null) {
try {
in.close();
} catch (Exception e2) {}
in = null;
}
}
if (props == null) if (props == null)
return new MessageResolver(); return new MessageResolver();
else else

View File

@ -36,9 +36,10 @@ public class StoredHtmlSrc extends MultiPartElement implements Printable {
return; return;
} }
InputStreamReader ins; InputStreamReader ins;
BufferedReader bufferedReader = null;
try { try {
ins = new InputStreamReader(url.openStream()); ins = new InputStreamReader(url.openStream());
BufferedReader bufferedReader = new BufferedReader( ins ); bufferedReader = new BufferedReader( ins );
String cssLine; String cssLine;
String result=""; String result="";
while ((cssLine = bufferedReader.readLine()) != null) while ((cssLine = bufferedReader.readLine()) != null)
@ -47,5 +48,13 @@ public class StoredHtmlSrc extends MultiPartElement implements Printable {
} catch (IOException e1) { } catch (IOException e1) {
log.warning("failed to load html-src: " + srcLocation); log.warning("failed to load html-src: " + srcLocation);
} }
finally{
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {}
bufferedReader = null;
}
}
} }
} }

View File

@ -162,6 +162,14 @@ public class KeyStoreMgt
log.log(Level.SEVERE, "load", e); log.log(Level.SEVERE, "load", e);
return null; return null;
} }
finally{
if (is != null) {
try {
is.close();
} catch (Exception e2) {}
is = null;
}
}
m_keyStore.load(is, m_password); m_keyStore.load(is, m_password);
} }
else else

View File

@ -196,10 +196,10 @@ public class PackIn {
byte[] data = null; byte[] data = null;
File file = new File(m_packageDirectory+File.separator+"blobs"+File.separator, fileName); File file = new File(m_packageDirectory+File.separator+"blobs"+File.separator, fileName);
FileInputStream fis = null; FileInputStream fis = null;
BufferedInputStream bis = null;
try { try {
fis = new FileInputStream(file); fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis); bis = new BufferedInputStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (true) { while (true) {
int b = bis.read(); int b = bis.read();
@ -215,6 +215,12 @@ public class PackIn {
fis.close(); fis.close();
} catch (IOException e) {} } catch (IOException e) {}
} }
if (bis != null) {
try {
bis.close();
} catch (Exception e) {}
bis = null;
}
} }
return data; return data;
} }

View File

@ -101,8 +101,9 @@ public class Zipper {
} }
static public String getParentDir(File zipFilepath) static public String getParentDir(File zipFilepath)
{ {
ZipFile zipFile = null;
try { try {
ZipFile zipFile = new ZipFile(zipFilepath); zipFile = new ZipFile(zipFilepath);
Enumeration<? extends ZipEntry> entries = zipFile.entries(); Enumeration<? extends ZipEntry> entries = zipFile.entries();
ZipEntry entry = entries.nextElement(); ZipEntry entry = entries.nextElement();
File tempfile = new File(entry.getName()); File tempfile = new File(entry.getName());
@ -114,6 +115,14 @@ public class Zipper {
ioe.printStackTrace(); ioe.printStackTrace();
return ""; return "";
} }
finally{
if (zipFile != null) {
try {
zipFile.close();
} catch (Exception e) {}
zipFile = null;
}
}
} }
}// CreateZipFile }// CreateZipFile

View File

@ -75,11 +75,12 @@ public class AdempiereActivator implements BundleActivator {
URL packout = context.getBundle().getEntry("/META-INF/2Pack.zip"); URL packout = context.getBundle().getEntry("/META-INF/2Pack.zip");
if (packout != null) { if (packout != null) {
IDictionaryService service = Service.locator().locate(IDictionaryService.class).getService(); IDictionaryService service = Service.locator().locate(IDictionaryService.class).getService();
FileOutputStream zipstream = null;
try { try {
// copy the resource to a temporary file to process it with 2pack // copy the resource to a temporary file to process it with 2pack
InputStream stream = context.getBundle().getEntry("/META-INF/2Pack.zip").openStream(); InputStream stream = context.getBundle().getEntry("/META-INF/2Pack.zip").openStream();
File zipfile = File.createTempFile(getName(), ".zip"); File zipfile = File.createTempFile(getName(), ".zip");
FileOutputStream zipstream = new FileOutputStream(zipfile); zipstream = new FileOutputStream(zipfile);
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int read; int read;
while((read = stream.read(buffer)) != -1){ while((read = stream.read(buffer)) != -1){
@ -90,6 +91,13 @@ public class AdempiereActivator implements BundleActivator {
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.SEVERE, "Error on Dictionary service", e); logger.log(Level.SEVERE, "Error on Dictionary service", e);
} }
finally{
if (zipstream != null) {
try {
zipstream.close();
} catch (Exception e2) {}
}
}
} }
} }

View File

@ -114,9 +114,10 @@ public class HtmlDashboard extends JPanel implements MouseListener,
URL url = getClass().getClassLoader(). URL url = getClass().getClassLoader().
getResource("org/compiere/css/PAPanel.css"); getResource("org/compiere/css/PAPanel.css");
InputStreamReader ins; InputStreamReader ins;
BufferedReader bufferedReader = null;
try { try {
ins = new InputStreamReader(url.openStream()); ins = new InputStreamReader(url.openStream());
BufferedReader bufferedReader = new BufferedReader( ins ); bufferedReader = new BufferedReader( ins );
String cssLine; String cssLine;
result.append("<style type=\"text/css\">"); result.append("<style type=\"text/css\">");
while ((cssLine = bufferedReader.readLine()) != null) while ((cssLine = bufferedReader.readLine()) != null)
@ -125,6 +126,14 @@ public class HtmlDashboard extends JPanel implements MouseListener,
} catch (IOException e1) { } catch (IOException e1) {
log.log(Level.SEVERE, e1.getLocalizedMessage(), e1); log.log(Level.SEVERE, e1.getLocalizedMessage(), e1);
} }
finally{
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (Exception e) {}
bufferedReader = null;
}
}
//System.out.println(result); //System.out.println(result);
switch (requestPage) { switch (requestPage) {
case PAGE_LOGO: case PAGE_LOGO:

View File

@ -210,9 +210,10 @@ public class DashboardController implements EventListener<Event> {
URL url = getClass().getClassLoader().getResource("org/compiere/css/PAPanel.css"); URL url = getClass().getClassLoader().getResource("org/compiere/css/PAPanel.css");
InputStreamReader ins; InputStreamReader ins;
BufferedReader bufferedReader = null;
try { try {
ins = new InputStreamReader(url.openStream()); ins = new InputStreamReader(url.openStream());
BufferedReader bufferedReader = new BufferedReader( ins ); bufferedReader = new BufferedReader( ins );
String cssLine; String cssLine;
result.append("<style type=\"text/css\">"); result.append("<style type=\"text/css\">");
while ((cssLine = bufferedReader.readLine()) != null) while ((cssLine = bufferedReader.readLine()) != null)
@ -221,7 +222,14 @@ public class DashboardController implements EventListener<Event> {
} catch (Exception e1) { } catch (Exception e1) {
logger.log(Level.SEVERE, e1.getLocalizedMessage(), e1); logger.log(Level.SEVERE, e1.getLocalizedMessage(), e1);
} }
finally{
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (Exception e) {}
bufferedReader = null;
}
}
result.append("</head><body><div class=\"content\">\n"); result.append("</head><body><div class=\"content\">\n");
// if(description != null) // if(description != null)

View File

@ -207,9 +207,10 @@ public class ConfigOracle implements IDatabaseConfig
log.fine(tnsnames); log.fine(tnsnames);
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
FileReader fr = null;
try try
{ {
FileReader fr = new FileReader (tnsfile); fr = new FileReader (tnsfile);
int c; int c;
while ((c = fr.read()) != -1) while ((c = fr.read()) != -1)
sb.append((char)c); sb.append((char)c);
@ -220,6 +221,14 @@ public class ConfigOracle implements IDatabaseConfig
ex.printStackTrace(); ex.printStackTrace();
return null; return null;
} }
finally{
if (fr != null) {
try {
fr.close();
} catch (Exception e) {}
fr = null;
}
}
if (sb.length() == 0) if (sb.length() == 0)
return null; return null;
return sb; return sb;