Author: radu
Date: Tue May 12 15:49:25 2015
New Revision: 1678985

URL: http://svn.apache.org/r1678985
Log:
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

Added:
    sling/trunk/bundles/scripting/sightly/repl/src/main/java/
    sling/trunk/bundles/scripting/sightly/repl/src/main/java/org/
    sling/trunk/bundles/scripting/sightly/repl/src/main/java/org/apache/
    sling/trunk/bundles/scripting/sightly/repl/src/main/java/org/apache/sling/
    
sling/trunk/bundles/scripting/sightly/repl/src/main/java/org/apache/sling/scripting/
    
sling/trunk/bundles/scripting/sightly/repl/src/main/java/org/apache/sling/scripting/sightly/
    
sling/trunk/bundles/scripting/sightly/repl/src/main/java/org/apache/sling/scripting/sightly/repl/
    
sling/trunk/bundles/scripting/sightly/repl/src/main/java/org/apache/sling/scripting/sightly/repl/REPLJavaSourceCodeServlet.java
Removed:
    
sling/trunk/bundles/scripting/sightly/repl/src/main/resources/SLING-INF/apps/repl/components/repl/repl.js
Modified:
    sling/trunk/bundles/scripting/sightly/repl/pom.xml
    
sling/trunk/bundles/scripting/sightly/repl/src/main/resources/SLING-INF/apps/repl/components/repl/repl.html

Modified: sling/trunk/bundles/scripting/sightly/repl/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/repl/pom.xml?rev=1678985&r1=1678984&r2=1678985&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/sightly/repl/pom.xml (original)
+++ sling/trunk/bundles/scripting/sightly/repl/pom.xml Tue May 12 15:49:25 2015
@@ -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>

Added: 
sling/trunk/bundles/scripting/sightly/repl/src/main/java/org/apache/sling/scripting/sightly/repl/REPLJavaSourceCodeServlet.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/repl/src/main/java/org/apache/sling/scripting/sightly/repl/REPLJavaSourceCodeServlet.java?rev=1678985&view=auto
==============================================================================
--- 
sling/trunk/bundles/scripting/sightly/repl/src/main/java/org/apache/sling/scripting/sightly/repl/REPLJavaSourceCodeServlet.java
 (added)
+++ 
sling/trunk/bundles/scripting/sightly/repl/src/main/java/org/apache/sling/scripting/sightly/repl/REPLJavaSourceCodeServlet.java
 Tue May 12 15:49:25 2015
@@ -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 "";
+    }
+
+}

Modified: 
sling/trunk/bundles/scripting/sightly/repl/src/main/resources/SLING-INF/apps/repl/components/repl/repl.html
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/repl/src/main/resources/SLING-INF/apps/repl/components/repl/repl.html?rev=1678985&r1=1678984&r2=1678985&view=diff
==============================================================================
--- 
sling/trunk/bundles/scripting/sightly/repl/src/main/resources/SLING-INF/apps/repl/components/repl/repl.html
 (original)
+++ 
sling/trunk/bundles/scripting/sightly/repl/src/main/resources/SLING-INF/apps/repl/components/repl/repl.html
 Tue May 12 15:49:25 2015
@@ -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>
 


Reply via email to