From ed6aaf35dabae4070dba5c41798e351c25e8ce4d Mon Sep 17 00:00:00 2001 From: Bharat saraswal Date: Thu, 21 Sep 2017 11:17:57 +0530 Subject: Fix Sonar Issues in sli/core module Few major issues *Remove unused variables To increase code readability *Replace the type specification with the diamond operator *Add the "@Override" annotation above this method signature To increase code readability" Issue-Id: CCSDK-87 Change-Id: I2f0c16a83ab10a0a3e39a2a1d4db3c195ec2bba8 Signed-off-by: Bharat saraswal --- .../ccsdk/sli/core/dblib/CachedDataSource.java | 4 +- .../ccsdk/sli/core/dblib/DBResourceManager.java | 4 +- .../sli/core/dblib/factory/DBConfigFactory.java | 120 +++---- .../sli/core/dblib/pm/SQLExecutionMonitor.java | 383 ++++++++++----------- 4 files changed, 249 insertions(+), 262 deletions(-) diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/CachedDataSource.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/CachedDataSource.java index 4d920e6a..072a6f47 100755 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/CachedDataSource.java +++ b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/CachedDataSource.java @@ -123,7 +123,7 @@ public abstract class CachedDataSource implements DataSource, SQLExecutionMonito connection = null; } - monitor.deregisterReguest(testObject); + monitor.deregisterRequest(testObject); } } @@ -153,7 +153,7 @@ public abstract class CachedDataSource implements DataSource, SQLExecutionMonito connection = null; } - monitor.deregisterReguest(testObject); + monitor.deregisterRequest(testObject); } } diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBResourceManager.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBResourceManager.java index 014dab31..e7a94e64 100755 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBResourceManager.java +++ b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBResourceManager.java @@ -884,7 +884,7 @@ public class DBResourceManager implements DataSource, DataAccessor, DBResourceOb CachedDataSource first = snapshot.getFirst(); CachedDataSource last = snapshot.getLast(); - int delta = first.getMonitor().getPorcessedConnectionsCount() - last.getMonitor().getPorcessedConnectionsCount(); + int delta = first.getMonitor().getProcessedConnectionsCount() - last.getMonitor().getProcessedConnectionsCount(); if(delta < 0) { flipper.set(false); } else if(delta > 0) { @@ -921,7 +921,7 @@ public class DBResourceManager implements DataSource, DataAccessor, DBResourceOb CachedDataSource first = snapshot.getFirst(); CachedDataSource last = snapshot.getLast(); - int delta = first.getMonitor().getPorcessedConnectionsCount() - last.getMonitor().getPorcessedConnectionsCount(); + int delta = first.getMonitor().getProcessedConnectionsCount() - last.getMonitor().getProcessedConnectionsCount(); if(delta < 0) { flipper.set(false); } else if(delta > 0) { diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/DBConfigFactory.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/DBConfigFactory.java index 3caa7767..c3503576 100644 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/DBConfigFactory.java +++ b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/DBConfigFactory.java @@ -24,7 +24,6 @@ package org.onap.ccsdk.sli.core.dblib.factory; import java.util.ArrayList; import java.util.Iterator; import java.util.Properties; - import org.onap.ccsdk.sli.core.dblib.config.BaseDBConfiguration; import org.onap.ccsdk.sli.core.dblib.config.DbConfigPool; import org.onap.ccsdk.sli.core.dblib.config.JDBCConfiguration; @@ -39,61 +38,66 @@ import org.slf4j.LoggerFactory; */ public class DBConfigFactory { - public static DbConfigPool createConfig(Properties resource) { - return getConfigparams(resource); - } - - static DbConfigPool getConfigparams(Properties properties){ - DbConfigPool xmlConfig = new DbConfigPool(properties); - ArrayList propertySets = new ArrayList(); - - if("JDBC".equalsIgnoreCase(xmlConfig.getType())) { - String hosts = properties.getProperty(BaseDBConfiguration.DATABASE_HOSTS); - if(hosts == null || hosts.isEmpty()) { - propertySets.add(properties); - } else { - String[] newhost = hosts.split(","); - for(int i=0; i< newhost.length; i++) { - Properties localset = new Properties(); - localset.putAll(properties); - String url = localset.getProperty(BaseDBConfiguration.DATABASE_URL); - if(url.contains("DBHOST")) - url = url.replace("DBHOST", newhost[i]); - if(url.contains("dbhost")) - url = url.replace("dbhost", newhost[i]); - localset.setProperty(BaseDBConfiguration.DATABASE_URL, url); - localset.setProperty(BaseDBConfiguration.CONNECTION_NAME, newhost[i]); - propertySets.add(localset); - } - } - } else { - propertySets.add(properties); - } - try { - Iterator it = propertySets.iterator(); - while(it.hasNext()) { - BaseDBConfiguration config = parse(it.next()); - xmlConfig.addConfiguration(config); - } - - } catch (Exception e) { - LoggerFactory.getLogger(DBConfigFactory.class).warn("",e); - } - - return xmlConfig; - } - - public static BaseDBConfiguration parse(Properties props) throws Exception { - - String type = props.getProperty(BaseDBConfiguration.DATABASE_TYPE); - - BaseDBConfiguration config = null; - - if("JDBC".equalsIgnoreCase(type)) { - config = new JDBCConfiguration(props); - } - - return config; - - } + public static DbConfigPool createConfig(Properties resource) { + return getConfigparams(resource); + } + + static DbConfigPool getConfigparams(Properties properties) { + DbConfigPool xmlConfig = new DbConfigPool(properties); + ArrayList propertySets = new ArrayList(); + + if ("JDBC".equalsIgnoreCase(xmlConfig.getType())) { + String hosts = properties.getProperty(BaseDBConfiguration.DATABASE_HOSTS); + if (hosts == null || hosts.isEmpty()) { + propertySets.add(properties); + } else { + setPropertyWhenHostsNonEmpty(hosts, properties, propertySets); + } + } else { + propertySets.add(properties); + } + try { + Iterator it = propertySets.iterator(); + while (it.hasNext()) { + BaseDBConfiguration config = parse(it.next()); + xmlConfig.addConfiguration(config); + } + } catch (Exception e) { + LoggerFactory.getLogger(DBConfigFactory.class).warn("", e); + } + + return xmlConfig; + } + + private static void setPropertyWhenHostsNonEmpty(String hosts, Properties properties, ArrayList + propertySets) { + String[] newhost = hosts.split(","); + for (String aNewhost : newhost) { + Properties localSet = new Properties(); + localSet.putAll(properties); + String url = localSet.getProperty(BaseDBConfiguration.DATABASE_URL); + if (url.contains("DBHOST")) { + url = url.replace("DBHOST", aNewhost); + } + if (url.contains("dbhost")) { + url = url.replace("dbhost", aNewhost); + } + localSet.setProperty(BaseDBConfiguration.DATABASE_URL, url); + localSet.setProperty(BaseDBConfiguration.CONNECTION_NAME, aNewhost); + propertySets.add(localSet); + } + } + + public static BaseDBConfiguration parse(Properties props) throws Exception { + + String type = props.getProperty(BaseDBConfiguration.DATABASE_TYPE); + + BaseDBConfiguration config = null; + + if ("JDBC".equalsIgnoreCase(type)) { + config = new JDBCConfiguration(props); + } + + return config; + } } diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/pm/SQLExecutionMonitor.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/pm/SQLExecutionMonitor.java index bcd4360e..95172ad7 100644 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/pm/SQLExecutionMonitor.java +++ b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/pm/SQLExecutionMonitor.java @@ -23,6 +23,7 @@ package org.onap.ccsdk.sli.core.dblib.pm; import java.io.Serializable; import java.util.Arrays; import java.util.Collections; +import java.util.Objects; import java.util.Observable; import java.util.Observer; import java.util.SortedSet; @@ -30,208 +31,190 @@ import java.util.Timer; import java.util.TimerTask; import java.util.TreeSet; import java.util.concurrent.atomic.AtomicLong; - import org.onap.ccsdk.sli.core.dblib.DBResourceObserver; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class SQLExecutionMonitor extends Observable -{ - private static Logger LOGGER = LoggerFactory.getLogger(SQLExecutionMonitor.class); - - final static long MILISECOND = 1000000L; - final static long SECOND = 1000L*MILISECOND; - - private final Timer timer; - // collection - private final SortedSet innerSet; - private SQLExecutionMonitorObserver parent = null; - private final AtomicLong completionCounter; - private boolean activeState = false; - private final long interval; - private final long initialDelay; - private final long EXPECTED_TIME_TO_COMPLETE; - private final long UNPROCESSED_FAILOVER_THRESHOLD; - - private final class MonitoringTask extends TimerTask - { - - public void run() - { - try { - TestObject testObj = new TestObject(); - testObj.setStartTime(testObj.getStartTime() - EXPECTED_TIME_TO_COMPLETE); - - // take a snapshot of the current task list - TestObject[] array = innerSet.toArray(new TestObject[0]); - SortedSet copyCurrent = new TreeSet(Arrays.asList(array)); - // get the list of the tasks that are older than the specified - // interval. - SortedSet unprocessed = copyCurrent.headSet(testObj); - - long succesfulCount = completionCounter.get(); - int unprocessedCount = unprocessed.size(); - - if (!unprocessed.isEmpty() && unprocessedCount > UNPROCESSED_FAILOVER_THRESHOLD && succesfulCount == 0) - { - // switch the Connection Pool to passive - setChanged(); - notifyObservers("Open JDBC requests=" + unprocessedCount+" in "+SQLExecutionMonitor.this.parent.getDbConnectionName()); - } - } catch (Exception exc) { - LOGGER.error("", exc); - } finally { - completionCounter.set(0L); - } - } - } - - public static class TestObject implements Comparable, Serializable - { - - private static final long serialVersionUID = 1L; - private long starttime; - private long randId; - - public TestObject() - { - starttime = System.nanoTime(); - } - - public long getStartTime() - { - return starttime; - } - - public void setStartTime(long newTime) - { - starttime = newTime; - } - - public int compareTo(TestObject o) - { - if( this == o) - return 0; - if(this.starttime > o.getStartTime()) - return 1; - if(this.starttime < o.getStartTime()) - return -1; - - if(this.hashCode() > o.hashCode()) - return 1; - if(this.hashCode() < o.hashCode()) - return -1; - - return 0; - } - - public String toString() - { - return Long.toString(starttime)+"#"+ this.hashCode(); - } - - public boolean equals(Object obj) - { - if (this == obj) - return true; - - return (obj instanceof TestObject - && starttime == ((TestObject) obj).getStartTime() - && hashCode() == ((TestObject) obj).hashCode()); - } - } - - public SQLExecutionMonitor(SQLExecutionMonitorObserver parent) - { - this.parent = parent; - completionCounter = new AtomicLong(0L); - interval = parent.getInterval(); - initialDelay = parent.getInitialDelay(); - this.UNPROCESSED_FAILOVER_THRESHOLD = parent.getUnprocessedFailoverThreshold(); - this.EXPECTED_TIME_TO_COMPLETE = parent.getExpectedCompletionTime()*MILISECOND; - - innerSet = Collections.synchronizedSortedSet(new TreeSet()); - timer = new Timer(); - } - - public void cleanup() - { - timer.cancel(); - } - - // registerRequest - public TestObject registerRequest() - { - if(activeState) - { - TestObject test = new TestObject(); - if(innerSet.add(test)) - return test; - } - return null; - } - - // deregisterSuccessfulReguest - public boolean deregisterReguest(TestObject test) - { - if(test == null) - return false; - // remove from the collection - if(innerSet.remove(test) && activeState) - { - completionCounter.incrementAndGet(); - return true; - } - return false; - } - - public void terminate() { - timer.cancel(); - } - - /** - * @return the parent - */ - public final Object getParent() { - return parent; - } - - public void addObserver(Observer observer) - { - if(observer instanceof DBResourceObserver) - { - DBResourceObserver dbObserver = (DBResourceObserver)observer; - if(dbObserver.isMonitorDbResponse()) - { - if(countObservers() == 0) - { - TimerTask remindTask = new MonitoringTask(); - timer.schedule(remindTask, initialDelay, interval); - activeState = true; - } - } - } - super.addObserver(observer); - } - - public void deleteObserver(Observer observer) - { - super.deleteObserver(observer); - if(observer instanceof DBResourceObserver) - { - DBResourceObserver dbObserver = (DBResourceObserver)observer; - if(dbObserver.isMonitorDbResponse()) - { - if(countObservers() == 0) - { - timer.cancel(); - activeState = false; - } - } - } - } - - public final int getPorcessedConnectionsCount() { - return innerSet.size(); - } +public class SQLExecutionMonitor extends Observable { + + private static final Logger LOGGER = LoggerFactory.getLogger(SQLExecutionMonitor.class); + + static final long MILISECOND = 1000000L; + static final long SECOND = 1000L * MILISECOND; + + private final Timer timer; + // collection + private final SortedSet innerSet; + private SQLExecutionMonitorObserver parent = null; + private final AtomicLong completionCounter; + private boolean activeState = false; + private final long interval; + private final long initialDelay; + private final long EXPECTED_TIME_TO_COMPLETE; + private final long UNPROCESSED_FAILOVER_THRESHOLD; + + private final class MonitoringTask extends TimerTask { + + public void run() { + try { + TestObject testObj = new TestObject(); + testObj.setStartTime(testObj.getStartTime() - EXPECTED_TIME_TO_COMPLETE); + + // take a snapshot of the current task list + TestObject[] array = innerSet.toArray(new TestObject[0]); + SortedSet copyCurrent = new TreeSet<>(Arrays.asList(array)); + // get the list of the tasks that are older than the specified + // interval. + SortedSet unprocessed = copyCurrent.headSet(testObj); + + long successfulCount = completionCounter.get(); + int unprocessedCount = unprocessed.size(); + + if (!unprocessed.isEmpty() && unprocessedCount > UNPROCESSED_FAILOVER_THRESHOLD + && successfulCount == 0) { + // switch the Connection Pool to passive + setChanged(); + notifyObservers("Open JDBC requests=" + unprocessedCount + " in " + SQLExecutionMonitor.this.parent + .getDbConnectionName()); + } + } catch (Exception exc) { + LOGGER.error("", exc); + } finally { + completionCounter.set(0L); + } + } + } + + public static class TestObject implements Comparable, Serializable { + + private static final long serialVersionUID = 1L; + private long startTime; + private long randId; + + public TestObject() { + startTime = System.nanoTime(); + } + + public long getStartTime() { + return startTime; + } + + public void setStartTime(long newTime) { + startTime = newTime; + } + + public int compareTo(TestObject o) { + if (this == o) { + return 0; + } + if (this.startTime > o.getStartTime()) { + return 1; + } + if (this.startTime < o.getStartTime()) { + return -1; + } + + if (this.hashCode() > o.hashCode()) { + return 1; + } + if (this.hashCode() < o.hashCode()) { + return -1; + } + + return 0; + } + + public String toString() { + return Long.toString(startTime) + "#" + this.hashCode(); + } + + public int hashCode() { + return Objects.hash(startTime, randId); + } + + public boolean equals(Object obj) { + return this == obj || (obj instanceof TestObject && startTime == ((TestObject) obj).getStartTime() + && hashCode() == obj.hashCode()); + } + } + + public SQLExecutionMonitor(SQLExecutionMonitorObserver parent) { + this.parent = parent; + completionCounter = new AtomicLong(0L); + interval = parent.getInterval(); + initialDelay = parent.getInitialDelay(); + this.UNPROCESSED_FAILOVER_THRESHOLD = parent.getUnprocessedFailoverThreshold(); + this.EXPECTED_TIME_TO_COMPLETE = parent.getExpectedCompletionTime() * MILISECOND; + + innerSet = Collections.synchronizedSortedSet(new TreeSet()); + timer = new Timer(); + } + + public void cleanup() { + timer.cancel(); + } + + // registerRequest + public TestObject registerRequest() { + if (activeState) { + TestObject test = new TestObject(); + if (innerSet.add(test)) { + return test; + } + } + return null; + } + + // deregisterSuccessfulRequest + public boolean deregisterRequest(TestObject test) { + if (test == null) { + return false; + } + // remove from the collection + if (innerSet.remove(test) && activeState) { + completionCounter.incrementAndGet(); + return true; + } + return false; + } + + public void terminate() { + timer.cancel(); + } + + /** + * @return the parent + */ + public final Object getParent() { + return parent; + } + + @Override + public void addObserver(Observer observer) { + if (observer instanceof DBResourceObserver) { + DBResourceObserver dbObserver = (DBResourceObserver) observer; + if (dbObserver.isMonitorDbResponse() && countObservers() == 0) { + TimerTask remindTask = new MonitoringTask(); + timer.schedule(remindTask, initialDelay, interval); + activeState = true; + } + } + super.addObserver(observer); + } + + @Override + public void deleteObserver(Observer observer) { + super.deleteObserver(observer); + if (observer instanceof DBResourceObserver) { + DBResourceObserver dbObserver = (DBResourceObserver) observer; + if (dbObserver.isMonitorDbResponse() && countObservers() == 0) { + timer.cancel(); + activeState = false; + } + } + } + + public final int getProcessedConnectionsCount() { + return innerSet.size(); + } } -- cgit 1.2.3-korg