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-samples.git
The following commit(s) were added to refs/heads/master by this push:
new 931df1b Add a sample for embedding nashorn
931df1b is described below
commit 931df1bc473bd21fbf2f30fc507bc2112b5e9660
Author: Robert Munteanu <[email protected]>
AuthorDate: Thu Dec 4 16:40:09 2025 +0100
Add a sample for embedding nashorn
---
nashorn-external/README.md | 15 +++
nashorn-external/pom.xml | 150 +++++++++++++++++++++
.../sling/samples/nashorn/NashornTestServlet.java | 96 +++++++++++++
3 files changed, 261 insertions(+)
diff --git a/nashorn-external/README.md b/nashorn-external/README.md
new file mode 100644
index 0000000..af7c551
--- /dev/null
+++ b/nashorn-external/README.md
@@ -0,0 +1,15 @@
+# Apache Sling Samples - Nashorn External
+
+This OSGi bundle exemplifies the minimal setup required to use the Nashorn
JavaScript engine as an external script engine in Apache Sling.
+
+Significant implementation details:
+
+- embeds the [Nashorn engine](https://github.com/openjdk/nashorn) inside the
bundle
+- also embeds the transitive ASM depedndency to make the bundle fully
self-contained
+- uses a more recent ASM version for compatibility with recent JDKs
+- exposes a servlet at `/bin/nashorn` that executes a simple script using the
Nashorn engine
+- obtains the `ScriptEngineManager` as an OSGi reference
+
+## Usage
+
+Build with `mvn install`. Deploy with `mvn sling:install`. Test by accessing
http://localhost:8080/bin/nashorn-test .
diff --git a/nashorn-external/pom.xml b/nashorn-external/pom.xml
new file mode 100644
index 0000000..c9c7173
--- /dev/null
+++ b/nashorn-external/pom.xml
@@ -0,0 +1,150 @@
+<?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">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.samples.nashorn-external</artifactId>
+ <version>0.1.0-SNAPSHOT</version>
+ <packaging>jar</packaging>
+
+ <name>Apache Sling Samples - Nashorn External</name>
+ <description>Apache Sling sample that demonstrates how to embed an
external Nashorn bundle for continued compatibility with newer version of
Java</description>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <maven.compiler.source>11</maven.compiler.source>
+ <maven.compiler.target>11</maven.compiler.target>
+ <asm.version>9.7</asm.version>
+ </properties>
+
+ <dependencies>
+ <!-- Nashorn external bundle -->
+ <dependency>
+ <groupId>org.openjdk.nashorn</groupId>
+ <artifactId>nashorn-core</artifactId>
+ <version>15.6</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.ow2.asm</groupId>
+ <artifactId>*</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+
+ <!-- ASM dependencies - version 9.7 -->
+ <dependency>
+ <groupId>org.ow2.asm</groupId>
+ <artifactId>asm</artifactId>
+ <version>${asm.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.ow2.asm</groupId>
+ <artifactId>asm-commons</artifactId>
+ <version>${asm.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.ow2.asm</groupId>
+ <artifactId>asm-tree</artifactId>
+ <version>${asm.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.ow2.asm</groupId>
+ <artifactId>asm-util</artifactId>
+ <version>${asm.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.ow2.asm</groupId>
+ <artifactId>asm-analysis</artifactId>
+ <version>${asm.version}</version>
+ </dependency>
+
+ <!-- Sling and OSGi dependencies -->
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.servlets.annotations</artifactId>
+ <version>1.2.6</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.api</artifactId>
+ <version>2.27.2</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.service.component.annotations</artifactId>
+ <version>1.5.1</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>javax.servlet-api</artifactId>
+ <version>3.1.0</version>
+ <scope>provided</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>biz.aQute.bnd</groupId>
+ <artifactId>bnd-maven-plugin</artifactId>
+ <version>6.4.0</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>bnd-process</goal>
+ </goals>
+ <configuration>
+ <bnd><![CDATA[
+Bundle-SymbolicName: ${project.artifactId}
+Bundle-Version: ${project.version}
+-includeresource: \
+ @nashorn-core-15.6.jar!/!META-INF/MANIFEST.MF,\
+ @asm-9.7.jar!/!META-INF/MANIFEST.MF,\
+ @asm-commons-9.7.jar!/!META-INF/MANIFEST.MF,\
+ @asm-tree-9.7.jar!/!META-INF/MANIFEST.MF,\
+ @asm-util-9.7.jar!/!META-INF/MANIFEST.MF,\
+ @asm-analysis-9.7.jar!/!META-INF/MANIFEST.MF
+ ]]></bnd>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <version>3.3.0</version>
+ <configuration>
+ <archive>
+
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
+ </archive>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>sling-maven-plugin</artifactId>
+ <version>3.0.4</version>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git
a/nashorn-external/src/main/java/org/apache/sling/samples/nashorn/NashornTestServlet.java
b/nashorn-external/src/main/java/org/apache/sling/samples/nashorn/NashornTestServlet.java
new file mode 100644
index 0000000..5298e69
--- /dev/null
+++
b/nashorn-external/src/main/java/org/apache/sling/samples/nashorn/NashornTestServlet.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.samples.nashorn;
+
+import java.io.IOException;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.apache.sling.servlets.annotations.SlingServletPaths;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+
+@Component(service = Servlet.class)
+@SlingServletPaths(value = "/bin/nashorn-test")
+public class NashornTestServlet extends SlingSafeMethodsServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ @Reference
+ private ScriptEngineManager scriptEngineManager;
+
+ @Override
+ protected void doGet(SlingHttpServletRequest request,
SlingHttpServletResponse response)
+ throws ServletException, IOException {
+
+ response.setContentType("text/plain");
+ response.setCharacterEncoding("UTF-8");
+
+ try {
+ ScriptEngine engine =
scriptEngineManager.getEngineByName("nashorn");
+
+ if (engine == null) {
+ response.getWriter().println("Error: Nashorn scripting engine
not found!");
+ response.getWriter().println("Available engines:");
+ scriptEngineManager.getEngineFactories().forEach(factory -> {
+ try {
+ response.getWriter().println(" - " +
factory.getEngineName()
+ + " (names: " + factory.getNames() + ")");
+ } catch (IOException e) {
+ // ignore
+ }
+ });
+ return;
+ }
+
+ // Sample JavaScript to evaluate
+ String script = "var message = 'Hello from Nashorn!';\n" +
+ "var numbers = [1, 2, 3, 4, 5];\n" +
+ "var sum = numbers.reduce(function(a, b) { return a
+ b; }, 0);\n" +
+ "message + ' Sum of ' + numbers + ' is ' + sum;";
+
+ // Evaluate the script
+ Object result = engine.eval(script);
+
+ // Output the result
+ response.getWriter().println("Nashorn Scripting Engine Test");
+ response.getWriter().println("=============================");
+ response.getWriter().println();
+ response.getWriter().println("Engine: " +
engine.getFactory().getEngineName() + " "
+ + engine.getFactory().getEngineVersion());
+ response.getWriter().println("Language: " +
engine.getFactory().getLanguageName() + " "
+ + engine.getFactory().getLanguageVersion());
+ response.getWriter().println();
+ response.getWriter().println("Script executed:");
+ response.getWriter().println(script);
+ response.getWriter().println();
+ response.getWriter().println("Result:");
+ response.getWriter().println(result);
+
+ } catch (ScriptException e) {
+ throw new ServletException(e);
+ }
+ }
+}