You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2018/08/14 09:57:10 UTC
[sling-whiteboard] 02/03: add support for bundles providing
precompiled scripts
This is an automated email from the ASF dual-hosted git repository.
radu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
commit f079242e5b3c091f0689d230575dddaa831f87f1
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Tue Aug 14 11:40:52 2018 +0200
add support for bundles providing precompiled scripts
---
.../pom.xml | 144 +++++++++++++++++++++
.../main/resources/SLING-INF/content/example.xml | 46 +++++++
.../1.0.0/h.html | 19 +++
.../1.0.0/hello.html | 20 +++
.../1.0.0/w.html | 19 +++
.../2.0.0/h.html | 19 +++
.../2.0.0/hello.html | 20 +++
.../2.0.0/w.html | 19 +++
.../org-apache-sling-scripting-resolver/pom.xml | 6 +
.../resolver/internal/BundledScriptFinder.java | 52 ++++++--
.../resolver/internal/BundledScriptServlet.java | 16 ++-
.../resolver/internal/BundledScriptTracker.java | 8 +-
.../resolver/internal/PrecompiledScript.java | 55 ++++++++
.../sling/scripting/resolver/internal/Script.java | 39 +++---
.../resolver/internal/ScriptContextProvider.java | 11 +-
.../resolver/internal/ScriptEngineExecutable.java | 32 +++++
.../scripting/resolver/internal/EndpointIT.java | 24 ++++
.../src/test/provisioning/it-model.txt | 4 +-
18 files changed, 511 insertions(+), 42 deletions(-)
diff --git a/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/pom.xml b/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/pom.xml
new file mode 100644
index 0000000..54b23be
--- /dev/null
+++ b/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/pom.xml
@@ -0,0 +1,144 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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/xsd/maven-4.0.0.xsd">
+
+ <parent>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.scripting.resolver.reactor</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <relativePath>../../pom.xml</relativePath>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>org.apache.sling.scripting.examplebundle.precompiled</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <packaging>bundle</packaging>
+
+ <build>
+ <sourceDirectory>src/main/scripts</sourceDirectory>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.scripting.maven.plugin</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <configuration>
+ <scriptsDirectory>src/main/scripts/javax.script</scriptsDirectory>
+ </configuration>
+ <executions>
+ <execution>
+ <phase>prepare-package</phase>
+ <goals>
+ <goal>metadata</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <Export-Package>!*</Export-Package>
+ <Provide-Capability>
+ ${org.apache.sling.scripting.maven.plugin.Provide-Capability}
+ </Provide-Capability>
+ <Require-Capability>
+ osgi.extender;filter:="(&(osgi.extender=sling.scripting)(version>=1.0.0)(!(version>=2.0.0)))",
+ ${org.apache.sling.scripting.maven.plugin.Require-Capability}
+ </Require-Capability>
+ <Sling-Initial-Content>
+ SLING-INF/content;overwrite:=true;path:=/content/srr/examples
+ </Sling-Initial-Content>
+ <Sling-ResourceType-Precompiled>true</Sling-ResourceType-Precompiled>
+ </instructions>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>htl-maven-plugin</artifactId>
+ <version>1.1.8-1.4.0</version>
+ <executions>
+ <execution>
+ <id>validate-htl-scripts</id>
+ <goals>
+ <goal>validate</goal>
+ </goals>
+ <phase>generate-sources</phase>
+ <configuration>
+ <sourceDirectory>src/main/scripts/javax.script</sourceDirectory>
+ <generateJavaClasses>true</generateJavaClasses>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.rat</groupId>
+ <artifactId>apache-rat-plugin</artifactId>
+ <configuration>
+ <excludes>
+ <exclude>**/extends</exclude>
+ </excludes>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.api</artifactId>
+ <version>2.16.4</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>javax.servlet-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.models.api</artifactId>
+ <version>1.3.6</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-atinject_1.0_spec</artifactId>
+ <version>1.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.scripting.sightly.compiler.java</artifactId>
+ <version>1.0.24-1.4.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.scripting.sightly</artifactId>
+ <version>1.0.54-1.4.0</version>
+ <scope>provided</scope>
+ </dependency>
+ </dependencies>
+
+</project>
diff --git a/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/resources/SLING-INF/content/example.xml b/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/resources/SLING-INF/content/example.xml
new file mode 100644
index 0000000..0a67aa7
--- /dev/null
+++ b/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/resources/SLING-INF/content/example.xml
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
+<node>
+ <name>precompiled-hello</name>
+ <primaryNodeType>nt:unstructured</primaryNodeType>
+ <property>
+ <name>sling:resourceType</name>
+ <value>org.apache.sling.scripting.examplebundle.precompiled.hello</value>
+ <type>String</type>
+ </property>
+</node>
+<node>
+<name>precompiled-hello-v1</name>
+<primaryNodeType>nt:unstructured</primaryNodeType>
+<property>
+ <name>sling:resourceType</name>
+ <value>org.apache.sling.scripting.examplebundle.precompiled.hello/1.0.0</value>
+ <type>String</type>
+</property>
+</node>
+<node>
+<name>precompiled-hello-v2</name>
+<primaryNodeType>nt:unstructured</primaryNodeType>
+<property>
+ <name>sling:resourceType</name>
+ <value>org.apache.sling.scripting.examplebundle.precompiled.hello/2.0.0</value>
+ <type>String</type>
+</property>
+</node>
diff --git a/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/1.0.0/h.html b/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/1.0.0/h.html
new file mode 100644
index 0000000..45e1f49
--- /dev/null
+++ b/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/1.0.0/h.html
@@ -0,0 +1,19 @@
+<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
+<span data-sly-text="${'Hello'}"></span>
diff --git a/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/1.0.0/hello.html b/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/1.0.0/hello.html
new file mode 100644
index 0000000..48bd769
--- /dev/null
+++ b/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/1.0.0/hello.html
@@ -0,0 +1,20 @@
+<!--/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
+<h2>We're testing some serious scripting here</h2>
+<span data-sly-resource="${ @ selectors='h'}"></span> <span data-sly-resource="${ @ selectors='w'}"></span>
diff --git a/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/1.0.0/w.html b/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/1.0.0/w.html
new file mode 100644
index 0000000..1019aca
--- /dev/null
+++ b/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/1.0.0/w.html
@@ -0,0 +1,19 @@
+<!--/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
+<span data-sly-text="${'World'}"></span>
diff --git a/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/2.0.0/h.html b/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/2.0.0/h.html
new file mode 100644
index 0000000..cf193e9
--- /dev/null
+++ b/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/2.0.0/h.html
@@ -0,0 +1,19 @@
+<!--/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
+<span data-sly-text="${'Hello2'}"></span>
diff --git a/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/2.0.0/hello.html b/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/2.0.0/hello.html
new file mode 100644
index 0000000..7840f51
--- /dev/null
+++ b/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/2.0.0/hello.html
@@ -0,0 +1,20 @@
+<!--/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
+<h2>We're testing some serious scripting here in Version 2</h2>
+<span data-sly-resource="${ @ selectors='h'}"></span> <span data-sly-resource="${ @ selectors='w'}"></span>
diff --git a/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/2.0.0/w.html b/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/2.0.0/w.html
new file mode 100644
index 0000000..3c6b564
--- /dev/null
+++ b/scripting-resolver/examples/org-apache-sling-scripting-examplebundle-precompiled/src/main/scripts/javax.script/org.apache.sling.scripting.examplebundle.precompiled.hello/2.0.0/w.html
@@ -0,0 +1,19 @@
+<!--/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
+<span data-sly-text="${'World2'}"></span>
diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/pom.xml b/scripting-resolver/org-apache-sling-scripting-resolver/pom.xml
index 1a89ce9..f73d5ec 100644
--- a/scripting-resolver/org-apache-sling-scripting-resolver/pom.xml
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/pom.xml
@@ -158,6 +158,12 @@
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.scripting.examplebundle.precompiled</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.scripting.examplebundle.hi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptFinder.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptFinder.java
index ff43752..77f917a 100644
--- a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptFinder.java
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptFinder.java
@@ -25,6 +25,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import java.util.regex.Pattern;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
@@ -44,16 +45,16 @@ public class BundledScriptFinder {
private static final String SLASH = "/";
private static final String DOT = ".";
private static final Set<String> DEFAULT_METHODS = new HashSet<>(Arrays.asList("GET", "HEAD"));
- private static final Set<String> DEFAULT_EXTENSIONS = new HashSet<>(Arrays.asList("html"));
+ private static final Pattern STARTS_WITH_NUMBER = Pattern.compile("^[0-9].*");
@Reference
private ScriptEngineManager scriptEngineManager;
- Script getScript(SlingHttpServletRequest request, Bundle bundle) {
- return getScript(request, bundle, null);
+ ScriptEngineExecutable getScript(SlingHttpServletRequest request, Bundle bundle, boolean precompiledScripts) {
+ return getScript(request, bundle, precompiledScripts,null);
}
- Script getScript(SlingHttpServletRequest request, Bundle bundle, String delegatedResourceType) {
+ ScriptEngineExecutable getScript(SlingHttpServletRequest request, Bundle bundle, boolean precompiledScripts, String delegatedResourceType) {
List<String> scriptMatches;
if (StringUtils.isEmpty(delegatedResourceType)) {
scriptMatches = buildScriptMatches(request);
@@ -62,9 +63,24 @@ public class BundledScriptFinder {
}
for (String extension : getScriptEngineExtensions()) {
for (String match : scriptMatches) {
- URL bundledScriptURL = bundle.getEntry(NS_JAVAX_SCRIPT_CAPABILITY + SLASH + match + DOT + extension);
- if (bundledScriptURL != null) {
- return new Script(bundledScriptURL, scriptEngineManager.getEngineByExtension(extension));
+ URL bundledScriptURL;
+ if (precompiledScripts) {
+ String classResource = fromScriptPathToBundleResource(match);
+ bundledScriptURL = bundle.getEntry(classResource);
+
+ if (bundledScriptURL != null) {
+ try {
+ return new PrecompiledScript(scriptEngineManager.getEngineByExtension(extension),
+ bundle.loadClass(fromClassResourceToClassname(classResource)));
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ } else {
+ bundledScriptURL = bundle.getEntry(NS_JAVAX_SCRIPT_CAPABILITY + SLASH + match + DOT + extension);
+ if (bundledScriptURL != null) {
+ return new Script(bundledScriptURL, scriptEngineManager.getEngineByExtension(extension));
+ }
}
}
}
@@ -82,7 +98,7 @@ public class BundledScriptFinder {
String method = request.getMethod();
boolean defaultMethod = DEFAULT_METHODS.contains(method);
if (resourceType.contains(SLASH) && StringUtils.countMatches(resourceType, SLASH) == 1) {
- version = resourceType.substring(resourceType.indexOf(SLASH) + 1, resourceType.length());
+ version = resourceType.substring(resourceType.indexOf(SLASH) + 1);
resourceType = resourceType.substring(0, resourceType.length() - version.length() - 1);
}
String extension = request.getRequestPathInfo().getExtension();
@@ -130,4 +146,24 @@ public class BundledScriptFinder {
Collections.reverse(_scriptEngineExtensions);
return Collections.unmodifiableList(_scriptEngineExtensions);
}
+
+ private String fromScriptPathToBundleResource(String scriptPath) {
+ String[] parts = scriptPath.split("/");
+ StringBuilder stringBuilder = new StringBuilder();
+ for (String part : parts) {
+ if (stringBuilder.length() > 0) {
+ stringBuilder.append("/");
+ }
+ if (STARTS_WITH_NUMBER.matcher(part).matches()) {
+ stringBuilder.append("_");
+ }
+ stringBuilder.append(part.replaceAll("\\.", "_"));
+ }
+ stringBuilder.append(".class");
+ return stringBuilder.toString();
+ }
+
+ private String fromClassResourceToClassname(String classResource) {
+ return classResource.substring(0, classResource.length() - 6).replaceAll("/", ".");
+ }
}
diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptServlet.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptServlet.java
index f331587..3d3714a 100644
--- a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptServlet.java
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptServlet.java
@@ -50,12 +50,15 @@ class BundledScriptServlet extends GenericServlet {
private final String m_delegatedResourceType;
private final Set<String> m_wiredResourceTypes;
- private Map<String, Script> scriptsMap = new HashMap<>();
+ private Map<String, ScriptEngineExecutable> scriptsMap = new HashMap<>();
private ReadWriteLock lock = new ReentrantReadWriteLock();
+ private boolean precompiledScripts = false;
+
BundledScriptServlet(BundledScriptFinder bundledScriptFinder, Bundle bundle, ScriptContextProvider scriptContextProvider,
- Set<String> wiredResourceTypes) {
+ Set<String> wiredResourceTypes, boolean precompiledScripts) {
this(bundledScriptFinder, bundle, scriptContextProvider, null, wiredResourceTypes);
+ this.precompiledScripts = precompiledScripts;
}
BundledScriptServlet(BundledScriptFinder bundledScriptFinder, Bundle bundle, ScriptContextProvider scriptContextProvider, String
@@ -65,6 +68,7 @@ class BundledScriptServlet extends GenericServlet {
m_scriptContextProvider = scriptContextProvider;
m_delegatedResourceType = overridingResourceType;
m_wiredResourceTypes = wiredResourceTypes;
+
}
@Override
@@ -84,7 +88,7 @@ class BundledScriptServlet extends GenericServlet {
}
String scriptsMapKey = getScriptsMapKey(request);
- Script script = scriptsMap.get(scriptsMapKey);
+ ScriptEngineExecutable script = scriptsMap.get(scriptsMapKey);
lock.readLock().lock();
try {
if (script == null) {
@@ -94,16 +98,16 @@ class BundledScriptServlet extends GenericServlet {
script = scriptsMap.get(getScriptsMapKey(request));
if (script == null) {
if (StringUtils.isEmpty(m_delegatedResourceType)) {
- script = m_bundledScriptFinder.getScript(request, m_bundle);
+ script = m_bundledScriptFinder.getScript(request, m_bundle, precompiledScripts);
} else {
- script = m_bundledScriptFinder.getScript(request, m_bundle, m_delegatedResourceType);
+ script = m_bundledScriptFinder.getScript(request, m_bundle, precompiledScripts, m_delegatedResourceType);
}
if (script != null) {
scriptsMap.put(scriptsMapKey, script);
}
- lock.readLock().lock();
}
} finally {
+ lock.readLock().lock();
lock.writeLock().unlock();
}
}
diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptTracker.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptTracker.java
index 451b338..497b1bb 100644
--- a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptTracker.java
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptTracker.java
@@ -29,6 +29,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -115,9 +116,8 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic
.anyMatch(m_context.getBundle()::equals)) {
LOGGER.debug("Inspecting bundle {} for {} capability.", bundle.getSymbolicName(), NS_SLING_RESOURCE_TYPE);
List<BundleCapability> capabilities = bundleWiring.getCapabilities(NS_SLING_RESOURCE_TYPE);
-
if (!capabilities.isEmpty()) {
-
+ boolean precompiled = Boolean.parseBoolean(bundle.getHeaders().get("Sling-ResourceType-Precompiled"));
List<ServiceRegistration<Servlet>> serviceRegistrations = capabilities.stream().flatMap(cap ->
{
Hashtable<String, Object> properties = new Hashtable<>();
@@ -187,7 +187,7 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic
regs.add(bundle.getBundleContext()
.registerService(Servlet.class, new BundledScriptServlet(bundledScriptFinder, bundle, scriptContextProvider,
getWiredResourceTypes(new HashSet<>(Arrays.asList((String) attributes.get(NS_SLING_RESOURCE_TYPE))),
- new HashSet<>(Arrays.asList(resourceType)), bundle)),
+ new HashSet<>(Arrays.asList(resourceType)), bundle), precompiled),
properties));
}
return regs.stream();
@@ -204,7 +204,7 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic
private Set<String> getWiredResourceTypes(Set<String> rts, Set<String> initial, Bundle... bundles) {
- Set<String> wiredResourceTypes = new HashSet<>();
+ Set<String> wiredResourceTypes = new HashSet<>(initial);
wiredResourceTypes.addAll(initial);
for (Bundle bundle : bundles) {
BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/PrecompiledScript.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/PrecompiledScript.java
new file mode 100644
index 0000000..84bb5c1
--- /dev/null
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/PrecompiledScript.java
@@ -0,0 +1,55 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.resolver.internal;
+
+import java.io.StringReader;
+
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.sling.api.scripting.SlingScriptConstants;
+
+public class PrecompiledScript implements ScriptEngineExecutable {
+
+ private ScriptEngine scriptEngine;
+ private Class precompiledScript;
+
+ PrecompiledScript(ScriptEngine scriptEngine, Class precompiledScript) {
+ this.scriptEngine = scriptEngine;
+ this.precompiledScript = precompiledScript;
+ }
+
+ @Override
+ public String getName() {
+ return precompiledScript.getName();
+ }
+
+ @Override
+ public ScriptEngine getScriptEngine() {
+ return scriptEngine;
+ }
+
+ @Override
+ public void eval(ScriptContext context) throws ScriptException {
+ context.setAttribute("precompiled.unit", precompiledScript, SlingScriptConstants.SLING_SCOPE);
+ scriptEngine.eval(new StringReader(StringUtils.EMPTY), context);
+ }
+}
diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/Script.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/Script.java
index 1353138..bbefc16 100644
--- a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/Script.java
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/Script.java
@@ -34,7 +34,7 @@ import javax.script.ScriptException;
import org.apache.commons.io.IOUtils;
import org.apache.sling.scripting.core.ScriptNameAwareReader;
-class Script {
+class Script implements ScriptEngineExecutable {
private URL url;
private ScriptEngine scriptEngine;
@@ -54,30 +54,35 @@ class Script {
return sourceCode;
}
- String getName() {
+ public String getName() {
return url.getPath();
}
- ScriptEngine getScriptEngine() {
+ public ScriptEngine getScriptEngine() {
return scriptEngine;
}
- void eval(ScriptContext context) throws ScriptException, IOException {
- if (scriptEngine instanceof Compilable && compiledScript == null) {
- lock.lock();
- try {
- if (scriptEngine instanceof Compilable && compiledScript == null) {
- compiledScript =
- ((Compilable) scriptEngine).compile(new ScriptNameAwareReader(new StringReader(getSourceCode()), getName()));
+ public void eval(ScriptContext context) throws ScriptException {
+ try {
+ if (scriptEngine instanceof Compilable && compiledScript == null) {
+ lock.lock();
+ try {
+ if (scriptEngine instanceof Compilable && compiledScript == null) {
+ compiledScript =
+ ((Compilable) scriptEngine)
+ .compile(new ScriptNameAwareReader(new StringReader(getSourceCode()), getName()));
+ }
+ } finally {
+ lock.unlock();
}
- } finally {
- lock.unlock();
}
- }
- if (compiledScript != null) {
- compiledScript.eval(context);
- } else {
- scriptEngine.eval(getSourceCode(), context);
+ if (compiledScript != null) {
+ compiledScript.eval(context);
+ } else {
+ scriptEngine.eval(getSourceCode(), context);
+ }
+ } catch (IOException e) {
+ throw new ScriptException(e);
}
}
diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/ScriptContextProvider.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/ScriptContextProvider.java
index d1459ce..4715599 100644
--- a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/ScriptContextProvider.java
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/ScriptContextProvider.java
@@ -80,7 +80,7 @@ public class ScriptContextProvider {
m_bundleContext = componentContext.getBundleContext();
}
- ScriptContext prepareScriptContext(SlingHttpServletRequest request, SlingHttpServletResponse response, Script script)
+ ScriptContext prepareScriptContext(SlingHttpServletRequest request, SlingHttpServletResponse response, ScriptEngineExecutable executable)
throws IOException {
// prepare the SlingBindings
Bindings bindings = new SimpleBindings();
@@ -90,14 +90,14 @@ public class ScriptContextProvider {
bindings.put(SlingBindings.RESOURCE, request.getResource());
bindings.put(SlingBindings.RESOLVER, request.getResource().getResourceResolver());
bindings.put(SlingBindings.OUT, response.getWriter());
- Logger scriptLogger = LoggerFactory.getLogger(script.getName());
+ Logger scriptLogger = LoggerFactory.getLogger(executable.getName());
bindings.put(SlingBindings.LOG, scriptLogger);
bindings.put(SlingBindings.SLING, new ScriptHelper(m_bundleContext, null, request, response));
- bindings.put(ScriptEngine.FILENAME, script.getName());
- bindings.put(ScriptEngine.FILENAME.replaceAll("\\.", "_"), script.getName());
+ bindings.put(ScriptEngine.FILENAME, executable.getName());
+ bindings.put(ScriptEngine.FILENAME.replaceAll("\\.", "_"), executable.getName());
ProtectedBindings protectedBindings = new ProtectedBindings(bindings, PROTECTED_BINDINGS);
- for (BindingsValuesProvider bindingsValuesProvider : bvpTracker.getBindingsValuesProviders(script.getScriptEngine().getFactory(),
+ for (BindingsValuesProvider bindingsValuesProvider : bvpTracker.getBindingsValuesProviders(executable.getScriptEngine().getFactory(),
BindingsValuesProvider.DEFAULT_CONTEXT)) {
bindingsValuesProvider.addBindings(protectedBindings);
}
@@ -111,7 +111,6 @@ public class ScriptContextProvider {
scriptContext.setWriter(response.getWriter());
scriptContext.setErrorWriter(new LogWriter(scriptLogger));
scriptContext.setReader(request.getReader());
-
return scriptContext;
}
diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/ScriptEngineExecutable.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/ScriptEngineExecutable.java
new file mode 100644
index 0000000..fd0f8f3
--- /dev/null
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/ScriptEngineExecutable.java
@@ -0,0 +1,32 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.resolver.internal;
+
+import javax.script.ScriptContext;
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+
+interface ScriptEngineExecutable {
+
+ String getName();
+
+ ScriptEngine getScriptEngine();
+
+ void eval(ScriptContext context) throws ScriptException;
+}
diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/test/java/org/apache/sling/scripting/resolver/internal/EndpointIT.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/test/java/org/apache/sling/scripting/resolver/internal/EndpointIT.java
index 585a4cd..f4f93d0 100644
--- a/scripting-resolver/org-apache-sling-scripting-resolver/src/test/java/org/apache/sling/scripting/resolver/internal/EndpointIT.java
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/test/java/org/apache/sling/scripting/resolver/internal/EndpointIT.java
@@ -31,6 +31,14 @@ public class EndpointIT extends AbstractEndpointIT {
}
@Test
+ public void testPrecompiledHelloEndpoint() throws Exception {
+ Document document = getDocument("/content/srr/examples/precompiled-hello.html");
+ Assert.assertEquals("We're testing some serious scripting here in Version 2", document.select("h2").html());
+ Assert.assertTrue(document.body().html().contains("World2"));
+ Assert.assertTrue(document.body().html().contains("Hello2"));
+ }
+
+ @Test
public void testHelloEndpointV1() throws Exception {
Document document = getDocument("content/srr/examples/hello-v1.html");
Assert.assertEquals("We're testing some serious scripting here", document.select("h2").html());
@@ -39,6 +47,14 @@ public class EndpointIT extends AbstractEndpointIT {
}
@Test
+ public void testPrecompiledHelloEndpointV1() throws Exception {
+ Document document = getDocument("content/srr/examples/precompiled-hello-v1.html");
+ Assert.assertEquals("We're testing some serious scripting here", document.select("h2").html());
+ Assert.assertTrue(document.body().html().contains("World"));
+ Assert.assertTrue(document.body().html().contains("Hello"));
+ }
+
+ @Test
public void testHelloEndpointV2() throws Exception {
Document document = getDocument("content/srr/examples/hello-v2.html");
Assert.assertEquals("We're testing some serious scripting here in Version 2", document.select("h2").html());
@@ -47,6 +63,14 @@ public class EndpointIT extends AbstractEndpointIT {
}
@Test
+ public void testPrecompiledHelloEndpointV2() throws Exception {
+ Document document = getDocument("content/srr/examples/precompiled-hello-v2.html");
+ Assert.assertEquals("We're testing some serious scripting here in Version 2", document.select("h2").html());
+ Assert.assertTrue(document.body().html().contains("World2"));
+ Assert.assertTrue(document.body().html().contains("Hello2"));
+ }
+
+ @Test
public void testHiEndpoint() throws Exception {
Document document = getDocument("content/srr/examples/hi.html");
Assert.assertEquals("We're testing some serious scripting here", document.select("h2").html());
diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/test/provisioning/it-model.txt b/scripting-resolver/org-apache-sling-scripting-resolver/src/test/provisioning/it-model.txt
index 41c4539..a137ea3 100644
--- a/scripting-resolver/org-apache-sling-scripting-resolver/src/test/provisioning/it-model.txt
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/test/provisioning/it-model.txt
@@ -21,9 +21,11 @@
# Dependencies
[artifacts]
- org.apache.sling/org.apache.sling.starter/10/slingstart
+ org.apache.sling/org.apache.sling.starter/11-SNAPSHOT/slingstart
+ org.apache.sling/org.apache.sling.scripting.sightly/1.0.55-1.4.0-exp-SNAPSHOT
org.apache.sling/org.apache.sling.scripting.resolver
org.apache.sling/org.apache.sling.scripting.examplebundle
+ org.apache.sling/org.apache.sling.scripting.examplebundle.precompiled
org.apache.sling/org.apache.sling.scripting.examplebundle.hi
org.apache.sling/org.apache.sling.junit.core/1.0.26