You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:11:56 UTC

[sling-org-apache-sling-scripting-sightly-repl] 02/14: SLING-4713 - Update Sightly REPL to display generated Java source code

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.scripting.sightly.repl-1.0.2
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly-repl.git

commit 10d47b2618306353b4ec4826b07531b24a658ce0
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Tue May 12 15:49:25 2015 +0000

    SLING-4713 - Update Sightly REPL to display generated Java source code
    
    * Java source code for generated class is displayed by reading the *.java files from disk
    in case the org.apache.sling.commons.fsclassloader bundle is active and the classes' source
    code was generated by enabling Sightly's dev mode
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/scripting/sightly/repl@1678985 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            | 51 ++++++++++++++++
 .../sightly/repl/REPLJavaSourceCodeServlet.java    | 68 ++++++++++++++++++++++
 .../SLING-INF/apps/repl/components/repl/repl.html  |  4 +-
 .../SLING-INF/apps/repl/components/repl/repl.js    | 49 ----------------
 4 files changed, 121 insertions(+), 51 deletions(-)

diff --git a/pom.xml b/pom.xml
index 9ac1113..2d421e2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -63,6 +63,17 @@
         <plugins>
             <plugin>
                 <groupId>org.apache.felix</groupId>
+                <artifactId>maven-scr-plugin</artifactId>
+                <dependencies>
+                    <dependency>
+                        <groupId>javax.jcr</groupId>
+                        <artifactId>jcr</artifactId>
+                        <version>2.0</version>
+                    </dependency>
+                </dependencies>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
                 <artifactId>maven-bundle-plugin</artifactId>
                 <extensions>true</extensions>
                 <configuration>
@@ -74,4 +85,44 @@
             </plugin>
         </plugins>
     </build>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.4</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.api</artifactId>
+            <version>2.4.0</version>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
 </project>
diff --git a/src/main/java/org/apache/sling/scripting/sightly/repl/REPLJavaSourceCodeServlet.java b/src/main/java/org/apache/sling/scripting/sightly/repl/REPLJavaSourceCodeServlet.java
new file mode 100644
index 0000000..346df18
--- /dev/null
+++ b/src/main/java/org/apache/sling/scripting/sightly/repl/REPLJavaSourceCodeServlet.java
@@ -0,0 +1,68 @@
+package org.apache.sling.scripting.sightly.repl;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.sling.SlingServlet;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@SlingServlet(
+        resourceTypes = {"repl/components/repl"},
+        selectors = {"java"},
+        methods = "GET",
+        extensions = "html"
+)
+public class REPLJavaSourceCodeServlet extends SlingSafeMethodsServlet {
+
+    private static final String FS_CLASSLOADER_SN = "org.apache.sling.commons.fsclassloader";
+    private static final Logger LOGGER = LoggerFactory.getLogger(REPLJavaSourceCodeServlet.class);
+
+    private File classesFolder;
+
+    @Activate
+    @SuppressWarnings("unused")
+    protected void activate(ComponentContext componentContext) {
+        for (Bundle bundle : componentContext.getBundleContext().getBundles()) {
+            if (FS_CLASSLOADER_SN.equals(bundle.getSymbolicName())) {
+                BundleContext context = bundle.getBundleContext();
+                classesFolder = new File(context.getDataFile(""), "classes");
+            }
+        }
+    }
+
+    @Override
+    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
+        response.setContentType("text/plain");
+        response.getWriter().write(getClassSourceCode());
+    }
+
+    private String getClassSourceCode() {
+        if (classesFolder != null && classesFolder.isDirectory()) {
+            File classFile = new File(classesFolder, "/apps/repl/components/repl/SightlyJava_template.java");
+            if (classFile.isFile()) {
+                try {
+                    return IOUtils.toString(new FileInputStream(classFile), "UTF-8");
+                } catch (IOException e) {
+                    LOGGER.error("Unable to read file " + classFile.getAbsolutePath(), e);
+                    return "";
+                }
+            }
+        }
+        LOGGER.warn("Source code for " + (classesFolder.isDirectory() ? classesFolder.getAbsolutePath() : "") +
+                "/apps/repl/components/repl/SightlyJava_template.java was not found. Maybe you need to enable dev mode for Sightly?");
+        return "";
+    }
+
+}
diff --git a/src/main/resources/SLING-INF/apps/repl/components/repl/repl.html b/src/main/resources/SLING-INF/apps/repl/components/repl/repl.html
index a41811f..6d0d9e1 100644
--- a/src/main/resources/SLING-INF/apps/repl/components/repl/repl.html
+++ b/src/main/resources/SLING-INF/apps/repl/components/repl/repl.html
@@ -31,7 +31,7 @@
         <link rel="stylesheet" type="text/css" href="/etc/clientlibs/repl/style.css">
     </head>
 
-    <body data-sly-use.repl="repl.js">
+    <body>
         <div id="logo">&nbsp;</div>
         <h1><small>Read-Eval-Print Loop</small></h1>
 
@@ -58,7 +58,7 @@
                 </div>
                 <div id="source" class="output-view editor" data-src="/sightly/repl.template.html" data-mode="ace/mode/html"></div>
                 <iframe id="view" class="output-view hidden" src="/sightly/repl.template.html"></iframe>
-                <div id="java" class="output-view editor hidden" data-src="${repl.classPath}" data-mode="ace/mode/java"></div>
+                <div id="java" class="output-view editor hidden" data-src="/sightly/repl.java.html" data-mode="ace/mode/java"></div>
             </div>
         </div>
 
diff --git a/src/main/resources/SLING-INF/apps/repl/components/repl/repl.js b/src/main/resources/SLING-INF/apps/repl/components/repl/repl.js
deleted file mode 100644
index 09ca13a..0000000
--- a/src/main/resources/SLING-INF/apps/repl/components/repl/repl.js
+++ /dev/null
@@ -1,49 +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.
- ******************************************************************************/
-use(function () {
-
-    var slingSettings = sling.getService(Packages.org.apache.sling.settings.SlingSettingsService);
-    var CLASS_ROOT_FOLDER  = '/var/classes/' + slingSettings.getSlingId() + '/sightly';
-    var COMPONENT_PATH     = '/apps/repl/components/repl';
-    var JAVA_TEMPLATE_FILE = 'SightlyJava_template.java';
-
-    // Recursively walks down the given path until it finds an apps folder, then returns the full path of the Java compiled template file.
-    function getAppsPath(res) {
-        return res.getChildren().then(function (children) {
-            var length = children.length;
-
-            // Let's see if one of the children is the apps folder.
-            for (var i = 0; i < length; i++) {
-                if (children[i].name === 'apps') {
-                    return res.path + COMPONENT_PATH + '/' + JAVA_TEMPLATE_FILE;
-                }
-            }
-
-            // If apps wasn't found but there's only one child folder, then let's recrusively walk that one down.
-            if (length === 1) {
-                return getAppsPath(children[0]);
-            }
-        });
-    }
-
-    return {
-        classPath: sightly.resource.resolve(CLASS_ROOT_FOLDER).then(getAppsPath)
-    };
-
-});

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