X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-node%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fnode%2FRedirManager.java;h=4cd650b031526febde8ca09303bce9f015029671;hb=5e6a9f65049e8e8d39e8dcab227e5d75b328b173;hp=a557e7ad89f32a18f9a820c5bdaec03d69e6b459;hpb=1841cb5d8da7b21996f8faad9d24f858e6ce8a41;p=dmaap%2Fdatarouter.git diff --git a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/RedirManager.java b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/RedirManager.java index a557e7ad..4cd650b0 100644 --- a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/RedirManager.java +++ b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/RedirManager.java @@ -24,13 +24,21 @@ package org.onap.dmaap.datarouter.node; -import java.util.*; -import java.io.*; +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.IOException; +import java.io.OutputStream; +import java.util.Hashtable; +import java.util.Timer; /** * Track redirections of subscriptions */ public class RedirManager { + private static EELFLogger eelfLogger = EELFManager.getInstance().getLogger(RedirManager.class); private Hashtable sid2primary = new Hashtable(); private Hashtable sid2secondary = new Hashtable(); private String redirfile; @@ -39,9 +47,10 @@ public class RedirManager { /** * 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. + * @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; @@ -50,38 +59,41 @@ public class RedirManager { try { StringBuffer sb = new StringBuffer(); for (String s : sid2primary.keySet()) { - sb.append(s).append(' ').append(sid2primary.get(s)).append(' ').append(sid2secondary.get(s)).append('\n'); + sb.append(s).append(' ').append(sid2primary.get(s)).append(' ') + .append(sid2secondary.get(s)).append('\n'); + } + try (OutputStream os = new FileOutputStream(RedirManager.this.redirfile)) { + os.write(sb.toString().getBytes()); } - OutputStream os = new FileOutputStream(RedirManager.this.redirfile); - os.write(sb.toString().getBytes()); - os.close(); } catch (Exception e) { + eelfLogger.error("Exception", e); } } }; try { String s; - BufferedReader br = new BufferedReader(new FileReader(redirfile)); - while ((s = br.readLine()) != null) { - s = s.trim(); - String[] sx = s.split(" "); - if (s.startsWith("#") || sx.length != 3) { - continue; + try (BufferedReader br = new BufferedReader(new FileReader(redirfile))) { + while ((s = br.readLine()) != null) { + s = s.trim(); + String[] sx = s.split(" "); + if (s.startsWith("#") || sx.length != 3) { + continue; + } + sid2primary.put(sx[0], sx[1]); + sid2secondary.put(sx[0], sx[2]); } - sid2primary.put(sx[0], sx[1]); - sid2secondary.put(sx[0], sx[2]); } - br.close(); } catch (Exception e) { - // missing file is normal + eelfLogger.error("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. + * 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 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) { @@ -91,9 +103,10 @@ public class RedirManager { } /** - * Cancel redirection. If a request is to be sent to subscription ID sid, send it to its primary URL. + * 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. + * @param sid The subscription ID to remove from the table. */ public synchronized void forget(String sid) { sid2primary.remove(sid); @@ -102,10 +115,11 @@ public class RedirManager { } /** - * 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. + * 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. + * @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) {