Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit of v2 of the Jack servlet

  • Loading branch information...
commit 2162e7ae0f8bd9ee4d90dbe15a65c4797a76f874 0 parents
Tom Robinson authored
5 .gitignore
@@ -0,0 +1,5 @@
+.DS_Store
+war/WEB-INF/lib/appengine-*
+war/WEB-INF/lib/datanucleus-*
+war/WEB-INF/lib/geronimo-*
+war/WEB-INF/lib/jdo2-*
25 README.md
@@ -0,0 +1,25 @@
+Jack Servlet
+============
+
+AppEngine Instructions
+----------------------
+
+1. Set the APPENGINE_JAVA_SDK environment variable to the location of the AppEngine SDK, or change the "sdk.dir" property in the build.xml file.
+2. Ensure a Narhwal distribution is located in the parent directory and is named "narwhal" (alternatively, replace or modify the "war/WEB-INF/narwhal" with a Narwhal distribution)
+3. Add "jack.js" and the "jack" (or symlinks) from the lib directory in a Jack distribution to the lib directory of Narwhal.
+4. Place your Jack application in "war/WEB-INF/app/" with the main file called "app.js". The last statement of this file should be a Jack compatible function (alternatively, in "war/WEB-INF/web.xml" set a parameter named "module" to the name of the main file, and/or a paramter named "function" to the name of the function to use)
+5. "ant runserver" to run locally.
+6. Edit the AppEngine application ID in "war/WEB-INF/appengine-web.xml".
+7. "ant update" to deploy.
+
+Other Servlet Container Instructions
+------------------------------------
+
+Coming soon.
+
+Notes
+-----
+
+* The package loader doesn't currently work with AppEngine due to security restrictions on classloaders. Copy the JavaScript files and directories from additional packages to Narwhal's "lib" directory, and jars to "war/WEB-INF/lib".
+
+* In addition to the "war/WEB-INF/narwhal" symlink, there are relative symlinks to Rhino in "war/WEB-INF/lib/js.jar" and an example application in "war/WEB-INF/app/app.js"
67 build.xml
@@ -0,0 +1,67 @@
+<?xml version="1.0"?>
+<project>
+ <property environment="env"/>
+ <property name="sdk.dir" location="${env.APPENGINE_JAVA_SDK}"/>
+
+ <import file="${sdk.dir}/config/user/ant-macros.xml"/>
+
+ <path id="project.classpath">
+ <pathelement path="war/WEB-INF/classes"/>
+ <fileset dir="war/WEB-INF/lib">
+ <include name="**/*.jar"/>
+ </fileset>
+ <fileset dir="${sdk.dir}/lib">
+ <include name="shared/**/*.jar"/>
+ </fileset>
+ </path>
+
+ <target name="copyjars" description="Copies the App Engine JARs to the WAR.">
+ <copy todir="war/WEB-INF/lib" flatten="true">
+ <fileset dir="${sdk.dir}/lib/user">
+ <include name="**/*.jar"/>
+ </fileset>
+ </copy>
+ </target>
+
+ <target name="compile" depends="copyjars" description="Compiles Java source and copies other source files to the WAR.">
+ <mkdir dir="war/WEB-INF/classes"/>
+ <copy todir="war/WEB-INF/classes">
+ <fileset dir="src">
+ <exclude name="**/*.java"/>
+ </fileset>
+ </copy>
+ <javac srcdir="src" destdir="war/WEB-INF/classes" classpathref="project.classpath" debug="on"/>
+ </target>
+
+ <target name="datanucleusenhance" depends="compile" description="Performs JDO enhancement on compiled data classes.">
+ <!--<enhance_war war="war"/>-->
+ </target>
+
+ <target name="runserver" depends="datanucleusenhance" description="Starts the development server.">
+ <dev_appserver war="war"/>
+ </target>
+
+ <target name="update" depends="datanucleusenhance" description="Uploads the application to App Engine.">
+ <appcfg action="update" war="war"/>
+ </target>
+
+ <target name="update_indexes" depends="datanucleusenhance" description="Uploads just the datastore index configuration to App Engine.">
+ <appcfg action="update_indexes" war="war"/>
+ </target>
+
+ <target name="rollback" depends="datanucleusenhance" description="Rolls back an interrupted application update.">
+ <appcfg action="rollback" war="war"/>
+ </target>
+
+ <target name="request_logs" description="Downloads log data from App Engine for the application.">
+ <appcfg action="request_logs" war="war">
+ <options>
+ <arg value="--num_days=5"/>
+ </options>
+ <args>
+ <arg value="logs.txt"/>
+ </args>
+ </appcfg>
+ </target>
+
+</project>
68 src/org/jackjs/JackServlet.java
@@ -0,0 +1,68 @@
+package org.jackjs;
+import java.io.IOException;
+import javax.servlet.http.*;
+import javax.servlet.*;
+
+import java.io.*;
+
+import org.mozilla.javascript.*;
+
+@SuppressWarnings("serial")
+public class JackServlet extends HttpServlet {
+ private Scriptable scope;
+ private Function app;
+ private Function handler;
+
+ public void init(ServletConfig config) throws ServletException {
+ super.init(config);
+
+ final String modulePath = getServletContext().getRealPath(getInitParam(config, "modulePath", "WEB-INF/app"));
+ final String module = getInitParam(config, "module", "app.js");
+ final String function = getInitParam(config, "function", null);
+
+ final String narwhalHome = getServletContext().getRealPath("WEB-INF/narwhal");
+ final String narwhalFilename = "narwhal-rhino.js";
+
+ Context context = Context.enter();
+ try {
+ scope = new ImporterTopLevel(context);
+
+ ScriptableObject.putProperty(scope, "NARWHAL_HOME", Context.javaToJS(narwhalHome, scope));
+ //ScriptableObject.putProperty(scope, "$DEBUG", Context.javaToJS(true, scope));
+
+ // load Narwhal
+ context.evaluateReader(scope, new FileReader(narwhalHome+"/"+narwhalFilename), narwhalFilename, 1, null);
+
+ // load Servlet handler "process" method
+ handler = (Function)context.evaluateString(scope, "require('jack/handler/servlet').Servlet.process;", null, 1, null);
+
+ // load the app
+ Object possibleApp = context.evaluateReader(scope, new FileReader(modulePath+"/"+module), module, 1, null);
+ if (function != null)
+ possibleApp = scope.get(function, scope);
+
+ if (possibleApp instanceof Function)
+ app = (Function)possibleApp;
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ Context.exit();
+ }
+ }
+
+ public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
+ Context context = Context.enter();
+ try {
+ Object args[] = {app, request, response};
+ handler.call(context, scope, null, args);
+ } finally {
+ Context.exit();
+ }
+ }
+
+ private String getInitParam(ServletConfig config, String name, String defaultValue) {
+ String value = config.getInitParameter(name);
+ return value == null ? defaultValue : value;
+ }
+}
1  war/WEB-INF/app/app.js
7 war/WEB-INF/appengine-web.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
+
+ <application>jack-js</application>
+ <version>1</version>
+
+</appengine-web-app>
BIN  war/WEB-INF/classes/org/jackjs/JackServlet.class
Binary file not shown
1  war/WEB-INF/lib/js.jar
1  war/WEB-INF/narwhal
18 war/WEB-INF/web.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0"?>
+<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
+
+ <servlet>
+ <servlet-name>jack</servlet-name>
+ <servlet-class>org.jackjs.JackServlet</servlet-class>
+ <init-param>
+ <param-name>module</param-name>
+ <param-value>app.js</param-value>
+ </init-param>
+ </servlet>
+
+ <servlet-mapping>
+ <servlet-name>jack</servlet-name>
+ <url-pattern>/*</url-pattern>
+ </servlet-mapping>
+
+</web-app>
Please sign in to comment.
Something went wrong with that request. Please try again.