aboutsummaryrefslogtreecommitdiffstats
path: root/ncomp-utils-java-extra/src
diff options
context:
space:
mode:
authorlj1412 <lji@research.att.com>2017-02-14 15:11:54 +0000
committerlj1412 <lji@research.att.com>2017-02-14 15:11:56 +0000
commit8003361c89f8f497f1f2b327c652487f03b86267 (patch)
tree5fc8b05ecb2e721a0b00c380b946a9bb661f6c60 /ncomp-utils-java-extra/src
parent5cf23c1465091439bb06cb78384b1dc2597a6f5e (diff)
Init ncomp.utils
Change-Id: Ib092dff1d165296e2d86265137cf0b5ae8b20323 Signed-off-by: lj1412 <lji@research.att.com>
Diffstat (limited to 'ncomp-utils-java-extra/src')
-rw-r--r--ncomp-utils-java-extra/src/main/java/org/openecomp/ncomp/utils/extra/FileTail.java215
1 files changed, 215 insertions, 0 deletions
diff --git a/ncomp-utils-java-extra/src/main/java/org/openecomp/ncomp/utils/extra/FileTail.java b/ncomp-utils-java-extra/src/main/java/org/openecomp/ncomp/utils/extra/FileTail.java
new file mode 100644
index 0000000..623733d
--- /dev/null
+++ b/ncomp-utils-java-extra/src/main/java/org/openecomp/ncomp/utils/extra/FileTail.java
@@ -0,0 +1,215 @@
+
+/*-
+ * ============LICENSE_START==========================================
+ * OPENECOMP - DCAE
+ * ===================================================================
+ * Copyright (c) 2017 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END============================================
+ */
+
+package org.openecomp.ncomp.utils.extra;
+
+import java.io.File;
+import java.io.RandomAccessFile;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.json.JSONObject;
+
+import org.openecomp.ncomp.utils.FindFiles;
+import org.openecomp.ncomp.utils.FindFiles.ParameterizedFile;
+import org.openecomp.ncomp.utils.journaling.JournalingHashMap;
+import org.openecomp.ncomp.utils.logging.LoggingUtils;
+import org.openecomp.ncomp.webservice.utils.DateUtils;
+
+public class FileTail {
+ public static final Logger logger = Logger.getLogger(FileTail.class);
+ private String format;
+ private String directory;
+ private NewLineHandler handler;
+ private long checkFrequency = 60000; // milliseconds
+ private long checkNewFrequency = 300000; // milliseconds
+ private JournalingHashMap<Long> filePointerMap;
+ private long scanDuration = DateUtils.stringToDuration("2day");
+ private Object context;
+
+ public interface NewLineHandler {
+ void newLine(String file, String line, Object context);
+ void fixFilePermissions(File file);
+ }
+
+ @SuppressWarnings("unchecked")
+ public FileTail(String format, String directory, String scanFreq, String scanNewFreq, String scanDuration, NewLineHandler handler, Object context) {
+ super();
+ if (scanFreq != null)
+ checkFrequency = DateUtils.stringToDuration(scanFreq);
+ if (scanNewFreq != null)
+ checkNewFrequency = DateUtils.stringToDuration(scanNewFreq);
+ if (scanDuration != null)
+ this.scanDuration = DateUtils.stringToDuration(scanDuration);
+
+ this.format = format;
+ this.directory = directory;
+ this.handler = handler;
+ this.context = context;
+ filePointerMap = JournalingHashMap.create(new File(directory));
+ logger.info("initial status: " + getStatus().toString(2));
+ logger.info("Created: " + this);
+ new MonitorThread(format);
+ }
+ @SuppressWarnings("unchecked")
+ public FileTail(String filename, String directory, String scanFreq, NewLineHandler handler, Object context) {
+ if (scanFreq != null)
+ checkFrequency = DateUtils.stringToDuration(scanFreq);
+ this.directory = directory;
+ this.handler = handler;
+ this.context = context;
+ filePointerMap = JournalingHashMap.create(new File(directory));
+ if (filePointerMap.get(filename) == null) {
+ filePointerMap.put(filename, 0L);
+ }
+ new MonitorThread(filename);
+ }
+
+ @Override
+ public String toString() {
+ return "FileTail [format=" + format + ", directory=" + directory + ", checkFrequency="
+ + checkFrequency + ", checkNewFrequency=" + checkNewFrequency + "]";
+ }
+
+ private class MonitorThread implements Runnable {
+ public MonitorThread(String name) {
+ Thread t = new Thread(this, "filetail: " + name);
+ t.start();
+ }
+
+ @Override
+ public void run() {
+ long numIntervalsPerNewFileCheck = checkNewFrequency / checkFrequency;
+ long i = 0;
+ while (true) {
+ if ((i++ % numIntervalsPerNewFileCheck) == 0)
+ scanForNewFiles();
+ scanForNewLines();
+ try {
+ Thread.sleep(checkFrequency);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ return;
+ }
+ }
+ }
+ }
+
+ private void scanForNewFiles() {
+ if (format == null) return;
+ FindFiles ff = new FindFiles(format);
+ Date now = new Date();
+ HashMap<String, Long> map = new HashMap<String, Long>();
+ synchronized (this) {
+ for (ParameterizedFile f : ff.findFiles()) {
+ File f1 = new File(f.getFile());
+ if (f1.lastModified() + scanDuration < now.getTime()) continue;
+ if (filePointerMap.get(f.getFile()) == null) {
+ map.put(f.getFile(), 0L);
+ filePointerMap.put(f.getFile(), 0L);
+ logger.info("Found new file: " + f.getFile());
+ } else {
+ map.put(f.getFile(), filePointerMap.get(f.getFile()));
+ }
+ }
+ List<String> remove = new ArrayList<String>();
+ for (String f : filePointerMap.keySet()) {
+ if (map.get(f) == null) {
+ logger.info("Deleted file: " + f);
+ remove.add(f);
+ }
+ }
+ for (String f : remove) filePointerMap.remove(f);
+ filePointerMap.save();
+ }
+ }
+
+ private void scanForNewLines() {
+ logger.info("old status: " + getStatus().toString(2));
+ Set<String> files = filePointerMap.keySet();
+ for (String f : files) {
+ try {
+ long p;
+ synchronized (this) {
+ p = filePointerMap.get(f);
+ }
+ File file = new File(f);
+ long len = file.length();
+ if (len == p)
+ continue;
+ if (len < p) {
+ logger.info("Logfile reset. Restarted at start");
+ p = 0;
+ }
+ if (len > p) {
+ if (!file.canRead()) {
+ handler.fixFilePermissions(file);
+ }
+ if (!file.canRead()) {
+ LoggingUtils.dampingLogger(logger, Level.WARN, "Unable to read: " + file.getAbsolutePath());
+ continue;
+ }
+ RandomAccessFile rf = new RandomAccessFile(file, "r");
+ rf.seek(p);
+ String line = null;
+ while ((line = rf.readLine()) != null) {
+ if (line.length() == 0)
+ continue;
+ if (logger.isDebugEnabled())
+ logger.debug("New line from file: " + f + " " + line);
+ p = rf.getFilePointer();
+ try {
+ handler.newLine(file.getAbsolutePath(),line,context);
+ } catch (Exception e) {
+ logger.warn("Handler error: " + f + " " + e + " line=" + line);
+ e.printStackTrace();
+ }
+ }
+ rf.close();
+ }
+ synchronized (this) {
+ filePointerMap.put(f, p);
+ filePointerMap.save();
+ }
+ } catch (Exception e) {
+ e.printStackTrace(System.out);
+ logger.error("Scaning error: " + f + " " + e);
+ }
+ }
+ logger.info("new status: " + getStatus().toString(2));
+ }
+
+ public JSONObject getStatus() {
+ JSONObject o = new JSONObject();
+ synchronized (this) {
+ for (String f : filePointerMap.keySet()) {
+ o.put(f, filePointerMap.get(f));
+ }
+ }
+ return o;
+ }
+
+}