Fix Sonar issues in policy-utils 03/25103/2
authorCharles Cole <cc847m@att.com>
Tue, 28 Nov 2017 15:58:37 +0000 (09:58 -0600)
committerCharles Cole <cc847m@att.com>
Wed, 29 Nov 2017 14:39:13 +0000 (08:39 -0600)
Fixed the Sonar issues in policy-utils that required minimal
refactoring of the code.

Issue-ID: POLICY-460
Change-Id: Ie88fb1d819f343c8c0bc4d0b73e41089d79cdb6c
Signed-off-by: Charles Cole <cc847m@att.com>
policy-utils/src/main/java/org/onap/policy/drools/utils/OrderedServiceImpl.java
policy-utils/src/main/java/org/onap/policy/drools/utils/PropertyUtil.java
policy-utils/src/main/java/org/onap/policy/drools/utils/ReflectionUtil.java

index ee70a4b..99c4566 100644 (file)
@@ -43,7 +43,12 @@ public class OrderedServiceImpl<T extends OrderedService>
   private List<T> implementers = null;
 
   // 'ServiceLoader' that is used to discover and create the services
-  private ServiceLoader<T> serviceLoader = null; //ServiceLoader.load(T.class);
+  private ServiceLoader<T> serviceLoader = null;
+
+  // use this to ensure that we only use one unique instance of each class
+  @SuppressWarnings("rawtypes")
+  private static HashMap<Class,OrderedService> classToSingleton =
+       new HashMap<>();
 
   /**
    * Constructor - create the 'ServiceLoader' instance
@@ -114,7 +119,7 @@ public class OrderedServiceImpl<T extends OrderedService>
                                                           rval = o1.getClass().getName().compareTo
                                                                 (o2.getClass().getName());
                                                         }
-                                                  return(rval);
+                                                  return rval;
                                                 }
                                         });
 
@@ -124,11 +129,6 @@ public class OrderedServiceImpl<T extends OrderedService>
        return implementers;
   }
 
-  // use this to ensure that we only use one unique instance of each class
-  @SuppressWarnings("rawtypes")
-  static private HashMap<Class,OrderedService> classToSingleton =
-       new HashMap<>();
-
   /**
    * If a service implements multiple APIs managed by 'ServiceLoader', a
    * separate instance is created for each API. This method ensures that
@@ -140,7 +140,7 @@ public class OrderedServiceImpl<T extends OrderedService>
    *   the object of this class that was initially created is returned
    *   instead.
    */
