All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.blade.kit.reload.FileChangeDetector Maven / Gradle / Ivy

There is a newer version: 2.0.15.RELEASE
Show newest version
package com.blade.kit.reload;

import com.blade.Environment;
import com.blade.mvc.Const;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

/**
 * Created by Eddie Wang on 12/25/17.
 */
@Slf4j
public class FileChangeDetector {
    private WatchService watcher;
    private Map pathMap = new HashMap<>();

    public FileChangeDetector(String dirPath) throws IOException{
        watcher = FileSystems.getDefault().newWatchService();
        registerAll(Paths.get(dirPath));
    }

    private void register(Path dir) throws IOException{
        WatchKey key = dir.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
        pathMap.put(key,dir);
    }

    private void registerAll(Path dir) throws  IOException{
        Files.walkFileTree(dir, new SimpleFileVisitor(){
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                register(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    }

    public void processEvent(BiConsumer, Path> processor){
        for(;;){
            WatchKey key;
            try{
                key = watcher.take();
            }catch (InterruptedException e) {
                return;
            }

            Path dir = pathMap.get(key);
            for (WatchEvent event: key.pollEvents()){
                WatchEvent.Kind kind = event.kind();
                Path filePath = dir.resolve(((WatchEvent)event).context());

                if(Files.isDirectory(filePath)) continue;
                log.info("File {} changes detected!",filePath.toString());
                //copy updated files to target
                processor.accept(kind, filePath);
            }
            key.reset();
        }
    }

    public static Path getDestPath(Path src, Environment env){
        String templateDir = env.get(Const.ENV_KEY_TEMPLATE_PATH,"/templates");
        Optional staticDir =  env.get(Const.ENV_KEY_STATIC_DIRS);
        List templateOrStaticDirKeyword = new ArrayList<>();
        templateOrStaticDirKeyword.add(templateDir);
        if(staticDir.isPresent()){
            templateOrStaticDirKeyword.addAll(Arrays.asList(staticDir.get().split(",")));
        }else{
            templateOrStaticDirKeyword.addAll(Const.DEFAULT_STATICS);
        }

        List result = templateOrStaticDirKeyword.stream().filter(dir -> src.toString().indexOf(dir)!=-1).collect(Collectors.toList());
        if(result.size()!=1){
            log.info("Cannot get dest dir");
            return  null;
        }
        String key = (String)result.get(0);
        log.info(Const.CLASSPATH + src.toString().substring(src.toString().indexOf(key)));
        return Paths.get(Const.CLASSPATH + src.toString().substring(src.toString().indexOf(key)));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy