From bd96f0ee905af27a408eb202b8ccb08c6ee4355f Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 23 Oct 2012 23:23:47 +0200 Subject: o Renaming packages. --- .../trygvis/appsh/booter/jetty/JettyWebServer.java | 108 +++++++++++++++++++++ .../java/io/trygvis/appsh/booter/jetty/Main.java | 95 ++++++++++++++++++ .../appsh/booter/jetty8/JettyWebServer.java | 108 --------------------- .../java/io/trygvis/appsh/booter/jetty8/Main.java | 95 ------------------ 4 files changed, 203 insertions(+), 203 deletions(-) create mode 100644 src/main/java/io/trygvis/appsh/booter/jetty/JettyWebServer.java create mode 100644 src/main/java/io/trygvis/appsh/booter/jetty/Main.java delete mode 100644 src/main/java/io/trygvis/appsh/booter/jetty8/JettyWebServer.java delete mode 100644 src/main/java/io/trygvis/appsh/booter/jetty8/Main.java diff --git a/src/main/java/io/trygvis/appsh/booter/jetty/JettyWebServer.java b/src/main/java/io/trygvis/appsh/booter/jetty/JettyWebServer.java new file mode 100644 index 0000000..9ee8e24 --- /dev/null +++ b/src/main/java/io/trygvis/appsh/booter/jetty/JettyWebServer.java @@ -0,0 +1,108 @@ +package io.trygvis.appsh.booter.jetty; + +import org.eclipse.jetty.server.Connector; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.server.handler.ContextHandlerCollection; +import org.eclipse.jetty.server.nio.SelectChannelConnector; +import org.eclipse.jetty.util.log.StdErrLog; +import org.eclipse.jetty.webapp.WebAppContext; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class JettyWebServer { + private File basedir = new File("").getAbsoluteFile(); + private File tmp, extraClasspath; + private int httpPort = 8080; + + List contexts = new ArrayList(); + + public void setBasedir(File basedir) { + this.basedir = basedir; + } + + public void setHttpPort(int port) { + this.httpPort = port; + } + + public Context addContext(String contextPath, File webapp) throws Exception { + Context context = new Context(); + context.setContextPath(contextPath); + context.setWebapp(webapp); + contexts.add(context); + return context; + } + + public void run() throws Exception { + tmp = new File(basedir, "tmp"); + + if (!tmp.isDirectory() && !tmp.mkdirs()) { + throw new IOException("Could not create temp directory: " + tmp); + } + + extraClasspath = new File(basedir, "etc"); + + if (!extraClasspath.isDirectory()) { + extraClasspath = null; + } + + System.setProperty("org.mortbay.log.class", StdErrLog.class.getName()); + + Server server = new Server(); + if (httpPort != 0) { + Connector connector = new SelectChannelConnector(); + connector.setPort(httpPort); + server.addConnector(connector); + } + + ContextHandlerCollection handler = new ContextHandlerCollection(); + server.setHandler(handler); + for (Context context : contexts) { + handler.addHandler(context.toJetty()); + } + + server.start(); + server.join(); + } + + public class Context { + private File webapp; + private String contextPath; + + public void setWebapp(File webapp) throws IOException { + if (!webapp.exists()) { + throw new IOException("File has to exist: " + webapp); + } + this.webapp = webapp; + } + + public void setContextPath(String contextPath) { + if (!contextPath.startsWith("/")) { + throw new RuntimeException("The context path has to start with '/'."); + } + + this.contextPath = contextPath; + } + + public ContextHandler toJetty() { + WebAppContext context = new WebAppContext(); + context.setContextPath(this.contextPath); + context.setWar(webapp.getAbsolutePath()); + if (extraClasspath != null) { + context.setExtraClasspath(extraClasspath.getAbsolutePath()); + } + + context.setExtractWAR(true); + // TODO: Should the temp directory be cleaned out before starting? + String dir = contextPath.substring(1); + if (dir.length() == 0) { + dir = "ROOT"; + } + context.setTempDirectory(new File(tmp, dir)); + return context; + } + } +} 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 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); + } +} diff --git a/src/main/java/io/trygvis/appsh/booter/jetty8/JettyWebServer.java b/src/main/java/io/trygvis/appsh/booter/jetty8/JettyWebServer.java deleted file mode 100644 index b0a3fef..0000000 --- a/src/main/java/io/trygvis/appsh/booter/jetty8/JettyWebServer.java +++ /dev/null @@ -1,108 +0,0 @@ -package io.trygvis.appsh.booter.jetty8; - -import org.eclipse.jetty.server.Connector; -import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.server.handler.ContextHandler; -import org.eclipse.jetty.server.handler.ContextHandlerCollection; -import org.eclipse.jetty.server.nio.SelectChannelConnector; -import org.eclipse.jetty.util.log.StdErrLog; -import org.eclipse.jetty.webapp.WebAppContext; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -public class JettyWebServer { - private File basedir = new File("").getAbsoluteFile(); - private File tmp, extraClasspath; - private int httpPort = 8080; - - List contexts = new ArrayList(); - - public void setBasedir(File basedir) { - this.basedir = basedir; - } - - public void setHttpPort(int port) { - this.httpPort = port; - } - - public Context addContext(String contextPath, File webapp) throws Exception { - Context context = new Context(); - context.setContextPath(contextPath); - context.setWebapp(webapp); - contexts.add(context); - return context; - } - - public void run() throws Exception { - tmp = new File(basedir, "tmp"); - - if (!tmp.isDirectory() && !tmp.mkdirs()) { - throw new IOException("Could not create temp directory: " + tmp); - } - - extraClasspath = new File(basedir, "etc"); - - if (!extraClasspath.isDirectory()) { - extraClasspath = null; - } - - System.setProperty("org.mortbay.log.class", StdErrLog.class.getName()); - - Server server = new Server(); - if (httpPort != 0) { - Connector connector = new SelectChannelConnector(); - connector.setPort(httpPort); - server.addConnector(connector); - } - - ContextHandlerCollection handler = new ContextHandlerCollection(); - server.setHandler(handler); - for (Context context : contexts) { - handler.addHandler(context.toJetty()); - } - - server.start(); - server.join(); - } - - public class Context { - private File webapp; - private String contextPath; - - public void setWebapp(File webapp) throws IOException { - if (!webapp.exists()) { - throw new IOException("File has to exist: " + webapp); - } - this.webapp = webapp; - } - - public void setContextPath(String contextPath) { - if (!contextPath.startsWith("/")) { - throw new RuntimeException("The context path has to start with '/'."); - } - - this.contextPath = contextPath; - } - - public ContextHandler toJetty() { - WebAppContext context = new WebAppContext(); - context.setContextPath(this.contextPath); - context.setWar(webapp.getAbsolutePath()); - if (extraClasspath != null) { - context.setExtraClasspath(extraClasspath.getAbsolutePath()); - } - - context.setExtractWAR(true); - // TODO: Should the temp directory be cleaned out before starting? - String dir = contextPath.substring(1); - if (dir.length() == 0) { - dir = "ROOT"; - } - context.setTempDirectory(new File(tmp, dir)); - return context; - } - } -} diff --git a/src/main/java/io/trygvis/appsh/booter/jetty8/Main.java b/src/main/java/io/trygvis/appsh/booter/jetty8/Main.java deleted file mode 100644 index 15279dc..0000000 --- a/src/main/java/io/trygvis/appsh/booter/jetty8/Main.java +++ /dev/null @@ -1,95 +0,0 @@ -package io.trygvis.appsh.booter.jetty8; - -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 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); - } -} -- cgit v1.2.3