-  static private synchronized OrderedService
+  private static synchronized OrderedService
        getSingleton(OrderedService service)
   {
        // see if we already have an instance of this class
index d69ea33..4e00a6d 100644 (file)
@@ -31,12 +31,25 @@ import java.util.Set;
 import java.util.Timer;
 import java.util.TimerTask;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
  * This class provides utilities to read properties from a properties
  * file, and optionally get notifications of future changes
  */
 public class PropertyUtil
 {
+
+  // timer thread used for polling for property file changes
+  private static Timer timer = null;
+
+  // this table maps canonical file into a 'ListenerRegistration' instance
+  private static HashMap<File, ListenerRegistration> registrations =
+       new HashMap<>();
+  
+  private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class.getName());
+  
   /**
    * Read in a properties file
    * @param file the properties file
@@ -45,7 +58,7 @@ public class PropertyUtil
    *   does not exist or can't be opened, and 'IOException' if there is
    *   a problem loading the properties file.
    */
-  static public Properties getProperties(File file) throws IOException
+  public static Properties getProperties(File file) throws IOException
   {
        // create an InputStream (may throw a FileNotFoundException)
        FileInputStream fis = new FileInputStream(file);
@@ -73,16 +86,13 @@ public class PropertyUtil
    *   does not exist or can't be opened, and 'IOException' if there is
    *   a problem loading the properties file.
    */
-  static public Properties getProperties(String fileName) throws IOException
+  public static Properties getProperties(String fileName) throws IOException
   {
        return getProperties(new File(fileName));
   }
 
   /* ============================================================ */
 
-  // timer thread used for polling for property file changes
-  private static Timer timer = null;
-
   /**
    * This is the callback interface, used for sending notifications of
    * changes in the properties file.
@@ -99,10 +109,6 @@ public class PropertyUtil
        void propertiesChanged(Properties properties, Set<String> changedKeys);
   }
 
-  // this table maps canonical file into a 'ListenerRegistration' instance
-  static private HashMap<File, ListenerRegistration> registrations =
-       new HashMap<>();
-
   /**
    * This is an internal class - one instance of this exists for each
    * property file that is being monitored. Note that multiple listeners
@@ -170,7 +176,7 @@ public class PropertyUtil
                          }
                        catch (Exception e)
                          {
-                               System.err.println(e);
+                               logger.warn("Polling for property changes", e);
                          }
                  }
                };
@@ -303,29 +309,30 @@ public class PropertyUtil
    *   does not exist or can't be opened, and 'IOException' if there is
    *   a problem loading the properties file.
    */
-  static public Properties getProperties(File file, Listener listener)
+  public static Properties getProperties(File file, Listener listener)
        throws IOException
   {
+    File propFile = file;
        if (listener == null)
          {
                // no listener specified -- just fetch the properties
-               return getProperties(file);
+               return getProperties(propFile);
          }
 
        // Convert the file to a canonical form in order to avoid the situation
        // where different names refer to the same file.
-       file = file.getCanonicalFile();
+       propFile = propFile.getCanonicalFile();
 
        // See if there is an existing registration. The 'synchronized' block
        // is needed to handle the case where a new listener is added at about
        // the same time that another one is being removed.
        synchronized(registrations)
          {
-               ListenerRegistration reg = registrations.get(file);
+               ListenerRegistration reg = registrations.get(propFile);
                if (reg == null)
                  {
                        // a new registration is needed
-                       reg = new ListenerRegistration(file);
+                       reg = new ListenerRegistration(propFile);
                  }
                return reg.addListener(listener);
          }
@@ -347,7 +354,7 @@ public class PropertyUtil
    *   does not exist or can't be opened, and 'IOException' if there is
    *   a problem loading the properties file.
    */
-  static public Properties getProperties(String fileName, Listener listener)
+  public static Properties getProperties(String fileName, Listener listener)
        throws IOException
   {
        return getProperties(new File(fileName), listener);
@@ -359,7 +366,7 @@ public class PropertyUtil
    * @param notify if not null, this is a callback interface that was used for
    *   notifications of changes
    */
-  static public void stopListening(File file, Listener listener)
+  public static void stopListening(File file, Listener listener)
   {
        if (listener != null)
          {
@@ -377,7 +384,7 @@ public class PropertyUtil
    * @param notify if not null, this is a callback interface that was used for
    *   notifications of changes
    */
-  static public void stopListening(String fileName, Listener listener)
+  public static void stopListening(String fileName, Listener listener)
   {
        stopListening(new File(fileName), listener);
   }
index d300058..50135e8 100644 (file)
@@ -31,7 +31,7 @@ import org.slf4j.LoggerFactory;
  */
 public class ReflectionUtil {
        
-       protected final static Logger logger = LoggerFactory.getLogger(ReflectionUtil.class);
+       protected static final Logger logger = LoggerFactory.getLogger(ReflectionUtil.class);
 
        private ReflectionUtil(){
        }
@@ -55,10 +55,7 @@ public class ReflectionUtil {
                                                       classLoader + " must be provided");
                
                try {
-                       Class<?> aClass = Class.forName(className, 
-                                                               true, 
-                                                               classLoader);
-                       return aClass;
+                       return Class.forName(className, true, classLoader);
                } catch (Exception e) {
                        logger.error("class {} fetched in {} does not exist", className, classLoader, e);
                }