incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r687373 [1/2] - in /incubator/sling/trunk: ./ scripting/java/ scripting/java/src/ scripting/java/src/main/ scripting/java/src/main/java/ scripting/java/src/main/java/org/ scripting/java/src/main/java/org/apache/ scripting/java/src/main/java...
Date Wed, 20 Aug 2008 15:22:11 GMT
Author: cziegeler
Date: Wed Aug 20 08:22:10 2008
New Revision: 687373

URL: http://svn.apache.org/viewvc?rev=687373&view=rev
Log:
SLING-619 Add a scripting engine for java servlets.

Added:
    incubator/sling/trunk/scripting/java/   (with props)
    incubator/sling/trunk/scripting/java/pom.xml   (with props)
    incubator/sling/trunk/scripting/java/src/
    incubator/sling/trunk/scripting/java/src/main/
    incubator/sling/trunk/scripting/java/src/main/java/
    incubator/sling/trunk/scripting/java/src/main/java/org/
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilationContext.java   (with props)
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilerError.java   (with props)
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilerUtil.java   (with props)
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java   (with props)
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaServletConfig.java   (with props)
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaServletContext.java   (with props)
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/Options.java   (with props)
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletCache.java   (with props)
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java   (with props)
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/SlingIOProvider.java   (with props)
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/jdt/
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/jdt/CompilationUnit.java   (with props)
    incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/jdt/EclipseJavaCompiler.java   (with props)
Modified:
    incubator/sling/trunk/pom.xml

Modified: incubator/sling/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/pom.xml?rev=687373&r1=687372&r2=687373&view=diff
==============================================================================
--- incubator/sling/trunk/pom.xml (original)
+++ incubator/sling/trunk/pom.xml Wed Aug 20 08:22:10 2008
@@ -104,6 +104,7 @@
         <module>scripting/javascript</module>
         <module>scripting/jsp</module>
         <module>scripting/jsp-taglib</module>
+        <module>scripting/java</module>
         
         <!-- Extensions -->
         <module>extensions/adapter</module>

Propchange: incubator/sling/trunk/scripting/java/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Wed Aug 20 08:22:10 2008
@@ -0,0 +1 @@
+target

Added: incubator/sling/trunk/scripting/java/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/java/pom.xml?rev=687373&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/java/pom.xml (added)
+++ incubator/sling/trunk/scripting/java/pom.xml Wed Aug 20 08:22:10 2008
@@ -0,0 +1,139 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+    http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+-->
+<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/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.sling</groupId>
+        <artifactId>sling</artifactId>
+        <version>4-incubator-SNAPSHOT</version>
+        <relativePath>../../parent/pom.xml</relativePath>
+    </parent>
+
+    <artifactId>org.apache.sling.scripting.java</artifactId>
+    <version>2.0.0-incubator-SNAPSHOT</version>
+    <packaging>bundle</packaging>
+
+    <name>Sling - Scripting - Java Support</name>
+    <description>Support for scripting Java</description>
+
+    <scm>
+        <connection>scm:svn:http://svn.apache.org/repos/asf/incubator/sling/trunk/scripting/java</connection>
+        <developerConnection>scm:svn:https://svn.apache.org/repos/asf/incubator/sling/trunk/scripting/java</developerConnection>
+        <url>http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/java</url>
+    </scm>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-scr-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Import-Package>
+                            !org.eclipse.*, *
+                        </Import-Package>
+                        <Private-Package>
+                            org.apache.sling.scripting.java,
+                            org.apache.sling.scripting.java.jdt,
+                            org.eclipse.jdt.*
+                        </Private-Package>
+                        <DynamicImport-Package>*</DynamicImport-Package>
+
+                        <ScriptEngine-Name>${pom.name}</ScriptEngine-Name>
+                        <ScriptEngine-Version>${pom.version}</ScriptEngine-Version>
+
+                        <Embed-Dependency>
+                            jasper*
+                        </Embed-Dependency>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+    <reporting>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-javadoc-plugin</artifactId>
+                <configuration>
+                  <!-- No javadocs -->
+                    <excludePackageNames>
+                        org.apache.sling.scripting
+                    </excludePackageNames>
+                </configuration>
+            </plugin>
+        </plugins>
+    </reporting>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.api</artifactId>
+            <version>2.0.2-incubator</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.scripting.api</artifactId>
+            <version>2.0.2-incubator</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.jcr.api</artifactId>
+            <version>2.0.2-incubator</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.jcr.classloader</artifactId>
+            <version>2.0.2-incubator</version>
+        </dependency>
+
+     <!-- We use the same eclipse jdt as the jsp bundle -->
+        <dependency>
+            <groupId>org.apache.tomcat</groupId>
+            <artifactId>jasper-jdt</artifactId>
+            <version>6.0.18</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <version>2.5</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+        </dependency>
+    </dependencies>
+</project>

