Merge "sonar major for error-handling"
[dmaap/messagerouter/mirroragent.git] / src / main / java / com / att / nsa / dmaapMMAgent / MirrorMakerAgent.java
index 35a64c3..ad1e20f 100644 (file)
@@ -36,6 +36,7 @@ import java.net.URL;
 import java.util.ArrayList;
 import java.util.Properties;
 
+import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 import org.jasypt.util.text.BasicTextEncryptor;
 
@@ -102,7 +103,7 @@ public class MirrorMakerAgent {
                                try {
                                        input.close();
                                } catch (IOException e) {
-                                       logger.error("IOException" + e);
+                                       logger.error(" IOException occers " + e);
                                }
                        }
                }
@@ -123,7 +124,6 @@ public class MirrorMakerAgent {
                                        input.close();
                                } catch (IOException e) {
                                        logger.error("IOException" + e);
-                                       e.printStackTrace();
                                }
                        }
                }
@@ -153,6 +153,7 @@ public class MirrorMakerAgent {
                        }
                        input = new FileInputStream(mmagenthome + "/etc/" + agentName + propName + ".properties");
                } catch (IOException ex) {
+                       logger.error(" IOException will be handled " + ex);
                        try {
                                input = new FileInputStream(mmagenthome + "/etc/" + propName + ".properties");
                                Properties prop = new Properties();
@@ -167,14 +168,14 @@ public class MirrorMakerAgent {
                                prop.store(out, "");
 
                        } catch (Exception e) {
-                               e.printStackTrace();
+                               logger.error("Exception at checkPropertiesFile " +e);
                        }
                } finally {
                        if (input != null) {
                                try {
                                        input.close();
                                } catch (IOException e) {
-                                       e.printStackTrace();
+                                       logger.error("Exception occurred is " +e);
                                }
                        }
                        if (out != null) {
@@ -182,6 +183,7 @@ public class MirrorMakerAgent {
                                        out.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
+                                       logger.error("Exception is : "+e);
                                }
                        }
                }
@@ -211,6 +213,7 @@ public class MirrorMakerAgent {
                                        try {
                                                Thread.sleep(1000);
                                        } catch (InterruptedException e) {
+                                                Thread.currentThread().interrupt();
                                        }
                                        mirrorMakers.getListMirrorMaker().set(i, mm);
                                } else {
@@ -251,6 +254,7 @@ public class MirrorMakerAgent {
                                return topicMessage[0];
                        }
                } catch (Exception e) {
+                       logger.error(" Exception Occered " + e);
                        return "ERROR:" + e.getMessage() + " Server Response is:" + response;
                }
                return null;
@@ -281,6 +285,7 @@ public class MirrorMakerAgent {
                        return response;
 
                } catch (Exception e) {
+                       logger.error(" Exception Occered " + e);
                        return "ERROR:" + e.getLocalizedMessage();
                }
        }
@@ -346,7 +351,7 @@ public class MirrorMakerAgent {
                                        } catch (Exception ex) {
                                                connectionattempt++;
                                                if (connectionattempt > 5) {
-                                                       logger.info("Can't connect to the topic, mmagent shutting down , " + topicMessage);
+                                                       logger.info("Can't connect to the topic, mmagent shutting down , " + topicMessage + ex);
                                                        return;
                                                }
                                                logger.info("Can't connect to the topic, " + topicMessage + " Retrying " + connectionattempt
@@ -361,7 +366,7 @@ public class MirrorMakerAgent {
 
                        }
                } catch (Exception e) {
-                       e.printStackTrace();
+                       logger.error("Exception at readAgentTopic : " + e);
                }
 
        }
@@ -385,7 +390,7 @@ public class MirrorMakerAgent {
                } else if (exists == false && mirrorMakers == null) {
                        mirrorMakers = new ListMirrorMaker();
                        ArrayList<MirrorMaker> list = mirrorMakers.getListMirrorMaker();
-                       list = new ArrayList<MirrorMaker>();
+                       list = new ArrayList();
                        list.add(newMirrorMaker);
                        mirrorMakers.setListMirrorMaker(list);
                }
@@ -399,7 +404,7 @@ public class MirrorMakerAgent {
                        out = new FileOutputStream(mmagenthome + "/etc/mmagent.config");
                        mirrorMakerProperties.store(out, "");
                } catch (IOException ex) {
-                       ex.printStackTrace();
+                       logger.error(" IOException Occered " + ex);
                } finally {
                        if (out != null) {
                                try {
@@ -440,15 +445,17 @@ public class MirrorMakerAgent {
                                try {
                                        Thread.sleep(1000);
                                } catch (InterruptedException e) {
+                                       logger.log(Level.WARN, "Interrupted!", e);
+                                       Thread.currentThread().interrupt();
                                }
                        } catch (IOException ex) {
-                               ex.printStackTrace();
+                               logger.error(" IOException Occered " + ex);
                        } finally {
                                if (out != null) {
                                        try {
                                                out.close();
                                        } catch (IOException e) {
-                                               e.printStackTrace();
+                                               logger.error(" IOException Occered " + e);
                                        }
                                }
                        }
@@ -492,6 +499,7 @@ public class MirrorMakerAgent {
                                                out.close();
                                        } catch (IOException e) {
                                                e.printStackTrace();
+                                               logger.error("IOException occered " + e);
                                        }
                                }
                        }