Author: fmeschbe
Date: Sun Sep 28 23:58:10 2008
New Revision: 700001

URL: http://svn.apache.org/viewvc?rev=700001&view=rev
Log:
SLING-680 Add plugin for the Web Console to list known script engines

Added:
    
incubator/sling/trunk/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptEngineConsolePlugin.java
    
incubator/sling/trunk/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/helper/SlingScriptEngineManager.java
Modified:
    incubator/sling/trunk/scripting/core/pom.xml
    
incubator/sling/trunk/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/SlingScriptAdapterFactory.java

Modified: incubator/sling/trunk/scripting/core/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/core/pom.xml?rev=700001&r1=700000&r2=700001&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/core/pom.xml (original)
+++ incubator/sling/trunk/scripting/core/pom.xml Sun Sep 28 23:58:10 2008
@@ -61,6 +61,9 @@
                             org.apache.sling.scripting.core.impl,
                             org.apache.sling.scripting.core.impl.helper
                         </Private-Package>
+                        <DynamicImport-Package>
+                            org.apache.felix.webconsole
+                        </DynamicImport-Package>
                     </instructions>
                 </configuration>
             </plugin>
@@ -148,6 +151,11 @@
             <groupId>org.apache.felix</groupId>
             <artifactId>org.osgi.compendium</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.webconsole</artifactId>
+            <version>1.0.1-SNAPSHOT</version>
+        </dependency>
 
         <dependency>
             <groupId>org.slf4j</groupId>

Added: 
incubator/sling/trunk/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptEngineConsolePlugin.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptEngineConsolePlugin.java?rev=700001&view=auto
==============================================================================
--- 
incubator/sling/trunk/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptEngineConsolePlugin.java
 (added)
+++ 
incubator/sling/trunk/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptEngineConsolePlugin.java
 Sun Sep 28 23:58:10 2008
