This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-groovy.git
commit 09325fcd7a82730a57b4c62fe8a29d1157977cdf Author: Robert Munteanu <[email protected]> AuthorDate: Tue Sep 2 14:18:42 2014 +0000 SLING-3888 - Move groovy extension bundle to contrib/scripting Relocate the groovy extension bundle under contrib/scripting and also move it to the contrib reactor from the main reactor. git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1622011 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 96 ++++++++++++++++++++++ .../JsonBuilderBindingsValuesProvider.java | 46 +++++++++++ .../groovy/scripting/internal/GSPScriptEngine.java | 69 ++++++++++++++++ .../scripting/internal/GSPScriptEngineFactory.java | 68 +++++++++++++++ 4 files changed, 279 insertions(+) diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7cfa2a5 --- /dev/null +++ b/pom.xml @@ -0,0 +1,96 @@ +<?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> + <artifactId>sling</artifactId> + <groupId>org.apache.sling</groupId> + <version>20</version> + <relativePath>../../../parent/pom.xml</relativePath> + </parent> + + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.scripting.groovy</artifactId> + <version>1.0.0-SNAPSHOT</version> + <packaging>bundle</packaging> + + <name>Apache Sling Scripting Groovy Support</name> + <description> + Support for scripting with Groovy + </description> + + <scm> + <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/groovy</connection> + <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/groovy</developerConnection> + <url>http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/groovy</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> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>org.codehaus.groovy</groupId> + <artifactId>groovy-all</artifactId> + <version>1.8.0</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.scr.annotations</artifactId> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.scripting.api</artifactId> + <version>2.0.2-incubator</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.commons.classloader</artifactId> + <version>1.0.0</version> + <scope>provided</scope> + </dependency> + </dependencies> +</project> diff --git a/src/main/java/org/apache/sling/scripting/groovy/json/internal/JsonBuilderBindingsValuesProvider.java b/src/main/java/org/apache/sling/scripting/groovy/json/internal/JsonBuilderBindingsValuesProvider.java new file mode 100644 index 0000000..520f9ca --- /dev/null +++ b/src/main/java/org/apache/sling/scripting/groovy/json/internal/JsonBuilderBindingsValuesProvider.java @@ -0,0 +1,46 @@ +/* + * 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.groovy.json.internal; + +import groovy.json.JsonBuilder; + +import java.util.HashMap; + +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Properties; +import org.apache.felix.scr.annotations.Property; +import org.apache.felix.scr.annotations.Service; + +/** + * BindingsValuesProvider which binds an instance of JsonBuilder. + */ +@SuppressWarnings("serial") +@Component(immediate=true, metatype=false) +@Service +@Properties({ + @Property(name="service.description", value="JSONGroovyBuilder BindingsValuesProvider"), + @Property(name="service.vendor", value="The Apache Software Foundation"), + @Property(name="javax.script.name", value="groovy") +}) +public class JsonBuilderBindingsValuesProvider extends HashMap<String, Object> { + + public JsonBuilderBindingsValuesProvider() { + super(); + put("jsonBuilder", new JsonBuilder()); + } + +} diff --git a/src/main/java/org/apache/sling/scripting/groovy/scripting/internal/GSPScriptEngine.java b/src/main/java/org/apache/sling/scripting/groovy/scripting/internal/GSPScriptEngine.java new file mode 100644 index 0000000..f2fca41 --- /dev/null +++ b/src/main/java/org/apache/sling/scripting/groovy/scripting/internal/GSPScriptEngine.java @@ -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.scripting.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, ClassLoader classLoader) { + super(scriptEngineFactory); + this.templateEngine = new GStringTemplateEngine(classLoader); + } + + 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; + } + +} diff --git a/src/main/java/org/apache/sling/scripting/groovy/scripting/internal/GSPScriptEngineFactory.java b/src/main/java/org/apache/sling/scripting/groovy/scripting/internal/GSPScriptEngineFactory.java new file mode 100644 index 0000000..b00fb97 --- /dev/null +++ b/src/main/java/org/apache/sling/scripting/groovy/scripting/internal/GSPScriptEngineFactory.java @@ -0,0 +1,68 @@ +/* + * 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.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.Properties; +import org.apache.felix.scr.annotations.Property; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.Service; +import org.apache.sling.commons.classloader.DynamicClassLoaderManager; +import org.apache.sling.scripting.api.AbstractScriptEngineFactory; + +/** + * Script engine for Groovy Scripting Pages. + */ +@Component +@Service +@Properties({ + @Property(name="service.vendor", value="The Apache Software Foundation"), + @Property(name="service.description", value="GSP Script Engine"), + @Property(name="compatible.javax.script.name", value="groovy") +}) +public class GSPScriptEngineFactory extends AbstractScriptEngineFactory { + + public GSPScriptEngineFactory() { + setNames("gsp", "GSP"); + } + + @Reference + private DynamicClassLoaderManager dynamicClassLoaderManager; + + 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, dynamicClassLoaderManager.getDynamicClassLoader()); + } + +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
