Fix compilation issue due to generic Exception
[policy/engine.git] / LogParser / src / main / java / org / onap / xacml / parser / ParseLog.java
index 4612206..5ead822 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * LogParser
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 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.
@@ -46,10 +46,10 @@ import java.util.regex.Pattern;
 import java.util.stream.Stream;
 
 import org.apache.log4j.Logger;
-import org.onap.policy.common.im.AdministrativeStateException;
 import org.onap.policy.common.im.IntegrityMonitor;
-import org.onap.policy.common.im.StandbyStatusException;
+import org.onap.policy.common.im.IntegrityMonitorException;
 import org.onap.policy.common.logging.flexlogger.FlexLogger;
+import org.onap.policy.utils.CryptoUtils;
 import org.onap.xacml.parser.LogEntryObject.LOGTYPE;
 
 /**
@@ -77,22 +77,21 @@ public class ParseLog {
        private static String logFile;
        private static String debuglogFile;
        private static String errorlogFile;
-       private static String JDBC_URL;
-       private static String JDBC_USER;
-       private static String JDBC_PASSWORD;
-       private static String JDBC_DRIVER;
+       private static String jdbcUrl;
+       private static String jdbcUser;
+       private static String jdbcPassword;
+       private static String jdbcDriver;
        private static int maxLength = 255;   //Max length that is allowed in the DB table
        private static String resourceName;
        private static long sleepTimer = 50000;
        static  IntegrityMonitor im;
        private static boolean isMissingLogFile;
        //Default:Timer initial delay and the delay between in milliseconds before task is to be execute
-       private static int TIMER_DELAY_TIME = 1000;     
+       private static final int TIMER_DELAY_TIME = 1000;       
        //Default:Timer scheduleAtFixedRate period - time in milliseconds between successive task executions
-       private static int CHECK_INTERVAL = 86400000;  // run this clean up once a day
-       private static RandomAccessFile randomAccessFile;       
+       private static int checkInterval = 86400000;  // run this clean up once a day   
        private static String loggingProcess = "Error processing line in ";
-       private static int TIME_FRAME = 5;      
+       private static int defaultTimeFrame = 5;        
        private static String message =" value read in: ";      
        private static String lineFormat = "(\\r\\n|\\n)";      
        private static String lineRead = "-line-Read:"; 
@@ -126,20 +125,13 @@ public class ParseLog {
        
        private static boolean processLine(Path debugfilePath, String dataFileName, int lastNmRead, LOGTYPE logType){
                // log4jlogger must use .info
-               Stream<String> lines = null;
-               try {
-                       lines = Files.lines(debugfilePath, Charset.defaultCharset()).onClose(() -> log4jlogger.info(last+dataFileName+ lineRead + lastNmRead)).skip(lastNmRead);
+               try(Stream<String> lines = Files.lines(debugfilePath, Charset.defaultCharset()).onClose(() -> log4jlogger.info(last+dataFileName+ lineRead + lastNmRead)).skip(lastNmRead)){
                        lines.forEachOrdered(line -> process(line, type, logType));
                } catch (IOException e) {
                        logger.error(loggingProcess + dataFileName, e);
                        logger.error(breakLoop);
                        return true;
-               }finally{
-                       if(lines != null){
-                               lines.close();
-                       }
                }
-         
                return false;
        }
        private static void processDebugLogParser(File debugfile, Path debugfilePath, String dataFileName){
@@ -351,30 +343,32 @@ public class ParseLog {
                        file.createNewFile();
                        return null;
                }
-               randomAccessFile = new RandomAccessFile(file, "r");
-        StringBuilder builder = new StringBuilder();
-        long length = file.length();
-        logger.debug("dataFileName: " +dataFileName);
-        if(length > 0){
-               length--;               
-               randomAccessFile.seek(length);
-               for(long seek = length; seek >= 0; --seek){
-                   randomAccessFile.seek(seek);
-                   char c = (char)randomAccessFile.read();
-                   builder.append(c);
-                   if(c == '\n'){
-                       builder = builder.reverse();
-                       logger.debug("builder.toString(): " +builder.toString());
-                       if (builder.toString().contains(last+dataFileName+lineRead)){
-                               String[] parseString = builder.toString().split(last+dataFileName+lineRead);
-                               String returnValue = parseString[1].replace("\r", "");
-                               return returnValue.trim();
-                       }
-                       builder = new StringBuilder();
-                    }  
-               }
-        }
-        
+               try(RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");){
+                   StringBuilder builder = new StringBuilder();
+                   long length = file.length();
+                   logger.debug("dataFileName: " +dataFileName);
+                   if(length > 0){
+                       length--;               
+                       randomAccessFile.seek(length);
+                       for(long seek = length; seek >= 0; --seek){
+                           randomAccessFile.seek(seek);
+                           char c = (char)randomAccessFile.read();
+                           builder.append(c);
+                           if(c == '\n'){
+                               builder = builder.reverse();
+                               logger.debug("builder.toString(): " +builder.toString());
+                               if (builder.toString().contains(last+dataFileName+lineRead)){
+                                   String[] parseString = builder.toString().split(last+dataFileName+lineRead);
+                                   String returnValue = parseString[1].replace("\r", "");
+                                   randomAccessFile.close();
+                                   return returnValue.trim();
+                               }
+                               builder = new StringBuilder();
+                           }   
+                       }
+                   }
+                   randomAccessFile.close();
+               }
                return null;
        }
 
@@ -584,10 +578,8 @@ public class ParseLog {
                if (im!=null){
                        try {
                                im.startTransaction();
-                       } catch (AdministrativeStateException e) {
-                               logger.error("Error received" + e);                             
-                       } catch (StandbyStatusException ex) {
-                               logger.error("Error received" + ex);
+                       } catch (IntegrityMonitorException e) {
+                               logger.error("Error received" + e);
                        }
                }
                returnLogValue = pullOutLogValues(line, type);
@@ -609,7 +601,7 @@ public class ParseLog {
        
        private static void writeDB(LogEntryObject returnLogValue) {
 
-               Connection conn = dbConnection(JDBC_DRIVER, JDBC_URL, JDBC_USER,JDBC_PASSWORD);
+               Connection conn = dbConnection(jdbcDriver, jdbcUrl, jdbcUser,jdbcPassword);
                dbAccesss(conn, returnLogValue.getSystem(), returnLogValue.getDescription(),  
                                                returnLogValue.getDate(), returnLogValue.getRemote(), 
                                                returnLogValue.getSystemType(), returnLogValue.getLogType().toString());
@@ -620,8 +612,7 @@ public class ParseLog {
         
         try {
                Class.forName(driver);
-                       Connection conn = DriverManager.getConnection(jdbc, user, pass);
-                       return conn;
+                       return DriverManager.getConnection(jdbc, user, pass);
                } catch ( Exception e) {
                        logger.error("Error connecting to DB: " + e);
                }
@@ -720,19 +711,24 @@ public class ParseLog {
                if(cleanupInterval != null && !cleanupInterval.isEmpty()){
                        int intCheckInterval = Integer.parseInt(cleanupInterval);
                        if(intCheckInterval > 300000) {//must be longer than 5 minutes
-                                       CHECK_INTERVAL = intCheckInterval;
+                                       checkInterval = intCheckInterval;
                        }
                }else{
-                        logger.debug("No value defined for CHECK_INTERVAL in parserlog.properties, so use its default value:" + CHECK_INTERVAL + " milliseconds");
+                        logger.debug("No value defined for CHECK_INTERVAL in parserlog.properties, so use its default value:" + checkInterval + " milliseconds");
                }
                
-               if(timeFrame != null && !timeFrame.isEmpty()){
-                       int intTimeFrame = Integer.parseInt(timeFrame);
+               if(timeFrame != null && !timeFrame.trim().isEmpty()){
+                   int intTimeFrame = defaultTimeFrame;
+                   try{
+                       intTimeFrame = Integer.parseInt(timeFrame);
+                   }catch(NumberFormatException e){
+                       logger.debug("Improper value defined for TIME_FRAME in parserlog.properties, so use its default value:" + defaultTimeFrame + " days");
+                   }
                        if(intTimeFrame > 0){
-                               TIME_FRAME = intTimeFrame;
+                               defaultTimeFrame = intTimeFrame;
                        }
                }else{
-                        logger.debug("No value defined for TIME_FRAME in parserlog.properties, so use its default value:" + TIME_FRAME + " days");
+                        logger.debug("No value defined for TIME_FRAME in parserlog.properties, so use its default value:" + defaultTimeFrame + " days");
                }
        }
        
@@ -807,17 +803,18 @@ public class ParseLog {
                                        
                                        setLogFileProperties(splitString);
                                        
-                                       JDBC_URL = config.getProperty("JDBC_URL").replace("'", "");
-                                       JDBC_USER = config.getProperty("JDBC_USER");
-                                       JDBC_DRIVER =  config.getProperty("JDBC_DRIVER");
-                                       JDBC_PASSWORD = config.getProperty("JDBC_PASSWORD");
+                                       jdbcUrl = config.getProperty("JDBC_URL").replace("'", "");
+                                       jdbcUser = config.getProperty("JDBC_USER");
+                                       jdbcDriver =  config.getProperty("JDBC_DRIVER");
+                                       jdbcPassword = CryptoUtils.decryptTxtNoExStr(config.getProperty("JDBC_PASSWORD", ""));
+                                       config.setProperty("javax.persistence.jdbc.password", CryptoUtils.decryptTxtNoExStr(config.getProperty("javax.persistence.jdbc.password", "")));
                                        return config;
 
                                } catch (IOException e) {                                       
                                        logger.error("Error porcessing Config file will be unable to create Health Check" + e);
                                }catch(Exception e){
                                        logger.error("Error getPropertiesValue on TIME_FRAME", e);
-                                       logger.debug("Error getPropertiesValue on TIME_FRAME, so use its default value:" + TIME_FRAME + " days");
+                                       logger.debug("Error getPropertiesValue on TIME_FRAME, so use its default value:" + defaultTimeFrame + " days");
                                }                               
                        }
 
@@ -829,13 +826,13 @@ public class ParseLog {
        }
        
        public static Connection getDbConnection(){
-               return dbConnection(JDBC_DRIVER, JDBC_URL, JDBC_USER,JDBC_PASSWORD);
+               return dbConnection(jdbcDriver, jdbcUrl, jdbcUser,jdbcPassword);
        }
        private static void startCleanUp(){
-               Connection conn = dbConnection(JDBC_DRIVER, JDBC_URL, JDBC_USER,JDBC_PASSWORD);
-               CleanUpSystemLogDB cleanUp = new CleanUpSystemLogDB(conn, TIME_FRAME); 
+               Connection conn = dbConnection(jdbcDriver, jdbcUrl, jdbcUser,jdbcPassword);
+               CleanUpSystemLogDB cleanUp = new CleanUpSystemLogDB(conn, defaultTimeFrame); 
                Timer timer = new Timer(true);
-               timer.scheduleAtFixedRate(cleanUp, TIMER_DELAY_TIME, CHECK_INTERVAL);
+               timer.scheduleAtFixedRate(cleanUp, TIMER_DELAY_TIME, checkInterval);
                logger.info("startCleanUp begins! : " + new Date());
        }
 }
\ No newline at end of file