Propchange: incubator/sling/trunk/scripting/java/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/scripting/java/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: incubator/sling/trunk/scripting/java/pom.xml
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilationContext.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilationContext.java?rev=687373&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilationContext.java (added)
+++ incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilationContext.java Wed Aug 20 08:22:10 2008
@@ -0,0 +1,232 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.scripting.java;
+
+import java.io.FileNotFoundException;
+import java.util.List;
+
+import javax.servlet.ServletException;
+
+import org.apache.sling.scripting.java.jdt.EclipseJavaCompiler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ *
+ */
+public class CompilationContext {
+
+    private Logger logger = LoggerFactory.getLogger(getClass());
+
+    /** The name of the generated class. */
+    private final String className;
+
+    /** The path to the servlet. */
+    private final String sourcePath;
+
+    /** The mapped path. */
+    private final String mappedSourcePath;
+
+    /** Compilation options. */
+    private final Options options;
+
+    /** The compiler instance. */
+    private final EclipseJavaCompiler compiler;
+
+    /** Sling IO Provider. */
+    private final SlingIOProvider ioProvider;
+
+    private ServletCache servletCache;
+
+    private long lastModificationTest = 0L;
+    private int removed = 0;
+
+    private Class servletClass;
+
+    private final ServletWrapper wrapper;
+
+    /**
+     * A new compilation context.
+     * @param sourcePath The path to the servlet source.
+     * @param options The compiler options
+     * @param provider The Sling IO Provider
+     * @param servletCache
+     */
+    public CompilationContext(final String sourcePath,
+                              final Options options,
+                              final SlingIOProvider provider,
+                              ServletCache servletCache,
+                              final ServletWrapper wrapper) {
+        this.sourcePath = sourcePath;
+        this.mappedSourcePath = CompilerUtil.mapSourcePath(this.sourcePath);
+        this.className = CompilerUtil.makeClassPath(this.mappedSourcePath);
+
+        this.options = options;
+        this.ioProvider = provider;
+        this.compiler = new EclipseJavaCompiler(this);
+
+        this.servletCache = servletCache;
+        this.wrapper = wrapper;
+    }
+
+    /**
+     * Options
+     */
+    public Options getCompilerOptions() {
+        return options;
+    }
+
+    /**
+     * Provider
+     */
+    public SlingIOProvider getIOProvider() {
+        return this.ioProvider;
+    }
+
+    /**
+     * Return the path to the java servlet source file
+     * @return The source file path.
+     */
+    public String getSourcePath() {
+        return this.sourcePath;
+    }
+
+    public String getJavaClassName() {
+        return this.mappedSourcePath.replace('/', '.');
+    }
+
+    /**
+     * Return the path to the generated class file.
+     * @return The class file path.
+     */
+    public String getClassFilePath() {
+        return this.className;
+    }
+
+    public void incrementRemoved() {
+        if (removed == 0 && servletCache != null) {
+            servletCache.removeWrapper(sourcePath);
+        }
+        removed++;
+    }
+
+    public boolean isRemoved() {
+        if (removed > 1 ) {
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * Check if the compiled class file is older than the source file
+     */
+    public boolean isOutDated() {
+        if (this.options.getModificationTestInterval() > 0) {
+
+            if (this.lastModificationTest
+                + (this.options.getModificationTestInterval() * 1000) > System.currentTimeMillis()) {
+                return false;
+            }
+            this.lastModificationTest = System.currentTimeMillis();
+        }
+
+        final long sourceLastModified = this.ioProvider.lastModified(getSourcePath());
+
+        final long targetLastModified = this.ioProvider.lastModified(getCompleteClassPath());
+        if (targetLastModified < 0) {
+            return true;
+        }
+
+        if (targetLastModified < sourceLastModified) {
+            if (logger.isDebugEnabled()) {
+                logger.debug("Compiler: outdated: " + getClassFilePath() + " "
+                        + targetLastModified);
+            }
+            return true;
+        }
+
+        return false;
+
+    }
+
+    private String getCompleteClassPath() {
+        return options.getDestinationPath() + getClassFilePath() + ".class";
+    }
+
+    // ==================== Compile and reload ====================
+
+    public void compile() throws ServletException, FileNotFoundException {
+        if (this.isOutDated()) {
+            try {
+                final List<CompilerError> errors = this.compiler.compile();
+                if ( errors != null ) {
+                    //this.ioProvider.delete(getCompleteClassPath());
+                    throw CompilerException.create(errors);
+                }
+                this.wrapper.setReload(true);
+                this.wrapper.setCompilationException(null);
+            } catch (ServletException se) {
+                this.wrapper.setCompilationException(se);
+                throw se;
+            } catch (Exception ex) {
+                final ServletException se = new ServletException("Unable to compile servlet.", ex);
+                // Cache compilation exception
+                this.wrapper.setCompilationException(se);
+                throw se;
+            }
+        }
+    }
+
+    /**
+     * Load the class.
+     */
+    public Class load()
+    throws ServletException, FileNotFoundException {
+        try {
+            servletClass = this.options.getClassLoader().loadClass(this.getClassFilePath().substring(1).replace('/', '.'));
+        } catch (ClassNotFoundException cex) {
+            throw new ServletException("Unable to load servlet class.", cex);
+        } catch (Exception ex) {
+            throw new ServletException("Unable to compile servlet.", ex);
+        }
+        removed = 0;
+        return servletClass;
+    }
+
+    protected final static class CompilerException extends ServletException {
+
+        public static CompilerException create(List<CompilerError> errors) {
+            final StringBuffer buffer = new StringBuffer();
+            buffer.append("Compilation errors:\n");
+            for(final CompilerError e : errors) {
+                buffer.append(e.getFile());
+                buffer.append(", line ");
+                buffer.append(e.getStartLine());
+                buffer.append(", column ");
+                buffer.append(e.getStartColumn());
+                buffer.append(" : " );
+                buffer.append(e.getMessage());
+                buffer.append("\n");
+            }
+            return new CompilerException(buffer.toString());
+        }
+
+        public CompilerException(final String message) {
+           super(message);
+        }
+    }
+}

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilationContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilationContext.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilationContext.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilerError.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilerError.java?rev=687373&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilerError.java (added)
+++ incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilerError.java Wed Aug 20 08:22:10 2008
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.scripting.java;
+
+/**
+ * This class encapsulates an error message produced by a programming language
+ * processor (whether interpreted or compiled)
+ * @version $Id$
+ * @since 2.0
+ */
+
+public class CompilerError {
+  /**
+   * Is this a severe error or a warning?
+   */
+  private boolean error;
+  /**
+   * The start line number of the offending program text
+   */
+  private int startline;
+  /**
+   * The start column number of the offending program text
+   */
+  private int startcolumn;
+  /**
+   * The end line number of the offending program text
+   */
+  private int endline;
+  /**
+   * The end column number of the offending program text
+   */
+  private int endcolumn;
+  /**
+   * The name of the file containing the offending program text
+   */
+  private String file;
+  /**
+   * The actual error text produced by the language processor
+   */
+  private String message;
+
+  /**
+   * The error message constructor.
+   *
+   * @param file The name of the file containing the offending program text
+   * @param error The actual error text produced by the language processor
+   * @param startline The start line number of the offending program text
+   * @param startcolumn The start column number of the offending program text
+   * @param endline The end line number of the offending program text
+   * @param endcolumn The end column number of the offending program text
+   * @param message The actual error text produced by the language processor
+   */
+  public CompilerError(
+    String file,
+    boolean error,
+    int startline,
+    int startcolumn,
+    int endline,
+    int endcolumn,
+    String message
+  )
+  {
+    this.file = file;
+    this.error = error;
+    this.startline = startline;
+    this.startcolumn = startcolumn;
+    this.endline = endline;
+    this.endcolumn = endcolumn;
+    this.message = message;
+  }
+
+  /**
+   * The error message constructor.
+   *
+   * @param message The actual error text produced by the language processor
+   */
+  public CompilerError(String message) {
+    this.message = message;
+  }
+
+  /**
+   * Return the filename associated with this compiler error.
+   *
+   * @return The filename associated with this compiler error
+   */
+  public String getFile() {
+    return file;
+  }
+
+  /**
+   * Assert whether this is a severe error or a warning
+   *
+   * @return Whether the error is severe
+   */
+  public boolean isError() {
+    return error;
+  }
+
+  /**
+   * Return the starting line number of the program text originating this error
+   *
+   * @return The starting line number of the program text originating this error
+   */
+  public int getStartLine() {
+    return startline;
+  }
+
+  /**
+   * Return the starting column number of the program text originating this
+   * error
+   *
+   * @return The starting column number of the program text originating this
+   * error
+   */
+  public int getStartColumn() {
+    return startcolumn;
+  }
+
+  /**
+   * Return the ending line number of the program text originating this error
+   *
+   * @return The ending line number of the program text originating this error
+   */
+  public int getEndLine() {
+    return endline;
+  }
+
+  /**
+   * Return the ending column number of the program text originating this
+   * error
+   *
+   * @return The ending column number of the program text originating this
+   * error
+   */
+  public int getEndColumn() {
+    return endcolumn;
+  }
+
+  /**
+   * Return the message produced by the language processor
+   *
+   * @return The message produced by the language processor
+   */
+  public String getMessage() {
+    return message;
+  }
+}

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilerError.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilerError.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilerError.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilerUtil.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilerUtil.java?rev=687373&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilerUtil.java (added)
+++ incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilerUtil.java Wed Aug 20 08:22:10 2008
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.scripting.java;
+
+public class CompilerUtil {
+
+    /**
+     * The path has to be mapped to form a proper java class name.
+     * @param path
+     * @return The mapped source path.
+     */
+    public static String mapSourcePath(final String path) {
+        final String str;
+        if ( path.endsWith(".java") ) {
+            str = path.substring(0, path.length() - 5);
+        } else {
+            str = path;
+        }
+        final int pos = str.lastIndexOf("/");
+        if ( pos == -1 ) {
+            return makeJavaIdentifier(str);
+        }
+        return str.substring(0, pos + 1) + makeJavaIdentifier(str.substring(pos + 1));
+    }
+
+    /**
+     * Create the class name from the source name
+     * @param sourcePath The source path
+     * @return The corresponding class file path.
+     */
+    public static String makeClassPath(String sourcePath) {
+        String str = sourcePath;
+        if (str.endsWith(".java")) {
+            str = str.substring(0, str.length() - 5);
+        }
+        return str;
+    }
+
+
+    /**
+     * Converts the given identifier to a legal Java identifier
+     *
+     * @param identifier Identifier to convert
+     *
+     * @return Legal Java identifier corresponding to the given identifier
+     */
+    public static final String makeJavaIdentifier(String identifier) {
+        StringBuffer modifiedIdentifier =
+            new StringBuffer(identifier.length());
+        if (!Character.isJavaIdentifierStart(identifier.charAt(0))) {
+            modifiedIdentifier.append('_');
+        }
+        for (int i = 0; i < identifier.length(); i++) {
+            char ch = identifier.charAt(i);
+            if (Character.isJavaIdentifierPart(ch) && ch != '_') {
+                modifiedIdentifier.append(ch);
+            } else if (ch == '.') {
+                modifiedIdentifier.append('_');
+            } else {
+                modifiedIdentifier.append(mangleChar(ch));
+            }
+        }
+        if (isJavaKeyword(modifiedIdentifier.toString())) {
+            modifiedIdentifier.append('_');
+        }
+        return modifiedIdentifier.toString();
+    }
+
+    /**
+     * Mangle the specified character to create a legal Java class name.
+     */
+    public static final String mangleChar(char ch) {
+        char[] result = new char[5];
+        result[0] = '_';
+        result[1] = Character.forDigit((ch >> 12) & 0xf, 16);
+        result[2] = Character.forDigit((ch >> 8) & 0xf, 16);
+        result[3] = Character.forDigit((ch >> 4) & 0xf, 16);
+        result[4] = Character.forDigit(ch & 0xf, 16);
+        return new String(result);
+    }
+
+    private static final String javaKeywords[] = {
+        "abstract", "assert", "boolean", "break", "byte", "case",
+        "catch", "char", "class", "const", "continue",
+        "default", "do", "double", "else", "enum", "extends",
+        "final", "finally", "float", "for", "goto",
+        "if", "implements", "import", "instanceof", "int",
+        "interface", "long", "native", "new", "package",
+        "private", "protected", "public", "return", "short",
+        "static", "strictfp", "super", "switch", "synchronized",
+        "this", "throws", "transient", "try", "void",
+        "volatile", "while" };
+
+    /**
+     * Test whether the argument is a Java keyword
+     */
+    public static boolean isJavaKeyword(String key) {
+        int i = 0;
+        int j = javaKeywords.length;
+        while (i < j) {
+            int k = (i+j)/2;
+            int result = javaKeywords[k].compareTo(key);
+            if (result == 0) {
+                return true;
+            }
+            if (result < 0) {
+                i = k+1;
+            } else {
+                j = k;
+            }
+        }
+        return false;
+    }
+
+}

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilerUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilerUtil.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilerUtil.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java?rev=687373&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java (added)
+++ incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java Wed Aug 20 08:22:10 2008
@@ -0,0 +1,271 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.scripting.java;
+
+import static org.apache.sling.api.scripting.SlingBindings.SLING;
+
+import java.io.Reader;
+
+import javax.jcr.RepositoryException;
+import javax.script.Bindings;
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+
+import org.apache.sling.api.SlingException;
+import org.apache.sling.api.SlingIOException;
+import org.apache.sling.api.SlingServletException;
+import org.apache.sling.api.scripting.SlingBindings;
+import org.apache.sling.api.scripting.SlingScript;
+import org.apache.sling.api.scripting.SlingScriptHelper;
+import org.apache.sling.jcr.api.SlingRepository;
+import org.apache.sling.jcr.classloader.RepositoryClassLoaderProvider;
+import org.apache.sling.scripting.api.AbstractScriptEngineFactory;
+import org.apache.sling.scripting.api.AbstractSlingScriptEngine;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The Java engine
+ *
+ * @scr.component label="%javahandler.name" description="%javahandler.description"
+ * @scr.property name="service.description" value="Java Servlet Script Handler"
+ * @scr.property name="service.vendor" value="The Apache Software Foundation"
+ * @scr.service
+ *
+ * @scr.property name="java.javaEncoding" value="UTF-8"
+ * @scr.property name="java.compilerSourceVM" value="1.5"
+ * @scr.property name="java.compilerTargetVM" value="1.5"
+ * @scr.property name="java.development" value="true"
+ * @scr.property name="java.outputPath" value="/var/classes"
+ * @scr.property name="java.modificationTestInterval" value="-1"
+ * @scr.property name="java.classdebuginfo" value="truie"
+ */
+public class JavaScriptEngineFactory extends AbstractScriptEngineFactory {
+
+    private static final String CLASSLOADER_NAME = "admin";
+
+    /** default logger */
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    /** @scr.reference */
+    private SlingRepository repository;
+
+    /**
+     * @scr.reference
+     */
+    private RepositoryClassLoaderProvider repoCLProvider;
+
+    /**
+     * The class loader
+     */
+    private ClassLoader javaClassLoader;
+
+    /** @scr.reference */
+    private ServletContext slingServletContext;
+
+    private SlingIOProvider ioProvider;
+
+    private JavaServletContext javaServletContext;
+
+    private ServletConfig servletConfig;
+
+    private ServletCache servletCache;
+
+    /** Compiler options. */
+    private Options compilerOptions;
+
+    public static final String SCRIPT_TYPE = "java";
+
+    /**
+     * Constructor
+     */
+    public JavaScriptEngineFactory() {
+        setExtensions(SCRIPT_TYPE);
+    }
+
+    /**
+     * @see javax.script.ScriptEngineFactory#getScriptEngine()
+     */
+    public ScriptEngine getScriptEngine() {
+        return new JavaScriptEngine(this);
+    }
+
+    /**
+     * @see javax.script.ScriptEngineFactory#getLanguageName()
+     */
+    public String getLanguageName() {
+        return "Java Servlet Compiler";
+    }
+
+    /**
+     * @see javax.script.ScriptEngineFactory#getLanguageVersion()
+     */
+    public String getLanguageVersion() {
+        return "1.5";
+    }
+
+    /**
+     * Activate this engine
+     * @param componentContext
+     */
+    protected void activate(ComponentContext componentContext) {
+        this.ioProvider = new SlingIOProvider(repository);
+        this.servletCache = new ServletCache();
+
+        this.javaServletContext = new JavaServletContext(ioProvider,
+            slingServletContext);
+
+        this.servletConfig = new JavaServletConfig(javaServletContext,
+            componentContext.getProperties());
+        this.compilerOptions = new Options(componentContext,
+                                           this.javaClassLoader);
+        if (log.isDebugEnabled()) {
+            log.debug("JavaServletScriptEngine.activate()");
+        }
+    }
+
+    /**
+     * Deactivate this engine
+     * @param oldComponentContext
+     */
+    protected void deactivate(ComponentContext oldComponentContext) {
+        if (log.isDebugEnabled()) {
+            log.debug("JavaServletScriptEngine.deactivate()");
+        }
+
+        ioProvider = null;
+        javaServletContext = null;
+        servletConfig = null;
+        servletCache = null;
+        compilerOptions = null;
+    }
+
+    /**
+     * Call the servlet.
+     * @param scriptHelper
+     * @throws SlingServletException
+     * @throws SlingIOException
+     */
+    private void callServlet(Bindings bindings, SlingScriptHelper scriptHelper) {
+
+        ioProvider.setRequestResourceResolver(scriptHelper.getRequest().getResourceResolver());
+        try {
+            ServletWrapper servlet = getWrapperAdapter(scriptHelper);
+            // create a SlingBindings object
+            final SlingBindings slingBindings = new SlingBindings();
+            slingBindings.putAll(bindings);
+            servlet.service(slingBindings);
+        } finally {
+            ioProvider.resetRequestResourceResolver();
+        }
+    }
+
+    private ServletWrapper getWrapperAdapter(
+            SlingScriptHelper scriptHelper) throws SlingException {
+
+        SlingScript script = scriptHelper.getScript();
+        final String scriptName = script.getScriptResource().getPath();
+        ServletWrapper wrapper = this.servletCache.getWrapper(scriptName);
+        if (wrapper != null) {
+            return wrapper;
+        }
+
+        synchronized (this) {
+            wrapper = this.servletCache.getWrapper(scriptName);
+            if (wrapper != null) {
+                return wrapper;
+            }
+
+            wrapper = new ServletWrapper(servletConfig,
+                    this.compilerOptions, ioProvider, scriptName, this.servletCache);
+            this.servletCache.addWrapper(scriptName, wrapper);
+
+            return wrapper;
+        }
+    }
+
+    /**
+     * Bind the class load provider.
+     * @param repositoryClassLoaderProvider the new provider
+     */
+    protected void bindRepositoryClassLoaderProvider(RepositoryClassLoaderProvider rclp) {
+        if ( this.javaClassLoader != null ) {
+            this.ungetClassLoader();
+        }
+        this.getClassLoader(rclp);
+    }
+
+    /**
+     * Unbind the class loader provider.
+     * @param repositoryClassLoaderProvider the old provider
+     */
+    protected void unbindRepositoryClassLoaderProvider(RepositoryClassLoaderProvider rclp) {
+        if ( this.repoCLProvider == rclp ) {
+            this.ungetClassLoader();
+        }
+    }
+
+    /**
+     * Get the class loader
+     */
+    private void getClassLoader(RepositoryClassLoaderProvider rclp) {
+        try {
+            this.repoCLProvider = rclp;
+            this.javaClassLoader = rclp.getClassLoader(CLASSLOADER_NAME);
+        } catch (RepositoryException re) {
+            log.error("Cannot get Java class loader", re);
+        }
+    }
+
+    /**
+     * Unget the class loader
+     */
+    private void ungetClassLoader() {
+        if ( this.repoCLProvider != null ) {
+            if ( this.javaClassLoader != null ) {
+                this.repoCLProvider.ungetClassLoader(this.javaClassLoader);
+                this.javaClassLoader = null;
+            }
+            this.repoCLProvider = null;
+        }
+    }
+    // ---------- Internal -----------------------------------------------------
+
+    private static class JavaScriptEngine extends AbstractSlingScriptEngine {
+
+        JavaScriptEngine(JavaScriptEngineFactory factory) {
+            super(factory);
+        }
+
+        /**
+         * @see javax.script.ScriptEngine#eval(java.io.Reader, javax.script.ScriptContext)
+         */
+        public Object eval(Reader script, ScriptContext context)
+        throws ScriptException {
+            final Bindings props = context.getBindings(ScriptContext.ENGINE_SCOPE);
+            final SlingScriptHelper scriptHelper = (SlingScriptHelper) props.get(SLING);
+            if (scriptHelper != null) {
+                ((JavaScriptEngineFactory)this.getFactory()).callServlet(props, scriptHelper);
+            }
+            return null;
+        }
+    }
+}

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaServletConfig.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaServletConfig.java?rev=687373&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaServletConfig.java (added)
+++ incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaServletConfig.java Wed Aug 20 08:22:10 2008
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.scripting.java;
+
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+
+import org.osgi.framework.Constants;
+
+/**
+ * The <code>JavaServletConfig</code>
+ * is passed to the compiled servlets.
+ */
+public class JavaServletConfig implements ServletConfig {
+
+    /** The servlet context. */
+    private final ServletContext servletContext;
+
+    /** The name of the servlet. */
+    private final String servletName;
+
+    private final Map<String, String> initParams;
+
+    public JavaServletConfig(ServletContext servletContext, Dictionary<?, ?> config) {
+        this.servletContext = servletContext;
+
+        // set the servlet name
+        if (config.get(Constants.SERVICE_DESCRIPTION) == null) {
+            servletName = "Java Script Handler";
+        } else{
+            servletName = config.get(Constants.SERVICE_DESCRIPTION).toString();
+        }
+
+        // copy the "java." properties
+        initParams = new HashMap<String, String>();
+        for (Enumeration<?> ke = config.keys(); ke.hasMoreElements();) {
+            String key = (String) ke.nextElement();
+            if (key.startsWith("java.")) {
+                initParams.put(key.substring("java.".length()),
+                    String.valueOf(config.get(key)));
+            }
+        }
+    }
+
+    /**
+     * @see javax.servlet.ServletConfig#getInitParameter(java.lang.String)
+     */
+    public String getInitParameter(String name) {
+        return initParams.get(name);
+    }
+
+    /**
+     * @see javax.servlet.ServletConfig#getInitParameterNames()
+     */
+    public Enumeration<String> getInitParameterNames() {
+        return Collections.enumeration(initParams.keySet());
+    }
+
+    /**
+     * @see javax.servlet.ServletConfig#getServletContext()
+     */
+    public ServletContext getServletContext() {
+        return servletContext;
+    }
+
+    /**
+     * @see javax.servlet.ServletConfig#getServletName()
+     */
+    public String getServletName() {
+        return servletName;
+    }
+}
\ No newline at end of file

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaServletConfig.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaServletConfig.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaServletConfig.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaServletContext.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaServletContext.java?rev=687373&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaServletContext.java (added)
+++ incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaServletContext.java Wed Aug 20 08:22:10 2008
@@ -0,0 +1,285 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.scripting.java;
+
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Set;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.Servlet;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The <code>JavaServletContext</code>
+ * is passed to the compiled servlets.
+ */
+public class JavaServletContext implements ServletContext {
+
+    /** default log */
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    private final SlingIOProvider ioProvider;
+    private final ServletContext delegatee;
+
+    JavaServletContext(SlingIOProvider ioProvider, ServletContext componentContext) {
+        this.ioProvider = ioProvider;
+        this.delegatee = componentContext;
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getResource(java.lang.String)
+     */
+    public URL getResource(String path) throws MalformedURLException {
+        if (path.startsWith("/")) {
+            URL url = ioProvider.getURL(path);
+            if (url != null) {
+                return url;
+            }
+        }
+
+        // fall back to trying a real URL
+        return getUrlForResource(path);
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getResourceAsStream(java.lang.String)
+     */
+    public InputStream getResourceAsStream(String path) {
+        // path might be an URL, so only check resource provider in case of an
+        // absolute path - assuming URLs have no leading slash at all, we
+        // don't care for the scheme separating colon here
+        if (path.startsWith("/")) {
+            try {
+                return ioProvider.getInputStream(path);
+            } catch (Exception ex) {
+                // FileNotFoundException or IOException
+                log.debug("getResourceAsStream: Cannot get resource {}: {}",
+                    path, ex.getMessage());
+            }
+        }
+
+        // check whether we can resolve as an URL ...
+        try {
+            // create the URL and try to access
+            URL url = getUrlForResource(path);
+            if (url != null) {
+                return url.openStream();
+            }
+        } catch (Exception e) {
+            log.debug(
+                "getResourceAsStream: Cannot access resource {} through URL: {}",
+                path, e.getMessage());
+        }
+
+        return null;
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getResourcePaths(java.lang.String)
+     */
+    public Set<?> getResourcePaths(String path) {
+        return ioProvider.getResourcePaths(path);
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#log(java.lang.String)
+     */
+    public void log(String msg) {
+        log.info(msg);
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#log(java.lang.Exception, java.lang.String)
+     */
+    @Deprecated
+    public void log(Exception exception, String msg) {
+        log(msg, exception);
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#log(java.lang.String, java.lang.Throwable)
+     */
+    public void log(String message, Throwable throwable) {
+        log.error(message, throwable);
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getAttribute(java.lang.String)
+     */
+    public Object getAttribute(String name) {
+        return delegatee.getAttribute(name);
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getAttributeNames()
+     */
+    public Enumeration<?> getAttributeNames() {
+        return delegatee.getAttributeNames();
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
+     */
+    public void removeAttribute(String name) {
+        delegatee.removeAttribute(name);
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object)
+     */
+    public void setAttribute(String name, Object object) {
+        delegatee.setAttribute(name, object);
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getContext(java.lang.String)
+     */
+    public ServletContext getContext(String uripath) {
+        return delegatee.getContext(uripath);
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getInitParameter(java.lang.String)
+     */
+    public String getInitParameter(String name) {
+        return delegatee.getInitParameter(name);
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getInitParameterNames()
+     */
+    public Enumeration<?> getInitParameterNames() {
+        return delegatee.getInitParameterNames();
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getMajorVersion()
+     */
+    public int getMajorVersion() {
+        return delegatee.getMajorVersion();
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getMimeType(java.lang.String)
+     */
+    public String getMimeType(String file) {
+        return delegatee.getMimeType(file);
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getMinorVersion()
+     */
+    public int getMinorVersion() {
+        return delegatee.getMinorVersion();
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
+     */
+    public RequestDispatcher getNamedDispatcher(String name) {
+        return delegatee.getNamedDispatcher(name);
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getRealPath(java.lang.String)
+     */
+    public String getRealPath(String path) {
+        return delegatee.getRealPath(path);
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getRequestDispatcher(java.lang.String)
+     */
+    public RequestDispatcher getRequestDispatcher(String path) {
+        return delegatee.getRequestDispatcher(path);
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getServerInfo()
+     */
+    public String getServerInfo() {
+        return delegatee.getServerInfo();
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getServlet(java.lang.String)
+     */
+    @Deprecated
+    public Servlet getServlet(String name) throws ServletException {
+        return delegatee.getServlet(name);
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getServletContextName()
+     */
+    public String getServletContextName() {
+        return delegatee.getServletContextName();
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getServletNames()
+     */
+    @Deprecated
+    public Enumeration<?> getServletNames() {
+        return delegatee.getServletNames();
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getServlets()
+     */
+    @Deprecated
+    public Enumeration<?> getServlets() {
+        return delegatee.getServlets();
+    }
+
+    /**
+     * @see javax.servlet.ServletContext#getContextPath()
+     */
+    public String getContextPath() {
+        return delegatee.getContextPath();
+    }
+
+    //---------- internal -----------------------------------------------------
+
+    private URL getUrlForResource(String path) {
+        int cs = path.indexOf(":/");
+        if (cs > 0 && cs < path.length()-2) {
+            // insert second slash after scheme (was canonicalized away)
+            cs += 2;
+            if (cs < path.length() && path.charAt(cs) != '/') {
+                path = path.substring(0, cs) + "/" + path.substring(cs);
+            }
+
+            // create the URL and try to access
+            try {
+                return new URL(path);
+            } catch (MalformedURLException mue) {
+                log.debug("getUrlForResource: Cannot create URL for {}: {}",
+                    path, mue.getMessage());
+            }
+        }
+
+        return null;
+    }
+}

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaServletContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaServletContext.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaServletContext.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/Options.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/Options.java?rev=687373&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/Options.java (added)
+++ incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/Options.java Wed Aug 20 08:22:10 2008
@@ -0,0 +1,177 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.scripting.java;
+
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Properties;
+
+import org.osgi.service.component.ComponentContext;
+
+
+/**
+ * A class to hold all init parameters specific to the compiler
+ */
+public class Options {
+
+    private static final String PROPERTY_JAVA_ENCODING = "javaEncoding";
+
+    private static final String PROPERTY_COMPILER_SOURCE_V_M = "compilerSourceVM";
+
+    private static final String PROPERTY_COMPILER_TARGET_V_M = "compilerTargetVM";
+
+    private static final String PROPERTY_DEVELOPMENT = "development";
+
+    private static final String PROPERTY_OUTPUT_PATH = "outputPath";
+
+    private static final String PROPERTY_MODIFICATION_TEST_INTERVAL = "modificationTestInterval";
+
+    private static final String PROPERTY_CLASSDEBUGINFO = "classdebuginfo";
+
+    /** Default source and target VM version (value is "1.5"). */
+    private static final String DEFAULT_VM_VERSION = "1.5";
+
+    /**
+     * Is the engine being used in development mode?
+     */
+    private final boolean development;
+
+    /**
+     * Do we want to include debugging information in the class file?
+     */
+    private final boolean classDebugInfo;
+
+    /**
+     * Compiler target VM.
+     */
+    private final String compilerTargetVM;
+
+    /**
+     * The compiler source VM.
+     */
+    private final String compilerSourceVM;
+
+    /**
+     * Java platform encoding to generate the servlet.
+     */
+    private final String javaEncoding;
+
+    /**
+     * Modification test interval.
+     */
+    private final int modificationTestInterval;
+
+    /**
+     * Classloader
+     */
+    private final ClassLoader classLoader;
+
+    private final String destinationDir;
+
+    /**
+     * Create an compiler options object using data available from
+     * the component configuration.
+     */
+    public Options(final ComponentContext componentContext,
+                   final ClassLoader classLoader) {
+
+        this.classLoader = classLoader;
+
+        // generate properties
+        final Properties properties = new Properties();
+        // set default values first
+        properties.put(PROPERTY_CLASSDEBUGINFO, "true");
+        properties.put(PROPERTY_DEVELOPMENT, "true");
+        properties.put(PROPERTY_MODIFICATION_TEST_INTERVAL, "-1");
+        properties.put(PROPERTY_COMPILER_TARGET_V_M, DEFAULT_VM_VERSION);
+        properties.put(PROPERTY_COMPILER_SOURCE_V_M, DEFAULT_VM_VERSION);
+        properties.put(PROPERTY_JAVA_ENCODING, "UTF-8");
+        properties.put(PROPERTY_OUTPUT_PATH, "/var/classes");
+
+        // now check component properties
+        Dictionary<?, ?> config = componentContext.getProperties();
+        Enumeration<?> enumeration = config.keys();
+        while (enumeration.hasMoreElements()) {
+            String key = (String) enumeration.nextElement();
+            if (key.startsWith("java.")) {
+                Object value = config.get(key);
+                if (value != null) {
+                    properties.put(key.substring("java.".length()),
+                        value.toString());
+                }
+            }
+        }
+
+        this.destinationDir = properties.get(PROPERTY_OUTPUT_PATH).toString();
+        this.classDebugInfo = Boolean.valueOf(properties.get(PROPERTY_CLASSDEBUGINFO).toString());
+        this.modificationTestInterval = Integer.valueOf(properties.get(PROPERTY_MODIFICATION_TEST_INTERVAL).toString());
+        this.development = Boolean.valueOf(properties.get(PROPERTY_DEVELOPMENT).toString());
+        this.compilerTargetVM = properties.get(PROPERTY_COMPILER_TARGET_V_M).toString();
+        this.compilerSourceVM = properties.get(PROPERTY_COMPILER_SOURCE_V_M).toString();
+        this.javaEncoding = properties.get(PROPERTY_JAVA_ENCODING).toString();
+    }
+
+    /**
+     * Return the destination directory.
+     */
+    public String getDestinationPath() {
+        return this.destinationDir;
+    }
+
+    /**
+     * Should class files be compiled with debug information?
+     */
+    public boolean getClassDebugInfo() {
+        return this.classDebugInfo;
+    }
+
+    /**
+     * Modification test interval.
+     */
+    public int getModificationTestInterval() {
+        return this.modificationTestInterval;
+    }
+
+    /**
+     * Is the engine being used in development mode?
+     */
+    public boolean getDevelopment() {
+        return this.development;
+    }
+
+    public ClassLoader getClassLoader() {
+        return this.classLoader;
+    }
+
+    /**
+     * @see Options#getCompilerTargetVM
+     */
+    public String getCompilerTargetVM() {
+        return this.compilerTargetVM;
+    }
+
+    /**
+     * @see Options#getCompilerSourceVM
+     */
+    public String getCompilerSourceVM() {
+        return this.compilerSourceVM;
+    }
+
+    public String getJavaEncoding() {
+        return this.javaEncoding;
+    }
+}

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/Options.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/Options.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/Options.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletCache.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletCache.java?rev=687373&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletCache.java (added)
+++ incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletCache.java Wed Aug 20 08:22:10 2008
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.sling.scripting.java;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * This is a simple in memory cache for compiled servlets.
+ */
+public final class ServletCache {
+
+    /**
+     * Maps servlet source urls to servlet wrappers.
+     */
+    private Map<String, ServletWrapper> servlets = new ConcurrentHashMap<String, ServletWrapper>();
+
+    /**
+     * Add a new ServletWrapper.
+     *
+     * @param servletUri Servlet URI
+     * @param sw Servlet wrapper
+     */
+    public void addWrapper(String servletUri, ServletWrapper sw) {
+        servlets.put(servletUri, sw);
+    }
+
+    /**
+     * Get an already existing ServletWrapper.
+     *
+     * @param servletUri Servlet URI
+     * @return ServletWrapper
+     */
+    public ServletWrapper getWrapper(String servletUri) {
+        return servlets.get(servletUri);
+    }
+
+    /**
+     * Remove a  ServletWrapper.
+     *
+     * @param servletUri Servlet URI
+     */
+    public void removeWrapper(String servletUri) {
+        servlets.remove(servletUri);
+    }
+
+    /**
+     * Process a "destory" event for this web application context.
+     */
+    public void destroy() {
+        Iterator<ServletWrapper> i = this.servlets.values().iterator();
+        while (i.hasNext()) {
+            i.next().destroy();
+        }
+    }
+}

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletCache.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletCache.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java?rev=687373&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java (added)
+++ incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java Wed Aug 20 08:22:10 2008
@@ -0,0 +1,275 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.sling.scripting.java;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.SingleThreadModel;
+import javax.servlet.UnavailableException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.api.SlingException;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingIOException;
+import org.apache.sling.api.SlingServletException;
+import org.apache.sling.api.scripting.SlingBindings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+
+ */
+
+public class ServletWrapper {
+
+    /** The logger. */
+    private final Logger logger = LoggerFactory.getLogger(getClass());
+
+    private final String servletUri;
+
+    /** The servlet config. */
+    private ServletConfig config;
+
+    /** Reload flag. */
+    private boolean reload = true;
+
+    /** Compiler options. */
+    private final Options options;
+
+    private Servlet theServlet;
+    private long available = 0L;
+    private boolean firstTime = true;
+    private ServletException compileException;
+    private CompilationContext ctxt;
+
+    /**
+     * A wrapper for servlets.
+     */
+    public ServletWrapper(final ServletConfig config,
+                          final Options options,
+                          final SlingIOProvider ioProvider,
+                          final String servletPath,
+                          final ServletCache servletCache) {
+        this.config = config;
+        this.servletUri = servletPath;
+        this.options = options;
+        this.ctxt = new CompilationContext(servletUri, options,
+                ioProvider, servletCache, this);
+    }
+
+    /**
+     * Set the reload flag.
+     * @param reload
+     */
+    public void setReload(boolean reload) {
+        this.reload = reload;
+    }
+
+    /**
+     * Get the servlet.
+     * @return The servlet.
+     * @throws ServletException
+     * @throws IOException
+     * @throws FileNotFoundException
+     */
+    private Servlet getServlet()
+    throws ServletException, IOException, FileNotFoundException {
+        if (reload) {
+            synchronized (this) {
+                if (reload) {
+                    destroy();
+
+                    Servlet servlet = null;
+
+                    try {
+                        final Class servletClass = ctxt.load();
+                        servlet = (Servlet) servletClass.newInstance();
+                    } catch (IllegalAccessException e) {
+                        throw new ServletException(e);
+                    } catch (InstantiationException e) {
+                        throw new ServletException(e);
+                    } catch (Exception e) {
+                        throw new ServletException(e);
+                    }
+
+                    servlet.init(config);
+
+                    theServlet = servlet;
+                    reload = false;
+                }
+            }
+        }
+        return theServlet;
+    }
+
+    /**
+     * Sets the compilation exception for this ServletWrapper.
+     *
+     * @param je The compilation exception
+     */
+    public void setCompilationException(ServletException je) {
+        this.compileException = je;
+    }
+
+    /**
+     * @param bindings
+     * @throws SlingIOException
+     * @throws SlingServletException
+     * @throws IllegalArgumentException if the Jasper Precompile controller
+     *             request parameter has an illegal value.
+     */
+    public void service(SlingBindings bindings) {
+        final SlingHttpServletRequest request = bindings.getRequest();
+        final Object oldValue = request.getAttribute(SlingBindings.class.getName());
+        try {
+            request.setAttribute(SlingBindings.class.getName(), bindings);
+            service(request, bindings.getResponse());
+        } catch (SlingException se) {
+            // rethrow as is
+            throw se;
+        } catch (IOException ioe) {
+            throw new SlingIOException(ioe);
+        } catch (ServletException se) {
+            throw new SlingServletException(se);
+        } finally {
+            request.setAttribute(SlingBindings.class.getName(), oldValue);
+        }
+    }
+
+    /**
+     * Call the servlet.
+     * @param request The current request.
+     * @param response The current response.
+     * @throws ServletException
+     * @throws IOException
+     * @throws FileNotFoundException
+     */
+    public void service(HttpServletRequest request,
+                        HttpServletResponse response)
+    throws ServletException, IOException, FileNotFoundException {
+
+        try {
+
+            if (ctxt.isRemoved()) {
+                throw new FileNotFoundException(servletUri);
+            }
+
+            if ((available > 0L) && (available < Long.MAX_VALUE)) {
+                if (available > System.currentTimeMillis()) {
+                    response.setDateHeader("Retry-After", available);
+                    response.sendError
+                        (HttpServletResponse.SC_SERVICE_UNAVAILABLE,
+                         "Servlet unavailable.");
+                    return;
+                }
+                // Wait period has expired. Reset.
+                available = 0;
+            }
+
+            /*
+             * (1) Compile
+             */
+            if (options.getDevelopment() || firstTime ) {
+                synchronized (this) {
+                    firstTime = false;
+
+                    // The following sets reload to true, if necessary
+                    ctxt.compile();
+                }
+            } else {
+                if (compileException != null) {
+                    // Throw cached compilation exception
+                    throw compileException;
+                }
+            }
+
+            /*
+             * (2) (Re)load servlet class file
+             */
+            getServlet();
+
+        } catch (FileNotFoundException ex) {
+            ctxt.incrementRemoved();
+            try {
+                response.sendError(HttpServletResponse.SC_NOT_FOUND,
+                                  ex.getMessage());
+            } catch (IllegalStateException ise) {
+                logger.error("Java servlet source not found." +
+                       ex.getMessage(), ex);
+            }
+        } catch (ServletException ex) {
+            throw ex;
+        } catch (IOException ex) {
+            throw ex;
+        } catch (IllegalStateException ex) {
+            throw ex;
+        } catch (Exception ex) {
+            throw new ServletException(ex);
+        }
+
+        try {
+
+            /*
+             * (3) Service request
+             */
+            if (theServlet instanceof SingleThreadModel) {
+               // sync on the wrapper so that the freshness
+               // of the page is determined right before servicing
+               synchronized (this) {
+                   theServlet.service(request, response);
+                }
+            } else {
+                theServlet.service(request, response);
+            }
+
+        } catch (UnavailableException ex) {
+            int unavailableSeconds = ex.getUnavailableSeconds();
+            if (unavailableSeconds <= 0) {
+                unavailableSeconds = 60;        // Arbitrary default
+            }
+            available = System.currentTimeMillis() +
+                (unavailableSeconds * 1000L);
+            response.sendError
+                (HttpServletResponse.SC_SERVICE_UNAVAILABLE,
+                 ex.getMessage());
+        } catch (ServletException ex) {
+            throw ex;
+        } catch (IOException ex) {
+            throw ex;
+        } catch (IllegalStateException ex) {
+            throw ex;
+        } catch (Exception ex) {
+            throw new ServletException(ex);
+        }
+    }
+
+    /**
+     * Destroy the servlet.
+     */
+    public void destroy() {
+        if (theServlet != null) {
+            theServlet.destroy();
+            theServlet = null;
+        }
+    }
+}

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: incubator/sling/trunk/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message