You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ol...@apache.org on 2018/01/02 10:28:24 UTC

[sling-org-apache-sling-scripting-freemarker] 04/06: SLING-7342 Use Configuration provided as OSGi service

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-freemarker.git

commit 9e31799f3d297ccba7a9d98d6bcdd2a263813ff8
Author: Oliver Lietz <ol...@apache.org>
AuthorDate: Tue Jan 2 11:23:25 2018 +0100

    SLING-7342 Use Configuration provided as OSGi service
---
 .../internal/FreemarkerScriptEngine.java           |  6 +---
 .../internal/FreemarkerScriptEngineFactory.java    | 24 ++++++++++++++
 .../freemarker/it/app/Ranked1Configuration.java    | 37 ++++++++++++++++++++++
 .../freemarker/it/app/Ranked2Configuration.java    | 37 ++++++++++++++++++++++
 .../it/tests/FreemarkerScriptEngineFactoryIT.java  | 24 +++++++++++++-
 5 files changed, 122 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/apache/sling/scripting/freemarker/internal/FreemarkerScriptEngine.java b/src/main/java/org/apache/sling/scripting/freemarker/internal/FreemarkerScriptEngine.java
index 12d4f0c..7436fa1 100644
--- a/src/main/java/org/apache/sling/scripting/freemarker/internal/FreemarkerScriptEngine.java
+++ b/src/main/java/org/apache/sling/scripting/freemarker/internal/FreemarkerScriptEngine.java
@@ -17,7 +17,6 @@
 package org.apache.sling.scripting.freemarker.internal;
 
 import java.io.Reader;
-import java.nio.charset.StandardCharsets;
 
 import javax.script.Bindings;
 import javax.script.ScriptContext;
