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

com.dangdang.config.service.file.protocol.LocalFileProtocol Maven / Gradle / Ivy

package com.dangdang.config.service.file.protocol;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchService;

import com.dangdang.config.service.exception.InvalidPathException;
import com.dangdang.config.service.file.FileChangeEventListener;
import com.dangdang.config.service.file.FileConfigGroup;
import com.dangdang.config.service.file.FileLocation;

/**
 * @author Yuxuan Wang
 *
 */
public abstract class LocalFileProtocol implements Protocol {

	private WatchService watcher;

	@Override
	public final byte[] read(FileLocation location) throws InvalidPathException {
		try {
			Path path = getPath(location);
			if (!Files.exists(path)) {
				throw new InvalidPathException("The file is not exists.");
			}
			return Files.readAllBytes(path);
		} catch (IOException e) {
			throw new InvalidPathException(e);
		}
	}

	@Override
	public final void watch(FileLocation location, FileConfigGroup fileConfigGroup) throws InvalidPathException {
		// Register file change listener
		try {
			watcher = FileSystems.getDefault().newWatchService();
			Path path = getPath(location).toAbsolutePath();

			path.getParent().register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
			new Thread(new FileChangeEventListener(watcher, fileConfigGroup, path)).start();
		} catch (IOException e) {
			throw new InvalidPathException(e);
		}
	}

	protected abstract Path getPath(FileLocation location) throws InvalidPathException;

	@Override
	public void close() throws IOException {
		if (watcher != null) {
			watcher.close();
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy