2 * Rhynodge - Starter.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.rhynodge.engine;
20 import java.io.FileInputStream;
21 import java.io.IOException;
23 import net.pterodactylus.rhynodge.actions.EmailAction;
24 import net.pterodactylus.rhynodge.loader.ChainWatcher;
25 import net.pterodactylus.rhynodge.states.StateManager;
27 import com.lexicalscope.jewel.cli.CliFactory;
28 import com.lexicalscope.jewel.cli.Option;
31 * Rhynodge main starter class.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class Starter {
38 * JVM main entry method.
41 * Command-line arguments
43 public static void main(String... arguments) throws IOException {
45 /* parse command line. */
46 Parameters parameters = CliFactory.parseArguments(Parameters.class, arguments);
47 Configuration configuration = loadConfiguration(parameters.getConfigurationFile());
49 /* create the state manager. */
50 StateManager stateManager = new StateManager(parameters.getStateDirectory());
52 /* create the engine. */
53 Engine engine = new Engine(stateManager, createErrorEmailAction(configuration));
55 /* start a watcher. */
56 ChainWatcher chainWatcher = new ChainWatcher(engine, parameters.getChainDirectory());
60 private static Configuration loadConfiguration(String configurationFile) throws IOException {
61 try (FileInputStream configInputStream = new FileInputStream(configurationFile)) {
62 return Configuration.from(configInputStream);
66 private static EmailAction createErrorEmailAction(Configuration configuration) {
67 return new EmailAction(configuration.getSmtpHostname(), configuration.getErrorEmailSender(), configuration.getErrorEmailRecipient());
71 * Definition of the command-line parameters.
73 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
75 private static interface Parameters {
78 * Returns the directory to watch for chains.
80 * @return The chain directory
82 @Option(defaultValue = "chains", longName = "chains", shortName = "c", description = "The directory to watch for chains")
83 String getChainDirectory();
86 * Returns the directory to store states in.
88 * @return The states directory
90 @Option(defaultValue = "states", longName = "states", shortName = "s", description = "The directory to store states in")
91 String getStateDirectory();
93 @Option(defaultValue = "/etc/rhynodge/rhynodge.json", longName = "config", shortName = "C", description = "The name of the configuration file")
94 String getConfigurationFile();