Example usage for java.nio.file DirectoryIteratorException getLocalizedMessage

List of usage examples for java.nio.file DirectoryIteratorException getLocalizedMessage

Introduction

In this page you can find the example usage for java.nio.file DirectoryIteratorException getLocalizedMessage.

Prototype

public String getLocalizedMessage() 

Source Link

Document

Creates a localized description of this throwable.

Usage

From source file:com.devti.JavaXMPPBot.JavaXMPPBot.java

@Override
public void init(DaemonContext context) throws DaemonInitException, Exception {
    // Get paths/* w w w. ja v a 2 s .  co m*/
    String[] args = context.getArguments();
    Path configsDir, logsDir;
    if (args.length > 0) {
        configsDir = Paths.get(args[0]);
    } else {
        configsDir = Paths.get(System.getProperty("user.home"), "JavaXMPPBot", "conf.d");
    }
    if (args.length > 1) {
        logsDir = Paths.get(args[1]);
    } else {
        logsDir = Paths.get(System.getProperty("user.home"), "JavaXMPPBot", "log.d");
    }
    if (!logsDir.toFile().exists()) {
        throw new DaemonInitException("Logs directory '" + logsDir + "' doesn't exist.");
    }

    // Get bot configs
    List<Path> botConfigs = new ArrayList<>();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(configsDir, "*.conf")) {
        for (Path entry : stream) {
            botConfigs.add(entry);
        }
    } catch (DirectoryIteratorException e) {
        throw new DaemonInitException(
                "Can't list configs directory '" + configsDir + "': " + e.getLocalizedMessage());
    }
    if (botConfigs.size() <= 0) {
        throw new DaemonInitException("There is no .conf files in '" + configsDir + "' directory.");
    }

    // Load bots
    for (Path config : botConfigs) {
        String id = config.getFileName().toString().replaceFirst(".conf$", "");
        Path log = Paths.get(logsDir.toString(), id + ".log");
        try {
            bots.add(new XMPPBot(id, config, new Log(log)));
        } catch (Exception e) {
            throw new DaemonInitException(
                    "Can't load a bot with config file '" + config + "': " + e.getLocalizedMessage());
        }
    }
}