// Path: src/main/java/org/apache/storm/hdfs/bolt/format/FileNameFormat.java
// public interface FileNameFormat extends Serializable {
// 
//     void prepare(Map conf, TopologyContext topologyContext);
// 
//     /**
//      * Returns the filename the HdfsBolt will create.
//      * @param rotation the current file rotation number (incremented on every rotation)
//      * @param timeStamp current time in milliseconds when the rotation occurs
//      * @return
//      */
//     String getName(long rotation, long timeStamp);
// 
//     String getPath();
// }
// 
// Path: src/main/java/org/apache/storm/hdfs/bolt/rotation/FileRotationPolicy.java
// public interface FileRotationPolicy extends Serializable {
//     /**
//      * Called for every tuple the HdfsBolt executes.
//      *
//      * @param tuple The tuple executed.
//      * @param offset current offset of file being written
//      * @return true if a file rotation should be performed
//      */
//     boolean mark(Tuple tuple, long offset);
// 
// 
//     /**
//      * Called after the HdfsBolt rotates a file.
//      *
//      */
//     void reset();
// }
// 
// Path: src/main/java/org/apache/storm/hdfs/bolt/rotation/TimedRotationPolicy.java
// public class TimedRotationPolicy implements FileRotationPolicy {
// 
//     public static enum TimeUnit {
// 
//         SECONDS((long)1000),
//         MINUTES((long)1000*60),
//         HOURS((long)1000*60*60),
//         DAYS((long)1000*60*60*24);
// 
//         private long milliSeconds;
// 
//         private TimeUnit(long milliSeconds){
//             this.milliSeconds = milliSeconds;
//         }
// 
//         public long getMilliSeconds(){
//             return milliSeconds;
//         }
//     }
// 
//     private long interval;
// 
//     public TimedRotationPolicy(float count, TimeUnit units){
//         this.interval = (long)(count * units.getMilliSeconds());
//     }
// 
// 
//     /**
//      * Called for every tuple the HdfsBolt executes.
//      *
//      * @param tuple  The tuple executed.
//      * @param offset current offset of file being written
//      * @return true if a file rotation should be performed
//      */
//     @Override
//     public boolean mark(Tuple tuple, long offset) {
//         return false;
//     }
// 
//     /**
//      * Called after the HdfsBolt rotates a file.
//      */
//     @Override
//     public void reset() {
// 
//     }
// 
//     public long getInterval(){
//         return this.interval;
//     }
// }
// 
// Path: src/main/java/org/apache/storm/hdfs/bolt/sync/SyncPolicy.java
// public interface SyncPolicy extends Serializable {
//     /**
//      * Called for every tuple the HdfsBolt executes.
//      *
//      * @param tuple The tuple executed.
//      * @param offset current offset for the file being written
//      * @return true if a sync should be performed
//      */
//     boolean mark(Tuple tuple, long offset);
// 
// 
//     /**
//      * Called after the HdfsBolt performs a sync.
//      *
//      */
//     void reset();
// 
// }
// 
// Path: src/main/java/org/apache/storm/hdfs/common/rotation/RotationAction.java
// public interface RotationAction extends Serializable {
//     void execute(FileSystem fileSystem, Path filePath) throws IOException;
// }
// 
// Path: src/main/java/org/apache/storm/hdfs/common/security/HdfsSecurityUtil.java
// public class HdfsSecurityUtil {
//     public static final String STORM_KEYTAB_FILE_KEY = "hdfs.keytab.file";
//     public static final String STORM_USER_NAME_KEY = "hdfs.kerberos.principal";
// 
//     public static void login(Map conf, Configuration hdfsConfig) throws IOException {
//         if (UserGroupInformation.isSecurityEnabled()) {
//             String keytab = (String) conf.get(STORM_KEYTAB_FILE_KEY);
//             if (keytab != null) {
//               hdfsConfig.set(STORM_KEYTAB_FILE_KEY, keytab);
//             }
//             String userName = (String) conf.get(STORM_USER_NAME_KEY);
//             if (userName != null) {
//               hdfsConfig.set(STORM_USER_NAME_KEY, userName);
//             }
//             SecurityUtil.login(hdfsConfig, STORM_KEYTAB_FILE_KEY, STORM_USER_NAME_KEY);
//         }
//     }
// }


