diff options
Diffstat (limited to 'feature-state-management/src/main/java')
6 files changed, 116 insertions, 142 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 8de170c7..cde6e4e4 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 @@ -46,6 +46,9 @@ public class DbAudit extends DroolsPDPIntegrityMonitor.AuditBase static public boolean isJunit = false; + synchronized private static void setCreateTableNeeded(boolean b) { + DbAudit.createTableNeeded = b; + } /** * @return the single 'DbAudit' instance */ @@ -103,26 +106,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) @@ -131,93 +125,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..3b7410fa 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' @@ -90,62 +87,62 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor if (resourceName == null) { logger.error("init: Missing IntegrityMonitor property: 'resource.name'"); - throw(new Exception - ("Missing IntegrityMonitor property: 'resource.name'")); + throw new Exception + ("Missing IntegrityMonitor property: 'resource.name'"); } if (hostPort == null) { logger.error("init: Missing IntegrityMonitor property: 'hostPort'"); - throw(new Exception - ("Missing IntegrityMonitor property: 'hostPort'")); + throw new Exception + ("Missing IntegrityMonitor property: 'hostPort'"); } if (fpMonitorInterval == null) { logger.error("init: Missing IntegrityMonitor property: 'fp_monitor_interval'"); - throw(new Exception - ("Missing IntegrityMonitor property: 'fp_monitor_interval'")); + throw new Exception + ("Missing IntegrityMonitor property: 'fp_monitor_interval'"); } if (failedCounterThreshold == null) { logger.error("init: Missing IntegrityMonitor property: 'failed_counter_threshold'"); - throw(new Exception - ("Missing IntegrityMonitor property: 'failed_counter_threshold'")); + throw new Exception + ("Missing IntegrityMonitor property: 'failed_counter_threshold'"); } if (testTransInterval == null) { logger.error("init: Missing IntegrityMonitor property: 'test_trans_interval'"); - throw(new Exception - ("Missing IntegrityMonitor property: 'test_trans_interval'")); + throw new Exception + ("Missing IntegrityMonitor property: 'test_trans_interval'"); } if (writeFpcInterval == null) { logger.error("init: Missing IntegrityMonitor property: 'write_fpc_interval'"); - throw(new Exception - ("Missing IntegrityMonitor property: 'write_fpc_interval'")); + throw new Exception + ("Missing IntegrityMonitor property: 'write_fpc_interval'"); } if (siteName == null) { logger.error("init: Missing IntegrityMonitor property: 'site_name'"); - throw(new Exception - ("Missing IntegrityMonitor property: 'site_name'")); + throw new Exception + ("Missing IntegrityMonitor property: 'site_name'"); } if (nodeType == null) { logger.error("init: Missing IntegrityMonitor property: 'node_type'"); - throw(new Exception - ("Missing IntegrityMonitor property: 'node_type'")); + throw new Exception + ("Missing IntegrityMonitor property: 'node_type'"); } if (dependencyGroups == null) { logger.error("init: Missing IntegrityMonitor property: 'dependency_groups'"); - throw(new Exception - ("Missing IntegrityMonitor property: 'dependency_groups'")); + throw new Exception + ("Missing IntegrityMonitor property: 'dependency_groups'"); } if (javaxPersistenceJdbcDriver == null) { logger.error("init: Missing IntegrityMonitor property: 'javax.persistence.jbdc.driver for xacml DB'"); - throw(new Exception - ("Missing IntegrityMonitor property: 'javax.persistence.jbdc.driver for xacml DB'")); + throw new Exception + ("Missing IntegrityMonitor property: 'javax.persistence.jbdc.driver for xacml DB'"); } if (javaxPersistenceJdbcUrl == null) { @@ -156,14 +153,14 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor if (javaxPersistenceJdbcUser == null) { logger.error("init: Missing IntegrityMonitor property: 'javax.persistence.jbdc.user for xacml DB'"); - throw(new Exception - ("Missing IntegrityMonitor property: 'javax.persistence.jbdc.user for xacml DB'")); + throw new Exception + ("Missing IntegrityMonitor property: 'javax.persistence.jbdc.user for xacml DB'"); } if (javaxPersistenceJdbcPassword == null) { logger.error("init: Missing IntegrityMonitor property: 'javax.persistence.jbdc.password for xacml DB'"); - throw(new Exception - ("Missing IntegrityMonitor property: 'javax.persistence.jbdc.password' for xacml DB'")); + throw new Exception + ("Missing IntegrityMonitor property: 'javax.persistence.jbdc.password' for xacml DB'"); } // Now that we've validated the properties, create Drools Integrity Monitor @@ -303,7 +300,7 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor */ public String getName() { - return(name); + return name; } /** @@ -311,7 +308,7 @@ public class DroolsPDPIntegrityMonitor extends IntegrityMonitor */ public String getResponse() { - return(response); + return response; } /** @@ -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 94464cd4..b36c1657 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 @@ -53,7 +53,7 @@ public class RepositoryAudit extends DroolsPDPIntegrityMonitor.AuditBase */ public static DroolsPDPIntegrityMonitor.AuditBase getInstance() { - return(instance); + return instance; } /** @@ -123,8 +123,8 @@ public class RepositoryAudit extends DroolsPDPIntegrityMonitor.AuditBase String repositoryPassword = StateManagementProperties.getProperty("repository.audit.password"); boolean upload = - (repositoryId != null && repositoryUrl != null - && repositoryUsername != null && repositoryPassword != null); + repositoryId != null && repositoryUrl != null + && repositoryUsername != null && repositoryPassword != null; // used to incrementally construct response as problems occur // (empty = no problems) @@ -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; @@ -404,8 +410,8 @@ public class RepositoryAudit extends DroolsPDPIntegrityMonitor.AuditBase "curl", "--request", "DELETE", "--user", repositoryUsername + ":" + repositoryPassword, - (repositoryUrl + "/" + groupId.replace('.', '/') + "/" + - artifactId + "/" + version)) + repositoryUrl + "/" + groupId.replace('.', '/') + "/" + + artifactId + "/" + version) != 0) { logger.error @@ -431,13 +437,15 @@ public class RepositoryAudit extends DroolsPDPIntegrityMonitor.AuditBase (dir, new SimpleFileVisitor<Path>() { + @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { // logger.info("RepositoryAudit: Delete " + file); file.toFile().delete(); - return(FileVisitResult.CONTINUE); + return FileVisitResult.CONTINUE; } + @Override public FileVisitResult postVisitDirectory(Path file, IOException e) throws IOException { @@ -445,11 +453,11 @@ public class RepositoryAudit extends DroolsPDPIntegrityMonitor.AuditBase { // logger.info("RepositoryAudit: Delete " + file); file.toFile().delete(); - return(FileVisitResult.CONTINUE); + return FileVisitResult.CONTINUE; } else { - throw(e); + throw e; } } }); @@ -486,12 +494,12 @@ public class RepositoryAudit extends DroolsPDPIntegrityMonitor.AuditBase if (process.waitFor(timeoutInSeconds, TimeUnit.SECONDS)) { // process terminated before the timeout - return(process.exitValue()); + return process.exitValue(); } // process timed out -- kill it, and return -1 process.destroyForcibly(); - return(-1); + return -1; } /* ============================================================ */ @@ -532,21 +540,22 @@ public class RepositoryAudit extends DroolsPDPIntegrityMonitor.AuditBase String[] segments = artifact.split("/"); if (segments.length != 4 && segments.length != 3) { - throw(new IllegalArgumentException("groupId/artifactId/version/type")); + throw new IllegalArgumentException("groupId/artifactId/version/type"); } groupId = segments[0]; artifactId = segments[1]; version = segments[2]; - type = (segments.length == 4 ? segments[3] : "jar"); + type = segments.length == 4 ? segments[3] : "jar"; } /** * @return the artifact id in the form: * "<groupId>/<artifactId>/<version>/<type>" */ + @Override public String toString() { - return(groupId + "/" + artifactId + "/" + version + "/" + type); + return groupId + "/" + artifactId + "/" + version + "/" + type; } } } 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(); } } diff --git a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementProperties.java b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementProperties.java index c8e17ea9..f90f7482 100644 --- a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementProperties.java +++ b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/StateManagementProperties.java @@ -38,6 +38,9 @@ public class StateManagementProperties { public static final String DB_PWD = "javax.persistence.jdbc.password"; private static Properties properties = null; + + private StateManagementProperties(){ + } /* * Initialize the parameter values from the feature-state-management.properties file values * |