Remove major and minor code smells in dr-prov
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / SynchronizerTask.java
index 9eeac23..d3ae4fe 100644 (file)
@@ -50,6 +50,8 @@ import java.util.TreeSet;
 
 import javax.servlet.http.HttpServletResponse;
 
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpGet;
@@ -60,7 +62,6 @@ import org.apache.http.entity.ByteArrayEntity;
 import org.apache.http.entity.ContentType;
 import org.apache.http.impl.client.AbstractHttpClient;
 import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.log4j.Logger;
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
@@ -117,7 +118,7 @@ public class SynchronizerTask extends TimerTask {
     private static final String[] stnames = {"UNKNOWN", "ACTIVE", "STANDBY"};
     private static final long ONE_HOUR = 60 * 60 * 1000L;
 
-    private final Logger logger;
+    private final EELFLogger logger;
     private final Timer rolex;
     private final String spooldir;
     private int state;
@@ -139,7 +140,7 @@ public class SynchronizerTask extends TimerTask {
 
     @SuppressWarnings("deprecation")
     private SynchronizerTask() {
-        logger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");
+        logger = EELFManager.getInstance().getLogger("InternalLog");
         rolex = new Timer();
         spooldir = (new DB()).getProperties().getProperty("org.onap.dmaap.datarouter.provserver.spooldir");
         state = UNKNOWN;
@@ -151,36 +152,37 @@ public class SynchronizerTask extends TimerTask {
             Properties props = (new DB()).getProperties();
             String type = props.getProperty(Main.KEYSTORE_TYPE_PROPERTY, "jks");
             String store = props.getProperty(Main.KEYSTORE_PATH_PROPERTY);
-            String pass = props.getProperty(Main.KEYSTORE_PASSWORD_PROPERTY);
+            String pass = props.getProperty(Main.KEYSTORE_PASS_PROPERTY);
             KeyStore keyStore = KeyStore.getInstance(type);
-            FileInputStream instream = new FileInputStream(new File(store));
-            keyStore.load(instream, pass.toCharArray());
-            instream.close();
-
-            store = props.getProperty(Main.TRUSTSTORE_PATH_PROPERTY);
-            pass = props.getProperty(Main.TRUSTSTORE_PASSWORD_PROPERTY);
-            KeyStore trustStore = null;
-            if (store != null && store.length() > 0) {
-                trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
-                instream = new FileInputStream(new File(store));
-                trustStore.load(instream, pass.toCharArray());
-                instream.close();
+            try(FileInputStream instream = new FileInputStream(new File(store))) {
+                keyStore.load(instream, pass.toCharArray());
+
             }
+                store = props.getProperty(Main.TRUSTSTORE_PATH_PROPERTY);
+                pass = props.getProperty(Main.TRUSTSTORE_PASS_PROPERTY);
+                KeyStore trustStore = null;
+                if (store != null && store.length() > 0) {
+                    trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
+                    try(FileInputStream instream = new FileInputStream(new File(store))){
+                        trustStore.load(instream, pass.toCharArray());
+
+                    }
+                }
 
             // We are connecting with the node name, but the certificate will have the CNAME
             // So we need to accept a non-matching certificate name
             String keystorepass = props.getProperty(
-                Main.KEYSTORE_PASSWORD_PROPERTY); //itrack.web.att.com/browse/DATARTR-6 for changing hard coded passphase ref
-            AbstractHttpClient hc = new DefaultHttpClient();
-            SSLSocketFactory socketFactory =
-                (trustStore == null)
-                    ? new SSLSocketFactory(keyStore, keystorepass)
-                    : new SSLSocketFactory(keyStore, keystorepass, trustStore);
-            socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
-            Scheme sch = new Scheme("https", 443, socketFactory);
-            hc.getConnectionManager().getSchemeRegistry().register(sch);
+                Main.KEYSTORE_PASS_PROPERTY); //itrack.web.att.com/browse/DATARTR-6 for changing hard coded passphase ref
+           try(AbstractHttpClient hc = new DefaultHttpClient()) {
+               SSLSocketFactory socketFactory =
+                       (trustStore == null)
+                               ? new SSLSocketFactory(keyStore, keystorepass)
+                               : new SSLSocketFactory(keyStore, keystorepass, trustStore);
+               socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
+               Scheme sch = new Scheme("https", 443, socketFactory);
+               hc.getConnectionManager().getSchemeRegistry().register(sch);
             httpclient = hc;
-
+           }
             // Run once every 5 seconds to check DNS, etc.
             long interval = 0;
             try {
@@ -281,7 +283,6 @@ public class SynchronizerTask extends TimerTask {
             }
         } catch (Exception e) {
             logger.warn("PROV0020: Caught exception in SynchronizerTask: " + e);
-            e.printStackTrace();
         }
     }
 
@@ -301,7 +302,7 @@ public class SynchronizerTask extends TimerTask {
             String thisPod = myaddr.getHostName();
             Set<String> pods = new TreeSet<>(Arrays.asList(BaseServlet.getPods()));
             if (pods.contains(thisPod)) {
-                InetAddress pserver = InetAddress.getByName(BaseServlet.activeProvName);
+                InetAddress pserver = InetAddress.getByName(BaseServlet.getActiveProvName());
                 newstate = myaddr.equals(pserver) ? ACTIVE : STANDBY;
                 if (logger.isDebugEnabled() && System.currentTimeMillis() >= nextMsg) {
                     logger.debug("Active POD = " + pserver + ", Current state is " + stnames[newstate]);
@@ -311,7 +312,7 @@ public class SynchronizerTask extends TimerTask {
                 logger.warn("PROV5003: My name (" + thisPod + ") is missing from the list of provisioning servers.");
             }
         } catch (UnknownHostException e) {
-            logger.warn("PROV5002: Cannot determine the name of this provisioning server.");
+            logger.warn("PROV5002: Cannot determine the name of this provisioning server.", e);
         }
 
         if (newstate != state) {
@@ -327,13 +328,13 @@ public class SynchronizerTask extends TimerTask {
      * Synchronize the Feeds in the JSONArray, with the Feeds in the DB.
      */
     private void syncFeeds(JSONArray ja) {
-        Collection<Syncable> coll = new ArrayList<Syncable>();
+        Collection<Syncable> coll = new ArrayList<>();
         for (int n = 0; n < ja.length(); n++) {
             try {
                 Feed f = new Feed(ja.getJSONObject(n));
                 coll.add(f);
             } catch (Exception e) {
-                logger.warn("PROV5004: Invalid object in feed: " + ja.optJSONObject(n));
+                logger.warn("PROV5004: Invalid object in feed: " + ja.optJSONObject(n), e);
             }
         }
         if (sync(coll, Feed.getAllFeeds())) {
@@ -345,7 +346,7 @@ public class SynchronizerTask extends TimerTask {
      * Synchronize the Subscriptions in the JSONArray, with the Subscriptions in the DB.
      */
     private void syncSubs(JSONArray ja) {
-        Collection<Syncable> coll = new ArrayList<Syncable>();
+        Collection<Syncable> coll = new ArrayList<>();
         for (int n = 0; n < ja.length(); n++) {
             try {
                 //Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047.
@@ -354,7 +355,7 @@ public class SynchronizerTask extends TimerTask {
                 Subscription s = new Subscription(j);
                 coll.add(s);
             } catch (Exception e) {
-                logger.warn("PROV5004: Invalid object in subscription: " + ja.optJSONObject(n));
+                logger.warn("PROV5004: Invalid object in subscription: " + ja.optJSONObject(n), e);
             }
         }
         if (sync(coll, Subscription.getAllSubscriptions())) {
@@ -366,13 +367,13 @@ public class SynchronizerTask extends TimerTask {
      * Rally:US708115  - Synchronize the Groups in the JSONArray, with the Groups in the DB.
      */
     private void syncGroups(JSONArray ja) {
-        Collection<Syncable> coll = new ArrayList<Syncable>();
+        Collection<Syncable> coll = new ArrayList<>();
         for (int n = 0; n < ja.length(); n++) {
             try {
                 Group g = new Group(ja.getJSONObject(n));
                 coll.add(g);
             } catch (Exception e) {
-                logger.warn("PROV5004: Invalid object in subscription: " + ja.optJSONObject(n));
+                logger.warn("PROV5004: Invalid object in group: " + ja.optJSONObject(n), e);
             }
         }
         if (sync(coll, Group.getAllgroups())) {
@@ -385,15 +386,17 @@ public class SynchronizerTask extends TimerTask {
      * Synchronize the Parameters in the JSONObject, with the Parameters in the DB.
      */
     private void syncParams(JSONObject jo) {
-        Collection<Syncable> coll = new ArrayList<Syncable>();
+        Collection<Syncable> coll = new ArrayList<>();
         for (String k : jo.keySet()) {
             String v = "";
             try {
                 v = jo.getString(k);
             } catch (JSONException e) {
+                logger.warn("PROV5004: Invalid object in parameters: " + jo.optJSONObject(k), e);
                 try {
                     v = "" + jo.getInt(k);
                 } catch (JSONException e1) {
+                    logger.warn("PROV5004: Invalid object in parameters: " + jo.optInt(k), e);
                     JSONArray ja = jo.getJSONArray(k);
                     for (int i = 0; i < ja.length(); i++) {
                         if (i > 0) {
@@ -412,7 +415,7 @@ public class SynchronizerTask extends TimerTask {
     }
 
     private void syncIngressRoutes(JSONArray ja) {
-        Collection<Syncable> coll = new ArrayList<Syncable>();
+        Collection<Syncable> coll = new ArrayList<>();
         for (int n = 0; n < ja.length(); n++) {
             try {
                 IngressRoute in = new IngressRoute(ja.getJSONObject(n));
@@ -427,7 +430,7 @@ public class SynchronizerTask extends TimerTask {
     }
 
     private void syncEgressRoutes(JSONObject jo) {
-        Collection<Syncable> coll = new ArrayList<Syncable>();
+        Collection<Syncable> coll = new ArrayList<>();
         for (String key : jo.keySet()) {
             try {
                 int sub = Integer.parseInt(key);
@@ -435,9 +438,9 @@ public class SynchronizerTask extends TimerTask {
                 EgressRoute er = new EgressRoute(sub, node);
                 coll.add(er);
             } catch (NumberFormatException e) {
-                logger.warn("PROV5004: Invalid subid in egress routes: " + key);
+                logger.warn("PROV5004: Invalid subid in egress routes: " + key, e);
             } catch (IllegalArgumentException e) {
-                logger.warn("PROV5004: Invalid node name in egress routes: " + key);
+                logger.warn("PROV5004: Invalid node name in egress routes: " + key, e);
             }
         }
         if (sync(coll, EgressRoute.getAllEgressRoutes())) {
@@ -446,7 +449,7 @@ public class SynchronizerTask extends TimerTask {
     }
 
     private void syncNetworkRoutes(JSONArray ja) {
-        Collection<Syncable> coll = new ArrayList<Syncable>();
+        Collection<Syncable> coll = new ArrayList<>();
         for (int n = 0; n < ja.length(); n++) {
             try {
                 NetworkRoute nr = new NetworkRoute(ja.getJSONObject(n));
@@ -465,7 +468,7 @@ public class SynchronizerTask extends TimerTask {
         try {
             Map<String, Syncable> newmap = getMap(newc);
             Map<String, Syncable> oldmap = getMap(oldc);
-            Set<String> union = new TreeSet<String>(newmap.keySet());
+            Set<String> union = new TreeSet<>(newmap.keySet());
             union.addAll(oldmap.keySet());
             DB db = new DB();
             @SuppressWarnings("resource")
@@ -502,13 +505,12 @@ public class SynchronizerTask extends TimerTask {
             db.release(conn);
         } catch (SQLException e) {
             logger.warn("PROV5009: problem during sync, exception: " + e);
-            e.printStackTrace();
         }
         return changes;
     }
 
     private Map<String, Syncable> getMap(Collection<? extends Syncable> c) {
-        Map<String, Syncable> map = new HashMap<String, Syncable>();
+        Map<String, Syncable> map = new HashMap<>();
         for (Syncable v : c) {
             map.put(v.getKey(), v);
         }
@@ -586,7 +588,7 @@ public class SynchronizerTask extends TimerTask {
         String url = URLUtilities.generatePeerLogsURL();
 
         //Fixing if only one Prov is configured, not to give exception to fill logs, return empty bitset.
-        if (url.equals("")) {
+        if ("".equals(url)) {
             return bs;
         }
         //End of fix.
@@ -601,7 +603,7 @@ public class SynchronizerTask extends TimerTask {
             }
             HttpEntity entity = response.getEntity();
             String ctype = entity.getContentType().getValue().trim();
-            if (!ctype.equals("text/plain")) {
+            if (!"text/plain".equals(ctype)) {
                 logger.warn("PROV5011: readRemoteLoglist failed, bad content type: " + ctype);
                 return bs;
             }
@@ -647,7 +649,7 @@ public class SynchronizerTask extends TimerTask {
             }
             HttpEntity entity = response.getEntity();
             String ctype = entity.getContentType().getValue().trim();
-            if (!ctype.equals("text/plain")) {
+            if (!"text/plain".equals(ctype)) {
                 logger.warn("PROV5011: replicateDRLogs failed, bad content type: " + ctype);
                 return;
             }