05aff64092c8f4ee5421720884b632064b1df26b
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / engine / Starter.java
1 /*
2  * Rhynodge - Starter.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.rhynodge.engine;
19
20 import java.io.FileInputStream;
21 import java.io.IOException;
22
23 import net.pterodactylus.rhynodge.actions.EmailAction;
24 import net.pterodactylus.rhynodge.loader.ChainWatcher;
25 import net.pterodactylus.rhynodge.states.StateManager;
26
27 import com.lexicalscope.jewel.cli.CliFactory;
28 import com.lexicalscope.jewel.cli.Option;
29
30 /**
31  * Rhynodge main starter class.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class Starter {
36
37         /**
38          * JVM main entry method.
39          *
40          * @param arguments
41          *            Command-line arguments
42          */
43         public static void main(String... arguments) throws IOException {
44
45                 /* parse command line. */
46                 Parameters parameters = CliFactory.parseArguments(Parameters.class, arguments);
47                 Configuration configuration = loadConfiguration(parameters.getConfigurationFile());
48
49                 /* create the state manager. */
50                 StateManager stateManager = new StateManager(parameters.getStateDirectory());
51
52                 /* create the engine. */
53                 Engine engine = new Engine(stateManager, createErrorEmailAction(configuration));
54
55                 /* start a watcher. */
56                 ChainWatcher chainWatcher = new ChainWatcher(engine, parameters.getChainDirectory());
57                 chainWatcher.start();
58         }
59
60         private static Configuration loadConfiguration(String configurationFile) throws IOException {
61                 try (FileInputStream configInputStream = new FileInputStream(configurationFile)) {
62                         return Configuration.from(configInputStream);
63                 }
64         }
65
66         private static EmailAction createErrorEmailAction(Configuration configuration) {
67                 return new EmailAction(configuration.getSmtpHostname(), configuration.getErrorEmailSender(), configuration.getErrorEmailRecipient());
68         }
69
70         /**
71          * Definition of the command-line parameters.
72          *
73          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
74          */
75         private static interface Parameters {
76
77                 /**
78                  * Returns the directory to watch for chains.
79                  *
80                  * @return The chain directory
81                  */
82                 @Option(defaultValue = "chains", longName = "chains", shortName = "c", description = "The directory to watch for chains")
83                 String getChainDirectory();
84
85                 /**
86                  * Returns the directory to store states in.
87                  *
88                  * @return The states directory
89                  */
90                 @Option(defaultValue = "states", longName = "states", shortName = "s", description = "The directory to store states in")
91                 String getStateDirectory();
92
93                 @Option(defaultValue = "/etc/rhynodge/rhynodge.json", longName = "config", shortName = "C", description = "The name of the configuration file")
94                 String getConfigurationFile();
95
96         }
97
98 }