Author: justin
Date: Tue Aug 17 17:55:17 2010
New Revision: 986420

URL: http://svn.apache.org/viewvc?rev=986420&view=rev
Log:
SLING-1662 - creating gsp scripting engine

Added:
    
sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/
    
sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/
    
sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/GSPScriptEngine.java
    
sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/GSPScriptEngineFactory.java
    
sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/rendering-test.gsp
Modified:
    sling/trunk/bundles/extensions/groovy/pom.xml
    
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/SlingResourceTypeRenderingTest.java

Modified: sling/trunk/bundles/extensions/groovy/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/groovy/pom.xml?rev=986420&r1=986419&r2=986420&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/groovy/pom.xml (original)
+++ sling/trunk/bundles/extensions/groovy/pom.xml Tue Aug 17 17:55:17 2010
@@ -99,5 +99,11 @@
             <version>1.0.0</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.scripting.api</artifactId>
+            <version>2.0.2-incubator</version>
+            <scope>provided</scope>
+        </dependency>
     </dependencies>
 </project>
\ No newline at end of file

Added: 
sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/GSPScriptEngine.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/GSPScriptEngine.java?rev=986420&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/GSPScriptEngine.java
 (added)
+++ 
sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/GSPScriptEngine.java
 Tue Aug 17 17:55:17 2010
@@ -0,0 +1,69 @@
+/*
+ * 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.extensions.groovy.scripting.internal;
+
+import groovy.lang.Writable;
+import groovy.text.GStringTemplateEngine;
+import groovy.text.Template;
+import groovy.text.TemplateEngine;
+
+import java.io.IOException;
+import java.io.Reader;
+
+import javax.script.Bindings;
+import javax.script.ScriptContext;
+import javax.script.ScriptEngineFactory;
+import javax.script.ScriptException;
+
+import org.apache.sling.scripting.api.AbstractSlingScriptEngine;
+
+/**
+ * The actual GSP Script Engine, which simply wraps Groovy's 
+ */
+public class GSPScriptEngine extends AbstractSlingScriptEngine {
+
+    private TemplateEngine templateEngine;
+
+    public GSPScriptEngine(ScriptEngineFactory scriptEngineFactory) {
+        super(scriptEngineFactory);
+        this.templateEngine = new GStringTemplateEngine();
+    }
+    
+    public Object eval(Reader reader, ScriptContext ctx) throws 
ScriptException {
+        Template template = null;
+        try {
+            template = templateEngine.createTemplate(reader);
+        } catch (IOException e) {
+            throw new ScriptException("Unable to compile GSP script: " + 
e.getMessage());
+        } catch (ClassNotFoundException e) {
+            throw new ScriptException("Unable to compile GSP script: " + 
e.getMessage());
+        }
+
+        Bindings bindings = ctx.getBindings(ScriptContext.ENGINE_SCOPE);
+
+        Writable result = template.make(bindings);
+
+        try {
+            result.writeTo(ctx.getWriter());
+        } catch (IOException e) {
+            throw new ScriptException("Unable to write result of script 
execution: " + e.getMessage());
+        }
+
+        return null;
+    }
+
+}

Added: 
sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/GSPScriptEngineFactory.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/GSPScriptEngineFactory.java?rev=986420&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/GSPScriptEngineFactory.java
 (added)
+++ 
sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/GSPScriptEngineFactory.java
 Tue Aug 17 17:55:17 2010
@@ -0,0 +1,52 @@
+/*
+ * 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.extensions.groovy.scripting.internal;
+
+import java.util.Collections;
+import java.util.List;
+
+import javax.script.ScriptEngine;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.scripting.api.AbstractScriptEngineFactory;
+
+/**
+ * Script engine for Groovy Scripting Pages.
+ */
+...@component
+...@service
+public class GSPScriptEngineFactory extends AbstractScriptEngineFactory {
+
+    public String getLanguageName() {
+        return "Groovy Scripting Pages";
+    }
+    
+    @Override
+    public List<String> getExtensions() {
+        return Collections.singletonList("gsp");
+    }
+
+    public String getLanguageVersion() {
+        return "1.0";
+    }
+
+    public ScriptEngine getScriptEngine() {
+        return new GSPScriptEngine(this);
+    }
+
+}

Modified: 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/SlingResourceTypeRenderingTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/SlingResourceTypeRenderingTest.java?rev=986420&r1=986419&r2=986420&view=diff
==============================================================================
--- 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/SlingResourceTypeRenderingTest.java
 (original)
+++ 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/SlingResourceTypeRenderingTest.java
 Tue Aug 17 17:55:17 2010
@@ -74,6 +74,18 @@ public class SlingResourceTypeRenderingT
         }
     }
 
