From 0f3451befbc14bd6ec29d9e1e3845f970f288653 Mon Sep 17 00:00:00 2001 From: Abhishek Koneru Date: Tue, 26 Jun 2012 15:37:15 -0400 Subject: LeftOver Cases in Resource Leaks and NULL_RETURNS --- .../cms/publish/publishers/FileBasedPublisher.java | 69 +++++++++++++++------- .../com/netscape/cmscore/dbs/KeyRepository.java | 5 +- .../com/netscape/cmscore/logging/LogSubsystem.java | 7 +-- .../src/com/netscape/cmscore/logging/Logger.java | 8 ++- 4 files changed, 61 insertions(+), 28 deletions(-) (limited to 'base/common/src/com') diff --git a/base/common/src/com/netscape/cms/publish/publishers/FileBasedPublisher.java b/base/common/src/com/netscape/cms/publish/publishers/FileBasedPublisher.java index 59effbe81..6a1d528ac 100644 --- a/base/common/src/com/netscape/cms/publish/publishers/FileBasedPublisher.java +++ b/base/common/src/com/netscape/cms/publish/publishers/FileBasedPublisher.java @@ -287,6 +287,7 @@ public class FileBasedPublisher implements ILdapPublisher, IExtendedPluginInfo { public void publish(LDAPConnection conn, String dn, Object object) throws ELdapException { CMS.debug("FileBasedPublisher: publish"); + try { if (object instanceof X509Certificate) { X509Certificate cert = (X509Certificate) object; @@ -295,28 +296,45 @@ public class FileBasedPublisher implements ILdapPublisher, IExtendedPluginInfo { File.separator + "cert-" + sno.toString(); if (mDerAttr) { - String fileName = name + ".der"; - FileOutputStream fos = new FileOutputStream(fileName); - fos.write(cert.getEncoded()); - fos.close(); + FileOutputStream fos = null; + try { + String fileName = name + ".der"; + fos = new FileOutputStream(fileName); + fos.write(cert.getEncoded()); + } finally { + if (fos != null) + fos.close(); + } } if (mB64Attr) { String fileName = name + ".b64"; - FileOutputStream fos = new FileOutputStream(fileName); - ByteArrayOutputStream output = new ByteArrayOutputStream(); - Base64OutputStream b64 = - new Base64OutputStream(new PrintStream(new FilterOutputStream(output))); - b64.write(cert.getEncoded()); - b64.flush(); - (new PrintStream(fos)).print(output.toString("8859_1")); - fos.close(); + PrintStream ps = null; + Base64OutputStream b64 = null; + FileOutputStream fos = null; + try { + fos = new FileOutputStream(fileName); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + b64 = new Base64OutputStream(new PrintStream(new FilterOutputStream(output))); + b64.write(cert.getEncoded()); + b64.flush(); + ps = new PrintStream(fos); + ps.print(output.toString("8859_1")); + } finally { + if (ps != null) { + ps.close(); + } + if (b64 != null) { + b64.close(); + } + if (fos != null) + fos.close(); + } } } else if (object instanceof X509CRL) { X509CRL crl = (X509CRL) object; String[] namePrefix = getCrlNamePrefix(crl, mTimeStamp.equals("GMT")); String baseName = mDir + File.separator + namePrefix[0]; String tempFile = baseName + ".temp"; - FileOutputStream fos; ZipOutputStream zos = null; byte[] encodedArray = null; File destFile = null; @@ -324,10 +342,15 @@ public class FileBasedPublisher implements ILdapPublisher, IExtendedPluginInfo { File renameFile = null; if (mDerAttr) { - fos = new FileOutputStream(tempFile); - encodedArray = crl.getEncoded(); - fos.write(encodedArray); - fos.close(); + FileOutputStream fos = null; + try { + fos = new FileOutputStream(tempFile); + encodedArray = crl.getEncoded(); + fos.write(encodedArray); + } finally { + if (fos != null) + fos.close(); + } if (mZipCRL) { try { zos = new ZipOutputStream(new FileOutputStream(baseName + ".zip")); @@ -368,10 +391,14 @@ public class FileBasedPublisher implements ILdapPublisher, IExtendedPluginInfo { if (mB64Attr == true) { if (encodedArray == null) encodedArray = crl.getEncoded(); - - fos = new FileOutputStream(tempFile); - fos.write(Utils.base64encode(encodedArray).getBytes()); - fos.close(); + FileOutputStream fos = null; + try { + fos = new FileOutputStream(tempFile); + fos.write(Utils.base64encode(encodedArray).getBytes()); + } finally { + if (fos != null) + fos.close(); + } destName = baseName + ".b64"; destFile = new File(destName); diff --git a/base/common/src/com/netscape/cmscore/dbs/KeyRepository.java b/base/common/src/com/netscape/cmscore/dbs/KeyRepository.java index fb3b9b24a..556cab9f5 100644 --- a/base/common/src/com/netscape/cmscore/dbs/KeyRepository.java +++ b/base/common/src/com/netscape/cmscore/dbs/KeyRepository.java @@ -515,7 +515,10 @@ public class KeyRepository extends Repository implements IKeyRepository { (KeyRecordList) findKeyRecordsInList(ldapfilter, attrs, serial_upper_bound.toString(10), "serialno", 5 * -1); - int size = recList.getSize(); + int size = -1; + if (recList != null) { + size = recList.getSize(); + } CMS.debug("KeyRepository: getLastSerialNumberInRange: recList size " + size); diff --git a/base/common/src/com/netscape/cmscore/logging/LogSubsystem.java b/base/common/src/com/netscape/cmscore/logging/LogSubsystem.java index 82b515846..aa5714668 100644 --- a/base/common/src/com/netscape/cmscore/logging/LogSubsystem.java +++ b/base/common/src/com/netscape/cmscore/logging/LogSubsystem.java @@ -200,15 +200,14 @@ public class LogSubsystem implements ILogSubsystem { public String getLogPluginName(ILogEventListener log) { IConfigStore cs = log.getConfigStore(); - + if (cs == null) { + return ""; + } try { return cs.getString("pluginName", ""); } catch (EBaseException e) { e.printStackTrace(); return ""; - } catch (NullPointerException e) { - e.printStackTrace(); - return ""; } } diff --git a/base/common/src/com/netscape/cmscore/logging/Logger.java b/base/common/src/com/netscape/cmscore/logging/Logger.java index b27147f20..aec4caec7 100644 --- a/base/common/src/com/netscape/cmscore/logging/Logger.java +++ b/base/common/src/com/netscape/cmscore/logging/Logger.java @@ -203,7 +203,9 @@ public class Logger implements ILogger { */ public void log(int evtClass, Properties prop, int source, int level, String msg, Object params[]) { - mLogQueue.log(create(evtClass, prop, source, level, msg, params, ILogger.L_SINGLELINE)); + ILogEvent iLEvent = create(evtClass, prop, source, level, msg, params, ILogger.L_SINGLELINE); + if (iLEvent != null) + mLogQueue.log(iLEvent); } //******************** multiline log ************************* @@ -342,7 +344,9 @@ public class Logger implements ILogger { */ public void log(int evtClass, Properties prop, int source, int level, String msg, Object params[], boolean multiline) { - mLogQueue.log(create(evtClass, prop, source, level, msg, params, multiline)); + ILogEvent iLEvent = create(evtClass, prop, source, level, msg, params, multiline); + if (iLEvent != null) + mLogQueue.log(iLEvent); } //******************** end multiline log ************************* -- cgit