You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2012/05/04 15:44:54 UTC
svn commit: r1333961 [1/3] - in /sling/trunk/contrib: ./
scripting/script-console/ scripting/script-console/src/
scripting/script-console/src/main/
scripting/script-console/src/main/appended-resources/
scripting/script-console/src/main/appended-resourc...
Author: fmeschbe
Date: Fri May 4 13:44:53 2012
New Revision: 1333961
URL: http://svn.apache.org/viewvc?rev=1333961&view=rev
Log:
SLING-2463 Apply contributed Scripting Console (thanks Chetan Mehrotra)
Added:
sling/trunk/contrib/scripting/script-console/
sling/trunk/contrib/scripting/script-console/pom.xml
sling/trunk/contrib/scripting/script-console/src/
sling/trunk/contrib/scripting/script-console/src/main/
sling/trunk/contrib/scripting/script-console/src/main/appended-resources/
sling/trunk/contrib/scripting/script-console/src/main/appended-resources/META-INF/
sling/trunk/contrib/scripting/script-console/src/main/appended-resources/META-INF/LICENSE
sling/trunk/contrib/scripting/script-console/src/main/java/
sling/trunk/contrib/scripting/script-console/src/main/java/org/
sling/trunk/contrib/scripting/script-console/src/main/java/org/apache/
sling/trunk/contrib/scripting/script-console/src/main/java/org/apache/sling/
sling/trunk/contrib/scripting/script-console/src/main/java/org/apache/sling/scripting/
sling/trunk/contrib/scripting/script-console/src/main/java/org/apache/sling/scripting/console/
sling/trunk/contrib/scripting/script-console/src/main/java/org/apache/sling/scripting/console/internal/
sling/trunk/contrib/scripting/script-console/src/main/java/org/apache/sling/scripting/console/internal/RuntimeScriptResource.java
sling/trunk/contrib/scripting/script-console/src/main/java/org/apache/sling/scripting/console/internal/ScriptConsolePlugin.java
sling/trunk/contrib/scripting/script-console/src/main/resources/
sling/trunk/contrib/scripting/script-console/src/main/resources/OSGI-INF/
sling/trunk/contrib/scripting/script-console/src/main/resources/OSGI-INF/l10n/
sling/trunk/contrib/scripting/script-console/src/main/resources/OSGI-INF/l10n/bundle.properties
sling/trunk/contrib/scripting/script-console/src/main/resources/res/
sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/
sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/
sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/
sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/codemirror.css
sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/codemirror.js
sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/util/
sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/util/loadmode.js
sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/util/runmode.js
sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/mode/
sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/mode/groovy/
sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/mode/groovy/groovy.js
sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/mode/javascript/
sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/mode/javascript/javascript.js
sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/script-console.css
sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/script-console.js
sling/trunk/contrib/scripting/script-console/src/main/resources/templates/
sling/trunk/contrib/scripting/script-console/src/main/resources/templates/script-console.html
sling/trunk/contrib/scripting/script-console/src/test/
sling/trunk/contrib/scripting/script-console/src/test/java/
sling/trunk/contrib/scripting/script-console/src/test/java/org/
sling/trunk/contrib/scripting/script-console/src/test/java/org/apache/
sling/trunk/contrib/scripting/script-console/src/test/java/org/apache/sling/
sling/trunk/contrib/scripting/script-console/src/test/java/org/apache/sling/scripting/
sling/trunk/contrib/scripting/script-console/src/test/java/org/apache/sling/scripting/console/
sling/trunk/contrib/scripting/script-console/src/test/java/org/apache/sling/scripting/console/ConsoleTestClient.java
sling/trunk/contrib/scripting/script-console/src/test/resources/
sling/trunk/contrib/scripting/script-console/src/test/resources/test.groovy
sling/trunk/contrib/scripting/script-console/src/test/resources/test.js
Modified:
sling/trunk/contrib/pom.xml
Modified: sling/trunk/contrib/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/pom.xml?rev=1333961&r1=1333960&r2=1333961&view=diff
==============================================================================
--- sling/trunk/contrib/pom.xml (original)
+++ sling/trunk/contrib/pom.xml Fri May 4 13:44:53 2012
@@ -92,6 +92,7 @@
<module>jcr/jackrabbit-client</module>
<module>jcr/compiler</module>
<module>jcr/prefs</module>
+ <module>scripting/console</module>
<module>scripting/jsp-taglib-atom</module>
<module>scripting/freemarker</module>
<module>scripting/java</module>
Added: sling/trunk/contrib/scripting/script-console/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console/pom.xml?rev=1333961&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/script-console/pom.xml (added)
+++ sling/trunk/contrib/scripting/script-console/pom.xml Fri May 4 13:44:53 2012
@@ -0,0 +1,151 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+ 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/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>sling</artifactId>
+ <version>12</version>
+ <relativePath>../../../parent/pom.xml</relativePath>
+ </parent>
+
+ <artifactId>org.apache.sling.scripting.console</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ <packaging>bundle</packaging>
+
+ <name>Apache Sling Script Console</name>
+ <description>Web Console plugin for evaluating scripts</description>
+
+ <scm>
+ <connection>
+ scm:svn:http://svn.apache.org/repos/asf/sling/trunk/contrib/scripting/script-console
+ </connection>
+ <developerConnection>
+ scm:svn:https://svn.apache.org/repos/asf/sling/trunk/contrib/scripting/script-console
+ </developerConnection>
+ <url>
+ http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console
+ </url>
+ </scm>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-scr-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <reporting>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-javadoc-plugin</artifactId>
+ <configuration>
+ <!-- No javadocs -->
+ <excludePackageNames>
+ org.apache.sling.scripting
+ </excludePackageNames>
+ </configuration>
+ </plugin>
+ </plugins>
+ </reporting>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <version>4.2.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.scr.annotations</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.4</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-fileupload</groupId>
+ <artifactId>commons-fileupload</artifactId>
+ <version>1.1.1</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>1.1</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.webconsole</artifactId>
+ <version>3.0.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.api</artifactId>
+ <version>2.1.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.scripting.api</artifactId>
+ <version>2.1.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.adapter</artifactId>
+ <version>2.0.4</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.commons.json</artifactId>
+ <version>2.0.6</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.tools</artifactId>
+ <version>1.0.2</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
Added: sling/trunk/contrib/scripting/script-console/src/main/appended-resources/META-INF/LICENSE
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console/src/main/appended-resources/META-INF/LICENSE?rev=1333961&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/script-console/src/main/appended-resources/META-INF/LICENSE (added)
+++ sling/trunk/contrib/scripting/script-console/src/main/appended-resources/META-INF/LICENSE Fri May 4 13:44:53 2012
@@ -0,0 +1,33 @@
+
+
+APACHE SLING SCRIPTING CONSOLE SUBCOMPONENTS:
+
+The Apache Sling Script Console includes a number of subcomponents
+with separate copyright notices and license terms. Your use of the source
+code for the these subcomponents is subject to the terms and conditions
+of the following licenses.
+
+
+Copyright (C) 2012 by Marijn Haverbeke <ma...@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+Please note that some subdirectories of the CodeMirror distribution
+include their own LICENSE files, and are released under different
+licences.
Added: sling/trunk/contrib/scripting/script-console/src/main/java/org/apache/sling/scripting/console/internal/RuntimeScriptResource.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console/src/main/java/org/apache/sling/scripting/console/internal/RuntimeScriptResource.java?rev=1333961&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/script-console/src/main/java/org/apache/sling/scripting/console/internal/RuntimeScriptResource.java (added)
+++ sling/trunk/contrib/scripting/script-console/src/main/java/org/apache/sling/scripting/console/internal/RuntimeScriptResource.java Fri May 4 13:44:53 2012
@@ -0,0 +1,177 @@
+/*
+ * 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.console.internal;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.Collections;
+import java.util.Iterator;
+
+import org.apache.sling.adapter.SlingAdaptable;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceMetadata;
+import org.apache.sling.api.resource.ResourceResolver;
+
+/**
+ * User: chetanm
+ * Date: 5/1/12
+ * Time: 12:20 PM
+ */
+class RuntimeScriptResource extends SlingAdaptable implements Resource {
+ private ResourceMetadata metadata = new ResourceMetadata();
+ private final String extension;
+ private final String path;
+ private final byte[] scriptContent;
+
+ public RuntimeScriptResource(String extension, String scriptText) {
+ this.extension = extension;
+ this.scriptContent = getScriptContent(scriptText);
+ this.path = "script." + extension;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
+ if(InputStream.class.isAssignableFrom(type)){
+ return (AdapterType) new ByteArrayInputStream(scriptContent);
+ }
+ return super.adaptTo(type);
+ }
+
+ /**
+ * Returns the absolute path of this resource in the resource tree.
+ */
+ public String getPath() {
+ return path;
+ }
+
+ /**
+ * Returns the name of this resource. The name of a resource is the last
+ * segment of the {@link #getPath() path}.
+ *
+ * @since 2.1.0
+ */
+ public String getName() {
+ return "script";
+ }
+
+ /**
+ * Returns the parent resource or <code>null</code> if this resource
+ * represents the root of the resource tree.
+ *
+ * @since 2.1.0
+ */
+ public Resource getParent() {
+ return null;
+ }
+
+ /**
+ * Returns an iterator of the direct children of this resource.
+ * <p/>
+ * This method is a convenience and returns exactly the same resources as
+ * calling <code>getResourceResolver().listChildren(resource)</code>.
+ *
+ * @see org.apache.sling.api.resource.ResourceResolver#listChildren(org.apache.sling.api.resource.Resource)
+ * @since 2.1.0
+ */
+ public Iterator<Resource> listChildren() {
+ return Collections.<Resource>emptyList().iterator();
+ }
+
+ /**
+ * Returns the child at the given relative path of this resource or
+ * <code>null</code> if no such child exists.
+ * <p/>
+ * This method is a convenience and returns exactly the same resources as
+ * calling <code>getResourceResolver().getResource(resource, relPath)</code>.
+ *
+ * @see org.apache.sling.api.resource.ResourceResolver#getResource(org.apache.sling.api.resource.Resource, String)
+ * @since 2.1.0
+ */
+ public Resource getChild(String relPath) {
+ return null;
+ }
+
+ /**
+ * The resource type is meant to point to rendering/processing scripts,
+ * editing dialogs, etc. It is usually a path in the repository, where
+ * scripts and other tools definitions are found, but the
+ * {@link org.apache.sling.api.resource.ResourceResolver} is free to set this to any suitable value such
+ * as the primary node type of the JCR node from which the resource is
+ * created.
+ * <p/>
+ * If the resource instance represents a resource which is not actually
+ * existing, this method returns {@link #RESOURCE_TYPE_NON_EXISTING}.
+ */
+ public String getResourceType() {
+ return RESOURCE_TYPE_NON_EXISTING;
+ }
+
+ /**
+ * Returns the super type of the type of the resource or <code>null</code>
+ * if the {@link #getResourceType()} has no supertype.
+ */
+ public String getResourceSuperType() {
+ return null;
+ }
+
+ /**
+ * Returns <code>true</code> if the resource type or any of the resource's
+ * super type(s) equals the given resource type.
+ *
+ * @param resourceType The resource type to check this resource against.
+ * @return <code>true</code> if the resource type or any of the resource's
+ * super type(s) equals the given resource type. <code>false</code>
+ * is also returned if <code>resourceType</code> is
+ * <code>null</code>.
+ * @since 2.1.0
+ */
+ public boolean isResourceType(String resourceType) {
+ return false;
+ }
+
+ /**
+ * Returns the metadata of this resource. The concrete data contained in the
+ * {@link org.apache.sling.api.resource.ResourceMetadata} object returned is implementation specific
+ * except for the {@link org.apache.sling.api.resource.ResourceMetadata#RESOLUTION_PATH} property which is
+ * required to be set to the part of the request URI used to resolve the
+ * resource.
+ *
+ * @see org.apache.sling.api.resource.ResourceMetadata
+ */
+ public ResourceMetadata getResourceMetadata() {
+ return metadata;
+ }
+
+ /**
+ * Returns the {@link org.apache.sling.api.resource.ResourceResolver} from which this resource has been
+ * retrieved.
+ */
+ public ResourceResolver getResourceResolver() {
+ return null;
+ }
+
+ private byte[] getScriptContent(String scriptText) {
+ try {
+ return scriptText.getBytes("utf-8");
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
Added: sling/trunk/contrib/scripting/script-console/src/main/java/org/apache/sling/scripting/console/internal/ScriptConsolePlugin.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console/src/main/java/org/apache/sling/scripting/console/internal/ScriptConsolePlugin.java?rev=1333961&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/script-console/src/main/java/org/apache/sling/scripting/console/internal/ScriptConsolePlugin.java (added)
+++ sling/trunk/contrib/scripting/script-console/src/main/java/org/apache/sling/scripting/console/internal/ScriptConsolePlugin.java Fri May 4 13:44:53 2012
@@ -0,0 +1,246 @@
+/*
+ * 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.console.internal;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import javax.script.ScriptEngineFactory;
+import javax.script.ScriptEngineManager;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.io.IOUtils;
+import org.apache.felix.scr.annotations.*;
+import org.apache.felix.webconsole.AbstractWebConsolePlugin;
+import org.apache.felix.webconsole.DefaultVariableResolver;
+import org.apache.felix.webconsole.SimpleWebConsolePlugin;
+import org.apache.felix.webconsole.WebConsoleUtil;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.scripting.SlingBindings;
+import org.apache.sling.api.scripting.SlingScript;
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.commons.json.io.JSONWriter;
+import org.osgi.framework.BundleContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * User: chetanm
+ * Date: 4/30/12
+ * Time: 9:06 PM
+ */
+@Component
+@Service
+@Property(name = "felix.webconsole.label", value = ScriptConsolePlugin.NAME)
+public class ScriptConsolePlugin extends SimpleWebConsolePlugin {
+ private Logger log = LoggerFactory.getLogger(getClass());
+ public static final String NAME = "scriptconsole";
+ private static final String TITLE = "%script.title";
+ private static final String[] CSS = {"/res/ui/codemirror/lib/codemirror.css","/res/ui/script-console.css"};
+ private final String TEMPLATE;
+ private BundleContext bundleContext;
+
+ @Reference
+ private ScriptEngineManager scriptEngineManager;
+
+ public ScriptConsolePlugin() {
+ super(NAME, TITLE, processFileNames(CSS));
+ TEMPLATE = readTemplateFile("/templates/script-console.html");
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ protected void renderContent(HttpServletRequest request,
+ HttpServletResponse response) throws ServletException, IOException {
+ final PrintWriter pw = response.getWriter();
+ DefaultVariableResolver varResolver = (DefaultVariableResolver) WebConsoleUtil.getVariableResolver(request);
+ varResolver.put("__scriptConfig__",getScriptConfig());
+ pw.println(TEMPLATE);
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ final String contentType = getContentType(req);
+ resp.setContentType(contentType);
+ if (contentType.startsWith("text/")) {
+ resp.setCharacterEncoding("UTF-8");
+ }
+ final String script = getCodeValue(req);
+ final SlingBindings bindings = new SlingBindings();
+ final PrintWriter pw = resp.getWriter();
+ //Populate bindings
+ bindings.put(SlingBindings.REQUEST, req);
+ bindings.put(SlingBindings.READER, new StringReader(script));
+ bindings.put(SlingBindings.RESPONSE, resp);
+ bindings.put(SlingBindings.OUT, pw);
+
+ //Also expose the bundleContext to simplify scripts interaction with the
+ //enclosing OSGi container
+ bindings.put("bundleContext", bundleContext);
+
+
+
+ final String lang = WebConsoleUtil.getParameter(req, "lang");
+ final Resource resource = new RuntimeScriptResource(lang, script);
+ final boolean webClient = "webconsole".equals(WebConsoleUtil.getParameter(req,"client"));
+
+ SlingScript slingScript = resource.adaptTo(SlingScript.class);
+ try {
+ log.debug("Executing script {}",script);
+ slingScript.eval(bindings);
+ } catch (Throwable t){
+ if(!webClient){
+ resp.setStatus(500);
+ }
+ pw.println(exceptionToString(t));
+ log.warn("Error in executing script",t);
+ }
+ }
+
+ private String getCodeValue(HttpServletRequest req) throws IOException {
+ String script = WebConsoleUtil.getParameter(req, "code");
+ if(script == null){
+ script = getContentFromFilePart(req, "code");
+ }
+ if(script == null){
+ throw new IllegalArgumentException("'code' parameter not passed");
+ }
+ return script;
+ }
+
+ private String getContentType(HttpServletRequest req) {
+ String passedContentType = WebConsoleUtil.getParameter(req,"responseContentType");
+ if(passedContentType != null){
+ return passedContentType;
+ }
+ return req.getPathInfo().endsWith(".json") ? "application/json" : "text/plain";
+ }
+
+ private String exceptionToString(Throwable t) {
+ StringWriter sw = new StringWriter();
+ t.printStackTrace(new PrintWriter(sw));
+ return sw.toString();
+ }
+
+
+ private static String[] processFileNames(String[] cssFiles) {
+ String[] css = new String[cssFiles.length];
+ for(int i = 0; i < cssFiles.length; i++){
+ css[i] = '/' + NAME + CSS[i];
+ }
+ return css;
+ }
+
+ private String getScriptConfig() {
+ try {
+ return getScriptConfig0();
+ } catch (JSONException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private String getScriptConfig0() throws JSONException {
+ StringWriter sw = new StringWriter();
+ JSONWriter jw = new JSONWriter(sw);
+ jw.setTidy(true);
+ jw.array();
+
+ for(ScriptEngineFactory sef : scriptEngineManager.getEngineFactories()){
+ jw.object();
+ if(sef.getExtensions().isEmpty()){
+ continue;
+ }
+ jw.key("langName").value(sef.getLanguageName());
+ jw.key("langCode").value(sef.getExtensions().get(0));
+
+ //Language mode as per CodeMirror names
+ String mode = determineMode(sef.getExtensions());
+ if(mode != null){
+ jw.key("mode").value(mode);
+ }
+
+ jw.endObject();
+ }
+
+ jw.endArray();
+ return sw.toString();
+ }
+
+ private String determineMode(List<String> extensions) {
+ if(extensions.contains("groovy")){
+ return "groovy";
+ }else if (extensions.contains("esp")){
+ return "javascript";
+ }
+ return null;
+ }
+
+ private String getContentFromFilePart(HttpServletRequest req, String paramName) throws IOException {
+ String value = WebConsoleUtil.getParameter(req, paramName);
+ if(value != null){
+ return value;
+ }
+ final Map params = (Map) req.getAttribute( AbstractWebConsolePlugin.ATTR_FILEUPLOAD );
+ if ( params == null ){
+ return null;
+ }
+ FileItem[] codeFile = getFileItems(params,paramName);
+ if(codeFile.length == 0){
+ return null;
+ }
+ InputStream is = null;
+ try{
+ is = codeFile[0].getInputStream();
+ StringWriter sw = new StringWriter();
+ IOUtils.copy(is,sw,"utf-8");
+ return sw.toString();
+ }finally{
+ IOUtils.closeQuietly(is);
+ }
+ }
+
+ private FileItem[] getFileItems(Map params, String name) {
+ final List files = new ArrayList();
+ FileItem[] items = (FileItem[]) params.get(name);
+ if (items != null) {
+ for (int i = 0; i < items.length; i++) {
+ if (!items[i].isFormField() && items[i].getSize() > 0) {
+ files.add(items[i]);
+ }
+ }
+ }
+ return (FileItem[]) files.toArray(new FileItem[files.size()]);
+ }
+
+ @Activate
+ public void activate(BundleContext bundleContext) {
+ super.activate(bundleContext);
+ this.bundleContext = bundleContext;
+ }
+
+ @Deactivate
+ public void deactivate() {
+ super.deactivate();
+ }
+
+
+}
Added: sling/trunk/contrib/scripting/script-console/src/main/resources/OSGI-INF/l10n/bundle.properties
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console/src/main/resources/OSGI-INF/l10n/bundle.properties?rev=1333961&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/script-console/src/main/resources/OSGI-INF/l10n/bundle.properties (added)
+++ sling/trunk/contrib/scripting/script-console/src/main/resources/OSGI-INF/l10n/bundle.properties Fri May 4 13:44:53 2012
@@ -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.
+#
+
+script.title=Script Console
+console.execute=Execute
+console.lang=Language
\ No newline at end of file
Added: sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/codemirror.css
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/codemirror.css?rev=1333961&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/codemirror.css (added)
+++ sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/codemirror.css Fri May 4 13:44:53 2012
@@ -0,0 +1,114 @@
+.CodeMirror {
+ line-height: 1em;
+ font-family: monospace;
+}
+
+.CodeMirror-scroll {
+ overflow: auto;
+ height: 300px;
+ /* This is needed to prevent an IE[67] bug where the scrolled content
+ is visible outside of the scrolling box. */
+ position: relative;
+ outline: none;
+}
+
+.CodeMirror-gutter {
+ position: absolute; left: 0; top: 0;
+ z-index: 10;
+ background-color: #f7f7f7;
+ border-right: 1px solid #eee;
+ min-width: 2em;
+ height: 100%;
+}
+.CodeMirror-gutter-text {
+ color: #aaa;
+ text-align: right;
+ padding: .4em .2em .4em .4em;
+ white-space: pre !important;
+}
+.CodeMirror-lines {
+ padding: .4em;
+ white-space: pre;
+}
+
+.CodeMirror pre {
+ -moz-border-radius: 0;
+ -webkit-border-radius: 0;
+ -o-border-radius: 0;
+ border-radius: 0;
+ border-width: 0; margin: 0; padding: 0; background: transparent;
+ font-family: inherit;
+ font-size: inherit;
+ padding: 0; margin: 0;
+ white-space: pre;
+ word-wrap: normal;
+ line-height: inherit;
+}
+
+.CodeMirror-wrap pre {
+ word-wrap: break-word;
+ white-space: pre-wrap;
+ word-break: normal;
+}
+.CodeMirror-wrap .CodeMirror-scroll {
+ overflow-x: hidden;
+}
+
+.CodeMirror textarea {
+ outline: none !important;
+}
+
+.CodeMirror pre.CodeMirror-cursor {
+ z-index: 10;
+ position: absolute;
+ visibility: hidden;
+ border-left: 1px solid black;
+ border-right:none;
+ width:0;
+}
+.CodeMirror pre.CodeMirror-cursor.CodeMirror-overwrite {}
+.CodeMirror-focused pre.CodeMirror-cursor {
+ visibility: visible;
+}
+
+div.CodeMirror-selected { background: #d9d9d9; }
+.CodeMirror-focused div.CodeMirror-selected { background: #d7d4f0; }
+
+.CodeMirror-searching {
+ background: #ffa;
+ background: rgba(255, 255, 0, .4);
+}
+
+/* Default theme */
+
+.cm-s-default span.cm-keyword {color: #708;}
+.cm-s-default span.cm-atom {color: #219;}
+.cm-s-default span.cm-number {color: #164;}
+.cm-s-default span.cm-def {color: #00f;}
+.cm-s-default span.cm-variable {color: black;}
+.cm-s-default span.cm-variable-2 {color: #05a;}
+.cm-s-default span.cm-variable-3 {color: #085;}
+.cm-s-default span.cm-property {color: black;}
+.cm-s-default span.cm-operator {color: black;}
+.cm-s-default span.cm-comment {color: #a50;}
+.cm-s-default span.cm-string {color: #a11;}
+.cm-s-default span.cm-string-2 {color: #f50;}
+.cm-s-default span.cm-meta {color: #555;}
+.cm-s-default span.cm-error {color: #f00;}
+.cm-s-default span.cm-qualifier {color: #555;}
+.cm-s-default span.cm-builtin {color: #30a;}
+.cm-s-default span.cm-bracket {color: #cc7;}
+.cm-s-default span.cm-tag {color: #170;}
+.cm-s-default span.cm-attribute {color: #00c;}
+.cm-s-default span.cm-header {color: #a0a;}
+.cm-s-default span.cm-quote {color: #090;}
+.cm-s-default span.cm-hr {color: #999;}
+.cm-s-default span.cm-link {color: #00c;}
+
+span.cm-header, span.cm-strong {font-weight: bold;}
+span.cm-em {font-style: italic;}
+span.cm-emstrong {font-style: italic; font-weight: bold;}
+span.cm-link {text-decoration: underline;}
+
+div.CodeMirror span.CodeMirror-matchingbracket {color: #0f0;}
+div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #f22;}