[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-prov / src / main / java / com / att / research / datarouter / provisioning / utils / PurgeLogDirTask.java
diff --git a/datarouter-prov/src/main/java/com/att/research/datarouter/provisioning/utils/PurgeLogDirTask.java b/datarouter-prov/src/main/java/com/att/research/datarouter/provisioning/utils/PurgeLogDirTask.java
new file mode 100644 (file)
index 0000000..b705e6f
--- /dev/null
@@ -0,0 +1,70 @@
+/*******************************************************************************\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 com.att.research.datarouter.provisioning.utils;\r
+\r
+import java.io.File;\r
+import java.util.Properties;\r
+import java.util.TimerTask;\r
+\r
+/**\r
+ * This class provides a {@link TimerTask} that purges old logfiles\r
+ * (older than the number of days specified by the com.att.research.datarouter.provserver.logretention property).\r
+ * @author Robert Eby\r
+ * @version $Id: PurgeLogDirTask.java,v 1.2 2013/07/05 13:48:05 eby Exp $\r
+ */\r
+public class PurgeLogDirTask extends TimerTask {\r
+       private static final long ONEDAY = 86400000L;\r
+\r
+       private final String logdir;\r
+       private final long interval;\r
+\r
+       public PurgeLogDirTask() {\r
+               Properties p = (new DB()).getProperties();\r
+               logdir   = p.getProperty("com.att.research.datarouter.provserver.accesslog.dir");\r
+               String s = p.getProperty("com.att.research.datarouter.provserver.logretention", "30");\r
+               long n = 30;\r
+               try {\r
+                       n = Long.parseLong(s);\r
+               } catch (NumberFormatException e) {\r
+                       // ignore\r
+               }\r
+               interval = n * ONEDAY;\r
+       }\r
+       @Override\r
+       public void run() {\r
+               try {\r
+                       File dir = new File(logdir);\r
+                       if (dir.exists()) {\r
+                               long exptime = System.currentTimeMillis() - interval;\r
+                               for (File logfile : dir.listFiles()) {\r
+                                       if (logfile.lastModified() < exptime)\r
+                                               logfile.delete();\r
+                               }\r
+                       }\r
+               } catch (Exception e) {\r
+                       e.printStackTrace();\r
+               }\r
+       }\r
+}\r