Fix Sonar Issues in sli/core module 97/14097/3
authorBharat saraswal <bharat.saraswal@huawei.com>
Thu, 21 Sep 2017 05:47:57 +0000 (11:17 +0530)
committerBharat saraswal <bharat.saraswal@huawei.com>
Thu, 21 Sep 2017 05:52:02 +0000 (05:52 +0000)
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 <bharat.saraswal@huawei.com>
dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/CachedDataSource.java
dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBResourceManager.java
dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/DBConfigFactory.java
dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/pm/SQLExecutionMonitor.java

index 4d920e6..072a6f4 100755 (executable)
@@ -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);
                }
        }
 
index 014dab3..e7a94e6 100755 (executable)
@@ -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) {
index 3caa776..c350357 100644 (file)
@@ -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<Properties> propertySets = new ArrayList<Properties>();
-
-               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<Properties>  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<Properties> propertySets = new ArrayList<Properties>();
+
+        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<Properties> 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<Properties>
+        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;
+    }
 }
index bcd4360..95172ad 100644 (file)
@@ -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<TestObject> 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<TestObject> copyCurrent = new TreeSet<TestObject>(Arrays.asList(array));
-                               // get the list of the tasks that are older than the specified
-                               // interval.
-                               SortedSet<TestObject> 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<TestObject>, 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<TestObject>());
-               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<TestObject> 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<TestObject> copyCurrent = new TreeSet<>(Arrays.asList(array));
+                // get the list of the tasks that are older than the specified
+                // interval.
+                SortedSet<TestObject> 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<TestObject>, 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<TestObject>());
+        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();
+    }
 }