X-Git-Url: https://gerrit.onap.org/r/gitweb?p=dmaap%2Fdatarouter.git;a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2FSynchronizerTask.java;h=5d0592c34b16132376ea6779788674554c26375b;hp=e1eeb78674a3f8abfe5077d8114545f65f2aafa0;hb=6fb37eb225e48b58b1f30fc0423d19046509a54b;hpb=24af12414c00a3e398e11ebe6d308490da3a618c diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTask.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTask.java index e1eeb786..5d0592c3 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTask.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTask.java @@ -24,6 +24,11 @@ package org.onap.dmaap.datarouter.provisioning; +import static org.onap.dmaap.datarouter.provisioning.BaseServlet.TEXT_CT; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; @@ -60,7 +65,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; @@ -84,7 +88,7 @@ import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities; *
  • Checking DNS once per minute to see which POD the DNS CNAME points to. The CNAME will point to * the active (master) POD.
  • *
  • On non-master (standby) PODs, fetches provisioning data and logs in order to keep MariaDB in sync.
  • - *
  • Providing information to other parts of the system as to the current role (ACTIVE, STANDBY, UNKNOWN) + *
  • Providing information to other parts of the system as to the current role (ACTIVE_POD, STANDBY_POD, UNKNOWN_POD) * of this POD.
  • * *

    For this to work correctly, the following code needs to be placed at the beginning of main().

    @@ -95,170 +99,156 @@ import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities; * @author Robert Eby * @version $Id: SynchronizerTask.java,v 1.10 2014/03/21 13:50:10 eby Exp $ */ + public class SynchronizerTask extends TimerTask { /** - * This is a singleton -- there is only one SynchronizerTask object in the server + * This is a singleton -- there is only one SynchronizerTask object in the server. */ private static SynchronizerTask synctask; /** - * This POD is unknown -- not on the list of PODs + * This POD is unknown -- not on the list of PODs. */ - public static final int UNKNOWN = 0; + public static final int UNKNOWN_POD = 0; /** - * This POD is active -- on the list of PODs, and the DNS CNAME points to us + * This POD is active -- on the list of PODs, and the DNS CNAME points to us. */ - public static final int ACTIVE = 1; + public static final int ACTIVE_POD = 1; /** - * This POD is standby -- on the list of PODs, and the DNS CNAME does not point to us + * This POD is standby -- on the list of PODs, and the DNS CNAME does not point to us. */ - public static final int STANDBY = 2; - private static final String[] stnames = {"UNKNOWN", "ACTIVE", "STANDBY"}; + public static final int STANDBY_POD = 2; + + private static final String[] stnames = {"UNKNOWN_POD", "ACTIVE_POD", "STANDBY_POD"}; private static final long ONE_HOUR = 60 * 60 * 1000L; - private final Logger logger; + private long nextMsg = 0; // only display the "Current podState" msg every 5 mins. + + private final EELFLogger logger; private final Timer rolex; private final String spooldir; - private int state; + private int podState; private boolean doFetch; private long nextsynctime; private AbstractHttpClient httpclient = null; - /** - * Get the singleton SynchronizerTask object. - * - * @return the SynchronizerTask - */ - public static synchronized SynchronizerTask getSynchronizer() { - if (synctask == null) { - synctask = new SynchronizerTask(); - } - return synctask; - } - @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; + podState = UNKNOWN_POD; doFetch = true; // start off with a fetch nextsynctime = 0; - logger.info("PROV5000: Sync task starting, server state is UNKNOWN"); + logger.info("PROV5000: Sync task starting, server podState is UNKNOWN_POD"); try { 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); - try(FileInputStream instream = new FileInputStream(new File(store))) { + 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_PASSWORD_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()); + 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 - 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 { - String s = props.getProperty("org.onap.dmaap.datarouter.provserver.sync_interval", "5000"); - interval = Long.parseLong(s); - } catch (NumberFormatException e) { - interval = 5000L; + String keystorepass = props.getProperty(Main.KEYSTORE_PASS_PROPERTY); + 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; } - rolex.scheduleAtFixedRate(this, 0L, interval); + setSynchTimer(props); } catch (Exception e) { logger.warn("PROV5005: Problem starting the synchronizer: " + e); } } + private void setSynchTimer(Properties props) { + // Run once every 5 seconds to check DNS, etc. + long interval; + try { + String str = props.getProperty("org.onap.dmaap.datarouter.provserver.sync_interval", "5000"); + interval = Long.parseLong(str); + } catch (NumberFormatException e) { + interval = 5000L; + } + rolex.scheduleAtFixedRate(this, 0L, interval); + } + /** - * What is the state of this POD? + * Get the singleton SynchronizerTask object. * - * @return one of ACTIVE, STANDBY, UNKNOWN + * @return the SynchronizerTask */ - public int getState() { - return state; + public static synchronized SynchronizerTask getSynchronizer() { + if (synctask == null) { + synctask = new SynchronizerTask(); + } + return synctask; } /** - * Is this the active POD? + * What is the podState of this POD?. + * + * @return one of ACTIVE_POD, STANDBY_POD, UNKNOWN_POD + */ + public int getPodState() { + return podState; + } + + /** + * Is this the active POD?. * * @return true if we are active (the master), false otherwise */ public boolean isActive() { - return state == ACTIVE; + return podState == ACTIVE_POD; } /** * This method is used to signal that another POD (the active POD) has sent us a /fetchProv request, and that we * should re-synchronize with the master. */ - public void doFetch() { + void doFetch() { doFetch = true; } /** * Runs once a minute in order to
      *
    1. lookup DNS names,
    2. - *
    3. determine the state of this POD,
    4. - *
    5. if this is a standby POD, and the fetch flag is set, perform a fetch of state from the active POD.
    6. + *
    7. determine the podState of this POD,
    8. + *
    9. if this is a standby POD, and the fetch flag is set, perform a fetch of podState from the active POD.
    10. *
    11. if this is a standby POD, check if there are any new log records to be replicated.
    12. - *
    + * . */ @Override public void run() { try { - state = lookupState(); - if (state == STANDBY) { + podState = lookupState(); + if (podState == STANDBY_POD) { // Only copy provisioning data FROM the active server TO the standby if (doFetch || (System.currentTimeMillis() >= nextsynctime)) { - logger.debug("Initiating a sync..."); - JSONObject jo = readProvisioningJSON(); - if (jo != null) { - doFetch = false; - syncFeeds(jo.getJSONArray("feeds")); - syncSubs(jo.getJSONArray("subscriptions")); - syncGroups(jo.getJSONArray("groups")); //Rally:US708115 - 1610 - syncParams(jo.getJSONObject("parameters")); - // The following will not be present in a version=1.0 provfeed - JSONArray ja = jo.optJSONArray("ingress"); - if (ja != null) { - syncIngressRoutes(ja); - } - JSONObject j2 = jo.optJSONObject("egress"); - if (j2 != null) { - syncEgressRoutes(j2); - } - ja = jo.optJSONArray("routing"); - if (ja != null) { - syncNetworkRoutes(ja); - } - } + syncProvisioningData(); logger.info("PROV5013: Sync completed."); nextsynctime = System.currentTimeMillis() + ONE_HOUR; } @@ -277,23 +267,47 @@ public class SynchronizerTask extends TimerTask { remote.andNot(local); if (!remote.isEmpty()) { logger.debug(" Replicating logs: " + remote); - replicateDRLogs(remote); + replicateDataRouterLogs(remote); } } } catch (Exception e) { logger.warn("PROV0020: Caught exception in SynchronizerTask: " + e); - e.printStackTrace(); + } + } + + private void syncProvisioningData() { + logger.debug("Initiating a sync..."); + JSONObject jo = readProvisioningJson(); + if (jo != null) { + doFetch = false; + syncFeeds(jo.getJSONArray("feeds")); + syncSubs(jo.getJSONArray("subscriptions")); + syncGroups(jo.getJSONArray("groups")); //Rally:US708115 - 1610 + syncParams(jo.getJSONObject("parameters")); + // The following will not be present in a version=1.0 provfeed + JSONArray ja = jo.optJSONArray("ingress"); + if (ja != null) { + syncIngressRoutes(ja); + } + JSONObject j2 = jo.optJSONObject("egress"); + if (j2 != null) { + syncEgressRoutes(j2); + } + ja = jo.optJSONArray("routing"); + if (ja != null) { + syncNetworkRoutes(ja); + } } } /** - * This method is used to lookup the CNAME that points to the active server. It returns 0 (UNKNOWN), 1(ACTIVE), or 2 - * (STANDBY) to indicate the state of this server. + * This method is used to lookup the CNAME that points to the active server. + * It returns 0 (UNKNOWN_POD), 1(ACTIVE_POD), or (STANDBY_POD) to indicate the podState of this server. * - * @return the current state + * @return the current podState */ - private int lookupState() { - int newstate = UNKNOWN; + int lookupState() { + int newPodState = UNKNOWN_POD; try { InetAddress myaddr = InetAddress.getLocalHost(); if (logger.isTraceEnabled()) { @@ -302,39 +316,37 @@ public class SynchronizerTask extends TimerTask { String thisPod = myaddr.getHostName(); Set pods = new TreeSet<>(Arrays.asList(BaseServlet.getPods())); if (pods.contains(thisPod)) { - InetAddress pserver = InetAddress.getByName(BaseServlet.activeProvName); - newstate = myaddr.equals(pserver) ? ACTIVE : STANDBY; + InetAddress pserver = InetAddress.getByName(BaseServlet.getActiveProvName()); + newPodState = myaddr.equals(pserver) ? ACTIVE_POD : STANDBY_POD; if (logger.isDebugEnabled() && System.currentTimeMillis() >= nextMsg) { - logger.debug("Active POD = " + pserver + ", Current state is " + stnames[newstate]); + logger.debug("Active POD = " + pserver + ", Current podState is " + stnames[newPodState]); nextMsg = System.currentTimeMillis() + (5 * 60 * 1000L); } } else { 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) { - logger - .info(String.format("PROV5001: Server state changed from %s to %s", stnames[state], stnames[newstate])); + if (newPodState != podState) { + logger.info(String.format("PROV5001: Server podState changed from %s to %s", + stnames[podState], stnames[newPodState])); } - return newstate; + return newPodState; } - private static long nextMsg = 0; // only display the "Current state" msg every 5 mins. - /** * Synchronize the Feeds in the JSONArray, with the Feeds in the DB. */ private void syncFeeds(JSONArray ja) { - Collection coll = new ArrayList(); + Collection coll = new ArrayList<>(); for (int n = 0; n < ja.length(); n++) { try { - Feed f = new Feed(ja.getJSONObject(n)); - coll.add(f); + Feed feed = new Feed(ja.getJSONObject(n)); + coll.add(feed); } 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())) { @@ -346,16 +358,16 @@ public class SynchronizerTask extends TimerTask { * Synchronize the Subscriptions in the JSONArray, with the Subscriptions in the DB. */ private void syncSubs(JSONArray ja) { - Collection coll = new ArrayList(); + Collection coll = new ArrayList<>(); for (int n = 0; n < ja.length(); n++) { try { //Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047. - JSONObject j = ja.getJSONObject(n); - j.put("sync", "true"); - Subscription s = new Subscription(j); - coll.add(s); + JSONObject jsonObject = ja.getJSONObject(n); + jsonObject.put("sync", "true"); + Subscription sub = new Subscription(jsonObject); + coll.add(sub); } 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())) { @@ -367,13 +379,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 coll = new ArrayList(); + Collection coll = new ArrayList<>(); for (int n = 0; n < ja.length(); n++) { try { - Group g = new Group(ja.getJSONObject(n)); - coll.add(g); + Group group = new Group(ja.getJSONObject(n)); + coll.add(group); } 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())) { @@ -386,25 +398,27 @@ public class SynchronizerTask extends TimerTask { * Synchronize the Parameters in the JSONObject, with the Parameters in the DB. */ private void syncParams(JSONObject jo) { - Collection coll = new ArrayList(); + Collection coll = new ArrayList<>(); for (String k : jo.keySet()) { - String v = ""; + String val = ""; try { - v = jo.getString(k); + val = jo.getString(k); } catch (JSONException e) { + logger.warn("PROV5004: Invalid object in parameters: " + jo.optJSONObject(k), e); try { - v = "" + jo.getInt(k); + val = "" + jo.getInt(k); } catch (JSONException e1) { + logger.warn("PROV5004: Invalid object in parameters: " + jo.optInt(k), e1); JSONArray ja = jo.getJSONArray(k); for (int i = 0; i < ja.length(); i++) { if (i > 0) { - v += "|"; + val += "|"; } - v += ja.getString(i); + val += ja.getString(i); } } } - coll.add(new Parameters(k, v)); + coll.add(new Parameters(k, val)); } if (sync(coll, Parameters.getParameterCollection())) { BaseServlet.provisioningDataChanged(); @@ -413,7 +427,7 @@ public class SynchronizerTask extends TimerTask { } private void syncIngressRoutes(JSONArray ja) { - Collection coll = new ArrayList(); + Collection coll = new ArrayList<>(); for (int n = 0; n < ja.length(); n++) { try { IngressRoute in = new IngressRoute(ja.getJSONObject(n)); @@ -428,7 +442,7 @@ public class SynchronizerTask extends TimerTask { } private void syncEgressRoutes(JSONObject jo) { - Collection coll = new ArrayList(); + Collection coll = new ArrayList<>(); for (String key : jo.keySet()) { try { int sub = Integer.parseInt(key); @@ -436,9 +450,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())) { @@ -447,13 +461,13 @@ public class SynchronizerTask extends TimerTask { } private void syncNetworkRoutes(JSONArray ja) { - Collection coll = new ArrayList(); + Collection coll = new ArrayList<>(); for (int n = 0; n < ja.length(); n++) { try { NetworkRoute nr = new NetworkRoute(ja.getJSONObject(n)); coll.add(nr); } catch (JSONException e) { - logger.warn("PROV5004: Invalid object in network routes: " + ja.optJSONObject(n)); + logger.warn("PROV5004: Invalid object in network routes: " + ja.optJSONObject(n), e); } } if (sync(coll, NetworkRoute.getAllNetworkRoutes())) { @@ -466,7 +480,7 @@ public class SynchronizerTask extends TimerTask { try { Map newmap = getMap(newc); Map oldmap = getMap(oldc); - Set union = new TreeSet(newmap.keySet()); + Set union = new TreeSet<>(newmap.keySet()); union.addAll(oldmap.keySet()); DB db = new DB(); @SuppressWarnings("resource") @@ -475,59 +489,64 @@ public class SynchronizerTask extends TimerTask { Syncable newobj = newmap.get(n); Syncable oldobj = oldmap.get(n); if (oldobj == null) { - if (logger.isDebugEnabled()) { - logger.debug(" Inserting record: " + newobj); - } - newobj.doInsert(conn); - changes = true; + changes = insertRecord(conn, newobj); } else if (newobj == null) { - if (logger.isDebugEnabled()) { - logger.debug(" Deleting record: " + oldobj); - } - oldobj.doDelete(conn); - changes = true; + changes = deleteRecord(conn, oldobj); } else if (!newobj.equals(oldobj)) { - if (logger.isDebugEnabled()) { - logger.debug(" Updating record: " + newobj); - } - newobj.doUpdate(conn); - - /**Rally US708115 - * Change Ownership of FEED - 1610, Syncronised with secondary DB. - * */ - checkChnageOwner(newobj, oldobj); - - changes = true; + changes = updateRecord(conn, newobj, oldobj); } } db.release(conn); } catch (SQLException e) { logger.warn("PROV5009: problem during sync, exception: " + e); - e.printStackTrace(); } return changes; } - private Map getMap(Collection c) { - Map map = new HashMap(); - for (Syncable v : c) { + private boolean updateRecord(Connection conn, Syncable newobj, Syncable oldobj) { + if (logger.isDebugEnabled()) { + logger.debug(" Updating record: " + newobj); + } + boolean changes = newobj.doUpdate(conn); + checkChangeOwner(newobj, oldobj); + + return changes; + } + + private boolean deleteRecord(Connection conn, Syncable oldobj) { + if (logger.isDebugEnabled()) { + logger.debug(" Deleting record: " + oldobj); + } + return oldobj.doDelete(conn); + } + + private boolean insertRecord(Connection conn, Syncable newobj) { + if (logger.isDebugEnabled()) { + logger.debug(" Inserting record: " + newobj); + } + return newobj.doInsert(conn); + } + + private Map getMap(Collection coll) { + Map map = new HashMap<>(); + for (Syncable v : coll) { map.put(v.getKey(), v); } return map; } - /**Change owner of FEED/SUBSCRIPTION*/ /** + * Change owner of FEED/SUBSCRIPTION. * Rally US708115 Change Ownership of FEED - 1610 */ - private void checkChnageOwner(Syncable newobj, Syncable oldobj) { + private void checkChangeOwner(Syncable newobj, Syncable oldobj) { if (newobj instanceof Feed) { Feed oldfeed = (Feed) oldobj; Feed newfeed = (Feed) newobj; if (!oldfeed.getPublisher().equals(newfeed.getPublisher())) { - logger.info("PROV5013 - Previous publisher: " + oldfeed.getPublisher() + ": New publisher-" + newfeed - .getPublisher()); + logger.info("PROV5013 - Previous publisher: " + + oldfeed.getPublisher() + ": New publisher-" + newfeed.getPublisher()); oldfeed.setPublisher(newfeed.getPublisher()); oldfeed.changeOwnerShip(); } @@ -536,8 +555,8 @@ public class SynchronizerTask extends TimerTask { Subscription newsub = (Subscription) newobj; if (!oldsub.getSubscriber().equals(newsub.getSubscriber())) { - logger.info("PROV5013 - Previous subscriber: " + oldsub.getSubscriber() + ": New subscriber-" + newsub - .getSubscriber()); + logger.info("PROV5013 - Previous subscriber: " + + oldsub.getSubscriber() + ": New subscriber-" + newsub.getSubscriber()); oldsub.setSubscriber(newsub.getSubscriber()); oldsub.changeOwnerShip(); } @@ -550,26 +569,26 @@ public class SynchronizerTask extends TimerTask { * * @return the provisioning data (as a JONObject) */ - private synchronized JSONObject readProvisioningJSON() { + private synchronized JSONObject readProvisioningJson() { String url = URLUtilities.generatePeerProvURL(); HttpGet get = new HttpGet(url); try { HttpResponse response = httpclient.execute(get); int code = response.getStatusLine().getStatusCode(); if (code != HttpServletResponse.SC_OK) { - logger.warn("PROV5010: readProvisioningJSON failed, bad error code: " + code); + logger.warn("PROV5010: readProvisioningJson failed, bad error code: " + code); return null; } HttpEntity entity = response.getEntity(); String ctype = entity.getContentType().getValue().trim(); - if (!ctype.equals(BaseServlet.PROVFULL_CONTENT_TYPE1) && !ctype - .equals(BaseServlet.PROVFULL_CONTENT_TYPE2)) { - logger.warn("PROV5011: readProvisioningJSON failed, bad content type: " + ctype); + if (!ctype.equals(BaseServlet.PROVFULL_CONTENT_TYPE1) + && !ctype.equals(BaseServlet.PROVFULL_CONTENT_TYPE2)) { + logger.warn("PROV5011: readProvisioningJson failed, bad content type: " + ctype); return null; } return new JSONObject(new JSONTokener(entity.getContent())); } catch (Exception e) { - logger.warn("PROV5012: readProvisioningJSON failed, exception: " + e); + logger.warn("PROV5012: readProvisioningJson failed, exception: " + e); return null; } finally { get.releaseConnection(); @@ -582,18 +601,18 @@ public class SynchronizerTask extends TimerTask { * * @return the bitset */ - private RLEBitSet readRemoteLoglist() { + RLEBitSet readRemoteLoglist() { RLEBitSet bs = new RLEBitSet(); 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. HttpGet get = new HttpGet(url); - try { + try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { HttpResponse response = httpclient.execute(get); int code = response.getStatusLine().getStatusCode(); if (code != HttpServletResponse.SC_OK) { @@ -602,13 +621,12 @@ public class SynchronizerTask extends TimerTask { } HttpEntity entity = response.getEntity(); String ctype = entity.getContentType().getValue().trim(); - if (!ctype.equals("text/plain")) { + if (!TEXT_CT.equals(ctype)) { logger.warn("PROV5011: readRemoteLoglist failed, bad content type: " + ctype); return bs; } InputStream is = entity.getContent(); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - int ch = 0; + int ch; while ((ch = is.read()) >= 0) { bos.write(ch); } @@ -629,27 +647,27 @@ public class SynchronizerTask extends TimerTask { * * @param bs the bitset (an RELBitSet) of log records to fetch */ - private void replicateDRLogs(RLEBitSet bs) { + void replicateDataRouterLogs(RLEBitSet bs) { String url = URLUtilities.generatePeerLogsURL(); HttpPost post = new HttpPost(url); try { - String t = bs.toString(); - HttpEntity body = new ByteArrayEntity(t.getBytes(), ContentType.create("text/plain")); + String str = bs.toString(); + HttpEntity body = new ByteArrayEntity(str.getBytes(), ContentType.create(TEXT_CT)); post.setEntity(body); if (logger.isDebugEnabled()) { - logger.debug("Requesting records: " + t); + logger.debug("Requesting records: " + str); } HttpResponse response = httpclient.execute(post); int code = response.getStatusLine().getStatusCode(); if (code != HttpServletResponse.SC_OK) { - logger.warn("PROV5010: replicateDRLogs failed, bad error code: " + code); + logger.warn("PROV5010: replicateDataRouterLogs failed, bad error code: " + code); return; } HttpEntity entity = response.getEntity(); String ctype = entity.getContentType().getValue().trim(); - if (!ctype.equals("text/plain")) { - logger.warn("PROV5011: replicateDRLogs failed, bad content type: " + ctype); + if (!TEXT_CT.equals(ctype)) { + logger.warn("PROV5011: replicateDataRouterLogs failed, bad content type: " + ctype); return; } @@ -660,7 +678,7 @@ public class SynchronizerTask extends TimerTask { Files.move(tmppath, donepath, StandardCopyOption.REPLACE_EXISTING); logger.info("Approximately " + bs.cardinality() + " records replicated."); } catch (Exception e) { - logger.warn("PROV5012: replicateDRLogs failed, exception: " + e); + logger.warn("PROV5012: replicateDataRouterLogs failed, exception: " + e); } finally { post.releaseConnection(); }