@@ -32,8 +31,6 @@ import org.apache.sling.scripting.api.AbstractSlingScriptEngine;
 
 public class FreemarkerScriptEngine extends AbstractSlingScriptEngine {
 
-    private final Configuration configuration;
-
     private final FreemarkerScriptEngineFactory freemarkerScriptEngineFactory;
 
     private final Logger logger = Logger.getLogger(FreemarkerScriptEngine.class.getName());
@@ -41,8 +38,6 @@ public class FreemarkerScriptEngine extends AbstractSlingScriptEngine {
     public FreemarkerScriptEngine(final FreemarkerScriptEngineFactory freemarkerScriptEngineFactory) {
         super(freemarkerScriptEngineFactory);
         this.freemarkerScriptEngineFactory = freemarkerScriptEngineFactory;
-        configuration = new Configuration(Configuration.getVersion());
-        configuration.setDefaultEncoding(StandardCharsets.UTF_8.name());
     }
 
     public Object eval(final Reader reader, final ScriptContext scriptContext) throws ScriptException {
@@ -55,6 +50,7 @@ public class FreemarkerScriptEngine extends AbstractSlingScriptEngine {
         bindings.putAll(freemarkerScriptEngineFactory.getTemplateModels());
 
         final String scriptName = helper.getScript().getScriptResource().getPath();
+        final Configuration configuration = freemarkerScriptEngineFactory.getConfiguration();
 
         try {
             final Template template = new Template(scriptName, reader, configuration);
diff --git a/src/main/java/org/apache/sling/scripting/freemarker/internal/FreemarkerScriptEngineFactory.java b/src/main/java/org/apache/sling/scripting/freemarker/internal/FreemarkerScriptEngineFactory.java
index ef11ede..c9fab1c 100644
--- a/src/main/java/org/apache/sling/scripting/freemarker/internal/FreemarkerScriptEngineFactory.java
+++ b/src/main/java/org/apache/sling/scripting/freemarker/internal/FreemarkerScriptEngineFactory.java
@@ -37,6 +37,10 @@ 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.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.ReferencePolicy;
+import org.osgi.service.component.annotations.ReferencePolicyOption;
 import org.osgi.service.metatype.annotations.Designate;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -54,15 +58,26 @@ import org.slf4j.LoggerFactory;
 )
 public class FreemarkerScriptEngineFactory extends AbstractScriptEngineFactory {
 
+    @Reference(
+        cardinality = ReferenceCardinality.OPTIONAL,
+        policy = ReferencePolicy.DYNAMIC,
+        policyOption = ReferencePolicyOption.GREEDY
+    )
+    private volatile Configuration configuration;
+
     private BundleContext bundleContext;
 
     private SortingServiceTracker<TemplateModel> templateModelTracker;
 
+    private final Configuration defaultConfiguration;
+
     private static final String FREEMARKER_NAME = "FreeMarker";
 
     private final Logger logger = LoggerFactory.getLogger(FreemarkerScriptEngineFactory.class);
 
     public FreemarkerScriptEngineFactory() {
+        defaultConfiguration = new Configuration(Configuration.getVersion());
+        defaultConfiguration.setDefaultEncoding(StandardCharsets.UTF_8.name());
     }
 
     @Activate
@@ -106,6 +121,15 @@ public class FreemarkerScriptEngineFactory extends AbstractScriptEngineFactory {
         return Configuration.getVersion().toString();
     }
 
+    Configuration getConfiguration() {
+        final Configuration configuration = this.configuration;
+        if (configuration != null) {
+            return configuration;
+        } else {
+            return defaultConfiguration;
+        }
+    }
+
     Map<String, Map<String, TemplateModel>> getTemplateModels() {
         final Map<String, Map<String, TemplateModel>> models = new HashMap<>();
         for (final ServiceReference<TemplateModel> serviceReference : templateModelTracker.getSortedServiceReferences()) {
diff --git a/src/test/java/org/apache/sling/scripting/freemarker/it/app/Ranked1Configuration.java b/src/test/java/org/apache/sling/scripting/freemarker/it/app/Ranked1Configuration.java
new file mode 100644
index 0000000..41317c5
--- /dev/null
+++ b/src/test/java/org/apache/sling/scripting/freemarker/it/app/Ranked1Configuration.java
@@ -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.
+ */
+package org.apache.sling.scripting.freemarker.it.app;
+
+import freemarker.template.Configuration;
+import org.osgi.service.component.annotations.Component;
+
+@Component(
+    service = Configuration.class,
+    property = {
+        "name=foo",
+        "service.ranking:Integer=1"
+    }
+)
+public class Ranked1Configuration extends Configuration {
+
+    public Ranked1Configuration() {
+        super(Configuration.getVersion());
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/scripting/freemarker/it/app/Ranked2Configuration.java b/src/test/java/org/apache/sling/scripting/freemarker/it/app/Ranked2Configuration.java
new file mode 100644
index 0000000..d708395
--- /dev/null
+++ b/src/test/java/org/apache/sling/scripting/freemarker/it/app/Ranked2Configuration.java
@@ -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.
+ */
+package org.apache.sling.scripting.freemarker.it.app;
+
+import freemarker.template.Configuration;
+import org.osgi.service.component.annotations.Component;
+
+@Component(
+    service = Configuration.class,
+    property = {
+        "name=bar",
+        "service.ranking:Integer=2"
+    }
+)
+public class Ranked2Configuration extends Configuration {
+
+    public Ranked2Configuration() {
+        super(Configuration.getVersion());
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/scripting/freemarker/it/tests/FreemarkerScriptEngineFactoryIT.java b/src/test/java/org/apache/sling/scripting/freemarker/it/tests/FreemarkerScriptEngineFactoryIT.java
index a05c395..7b0db77 100644
--- a/src/test/java/org/apache/sling/scripting/freemarker/it/tests/FreemarkerScriptEngineFactoryIT.java
+++ b/src/test/java/org/apache/sling/scripting/freemarker/it/tests/FreemarkerScriptEngineFactoryIT.java
@@ -18,6 +18,11 @@
  */
 package org.apache.sling.scripting.freemarker.it.tests;
 
+import javax.inject.Inject;
+
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.sling.scripting.freemarker.it.app.Ranked1Configuration;
+import org.apache.sling.scripting.freemarker.it.app.Ranked2Configuration;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.Configuration;
@@ -25,9 +30,11 @@ import org.ops4j.pax.exam.Option;
 import org.ops4j.pax.exam.junit.PaxExam;
 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
 import org.ops4j.pax.exam.spi.reactors.PerClass;
+import org.ops4j.pax.exam.util.Filter;
 
 import static org.hamcrest.Matchers.hasItem;
 import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.sameInstance;
 import static org.hamcrest.Matchers.startsWith;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertThat;
@@ -36,10 +43,18 @@ import static org.junit.Assert.assertThat;
 @ExamReactorStrategy(PerClass.class)
 public class FreemarkerScriptEngineFactoryIT extends FreemarkerTestSupport {
 
+    @Inject
+    @Filter("(name=bar)")
+    private freemarker.template.Configuration configuration;
+
     @Configuration
     public Option[] configuration() {
         return new Option[]{
-            baseConfiguration()
+            baseConfiguration(),
+            buildBundleWithBnd(
+                Ranked1Configuration.class,
+                Ranked2Configuration.class
+            )
         };
     }
 
@@ -68,4 +83,11 @@ public class FreemarkerScriptEngineFactoryIT extends FreemarkerTestSupport {
         assertThat(scriptEngineFactory.getNames(), hasItem("freemarker"));
     }
 
+    @Test
+    public void testConfiguration() throws IllegalAccessException {
+        final Object configuration = FieldUtils.readDeclaredField(scriptEngineFactory, "configuration", true);
+        assertThat(configuration, sameInstance(this.configuration));
+        assertThat(configuration.getClass().getName(), is("org.apache.sling.scripting.freemarker.it.app.Ranked2Configuration"));
+    }
+
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.