Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
Add com.redhat.ceylon.war module to provide WAR initialization logic.
Browse files Browse the repository at this point in the history
The module currently provides a single java class that initializes the
repo and metamodel from a WAR.
  • Loading branch information
tobias committed Nov 24, 2014
1 parent a829d53 commit ce9ea43
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 1 deletion.
4 changes: 3 additions & 1 deletion build.xml
Expand Up @@ -64,10 +64,11 @@
<module name="ceylon.time"/>
<module name="ceylon.unicode"/>
<module name="ceylon.locale"/>
<module name="com.redhat.ceylon.war"/>
<module name="com.redhat.ceylon.test"/>
<module name="com.redhat.ceylon.testjvm"/>
</moduleset>

<moduleset id="modules.sdk.js">
<module name="ceylon.collection"/>
<module name="ceylon.html"/>
Expand Down Expand Up @@ -250,6 +251,7 @@
<include name="ceylon/locale/**"/>
<include name="ceylon/interop/java/**"/>
<include name="ceylon/html/**"/>
<include name="com/redhat/ceylon/war/**"/>
</fileset>
</copy>
</target>
Expand Down
97 changes: 97 additions & 0 deletions source/com/redhat/ceylon/war/WarInitializer.java
@@ -0,0 +1,97 @@
package com.redhat.ceylon.war;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.redhat.ceylon.common.FileUtil;
import com.redhat.ceylon.compiler.java.runtime.tools.Backend;
import com.redhat.ceylon.compiler.java.runtime.tools.CeylonToolProvider;
import com.redhat.ceylon.compiler.java.runtime.tools.Runner;
import com.redhat.ceylon.compiler.java.runtime.tools.RunnerOptions;

public class WarInitializer implements ServletContextListener {

@Override
public void contextDestroyed(final ServletContextEvent sce) {
this.runner.cleanup();
if (this.tmpRepo != null) {
FileUtil.deleteQuietly(this.tmpRepo);
}
}

@Override
public void contextInitialized(final ServletContextEvent sce) {
final ServletContext context = sce.getServletContext();
final File repo = setupRepo(context);
final Properties properties = moduleProperties(context);
final RunnerOptions options = new RunnerOptions();

options.setOffline(true);
options.setSystemRepository("flat:" + repo.getAbsolutePath());

this.runner = CeylonToolProvider.getRunner(Backend.Java, options,
properties.getProperty("moduleName"),
properties.getProperty("moduleVersion"));

this.runner.run();
}

protected File setupRepo(final ServletContext context) {
try {
final URL libDir = context.getResource("/WEB-INF/lib");
if (libDir.getProtocol().equals("file")) {

return new File(libDir.toURI());
} else {
// we're running from the WAR, and need to extract a repo to disk
final URL libList = context.getResource("/META-INF/libs.txt");
this.tmpRepo = Files.createTempDirectory("ceylon_war_repo").toFile();

try (BufferedReader reader = new BufferedReader(new InputStreamReader(libList.openStream()))) {
String filename;
while ((filename = reader.readLine()) != null) {
copy(context.getResource("/WEB-INF/lib/" + filename), new File(this.tmpRepo, filename));
}
}

return this.tmpRepo;
}

} catch (IOException|URISyntaxException e) {
throw new RuntimeException(e);
}
}

protected Properties moduleProperties(final ServletContext context) {
final Properties properties = new Properties();
try (InputStream moduleProps = context.getResourceAsStream("/META-INF/module.properties")) {
properties.load(moduleProps);
} catch (IOException e) {
throw new RuntimeException(e);
}

return properties;
}

protected void copy(final URL src, final File dest) throws IOException {
try (InputStream stream = src.openStream()) {
Files.copy(stream, dest.toPath());
}
}

private File tmpRepo;
private Runner runner;
}


10 changes: 10 additions & 0 deletions source/com/redhat/ceylon/war/module.ceylon
@@ -0,0 +1,10 @@
"Provides a ServletContextListener for initializing the
Ceylon runtime when a Ceylon WAR is loaded by a Servlet
container. This module will automatically be added to a
WAR when it is created - you should never need to import
this module directly."
module com.redhat.ceylon.war "1.1.1" {
import java.base "7";
import com.redhat.ceylon.common "1.1.1";
import javax.servlet "3.1.0";
}
1 change: 1 addition & 0 deletions source/com/redhat/ceylon/war/package.ceylon
@@ -0,0 +1 @@
shared package com.redhat.ceylon.war;
4 changes: 4 additions & 0 deletions source/com/redhat/ceylon/war/run.ceylon
@@ -0,0 +1,4 @@
"Run the module `com.redhat.ceylon.war`."
shared void run() {

}

0 comments on commit ce9ea43

Please sign in to comment.