Remove major and minor code smells in dr-node
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / RedirManager.java
index 336eee3..83e3c30 100644 (file)
-/*******************************************************************************\r
- * ============LICENSE_START==================================================\r
- * * org.onap.dmaap\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- *  *      http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- *  * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-\r
-\r
-package org.onap.dmaap.datarouter.node;\r
-\r
-import java.util.*;\r
-import java.io.*;\r
-\r
-/**\r
- *     Track redirections of subscriptions\r
- */\r
-public class RedirManager      {\r
-       private Hashtable<String, String> sid2primary = new Hashtable<String, String>();\r
-       private Hashtable<String, String> sid2secondary = new Hashtable<String, String>();\r
-       private String  redirfile;\r
-       RateLimitedOperation    op;\r
-       /**\r
-        *      Create a mechanism for maintaining subscription redirections.\r
-        *      @param redirfile        The file to store the redirection information.\r
-        *      @param mininterval      The minimum number of milliseconds between writes to the redirection information file.\r
-        *      @param timer    The timer thread used to run delayed file writes.\r
-        */\r
-       public RedirManager(String redirfile, long mininterval, Timer timer) {\r
-               this.redirfile = redirfile;\r
-               op = new RateLimitedOperation(mininterval, timer) {\r
-                       public void run() {\r
-                               try {\r
-                                       StringBuffer sb = new StringBuffer();\r
-                                       for (String s: sid2primary.keySet()) {\r
-                                               sb.append(s).append(' ').append(sid2primary.get(s)).append(' ').append(sid2secondary.get(s)).append('\n');\r
-                                       }\r
-                                       OutputStream os = new FileOutputStream(RedirManager.this.redirfile);\r
-                                       os.write(sb.toString().getBytes());\r
-                                       os.close();\r
-                               } catch (Exception e) {\r
-                               }\r
-                       }\r
-               };\r
-               try {\r
-                       String s;\r
-                       BufferedReader br = new BufferedReader(new FileReader(redirfile));\r
-                       while ((s = br.readLine()) != null) {\r
-                               s = s.trim();\r
-                               String[] sx = s.split(" ");\r
-                               if (s.startsWith("#") || sx.length != 3) {\r
-                                       continue;\r
-                               }\r
-                               sid2primary.put(sx[0], sx[1]);\r
-                               sid2secondary.put(sx[0], sx[2]);\r
-                       }\r
-                       br.close();\r
-               } catch (Exception e) {\r
-                       // missing file is normal\r
-               }\r
-       }\r
-       /**\r
-        *      Set up redirection.  If a request is to be sent to subscription ID sid, and that is configured to go to URL primary, instead, go to secondary.\r
-        *      @param sid      The subscription ID to be redirected\r
-        *      @param primary  The URL associated with that subscription ID\r
-        *      @param secondary        The replacement URL to use instead\r
-        */\r
-       public synchronized void redirect(String sid, String primary, String secondary) {\r
-               sid2primary.put(sid, primary);\r
-               sid2secondary.put(sid, secondary);\r
-               op.request();\r
-       }\r
-       /**\r
-        *      Cancel redirection.  If a request is to be sent to subscription ID sid, send it to its primary URL.\r
-        *      @param  sid     The subscription ID to remove from the table.\r
-        */\r
-       public synchronized void forget(String sid) {\r
-               sid2primary.remove(sid);\r
-               sid2secondary.remove(sid);\r
-               op.request();\r
-       }\r
-       /**\r
-        *      Look up where to send a subscription.  If the primary has changed or there is no redirection, use the primary.  Otherwise, redirect to the secondary URL.\r
-        *      @param  sid     The subscription ID to look up.\r
-        *      @param  primary The configured primary URL.\r
-        *      @return The destination URL to really use.\r
-        */\r
-       public synchronized String lookup(String sid, String primary) {\r
-               String oprim = sid2primary.get(sid);\r
-               if (primary.equals(oprim)) {\r
-                       return(sid2secondary.get(sid));\r
-               } else if (oprim != null) {\r
-                       forget(sid);\r
-               }       \r
-               return(primary);\r
-       }\r
-       /**\r
-        *      Is a subscription redirected?\r
-        */\r
-       public synchronized boolean isRedirected(String sid) {\r
-               return(sid != null && sid2secondary.get(sid) != null);\r
-       }\r
-}\r
+/*******************************************************************************
+ * ============LICENSE_START==================================================
+ * * org.onap.dmaap
+ * * ===========================================================================
+ * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
+ * * ===========================================================================
+ * * Licensed under the Apache License, Version 2.0 (the "License");
+ * * you may not use this file except in compliance with the License.
+ * * You may obtain a copy of the License at
+ * *
+ *  *      http://www.apache.org/licenses/LICENSE-2.0
+ * *
+ *  * Unless required by applicable law or agreed to in writing, software
+ * * distributed under the License is distributed on an "AS IS" BASIS,
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * * See the License for the specific language governing permissions and
+ * * limitations under the License.
+ * * ============LICENSE_END====================================================
+ * *
+ * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ * *
+ ******************************************************************************/
+
+
+package org.onap.dmaap.datarouter.node;
+
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+import java.io.BufferedReader;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.OutputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Timer;
+
+/**
+ * Track redirections of subscriptions
+ */
+public class RedirManager {
+
+    private static EELFLogger eelfLogger = EELFManager.getInstance().getLogger(RedirManager.class);
+    RateLimitedOperation op;
+    private HashMap<String, String> sid2primary = new HashMap<>();
+    private HashMap<String, String> sid2secondary = new HashMap<>();
+    private String redirfile;
+
+    /**
+     * Create a mechanism for maintaining subscription redirections.
+     *
+     * @param redirfile The file to store the redirection information.
+     * @param mininterval The minimum number of milliseconds between writes to the redirection information file.
+     * @param timer The timer thread used to run delayed file writes.
+     */
+    public RedirManager(String redirfile, long mininterval, Timer timer) {
+        this.redirfile = redirfile;
+        op = new RateLimitedOperation(mininterval, timer) {
+            public void run() {
+                try {
+                    StringBuilder sb = new StringBuilder();
+                    for (Map.Entry<String, String> entry : sid2primary.entrySet()) {
+                        String s = entry.getKey();
+                        String value = entry.getValue();
+                        sb.append(s).append(' ').append(value).append(' ')
+                                .append(sid2secondary.get(s)).append('\n');
+                    }
+                    try (OutputStream os = new FileOutputStream(RedirManager.this.redirfile)) {
+                        os.write(sb.toString().getBytes());
+                    }
+                } catch (Exception e) {
+                    eelfLogger.error("Exception", e);
+                }
+            }
+        };
+        try {
+            String s;
+            try (BufferedReader br = new BufferedReader(new FileReader(redirfile))) {
+                while ((s = br.readLine()) != null) {
+                    addSubRedirInfo(s);
+                }
+            }
+        } catch (Exception e) {
+            eelfLogger.debug("Missing file is normal", e);
+        }
+    }
+
+    /**
+     * Set up redirection.  If a request is to be sent to subscription ID sid, and that is configured to go to URL
+     * primary, instead, go to secondary.
+     *
+     * @param sid The subscription ID to be redirected
+     * @param primary The URL associated with that subscription ID
+     * @param secondary The replacement URL to use instead
+     */
+    public synchronized void redirect(String sid, String primary, String secondary) {
+        sid2primary.put(sid, primary);
+        sid2secondary.put(sid, secondary);
+        op.request();
+    }
+
+    /**
+     * Cancel redirection.  If a request is to be sent to subscription ID sid, send it to its primary URL.
+     *
+     * @param sid The subscription ID to remove from the table.
+     */
+    public synchronized void forget(String sid) {
+        sid2primary.remove(sid);
+        sid2secondary.remove(sid);
+        op.request();
+    }
+
+    /**
+     * Look up where to send a subscription.  If the primary has changed or there is no redirection, use the primary.
+     * Otherwise, redirect to the secondary URL.
+     *
+     * @param sid The subscription ID to look up.
+     * @param primary The configured primary URL.
+     * @return The destination URL to really use.
+     */
+    public synchronized String lookup(String sid, String primary) {
+        String oprim = sid2primary.get(sid);
+        if (primary.equals(oprim)) {
+            return (sid2secondary.get(sid));
+        } else if (oprim != null) {
+            forget(sid);
+        }
+        return (primary);
+    }
+
+    /**
+     * Is a subscription redirected?
+     */
+    public synchronized boolean isRedirected(String sid) {
+        return (sid != null && sid2secondary.get(sid) != null);
+    }
+
+    private void addSubRedirInfo(String s) {
+        s = s.trim();
+        String[] sx = s.split(" ");
+        if (s.startsWith("#") || sx.length != 3) {
+            return;
+        }
+        sid2primary.put(sx[0], sx[1]);
+        sid2secondary.put(sx[0], sx[2]);
+    }
+}