diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2012-10-07 18:50:48 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2012-10-07 18:50:48 +0200 |
commit | eea29a05abbd9a89a181455ac858173fc3640f93 (patch) | |
tree | 61140a115206e065dbd929c104787566829561c0 | |
download | app.sh-java-eea29a05abbd9a89a181455ac858173fc3640f93.tar.gz app.sh-java-eea29a05abbd9a89a181455ac858173fc3640f93.tar.bz2 app.sh-java-eea29a05abbd9a89a181455ac858173fc3640f93.tar.xz app.sh-java-eea29a05abbd9a89a181455ac858173fc3640f93.zip |
o Initial import of Java utils for app.sh.
-rw-r--r-- | .gitignore | 5 | ||||
-rw-r--r-- | booter-jetty8/pom.xml | 34 | ||||
-rw-r--r-- | booter-jetty8/src/main/java/io/trygvis/appsh/booter/jetty8/JettyWebServer.java | 108 | ||||
-rw-r--r-- | booter-jetty8/src/main/java/io/trygvis/appsh/booter/jetty8/Main.java | 95 | ||||
-rw-r--r-- | examples/app-a-bundle/pom.xml | 77 | ||||
-rw-r--r-- | examples/app-a-bundle/src/main/unix/files/root/etc/booter.properties | 2 | ||||
-rw-r--r-- | examples/app-a-bundle/src/main/unix/files/scripts/postinstall | 6 | ||||
-rw-r--r-- | examples/app-a-web/pom.xml | 11 | ||||
-rw-r--r-- | examples/app-a-web/src/main/webapp/WEB-INF/web.xml | 4 | ||||
-rw-r--r-- | examples/app-a-web/src/main/webapp/index.jspx | 10 | ||||
-rw-r--r-- | pom.xml | 36 |
11 files changed, 388 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a750dc8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +target +*.iml +.idea +.project +.classpath diff --git a/booter-jetty8/pom.xml b/booter-jetty8/pom.xml new file mode 100644 index 0000000..78987d6 --- /dev/null +++ b/booter-jetty8/pom.xml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>io.trygvis.appsh</groupId> + <artifactId>appsh-parent</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + <artifactId>booter-jetty8</artifactId> + <dependencies> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-server</artifactId> + <version>${versions.jetty8}</version> + </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-webapp</artifactId> + <version>${versions.jetty8}</version> + </dependency> + <dependency> + <groupId>org.eclipse.jetty.orbit</groupId> + <artifactId>org.apache.jasper.glassfish</artifactId> + <version>2.2.2.v201112011158</version> + </dependency> + <dependency> + <groupId>org.eclipse.jetty.orbit</groupId> + <artifactId>javax.el</artifactId> + <version>2.2.0.v201108011116</version> + </dependency> + </dependencies> +</project> diff --git a/booter-jetty8/src/main/java/io/trygvis/appsh/booter/jetty8/JettyWebServer.java b/booter-jetty8/src/main/java/io/trygvis/appsh/booter/jetty8/JettyWebServer.java new file mode 100644 index 0000000..b0a3fef --- /dev/null +++ b/booter-jetty8/src/main/java/io/trygvis/appsh/booter/jetty8/JettyWebServer.java @@ -0,0 +1,108 @@ +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<Context> contexts = new ArrayList<Context>(); + + 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/booter-jetty8/src/main/java/io/trygvis/appsh/booter/jetty8/Main.java b/booter-jetty8/src/main/java/io/trygvis/appsh/booter/jetty8/Main.java new file mode 100644 index 0000000..b69ff1d --- /dev/null +++ b/booter-jetty8/src/main/java/io/trygvis/appsh/booter/jetty8/Main.java @@ -0,0 +1,95 @@ +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<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("http.port"); + 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/examples/app-a-bundle/pom.xml b/examples/app-a-bundle/pom.xml new file mode 100644 index 0000000..63b4c5a --- /dev/null +++ b/examples/app-a-bundle/pom.xml @@ -0,0 +1,77 @@ +<project> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>io.trygvis.appsh</groupId> + <artifactId>appsh-parent</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + <groupId>io.trygvis.appsh.examples</groupId> + <artifactId>app-a-bundle</artifactId> + <packaging>unix-zip</packaging> + <repositories> + <repository> + <id>trygvis.io-snapshots</id> + <url>http://repo.trygvis.io/snapshots</url> + </repository> + </repositories> + <dependencies> + <dependency> + <groupId>io.trygvis.appsh</groupId> + <artifactId>booter-jetty8</artifactId> + <version>1.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>io.trygvis.appsh.examples</groupId> + <artifactId>app-a-web</artifactId> + <version>${project.version}</version> + <type>war</type> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>appassembler-maven-plugin</artifactId> + <version>1.1</version> + <executions> + <execution> + <phase>generate-resources</phase> + <goals> + <goal>assemble</goal> + </goals> + </execution> + </executions> + <configuration> + <programs> + <program> + <name>app-a</name> + <mainClass>io.trygvis.appsh.booter.jetty8.Main</mainClass> + </program> + </programs> + </configuration> + </plugin> + <plugin> + <groupId>no.arktekk.unix</groupId> + <artifactId>unix-maven-plugin</artifactId> + <version>1.0-alpha-6</version> + <extensions>true</extensions> + <configuration> + <contact>Trygve Laugstol</contact> + <assembly> + <copyDirectory> + <from>target/appassembler</from> + <to>root</to> + <excludes> + <exclude>**/*.bat</exclude> + </excludes> + </copyDirectory> + <copyArtifact> + <artifact>io.trygvis.appsh.examples:app-a-web:war</artifact> + <toFile>root/wars/app-a-web.war</toFile> + </copyArtifact> + </assembly> + </configuration> + </plugin> + </plugins> + </build> +</project> diff --git a/examples/app-a-bundle/src/main/unix/files/root/etc/booter.properties b/examples/app-a-bundle/src/main/unix/files/root/etc/booter.properties new file mode 100644 index 0000000..8c36e9c --- /dev/null +++ b/examples/app-a-bundle/src/main/unix/files/root/etc/booter.properties @@ -0,0 +1,2 @@ +http.port=3001 +context./=wars/app-a-web.war diff --git a/examples/app-a-bundle/src/main/unix/files/scripts/postinstall b/examples/app-a-bundle/src/main/unix/files/scripts/postinstall new file mode 100644 index 0000000..381dff7 --- /dev/null +++ b/examples/app-a-bundle/src/main/unix/files/scripts/postinstall @@ -0,0 +1,6 @@ +#!/bin/bash + +echo "pwd: " +pwd +echo "env:" +env diff --git a/examples/app-a-web/pom.xml b/examples/app-a-web/pom.xml new file mode 100644 index 0000000..4258644 --- /dev/null +++ b/examples/app-a-web/pom.xml @@ -0,0 +1,11 @@ +<project> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>io.trygvis.appsh</groupId> + <artifactId>appsh-parent</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + <groupId>io.trygvis.appsh.examples</groupId> + <artifactId>app-a-web</artifactId> + <packaging>war</packaging> +</project> diff --git a/examples/app-a-web/src/main/webapp/WEB-INF/web.xml b/examples/app-a-web/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..50e8c1e --- /dev/null +++ b/examples/app-a-web/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="UTF-8"?> +<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" + version="3.0"> +</web-app> diff --git a/examples/app-a-web/src/main/webapp/index.jspx b/examples/app-a-web/src/main/webapp/index.jspx new file mode 100644 index 0000000..166ef4b --- /dev/null +++ b/examples/app-a-web/src/main/webapp/index.jspx @@ -0,0 +1,10 @@ +<html xmlns:jsp="http://java.sun.com/JSP/Page"> + <jsp:output doctype-root-element="HTML" doctype-system="about:legacy-compat"/> + <jsp:directive.page contentType="text/html;charset=UTF-8"/> + <head> + <title>Hello World!</title> + </head> + <body> + <h1>Hello World!</h1> + </body> +</html> @@ -0,0 +1,36 @@ +<project> + <modelVersion>4.0.0</modelVersion> + <groupId>io.trygvis.appsh</groupId> + <artifactId>appsh-parent</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>pom</packaging> + <prerequisites> + <maven>3.0.0</maven> + </prerequisites> + <properties> + <versions.jetty8>8.1.4.v20120524</versions.jetty8> + <maven.compiler.source>1.6</maven.compiler.source> + <maven.compiler.target>1.6</maven.compiler.target> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + <distributionManagement> + <snapshotRepository> + <id>trygvis.io-snapshots</id> + <url>dav:http://repo.trygvis.io/snapshots</url> + </snapshotRepository> + </distributionManagement> + <build> + <extensions> + <extension> + <groupId>org.apache.maven.wagon</groupId> + <artifactId>wagon-webdav-jackrabbit</artifactId> + <version>2.2</version> + </extension> + </extensions> + </build> + <modules> + <module>booter-jetty8</module> + <module>examples/app-a-bundle</module> + <module>examples/app-a-web</module> + </modules> +</project> |