+    public void testGspHtml() throws IOException {
+        final String toDelete = 
uploadTestScript("rendering-test.gsp","html.gsp");
+        try {
+            final String content = getContent(displayUrl + ".html", 
CONTENT_TYPE_HTML);
+            assertContains(content, "GSP template");
+            assertContains(content, "<p>" + testText + "</p>");
+            assertContains(content, "<div class=\"SLING-142\" id=\"22\"/>");
+        } finally {
+            testClient.delete(toDelete);
+        }
+    }
+
     public void testEspJavaCode() throws IOException {
         final String toDelete = 
uploadTestScript("rendering-test.esp","html.esp");
         try {
@@ -85,6 +97,17 @@ public class SlingResourceTypeRenderingT
         }
     }
 
+    public void testGspJavaCode() throws IOException {
+        final String toDelete = 
uploadTestScript("rendering-test.gsp","html.gsp");
+        try {
+            final String content = getContent(displayUrl + ".html", 
CONTENT_TYPE_HTML);
+            assertContains(content, "GSP template");
+            assertContains(content, "TestLinkedListTest");
+        } finally {
+            testClient.delete(toDelete);
+        }
+    }
+
     public void testEspHtmlInAppsFolder() throws IOException {
         // make sure there's no leftover rendering script
         {
@@ -105,6 +128,26 @@ public class SlingResourceTypeRenderingT
         }
     }
 
+    public void testGspHtmlInAppsFolder() throws IOException {
+        // make sure there's no leftover rendering script
+        {
+            final String content = getContent(displayUrl + ".html", 
CONTENT_TYPE_HTML);
+            assertFalse("Content must not contain script marker before 
testing", content.contains("GSP template"));
+        }
+
+        // put our script under /apps/<resource type>
+        final String path = "/apps/" + slingResourceType;
+        testClient.mkdirs(WEBDAV_BASE_URL, path);
+        final String toDelete = 
uploadTestScript(path,"rendering-test.gsp","html.gsp");
+        try {
+            final String content = getContent(displayUrl + ".html", 
CONTENT_TYPE_HTML);
+            assertContains(content, "GSP template");
+            assertContains(content, "<p>" + testText + "</p>");
+        } finally {
+            testClient.delete(toDelete);
+        }
+    }
+
     public void TODO_FAILS_testEspHtmlWithContentBasedPath() throws 
IOException {
 
         // make sure there's no leftover rendering script

Added: 
sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/rendering-test.gsp
URL: 
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/rendering-test.gsp?rev=986420&view=auto
==============================================================================
--- 
sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/rendering-test.gsp
 (added)
+++ 
sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/rendering-test.gsp
 Tue Aug 17 17:55:17 2010
@@ -0,0 +1,37 @@
+/*
+ * 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.
+ */
+ 
+<!-- used by ScriptedRenderingTest -->
+<html>
+       <body>
+               GSP template
+               <p><%= currentNode.getProperty("text").getString() %></p>
+       
+               <!-- test SLING-142, compact syntax -->
+               <div class="SLING-142" id="${'2' + '2'}"/>
+       
+               <!-- test access to microsling java classes -->
+               <%
+                       def list = new LinkedList()
+                       list << "LinkedListTest"
+               %>
+               <p>Test${list[0]}</p>
+
+       </body>
+</html>


Reply via email to