import backtype.storm.task.OutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichBolt;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.storm.hdfs.bolt.format.FileNameFormat;
import org.apache.storm.hdfs.bolt.rotation.FileRotationPolicy;
import org.apache.storm.hdfs.bolt.rotation.TimedRotationPolicy;
import org.apache.storm.hdfs.bolt.sync.SyncPolicy;
import org.apache.storm.hdfs.common.rotation.RotationAction;
import org.apache.storm.hdfs.common.security.HdfsSecurityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
    protected transient Object writeLock;
    protected transient Timer rotationTimer; // only used for TimedRotationPolicy

    protected transient Configuration hdfsConfig;

    protected void rotateOutputFile() throws IOException {
        LOG.info("Rotating output file...");
        long start = System.currentTimeMillis();
        synchronized (this.writeLock) {
            closeOutputFile();
            this.rotation++;

            Path newFile = createOutputFile();
            LOG.info("Performing {} file rotation actions.", this.rotationActions.size());
            for (RotationAction action : this.rotationActions) {
                action.execute(this.fs, this.currentFile);
            }
            this.currentFile = newFile;
        }
        long time = System.currentTimeMillis() - start;
        LOG.info("File rotation took {} ms.", time);
    }

    /**
     * Marked as final to prevent override. Subclasses should implement the doPrepare() method.
     * @param conf
     * @param topologyContext
     * @param collector
     */
    public final void prepare(Map conf, TopologyContext topologyContext, OutputCollector collector){
// Path: src/main/java/org/apache/storm/hdfs/bolt/format/FileNameFormat.java
// public interface FileNameFormat extends Serializable {
// 
//     void prepare(Map conf, TopologyContext topologyContext);
// 
//     /**
//      * Returns the filename the HdfsBolt will create.
//      * @param rotation the current file rotation number (incremented on every rotation)
//      * @param timeStamp current time in milliseconds when the rotation occurs
//      * @return
//      */
//     String getName(long rotation, long timeStamp);
// 
//     String getPath();
// }
// 
// Path: src/main/java/org/apache/storm/hdfs/bolt/rotation/FileRotationPolicy.java
// public interface FileRotationPolicy extends Serializable {
//     /**
//      * Called for every tuple the HdfsBolt executes.
//      *
//      * @param tuple The tuple executed.
//      * @param offset current offset of file being written
//      * @return true if a file rotation should be performed
//      */
//     boolean mark(Tuple tuple, long offset);
// 
// 
//     /**
//      * Called after the HdfsBolt rotates a file.
//      *
//      */
//     void reset();
// }
// 
// Path: src/main/java/org/apache/storm/hdfs/bolt/rotation/TimedRotationPolicy.java
// public class TimedRotationPolicy implements FileRotationPolicy {
// 
//     public static enum TimeUnit {
// 
//         SECONDS((long)1000),
//         MINUTES((long)1000*60),
//         HOURS((long)1000*60*60),
//         DAYS((long)1000*60*60*24);
// 
//         private long milliSeconds;
// 
//         private TimeUnit(long milliSeconds){
//             this.milliSeconds = milliSeconds;
//         }
// 
//         public long getMilliSeconds(){
//             return milliSeconds;
//         }
//     }
// 
//     private long interval;
// 
//     public TimedRotationPolicy(float count, TimeUnit units){
//         this.interval = (long)(count * units.getMilliSeconds());
//     }
// 
// 
//     /**
//      * Called for every tuple the HdfsBolt executes.
//      *
//      * @param tuple  The tuple executed.
//      * @param offset current offset of file being written
//      * @return true if a file rotation should be performed
//      */
//     @Override
//     public boolean mark(Tuple tuple, long offset) {
//         return false;
//     }
// 
//     /**
//      * Called after the HdfsBolt rotates a file.
//      */
//     @Override
//     public void reset() {
// 
//     }
// 
//     public long getInterval(){
//         return this.interval;
//     }
// }
// 
// Path: src/main/java/org/apache/storm/hdfs/bolt/sync/SyncPolicy.java
// public interface SyncPolicy extends Serializable {
//     /**
//      * Called for every tuple the HdfsBolt executes.
//      *
//      * @param tuple The tuple executed.
//      * @param offset current offset for the file being written
//      * @return true if a sync should be performed
//      */
//     boolean mark(Tuple tuple, long offset);
// 
// 
//     /**
//      * Called after the HdfsBolt performs a sync.
//      *
//      */
//     void reset();
// 
// }
// 
// Path: src/main/java/org/apache/storm/hdfs/common/rotation/RotationAction.java
// public interface RotationAction extends Serializable {
//     void execute(FileSystem fileSystem, Path filePath) throws IOException;
// }
// 
// Path: src/main/java/org/apache/storm/hdfs/common/security/HdfsSecurityUtil.java
// public class HdfsSecurityUtil {
//     public static final String STORM_KEYTAB_FILE_KEY = "hdfs.keytab.file";
//     public static final String STORM_USER_NAME_KEY = "hdfs.kerberos.principal";
// 
//     public static void login(Map conf, Configuration hdfsConfig) throws IOException {
//         if (UserGroupInformation.isSecurityEnabled()) {
//             String keytab = (String) conf.get(STORM_KEYTAB_FILE_KEY);
//             if (keytab != null) {
//               hdfsConfig.set(STORM_KEYTAB_FILE_KEY, keytab);
//             }
//             String userName = (String) conf.get(STORM_USER_NAME_KEY);
//             if (userName != null) {
//               hdfsConfig.set(STORM_USER_NAME_KEY, userName);
//             }
//             SecurityUtil.login(hdfsConfig, STORM_KEYTAB_FILE_KEY, STORM_USER_NAME_KEY);
//         }
//     }
// }


// Path: src/main/java/org/apache/storm/hdfs/bolt/AbstractHdfsBolt.java
import backtype.storm.task.OutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichBolt;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.storm.hdfs.bolt.format.FileNameFormat;
import org.apache.storm.hdfs.bolt.rotation.FileRotationPolicy;
import org.apache.storm.hdfs.bolt.rotation.TimedRotationPolicy;
import org.apache.storm.hdfs.bolt.sync.SyncPolicy;
import org.apache.storm.hdfs.common.rotation.RotationAction;
import org.apache.storm.hdfs.common.security.HdfsSecurityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

    protected transient Object writeLock;
    protected transient Timer rotationTimer; // only used for TimedRotationPolicy

    protected transient Configuration hdfsConfig;

    protected void rotateOutputFile() throws IOException {
        LOG.info("Rotating output file...");
        long start = System.currentTimeMillis();
        synchronized (this.writeLock) {
            closeOutputFile();
            this.rotation++;

            Path newFile = createOutputFile();
            LOG.info("Performing {} file rotation actions.", this.rotationActions.size());
            for (RotationAction action : this.rotationActions) {
                action.execute(this.fs, this.currentFile);
            }
            this.currentFile = newFile;
        }
        long time = System.currentTimeMillis() - start;
        LOG.info("File rotation took {} ms.", time);
    }

    /**
     * Marked as final to prevent override. Subclasses should implement the doPrepare() method.
     * @param conf
     * @param topologyContext
     * @param collector
     */
    public final void prepare(Map conf, TopologyContext topologyContext, OutputCollector collector){
        this.writeLock = new Object();