This is an automated email from the ASF dual-hosted git repository. olli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-groovy.git
commit 7c8b8ad84ec66500281e84875d38fa18054b58e1 Author: Oliver Lietz <[email protected]> AuthorDate: Thu Dec 28 17:46:29 2017 +0100 SLING-7336 Use OSGi R6 annotations --- pom.xml | 76 +++++++++++++---- .../GspScriptEngine.java} | 38 ++++----- .../groovy/internal/GspScriptEngineFactory.java | 96 ++++++++++++++++++++++ .../GspScriptEngineFactoryConfiguration.java | 59 +++++++++++++ .../JsonBuilderBindingsValuesProvider.java | 40 ++++----- .../scripting/internal/GSPScriptEngineFactory.java | 71 ---------------- 6 files changed, 252 insertions(+), 128 deletions(-) diff --git a/pom.xml b/pom.xml index 25f0455..a955284 100644 --- a/pom.xml +++ b/pom.xml @@ -51,44 +51,55 @@ <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> + <ScriptEngine-Name>${project.name}</ScriptEngine-Name> + <ScriptEngine-Version>${project.version}</ScriptEngine-Version> + <_removeheaders> + Embed-Dependency, + Private-Package, + Include-Resource + </_removeheaders> + </instructions> + </configuration> </plugin> </plugins> </build> <dependencies> + <!-- OSGi --> <dependency> - <groupId>org.codehaus.groovy</groupId> - <artifactId>groovy</artifactId> - <version>${groovy.version}</version> + <groupId>org.osgi</groupId> + <artifactId>osgi.annotation</artifactId> <scope>provided</scope> </dependency> <dependency> - <groupId>org.codehaus.groovy</groupId> - <artifactId>groovy-json</artifactId> - <version>${groovy.version}</version> + <groupId>org.osgi</groupId> + <artifactId>osgi.cmpn</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>osgi.core</artifactId> <scope>provided</scope> </dependency> <dependency> - <groupId>org.codehaus.groovy</groupId> - <artifactId>groovy-templates</artifactId> - <version>${groovy.version}</version> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.service.component.annotations</artifactId> <scope>provided</scope> </dependency> <dependency> - <groupId>org.apache.felix</groupId> - <artifactId>org.apache.felix.scr.annotations</artifactId> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.service.metatype.annotations</artifactId> <scope>provided</scope> </dependency> + <!-- Apache Sling --> <dependency> <groupId>org.apache.sling</groupId> - <artifactId>org.apache.sling.scripting.api</artifactId> - <version>2.1.0</version> + <artifactId>org.apache.sling.api</artifactId> + <version>2.11.0</version> <scope>provided</scope> </dependency> <dependency> @@ -97,6 +108,37 @@ <version>1.0.0</version> <scope>provided</scope> </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.scripting.api</artifactId> + <version>2.1.0</version> + <scope>provided</scope> + </dependency> + <!-- Groovy --> + <dependency> + <groupId>org.codehaus.groovy</groupId> + <artifactId>groovy</artifactId> + <version>${groovy.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.codehaus.groovy</groupId> + <artifactId>groovy-json</artifactId> + <version>${groovy.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.codehaus.groovy</groupId> + <artifactId>groovy-templates</artifactId> + <version>${groovy.version}</version> + <scope>provided</scope> + </dependency> + <!-- logging --> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <scope>provided</scope> + </dependency> </dependencies> </project> diff --git a/src/main/java/org/apache/sling/scripting/groovy/scripting/internal/GSPScriptEngine.java b/src/main/java/org/apache/sling/scripting/groovy/internal/GspScriptEngine.java similarity index 68% rename from src/main/java/org/apache/sling/scripting/groovy/scripting/internal/GSPScriptEngine.java rename to src/main/java/org/apache/sling/scripting/groovy/internal/GspScriptEngine.java index f2fca41..b7ddf20 100644 --- a/src/main/java/org/apache/sling/scripting/groovy/scripting/internal/GSPScriptEngine.java +++ b/src/main/java/org/apache/sling/scripting/groovy/internal/GspScriptEngine.java @@ -14,51 +14,49 @@ * 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; +package org.apache.sling.scripting.groovy.internal; import java.io.IOException; import java.io.Reader; +import java.io.Writer; import javax.script.Bindings; import javax.script.ScriptContext; import javax.script.ScriptEngineFactory; import javax.script.ScriptException; +import groovy.lang.Writable; +import groovy.text.GStringTemplateEngine; +import groovy.text.Template; +import groovy.text.TemplateEngine; import org.apache.sling.scripting.api.AbstractSlingScriptEngine; /** - * The actual GSP Script Engine, which simply wraps Groovy's + * The actual GSP Script Engine, which simply wraps Groovy's */ -public class GSPScriptEngine extends AbstractSlingScriptEngine { +public class GspScriptEngine extends AbstractSlingScriptEngine { - private TemplateEngine templateEngine; + private final TemplateEngine templateEngine; - public GSPScriptEngine(ScriptEngineFactory scriptEngineFactory, ClassLoader classLoader) { + public GspScriptEngine(final ScriptEngineFactory scriptEngineFactory, final ClassLoader classLoader) { super(scriptEngineFactory); this.templateEngine = new GStringTemplateEngine(classLoader); } - - public Object eval(Reader reader, ScriptContext ctx) throws ScriptException { - Template template = null; + + public Object eval(final Reader reader, final ScriptContext scriptContext) throws ScriptException { + Template template; try { template = templateEngine.createTemplate(reader); - } catch (IOException e) { - throw new ScriptException("Unable to compile GSP script: " + e.getMessage()); - } catch (ClassNotFoundException e) { + } catch (IOException | ClassNotFoundException e) { throw new ScriptException("Unable to compile GSP script: " + e.getMessage()); } - Bindings bindings = ctx.getBindings(ScriptContext.ENGINE_SCOPE); - - Writable result = template.make(bindings); + final Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE); + final Writer writer = scriptContext.getWriter(); + final Writable result = template.make(bindings); try { - result.writeTo(ctx.getWriter()); + result.writeTo(writer); } catch (IOException e) { throw new ScriptException("Unable to write result of script execution: " + e.getMessage()); } diff --git a/src/main/java/org/apache/sling/scripting/groovy/internal/GspScriptEngineFactory.java b/src/main/java/org/apache/sling/scripting/groovy/internal/GspScriptEngineFactory.java new file mode 100644 index 0000000..1d245c0 --- /dev/null +++ b/src/main/java/org/apache/sling/scripting/groovy/internal/GspScriptEngineFactory.java @@ -0,0 +1,96 @@ +/* + * 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.internal; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineFactory; + +import org.apache.sling.commons.classloader.DynamicClassLoaderManager; +import org.apache.sling.scripting.api.AbstractScriptEngineFactory; +import org.codehaus.groovy.util.ReleaseInfo; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; +import org.osgi.service.component.annotations.Modified; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.metatype.annotations.Designate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Script engine for Groovy Server Pages. + */ +@Component( + service = ScriptEngineFactory.class, + property = { + "service.description=GSP Script Engine", + "service.vendor=The Apache Software Foundation" + } +) +@Designate( + ocd = GspScriptEngineFactoryConfiguration.class +) +public class GspScriptEngineFactory extends AbstractScriptEngineFactory { + + @Reference + private DynamicClassLoaderManager dynamicClassLoaderManager; + + private GspScriptEngineFactoryConfiguration configuration; + + private final Logger logger = LoggerFactory.getLogger(GspScriptEngineFactory.class); + + public GspScriptEngineFactory() { + } + + @Activate + private void activate(final GspScriptEngineFactoryConfiguration configuration) { + logger.debug("activating"); + this.configuration = configuration; + configure(configuration); + } + + @Modified + private void modified(final GspScriptEngineFactoryConfiguration configuration) { + logger.debug("modifying"); + this.configuration = configuration; + configure(configuration); + } + + @Deactivate + private void deactivate() { + logger.debug("deactivating"); + } + + private void configure(final GspScriptEngineFactoryConfiguration configuration) { + setExtensions(configuration.extensions()); + setMimeTypes(configuration.mimeTypes()); + setNames(configuration.names()); + } + + public String getLanguageName() { + return "Groovy Server Pages"; + } + + public String getLanguageVersion() { + return ReleaseInfo.getVersion(); + } + + public ScriptEngine getScriptEngine() { + return new GspScriptEngine(this, dynamicClassLoaderManager.getDynamicClassLoader()); + } + +} diff --git a/src/main/java/org/apache/sling/scripting/groovy/internal/GspScriptEngineFactoryConfiguration.java b/src/main/java/org/apache/sling/scripting/groovy/internal/GspScriptEngineFactoryConfiguration.java new file mode 100644 index 0000000..d4cb8ff --- /dev/null +++ b/src/main/java/org/apache/sling/scripting/groovy/internal/GspScriptEngineFactoryConfiguration.java @@ -0,0 +1,59 @@ +/* + * 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.internal; + +import org.osgi.service.metatype.annotations.AttributeDefinition; +import org.osgi.service.metatype.annotations.ObjectClassDefinition; + +@ObjectClassDefinition( + name = "Apache Sling Scripting Groovy GSP “ScriptEngineFactory”", + description = "Script engine factory for Groovy's GString template engine." +) +@interface GspScriptEngineFactoryConfiguration { + + @AttributeDefinition( + name = "service ranking", + description = "Service property for identifying the service's ranking number." + ) + int service_ranking() default 0; + + @AttributeDefinition( + name = "extensions", + description = "The extensions this script engine is registered for." + ) + String[] extensions() default { + "gsp" + }; + + @AttributeDefinition( + name = "mime types", + description = "The MIME (content) types this script engine is registered for." + ) + String[] mimeTypes() default { + "text/x-gsp" + }; + + @AttributeDefinition( + name = "names", + description = "The names under which this script engine is registered." + ) + String[] names() default { + "GSP", + "gsp" + }; + +} diff --git a/src/main/java/org/apache/sling/scripting/groovy/json/internal/JsonBuilderBindingsValuesProvider.java b/src/main/java/org/apache/sling/scripting/groovy/internal/JsonBuilderBindingsValuesProvider.java similarity index 53% rename from src/main/java/org/apache/sling/scripting/groovy/json/internal/JsonBuilderBindingsValuesProvider.java rename to src/main/java/org/apache/sling/scripting/groovy/internal/JsonBuilderBindingsValuesProvider.java index 520f9ca..2b10d0b 100644 --- a/src/main/java/org/apache/sling/scripting/groovy/json/internal/JsonBuilderBindingsValuesProvider.java +++ b/src/main/java/org/apache/sling/scripting/groovy/internal/JsonBuilderBindingsValuesProvider.java @@ -14,33 +14,33 @@ * License for the specific language governing permissions and limitations under * the License. */ -package org.apache.sling.scripting.groovy.json.internal; +package org.apache.sling.scripting.groovy.internal; -import groovy.json.JsonBuilder; - -import java.util.HashMap; +import javax.script.Bindings; -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; +import groovy.json.JsonBuilder; +import org.apache.sling.scripting.api.BindingsValuesProvider; +import org.osgi.service.component.annotations.Component; /** * 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> { +@Component( + immediate = true, + service = BindingsValuesProvider.class, + property = { + "javax.script.name=gsp", + "service.description=Groovy JsonBuilder BindingsValuesProvider", + "service.vendor=The Apache Software Foundation" + + } +) +public class JsonBuilderBindingsValuesProvider implements BindingsValuesProvider { - public JsonBuilderBindingsValuesProvider() { - super(); - put("jsonBuilder", new JsonBuilder()); + @Override + public void addBindings(final Bindings bindings) { + final JsonBuilder jsonBuilder = new JsonBuilder(); + bindings.put("jsonBuilder", jsonBuilder); } } 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 deleted file mode 100644 index bdef117..0000000 --- a/src/main/java/org/apache/sling/scripting/groovy/scripting/internal/GSPScriptEngineFactory.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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; -import org.codehaus.groovy.util.ReleaseInfo; - -/** - * Script engine for Groovy Server Pages. - */ -@Component -@Service -@Properties({ - @Property(name="service.vendor", value="The Apache Software Foundation"), - @Property(name="service.description", value="GSP Script Engine"), - @Property(name="extensions", value = {"gsp"}), - @Property(name="names", value = {"gsp", "GSP"}), - @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 Server Pages"; - } - - @Override - public List<String> getExtensions() { - return Collections.singletonList("gsp"); - } - - public String getLanguageVersion() { - return ReleaseInfo.getVersion(); - } - - public ScriptEngine getScriptEngine() { - return new GSPScriptEngine(this, dynamicClassLoaderManager.getDynamicClassLoader()); - } - -} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
