package hcse.flume;

import java.util.List;

import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;

public class DateTimeSuffixInterceptor implements Interceptor {
    // private LRUMap<String, Integer> map;

    private int length = 13;
    private String sourceHeader = "basename";
    private String newHeader = "basename";

    @Override
    public void initialize() {
        // map = new LRUMap<String, Integer>(2048);
    }

    @Override
    public Event intercept(Event event) {
        String value = event.getHeaders().get(sourceHeader);

        if (value != null) {
            int start = value.lastIndexOf('.');
            if (start != -1) {
                start++;

                String tag = value.substring(start, start + length);

                event.getHeaders().put(newHeader, tag);
            }
        }

        return event;
    }

    @Override
    public List<Event> intercept(List<Event> events) {
        for (Event event : events) {
            String value = event.getHeaders().get(sourceHeader);

            if (value != null) {
                int start = value.lastIndexOf('.');
                if (start != -1) {
                    start++;

                    String tag = value.substring(start, start + length);

                    event.getHeaders().put(newHeader, tag);
                }
            }
        }

        return events;
    }

    @Override
    public void close() {

    }

    public static class Builder implements Interceptor.Builder {
        public Interceptor build() {
            return new DateTimeSuffixInterceptor();
        }

        @Override
        public void configure(Context context) {

        }
    }
}

@@ -150,16 +150,73 @@
 rceptor;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Apublic
@@ -269,24 +269,124 @@
 terceptor %7B%0A
+    private static final Logger logger = LoggerFactory.getLogger(DateTimeSuffixInterceptor.class);%0A%0A
     // priva
@@ -448,9 +448,9 @@
  = 1
-3
+5
 ;%0A  
@@ -537,16 +537,214 @@
 name%22;%0A%0A
+    DateTimeSuffixInterceptor(int maxlen, String sourceHeader, String newHeader) %7B%0A        this.length = maxlen;%0A        this.sourceHeader = sourceHeader;%0A        this.newHeader = newHeader;%0A    %7D%0A%0A
     @Ove
@@ -1083,32 +1083,123 @@
       start++;%0A%0A
+                int end = start + length;%0A%0A                if (end %3C value.length()) %7B%0A    
                 
@@ -1234,34 +1234,27 @@
 (start, 
-start + length);%0A%0A
+end);%0A%0A    
         
@@ -1293,32 +1293,50 @@
 ewHeader, tag);%0A
+                %7D%0A
             %7D%0A  
@@ -1434,32 +1434,76 @@
 vent%3E events) %7B%0A
+        int start = 0;%0A        int end = 0;%0A
         for (Eve
@@ -1630,36 +1630,32 @@
                 
-int 
 start = value.la
@@ -1727,32 +1727,127 @@
       start++;%0A%0A
+                    end = start + length;%0A%0A                    if (end %3C value.length()) %7B%0A    
                 
@@ -1890,26 +1890,19 @@
 rt, 
-start + length);%0A%0A
+end);%0A%0A    
     
@@ -1949,32 +1949,54 @@
 ewHeader, tag);%0A
+                    %7D%0A
                 
@@ -2178,35 +2178,365 @@
    p
-ublic Interceptor build() %7B
+rivate int maxlen = 15;%0A%0A        private String sourceHeader = Constants.DEFAULTHEADER;%0A        private String newHeader = Constants.DEFAULTHEADER;%0A%0A        public Interceptor build() %7B%0A            logger.info(String.format(%22Creating DateTimeSuffixInterceptor: maxlen=%25s,sourceHeader=%25s,newHeader=%25s%22,%0A                    maxlen, sourceHeader, newHeader));%0A
 %0A   
@@ -2581,16 +2581,47 @@
 rceptor(
+maxlen, sourceHeader, newHeader
 );%0A     
@@ -2693,26 +2693,637 @@
 text) %7B%0A
-%0A        %7D
+            maxlen = context.getInteger(Constants.MAXLEN, Constants.MAXLEN_DEFAULT);%0A%0A            sourceHeader = context.getString(Constants.SOURCEHEADER, Constants.DEFAULTHEADER);%0A            newHeader = context.getString(Constants.NEWHEADER, Constants.DEFAULTHEADER);%0A        %7D%0A    %7D%0A%0A    public static class Constants %7B%0A        public static final String MAXLEN = %22maxlen%22;%0A%0A        public static final int MAXLEN_DEFAULT = 15;%0A%0A        public static final String SOURCEHEADER = %22sourceHeader%22;%0A        public static final String NEWHEADER = %22newHeader%22;%0A%0A        public static final String DEFAULTHEADER = %22basename%22;
 %0A    %7D%0A%7D

change file name length.