summaryrefslogtreecommitdiff
path: root/src/main/java/io/trygvis/appsh/booter/jetty/Main.java
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-10-23 23:23:47 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-10-23 23:23:47 +0200
commitbd96f0ee905af27a408eb202b8ccb08c6ee4355f (patch)
treee556efab6a49239459a1c45261a1aed90824ee9f /src/main/java/io/trygvis/appsh/booter/jetty/Main.java
parentafd6cd235449af5387d5b243b1d57b8824e4b893 (diff)
downloadapp.sh-booter-jetty-bd96f0ee905af27a408eb202b8ccb08c6ee4355f.tar.gz
app.sh-booter-jetty-bd96f0ee905af27a408eb202b8ccb08c6ee4355f.tar.bz2
app.sh-booter-jetty-bd96f0ee905af27a408eb202b8ccb08c6ee4355f.tar.xz
app.sh-booter-jetty-bd96f0ee905af27a408eb202b8ccb08c6ee4355f.zip
o Renaming packages.
Diffstat (limited to 'src/main/java/io/trygvis/appsh/booter/jetty/Main.java')
-rw-r--r--src/main/java/io/trygvis/appsh/booter/jetty/Main.java95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/main/java/io/trygvis/appsh/booter/jetty/Main.java b/src/main/java/io/trygvis/appsh/booter/jetty/Main.java
new file mode 100644
index 0000000..d3920c7
--- /dev/null
+++ b/src/main/java/io/trygvis/appsh/booter/jetty/Main.java
@@ -0,0 +1,95 @@
+package io.trygvis.appsh.booter.jetty;
+
+import org.eclipse.jetty.util.IO;
+
+import java.io.*;
+import java.util.Map;
+import java.util.Properties;
+
+import static java.lang.Integer.parseInt;
+
+public class Main {
+
+ /**
+ * The file that this booter will read it's configuration from.
+ */
+ public static final String PROPERTIES_FILE = "etc/booter.properties";
+
+ public static void main(String[] args) throws Exception {
+ File basedir = new File(System.getProperty("basedir", new File("").getAbsolutePath()));
+
+ File booterPropertiesFile = new File(basedir, PROPERTIES_FILE);
+
+ Properties properties = new Properties();
+ InputStream is = null;
+ try {
+ is = new FileInputStream(booterPropertiesFile);
+ properties.load(new InputStreamReader(is, "utf-8"));
+ } catch (FileNotFoundException e) {
+ System.err.println("Can't read: " + booterPropertiesFile);
+ System.exit(1);
+ } catch (IOException e) {
+ System.err.println("Error reading: " + booterPropertiesFile);
+ System.exit(1);
+ } finally {
+ IO.close(is);
+ }
+
+ setStreams(basedir, properties);
+
+ JettyWebServer server;
+ try {
+ server = new JettyWebServer();
+ server.setBasedir(basedir);
+
+ for (Map.Entry<Object, Object> entry : properties.entrySet()) {
+ String key = entry.getKey().toString();
+ String value = entry.getValue().toString();
+
+ if (key.startsWith("context.")) {
+ server.addContext(key.substring(8), new File(basedir, value));
+ }
+ }
+
+ String httpPort = properties.getProperty("httpPort", System.getenv("httpPort"));
+ if (httpPort != null) {
+ server.setHttpPort(parseInt(httpPort));
+ }
+ } catch (Exception e) {
+ System.err.println("Error while configuring Jetty.");
+ e.printStackTrace();
+ System.exit(1);
+ return;
+ }
+
+ try {
+ server.run();
+ } catch (Exception e) {
+ System.err.println("Error while starting Jetty.");
+ e.printStackTrace();
+ System.exit(1);
+ }
+ }
+
+ private static void setStreams(File basedir, Properties properties) throws IOException {
+ String logS = properties.getProperty("log");
+
+ if(logS == null) {
+ return;
+ }
+
+ File log = new File(basedir, logS);
+
+ if(!log.getParentFile().isDirectory()) {
+ if(!log.getParentFile().mkdirs()) {
+ System.err.println("Unable to create directory: " + log.getAbsolutePath());
+ System.exit(1);
+ }
+ }
+
+ PrintStream writer = new PrintStream(new FileOutputStream(log));
+
+ System.setOut(writer);
+ System.setErr(writer);
+ }
+}