summaryrefslogtreecommitdiffstats
path: root/feature-state-management/src/main/java/org/onap
diff options
context:
space:
mode:
authorPamela Dragosh <pdragosh@research.att.com>2017-09-15 18:27:29 -0400
committerPamela Dragosh <pdragosh@research.att.com>2017-09-18 08:14:07 -0400
commit5622d33ddb975fa23e915a09f4d28def191b597d (patch)
tree0e58369d38146f5408e9c57fbbc91de80540f2a8 /feature-state-management/src/main/java/org/onap
parentfbed3c9c7b816b9fa4dd96dc218a0603b1d1c544 (diff)
Fix sonar blocker/critical
Also add back .gitignore Sonar blocker for change condition always true. Easier to re-write this using try-with-resources. Use synchronized static method to set a static variable. Don't use e.printStackTrace it causes sonar critical to log exception Log the bytes read. Ideally I would re-write this using Java 8 NIO Issue-ID: POLICY-195 Change-Id: I080d1ad4c8bea91f87c3eca109325700e1589558 Signed-off-by: Pamela Dragosh <pdragosh@research.att.com>
Diffstat (limited to 'feature-state-management/src/main/java/org/onap')
-rw-r--r--feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DbAudit.java137
-rw-r--r--feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DroolsPDPIntegrityMonitor.java12
-rw-r--r--feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/IntegrityMonitorRestManager.java2
-rw-r--r--feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/RepositoryAudit.java16
-rw-r--r--feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementFeature.java7
5 files changed, 71 insertions, 103 deletions
diff --git a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DbAudit.java b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DbAudit.java
index a86ac8ef..efecf887 100644
--- a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DbAudit.java
+++ b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DbAudit.java
@@ -44,6 +44,9 @@ public class DbAudit extends DroolsPDPIntegrityMonitor.AuditBase
// invoked -- doing this avoids the need to create the table in advance.
static private boolean createTableNeeded = true;
+ synchronized private static void setCreateTableNeeded(boolean b) {
+ DbAudit.createTableNeeded = b;
+ }
/**
* @return the single 'DbAudit' instance
*/
@@ -98,26 +101,17 @@ public class DbAudit extends DroolsPDPIntegrityMonitor.AuditBase
String user = properties.getProperty(StateManagementProperties.DB_USER);
String password = properties.getProperty(StateManagementProperties.DB_PWD);
- // connection to DB
- Connection connection = null;
-
- // supports SQL operations
- PreparedStatement statement = null;
- ResultSet rs = null;
-
// operation phase currently running -- used to construct an error
// message, if needed
String phase = null;
- try
+ // create connection to DB
+ phase = "creating connection";
+ if(logger.isDebugEnabled()){
+ logger.debug("DbAudit: Creating connection to {}", url);
+ }
+ try (Connection connection = DriverManager.getConnection(url, user, password))
{
- // create connection to DB
- phase = "creating connection";
- if(logger.isDebugEnabled()){
- logger.debug("DbAudit: Creating connection to {}", url);
- }
-
- connection = DriverManager.getConnection(url, user, password);
// create audit table, if needed
if (createTableNeeded)
@@ -126,93 +120,68 @@ public class DbAudit extends DroolsPDPIntegrityMonitor.AuditBase
if(logger.isDebugEnabled()){
logger.info("DbAudit: Creating 'Audit' table, if needed");
}
- statement = connection.prepareStatement
+ try (PreparedStatement statement = connection.prepareStatement
("CREATE TABLE IF NOT EXISTS Audit (\n"
+ " name varchar(64) DEFAULT NULL,\n"
+ " UNIQUE KEY name (name)\n"
- + ") DEFAULT CHARSET=latin1;");
- statement.execute();
- statement.close();
- createTableNeeded = false;
+ + ") DEFAULT CHARSET=latin1;")) {
+ statement.execute();
+ DbAudit.setCreateTableNeeded(false);
+ } catch (Exception e) {
+ throw e;
+ }
}
// insert an entry into the table
phase = "insert entry";
String key = UUID.randomUUID().toString();
- statement = connection.prepareStatement
- ("INSERT INTO Audit (name) VALUES (?)");
- statement.setString(1, key);
- statement.executeUpdate();
- statement.close();
-
+ try (PreparedStatement statement = connection.prepareStatement
+ ("INSERT INTO Audit (name) VALUES (?)")) {
+ statement.setString(1, key);
+ statement.executeUpdate();
+ } catch (Exception e) {
+ throw e;
+ }
+
// fetch the entry from the table
phase = "fetch entry";
- statement = connection.prepareStatement
- ("SELECT name FROM Audit WHERE name = ?");
- statement.setString(1, key);
- rs = statement.executeQuery();
- if (rs.first())
- {
- // found entry
- if(logger.isDebugEnabled()){
- logger.debug("DbAudit: Found key {}", rs.getString(1));
+ try (PreparedStatement statement = connection.prepareStatement
+ ("SELECT name FROM Audit WHERE name = ?")) {
+ statement.setString(1, key);
+ try (ResultSet rs = statement.executeQuery()) {
+ if (rs.first())
+ {
+ // found entry
+ if(logger.isDebugEnabled()){
+ logger.debug("DbAudit: Found key {}", rs.getString(1));
+ }
+ }
+ else
+ {
+ logger.error
+ ("DbAudit: can't find newly-created entry with key {}", key);
+ setResponse("Can't find newly-created entry");
+ }
+ } catch (Exception e) {
+ throw e;
}
- }
- else
- {
- logger.error
- ("DbAudit: can't find newly-created entry with key {}", key);
- setResponse("Can't find newly-created entry");
- }
- statement.close();
-
+ }
// delete entries from table
phase = "delete entry";
- statement = connection.prepareStatement
- ("DELETE FROM Audit WHERE name = ?");
- statement.setString(1, key);
- statement.executeUpdate();
- statement.close();
- statement = null;
- }
+ try (PreparedStatement statement = connection.prepareStatement
+ ("DELETE FROM Audit WHERE name = ?")) {
+ statement.setString(1, key);
+ statement.executeUpdate();
+ } catch (Exception e) {
+ throw e;
+ }
+ }
catch (Exception e)
{
String message = "DbAudit: Exception during audit, phase = " + phase;
logger.error(message, e);
setResponse(message);
}
- finally
- {
- if (rs != null)
- {
- try
- {
- rs.close();
- }
- catch (Exception e)
- {
- }
- }
- if (statement != null)
- {
- try
- {
- statement.close();
- }
- catch (Exception e)
- {
- }
- }
- if (connection != null)
- {
- try
- {
- connection.close();
- }
- catch (Exception e)
- {
- }
- }
- }
}
+
}
diff --git a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DroolsPDPIntegrityMonitor.java b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DroolsPDPIntegrityMonitor.java
index 73f6f738..2dc2e262 100644
--- a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DroolsPDPIntegrityMonitor.java
+++ b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DroolsPDPIntegrityMonitor.java
@@ -20,20 +20,17 @@
package org.onap.policy.drools.statemanagement;
-import java.io.File;
-import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Properties;
import org.onap.policy.common.im.IntegrityMonitor;
import org.onap.policy.common.im.IntegrityMonitorException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.onap.policy.drools.core.PolicyContainer;
import org.onap.policy.drools.http.server.HttpServletServer;
import org.onap.policy.drools.properties.Startable;
import org.onap.policy.drools.utils.PropertyUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* This class extends 'IntegrityMonitor' for use in the 'Drools PDP'
@@ -351,10 +348,11 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor
try {
server.waitedStart(5);
} catch (Exception e) {
- e.printStackTrace();
+ logger.error("Exception waiting for servers to start: ", e);
}
}
} catch (Exception e) {
+ logger.error("Exception building servers", e);
return false;
}
@@ -366,7 +364,7 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor
try {
server.stop();
} catch (Exception e) {
- e.printStackTrace();
+ logger.error("Exception during stop", e);
}
return true;
diff --git a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/IntegrityMonitorRestManager.java b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/IntegrityMonitorRestManager.java
index f5024299..ed522fce 100644
--- a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/IntegrityMonitorRestManager.java
+++ b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/IntegrityMonitorRestManager.java
@@ -77,8 +77,6 @@ public class IntegrityMonitorRestManager {
im = DroolsPDPIntegrityMonitor.getInstance();
} catch (Exception e) {
logger.error("IntegrityMonitorRestManager: test() interface caught an exception", e);
- e.printStackTrace();
-
body.append("\nException: " + e + "\n");
responseValue = 500;
}
diff --git a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/RepositoryAudit.java b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/RepositoryAudit.java
index 6171572a..84cb7e32 100644
--- a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/RepositoryAudit.java
+++ b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/RepositoryAudit.java
@@ -335,11 +335,17 @@ public class RepositoryAudit extends DroolsPDPIntegrityMonitor.AuditBase
// place output in 'fileContents' (replacing the Return characters
// with Newline)
byte[] outputData = new byte[(int)output.length()];
- FileInputStream fis = new FileInputStream(output);
- fis.read(outputData);
- String fileContents = new String(outputData).replace('\r','\n');
- fis.close();
-
+ String fileContents;
+ try (FileInputStream fis = new FileInputStream(output)) {
+ //
+ // Ideally this should be in a loop or even better use
+ // Java 8 nio functionality.
+ //
+ int bytesRead = fis.read(outputData);
+ logger.info("fileContents read {} bytes", bytesRead);
+ fileContents = new String(outputData).replace('\r','\n');
+ }
+
// generate log messages from 'Downloading' and 'Downloaded'
// messages within the 'mvn' output
int index = 0;
diff --git a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementFeature.java b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementFeature.java
index 6d47039e..0143c58b 100644
--- a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementFeature.java
+++ b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementFeature.java
@@ -24,14 +24,13 @@ import java.io.IOException;
import java.util.Observer;
import java.util.Properties;
-import org.onap.policy.drools.statemanagement.StateManagementFeatureAPI;
import org.onap.policy.common.im.StandbyStatusException;
import org.onap.policy.common.im.StateManagement;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.onap.policy.drools.core.PolicySessionFeatureAPI;
import org.onap.policy.drools.features.PolicyEngineFeatureAPI;
import org.onap.policy.drools.utils.PropertyUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* If this feature is supported, there is a single instance of it.
@@ -100,14 +99,12 @@ public class StateManagementFeature implements StateManagementFeatureAPI,
}
}
} catch (Exception e1) {
- String msg = " \n";
if(logger.isDebugEnabled()){
logger.debug("StateManagementFeature.globalInit(): DroolsPDPIntegrityMonitor"
+ " initialization failed with exception:", e1);
}
logger.error("DroolsPDPIntegrityMonitor.init(): StateManagementFeature startup failed "
+ "to get DroolsPDPIntegrityMonitor instance:", e1);
- e1.printStackTrace();
}
}