@@ -0,0 +1,184 @@
+/*
+ * 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.core.impl;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.script.ScriptEngineFactory;
+import javax.script.ScriptEngineManager;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.felix.webconsole.AbstractWebConsolePlugin;
+import org.apache.felix.webconsole.WebConsoleConstants;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
+
+public class ScriptEngineConsolePlugin extends AbstractWebConsolePlugin {
+
+    private static final String LABEL = "scriptengines";
+
+    // --------- setup and shutdown
+
+    private static ScriptEngineConsolePlugin INSTANCE;
+
+    static void initPlugin(BundleContext context,
+            SlingScriptAdapterFactory scriptAdapterFactory) {
+        if (INSTANCE == null) {
+            ScriptEngineConsolePlugin tmp = new ScriptEngineConsolePlugin(
+                scriptAdapterFactory);
+            tmp.activate(context);
+            INSTANCE = tmp;
+        }
+    }
+
+    static void destroyPlugin() {
+        if (INSTANCE != null) {
+            try {
+                INSTANCE.deactivate();
+            } finally {
+                INSTANCE = null;
+            }
+        }
+    }
+
+    private ServiceRegistration serviceRegistration;
+
+    private final SlingScriptAdapterFactory scriptAdapterFactory;
+
+    // private constructor to force using static setup and shutdown
+    private ScriptEngineConsolePlugin(
+            SlingScriptAdapterFactory scriptAdapterFactory) {
+        this.scriptAdapterFactory = scriptAdapterFactory;
+    }
+
+    @Override
+    public String getLabel() {
+        return LABEL;
+    }
+
+    @Override
+    public String getTitle() {
+        return "Script Engines";
+    }
+
+    @Override
+    protected void renderContent(HttpServletRequest req, HttpServletResponse 
res)
+            throws IOException {
+        PrintWriter pw = res.getWriter();
+
+        pw.println("<table class='content' cellpadding='0' cellspacing='0' 
width='100%'>");
+
+        ScriptEngineManager manager = 
scriptAdapterFactory.getScriptEngineManager();
+        List<?> factories = manager.getEngineFactories();
+        for (Iterator<?> fi = factories.iterator(); fi.hasNext();) {
+
+            ScriptEngineFactory factory = (ScriptEngineFactory) fi.next();
+
+            pw.println("<tr class='content'>");
+            pw.println("<th colspan='3'class='content container'>");
+            pw.print(factory.getEngineName());
+            pw.print(", ");
+            pw.println(factory.getEngineVersion());
+            pw.println("</th>");
+            pw.println("</tr>");
+
+            pw.println("<tr class='content'>");
+            pw.println("<td class='content'>&nbsp;</td>");
+            pw.println("<td class='content'>Language</td>");
+            pw.println("<td class='content'>");
+            pw.print(factory.getLanguageName());
+            pw.print(", ");
+            pw.println(factory.getLanguageVersion());
+            pw.println("</td>");
+            pw.println("</tr>");
+
+            pw.println("<tr class='content'>");
+            pw.println("<td class='content'>&nbsp;</td>");
+            pw.println("<td class='content'>Extensions</td>");
+            pw.println("<td class='content'>");
+            printArray(pw, factory.getExtensions());
+            pw.println("</td>");
+            pw.println("</tr>");
+
+            pw.println("<tr class='content'>");
+            pw.println("<td class='content'>&nbsp;</td>");
+            pw.println("<td class='content'>MIME Types</td>");
+            pw.println("<td class='content'>");
+            printArray(pw, factory.getMimeTypes());
+            pw.println("</td>");
+            pw.println("</tr>");
+
+            pw.println("<tr class='content'>");
+            pw.println("<td class='content'>&nbsp;</td>");
+            pw.println("<td class='content'>Names</td>");
+            pw.println("<td class='content'>");
+            printArray(pw, factory.getNames());
+            pw.println("</td>");
+            pw.println("</tr>");
+
+        }
+
+        pw.println("</table>");
+    }
+
+    private void printArray(PrintWriter pw, List<?> values) {
+        if (values == null || values.size() == 0) {
+            pw.println("-");
+        } else {
+            for (Iterator<?> vi = values.iterator(); vi.hasNext();) {
+                pw.print(vi.next());
+                if (vi.hasNext()) {
+                    pw.print(", ");
+                }
+            }
+            pw.println();
+        }
+    }
+
+    public void activate(BundleContext context) {
+        super.activate(context);
+
+        Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put(Constants.SERVICE_DESCRIPTION,
+            "Web Console Plugin for ScriptEngine implementations");
+        props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
+        props.put(Constants.SERVICE_PID, getClass().getName());
+        props.put(WebConsoleConstants.PLUGIN_LABEL, LABEL);
+
+        serviceRegistration = context.registerService(
+            WebConsoleConstants.SERVICE_NAME, this, props);
+    }
+
+    public void deactivate() {
+        if (serviceRegistration != null) {
+            serviceRegistration.unregister();
+            serviceRegistration = null;
+        }
+
+        super.deactivate();
+    }
+
+}

Modified: 
incubator/sling/trunk/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/SlingScriptAdapterFactory.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/SlingScriptAdapterFactory.java?rev=700001&r1=700000&r2=700001&view=diff
==============================================================================
--- 
incubator/sling/trunk/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/SlingScriptAdapterFactory.java
 (original)
+++ 
incubator/sling/trunk/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/SlingScriptAdapterFactory.java
 Sun Sep 28 23:58:10 2008
@@ -33,6 +33,7 @@
 
 import org.apache.sling.api.adapter.AdapterFactory;
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.scripting.core.impl.helper.SlingScriptEngineManager;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
@@ -42,21 +43,18 @@
 import org.slf4j.LoggerFactory;
 
 /**
- *  AdapterFactory that adapts Resources to the DefaultSlingScript servlet,
- *  which executes the Resources as scripts.
- *
- * @scr.component metatype="no"
+ * AdapterFactory that adapts Resources to the DefaultSlingScript servlet, 
which
+ * executes the Resources as scripts.
+ * 
+ * @scr.component metatype="no" immediate="true"
  * @scr.property name="service.vendor" value="The Apache Software Foundation"
  * @scr.property name="service.description" value="Default SlingScriptResolver"
- *
  * @scr.property name="adaptables"
  *               value="org.apache.sling.api.resource.Resource";
  * @scr.property name="adapters"
  *               values.0="org.apache.sling.api.scripting.SlingScript"
  *               values.1="javax.servlet.Servlet"
- *
  * @scr.service interface="org.apache.sling.api.adapter.AdapterFactory"
- *
  * @scr.reference name="ScriptEngineFactory"
  *                interface="javax.script.ScriptEngineFactory"
  *                cardinality="0..n" policy="dynamic"
@@ -82,7 +80,7 @@
 
     private BundleContext bundleContext;
 
-    //---------- AdapterFactory -----------------------------------------------
+    // ---------- AdapterFactory 
-----------------------------------------------
 
     @SuppressWarnings("unchecked")
     public <AdapterType> AdapterType getAdapter(Object adaptable,
@@ -92,25 +90,25 @@
         String path = resource.getPath();
         String ext = path.substring(path.lastIndexOf('.') + 1);
 
-        ScriptEngine engine = getScriptEngineManager().getEngineByExtension(
-            ext);
+        ScriptEngine engine = 
getScriptEngineManager().getEngineByExtension(ext);
         if (engine != null) {
             // unchecked cast
-            return (AdapterType) new DefaultSlingScript(this.bundleContext, 
resource, engine);
+            return (AdapterType) new DefaultSlingScript(this.bundleContext,
+                resource, engine);
         }
 
         return null;
     }
 
-    private ScriptEngineManager getScriptEngineManager() {
+    /* package */ ScriptEngineManager getScriptEngineManager() {
         if (scriptEngineManager == null) {
 
             // create (empty) script engine manager
             ClassLoader loader = getClass().getClassLoader();
-            ScriptEngineManager tmp = new ScriptEngineManager(loader);
+            SlingScriptEngineManager tmp = new 
SlingScriptEngineManager(loader);
 
             // register script engines from bundles
-            final SortedSet<Object> extensions = new TreeSet<Object>(); 
+            final SortedSet<Object> extensions = new TreeSet<Object>();
             for (Bundle bundle : engineSpiBundles) {
                 extensions.addAll(registerFactories(tmp, bundle));
             }
@@ -121,25 +119,23 @@
             }
 
             scriptEngineManager = tmp;
-            
+
             // Log messages to verify which ScriptEngine is actually used
             // for our registered extensions
-            if(log.isInfoEnabled()) {
-                for(Object o : extensions) {
+            if (log.isInfoEnabled()) {
+                for (Object o : extensions) {
                     final String ext = o.toString();
                     final ScriptEngine e = 
scriptEngineManager.getEngineByExtension(ext);
-                    if(e == null) {
-                        log.warn("No ScriptEngine found for extension '{}' 
that was just registered", ext);
+                    if (e == null) {
+                        log.warn(
+                            "No ScriptEngine found for extension '{}' that was 
just registered",
+                            ext);
                     } else {
                         log.info(
-                                "Script extension '{}' is now handled by 
ScriptEngine '{}', version='{}', class='{}'",
-                                new Object [] {
-                                        ext,
-                                        e.getFactory().getEngineName(),
-                                        e.getFactory().getEngineVersion(),
-                                        e.getClass().getName()
-                                }
-                                );
+                            "Script extension '{}' is now handled by 
ScriptEngine '{}', version='{}', class='{}'",
+                            new Object[] { ext, e.getFactory().getEngineName(),
+                                e.getFactory().getEngineVersion(),
+                                e.getClass().getName() });
                     }
                 }
             }
@@ -147,10 +143,11 @@
         return scriptEngineManager;
     }
 
-    private Collection<?> registerFactories(ScriptEngineManager mgr, Bundle 
bundle) {
+    private Collection<?> registerFactories(SlingScriptEngineManager mgr,
+            Bundle bundle) {
         URL url = bundle.getEntry(ENGINE_FACTORY_SERVICE);
         InputStream ins = null;
-        final SortedSet<String> extensions = new TreeSet<String>(); 
+        final SortedSet<String> extensions = new TreeSet<String>();
         try {
             ins = url.openStream();
             BufferedReader reader = new BufferedReader(new InputStreamReader(
@@ -176,28 +173,18 @@
                 }
             }
         }
-        
+
         return extensions;
     }
 
-    private Collection<?> registerFactory(ScriptEngineManager mgr,
+    private Collection<?> registerFactory(SlingScriptEngineManager mgr,
             ScriptEngineFactory factory) {
         log.info("Adding ScriptEngine {}, {} for language {}, {}",
             new Object[] { factory.getEngineName(), factory.getEngineVersion(),
                 factory.getLanguageName(), factory.getLanguageVersion() });
 
-        for (Object ext : factory.getExtensions()) {
-            mgr.registerEngineExtension((String) ext, factory);
-        }
+        mgr.registerScriptEngineFactory(factory);
 
-        for (Object mime : factory.getMimeTypes()) {
-            mgr.registerEngineMimeType((String) mime, factory);
-        }
-
-        for (Object name : factory.getNames()) {
-            mgr.registerEngineName((String) name, factory);
-        }
-        
         return factory.getExtensions();
     }
 
@@ -231,9 +218,23 @@
                 engineSpiBundles.add(bundle);
             }
         }
+
+        try {
+            
org.apache.sling.scripting.core.impl.ScriptEngineConsolePlugin.initPlugin(
+                context.getBundleContext(), this);
+        } catch (Throwable t) {
+            // so what ?
+        }
     }
 
     protected void deactivate(ComponentContext context) {
+
+        try {
+            
org.apache.sling.scripting.core.impl.ScriptEngineConsolePlugin.destroyPlugin();
+        } catch (Throwable t) {
+            // so what ?
+        }
+
         context.getBundleContext().removeBundleListener(this);
 
         engineSpiBundles.clear();

Added: 
incubator/sling/trunk/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/helper/SlingScriptEngineManager.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/helper/SlingScriptEngineManager.java?rev=700001&view=auto
==============================================================================
--- 
incubator/sling/trunk/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/helper/SlingScriptEngineManager.java
 (added)
+++ 
incubator/sling/trunk/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/helper/SlingScriptEngineManager.java
 Sun Sep 28 23:58:10 2008
@@ -0,0 +1,56 @@
+/*
+ * 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.core.impl.helper;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.script.ScriptEngineFactory;
+import javax.script.ScriptEngineManager;
+
+public class SlingScriptEngineManager extends ScriptEngineManager {
+
+    List<ScriptEngineFactory> factories = new ArrayList<ScriptEngineFactory>();
+
+    public SlingScriptEngineManager(ClassLoader classLoader) {
+        super(classLoader);
+    }
+
+    public void registerScriptEngineFactory(ScriptEngineFactory factory) {
+        for (Object ext : factory.getExtensions()) {
+            registerEngineExtension((String) ext, factory);
+        }
+
+        for (Object mime : factory.getMimeTypes()) {
+            registerEngineMimeType((String) mime, factory);
+        }
+
+        for (Object name : factory.getNames()) {
+            registerEngineName((String) name, factory);
+        }
+
+        factories.add(factory);
+    }
+
+    @Override
+    public List<?> getEngineFactories() {
+        return factories;
+    }
+
+}


Reply via email to