Author: fmeschbe
Date: Mon Jan 14 06:01:21 2008
New Revision: 611796
URL: http://svn.apache.org/viewvc?rev=611796&view=rev
Log:
Convert the former script resolver into an AdapterFactory providing Script
and Servlet instances for script resources.
Added:
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/SlingScriptAdapterFactory.java
- copied, changed from r607540,
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScriptResolver.java
Removed:
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/ScriptPathSupport.java
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScriptResolver.java
Modified:
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/pom.xml
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScript.java
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/LogWriter.java
Modified:
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/pom.xml?rev=611796&r1=611795&r2=611796&view=diff
==============================================================================
--- incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/pom.xml
(original)
+++ incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/pom.xml Mon
Jan 14 06:01:21 2008
@@ -85,6 +85,11 @@
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.osgi.commons</artifactId>
+ <version>2.0.0-incubator-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.jcr.api</artifactId>
<version>2.0.0-incubator-SNAPSHOT</version>
</dependency>
Modified:
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScript.java
URL:
http://svn.apache.org/viewvc/incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScript.java?rev=611796&r1=611795&r2=611796&view=diff
==============================================================================
---
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScript.java
(original)
+++
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScript.java
Mon Jan 14 06:01:21 2008
@@ -18,6 +18,8 @@
*/
package org.apache.sling.scripting.resolver.impl;
+import static java.lang.Boolean.TRUE;
+import static org.apache.sling.api.scripting.SlingBindings.FLUSH;
import static org.apache.sling.api.scripting.SlingBindings.LOG;
import static org.apache.sling.api.scripting.SlingBindings.OUT;
import static org.apache.sling.api.scripting.SlingBindings.REQUEST;
@@ -32,6 +34,9 @@
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Hashtable;
import java.util.Map;
import javax.script.Bindings;
@@ -40,8 +45,14 @@
import javax.script.ScriptException;
import javax.script.SimpleBindings;
import javax.script.SimpleScriptContext;
+import javax.servlet.Servlet;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import org.apache.sling.api.SlingException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
@@ -53,17 +64,23 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-class DefaultSlingScript implements SlingScript {
+class DefaultSlingScript implements SlingScript, Servlet, ServletConfig {
private Resource scriptResource;
private ScriptEngine scriptEngine;
+ private ServletContext servletContext;
+
+ private Dictionary<String, String> initParameters;
+
DefaultSlingScript(Resource scriptResource, ScriptEngine scriptEngine) {
this.scriptResource = scriptResource;
this.scriptEngine = scriptEngine;
}
+ // ---------- SlingScript interface
----------------------------------------
+
public Resource getScriptResource() {
return scriptResource;
}
@@ -83,21 +100,100 @@
try {
// evaluate the script
scriptEngine.eval(reader, ctx);
-
+
// optionall flush the output channel
Object flushObject = bindings.get(SlingBindings.FLUSH);
- if (flushObject instanceof Boolean && ((Boolean)
flushObject).booleanValue()) {
+ if (flushObject instanceof Boolean
+ && ((Boolean) flushObject).booleanValue()) {
ctx.getWriter().flush();
}
-
+
// allways flush the error channel
ctx.getErrorWriter().flush();
-
+
} catch (ScriptException se) {
throw new ServletException(se.getMessage(), se);
}
}
+ // ---------- Servlet interface
--------------------------------------------
+
+ public void init(ServletConfig servletConfig) {
+ if (servletConfig != null) {
+ Dictionary<String, String> params = new Hashtable<String,
String>();
+ for (Enumeration<?> ne = servletConfig.getInitParameterNames();
ne.hasMoreElements();) {
+ String name = String.valueOf(ne.nextElement());
+ String value = servletConfig.getInitParameter(name);
+ params.put(name, value);
+ }
+
+ servletContext = servletConfig.getServletContext();
+ }
+ }
+
+ public void service(ServletRequest req, ServletResponse res)
+ throws ServletException, IOException {
+
+ SlingHttpServletRequest request = (SlingHttpServletRequest) req;
+
+ try {
+ // prepare the properties for the script
+ SlingBindings props = new SlingBindings();
+ props.put(REQUEST, req);
+ props.put(RESPONSE, res);
+ props.put(FLUSH, TRUE);
+
+ res.setCharacterEncoding("UTF-8");
+ res.setContentType(request.getResponseContentType());
+
+ // evaluate the script now using the ScriptEngine
+ eval(props);
+
+ } catch (IOException ioe) {
+ throw ioe;
+ } catch (ServletException se) {
+ throw se;
+ } catch (Exception e) {
+ throw new SlingException("Cannot get DefaultSlingScript: "
+ + e.getMessage(), e);
+ }
+ }
+
+ public ServletConfig getServletConfig() {
+ return this;
+ }
+
+ public String getServletInfo() {
+ return "Script " + getScriptResource().getURI();
+ }
+
+ public void destroy() {
+ initParameters = null;
+ servletContext = null;
+ }
+
+ // ---------- ServletConfig
------------------------------------------------
+
+ public String getInitParameter(String name) {
+ Dictionary<String, String> params = initParameters;
+ return (params != null) ? params.get(name) : null;
+ }
+
+ public Enumeration<String> getInitParameterNames() {
+ Dictionary<String, String> params = initParameters;
+ return (params != null) ? params.keys() : null;
+ }
+
+ public ServletContext getServletContext() {
+ return servletContext;
+ }
+
+ public String getServletName() {
+ return getScriptResource().getURI();
+ }
+
+ // ---------- internal
-----------------------------------------------------
+
private Reader getScriptReader() throws IOException {
InputStream input = getScriptResource().adaptTo(InputStream.class);
@@ -208,7 +304,7 @@
bindings.put(entry.getKey(), entry.getValue());
}
}
-
+
return bindings;
}
Modified:
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/LogWriter.java
URL:
http://svn.apache.org/viewvc/incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/LogWriter.java?rev=611796&r1=611795&r2=611796&view=diff
==============================================================================
---
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/LogWriter.java
(original)
+++
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/LogWriter.java
Mon Jan 14 06:01:21 2008
@@ -18,7 +18,6 @@
*/
package org.apache.sling.scripting.resolver.impl;
-import java.io.IOException;
import java.io.Writer;
import org.slf4j.Logger;
Copied:
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/SlingScriptAdapterFactory.java
(from r607540,
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScriptResolver.java)
URL:
http://svn.apache.org/viewvc/incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/SlingScriptAdapterFactory.java?p2=incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/SlingScriptAdapterFactory.java&p1=incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScriptResolver.java&r1=607540&r2=611796&rev=611796&view=diff
==============================================================================
---
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScriptResolver.java
(original)
+++
incubator/sling/whiteboard/fmeschbe/resource/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/SlingScriptAdapterFactory.java
Mon Jan 14 06:01:21 2008
@@ -21,24 +21,15 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
-import java.util.ArrayList;
-import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
-import java.util.Vector;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
-import org.apache.sling.api.SlingException;
-import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.api.scripting.SlingScript;
-import org.apache.sling.api.scripting.SlingScriptResolver;
-import org.apache.sling.api.servlets.HttpConstants;
-import org.apache.sling.scripting.resolver.ScriptPathSupport;
+import org.apache.sling.osgi.commons.AdapterFactory;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleListener;
@@ -62,102 +53,53 @@
* @scr.component metatype="no"
* @scr.property name="service.vendor" value="The Apache Software Foundation"
* @scr.property name="service.description" value="Default SlingScriptResolver"
- * @scr.service
+ *
+ * @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.osgi.commons.AdapterFactory"
+ *
* @scr.reference name="ScriptEngineFactory"
* interface="javax.script.ScriptEngineFactory"
* cardinality="0..n" policy="dynamic"
*/
-public class DefaultSlingScriptResolver implements SlingScriptResolver,
+public class SlingScriptAdapterFactory implements AdapterFactory,
BundleListener {
- private static final Logger log =
LoggerFactory.getLogger(DefaultSlingScriptResolver.class);
+ private static final Logger log =
LoggerFactory.getLogger(SlingScriptAdapterFactory.class);
/**
* jcr:encoding
*/
public static final String JCR_ENCODING = "jcr:encoding";
- /** @scr.property values.1="/apps" values.2="/libs" */
- public static final String PROP_SCRIPT_PATH = "path";
-
private static final String ENGINE_FACTORY_SERVICE = "META-INF/services/"
+ ScriptEngineFactory.class.getName();
private ScriptEngineManager scriptEngineManager;
- private String[] scriptPath;
-
private List<Bundle> engineSpiBundles = new LinkedList<Bundle>();
private List<ScriptEngineFactory> engineSpiServices = new
LinkedList<ScriptEngineFactory>();
- /**
- * Try to find a script Node that can process the given request, based on
- * the rules defined above.
- *
- * @return null if not found.
- */
- public SlingScript resolveScript(final SlingHttpServletRequest request)
- throws SlingException {
-
- ResourceResolver resolver = request.getResourceResolver();
-
- String baseName = ScriptPathSupport.getScriptBaseName(request) + ".";
-
- SlingScript result = null;
- Iterator<String> pathIterator = ScriptPathSupport.getPathIterator(
- request, scriptPath);
- while (result == null && pathIterator.hasNext()) {
+ //---------- AdapterFactory -----------------------------------------------
+
+ @SuppressWarnings("unchecked")
+ public <AdapterType> AdapterType getAdapter(Object adaptable,
+ Class<AdapterType> type) {
- Resource scriptRoot = resolver.getResource(pathIterator.next());
- if (scriptRoot != null) {
-
- log.debug("Looking for script with filename={} under {}",
- baseName, scriptRoot.getURI());
-
- // get the item and ensure it is a node
- Iterator<Resource> children =
resolver.listChildren(scriptRoot);
- while (result == null && children.hasNext()) {
- Resource resource = children.next();
- result = getScript(resource, baseName);
- }
- }
- }
-
- if (result != null) {
- log.info("Script {} found for Resource={}",
- result.getScriptResource().getURI(), request.getResource());
- } else {
- log.debug("No script found for Resource={}",
request.getResource());
- }
-
- return result;
- }
-
- public SlingScript findScript(ResourceResolver resourceResolver, String
path)
- throws SlingException {
- Resource scriptResource;
- if (path.startsWith("/")) {
- scriptResource = resourceResolver.getResource(path);
- } else {
- scriptResource = null;
- for (int i = 0; scriptResource == null && i < scriptPath.length;
i++) {
- String testPath = scriptPath[i] + "/" + path;
- scriptResource = resourceResolver.getResource(testPath);
- }
- }
+ Resource resource = (Resource) adaptable;
+ String path = resource.getURI();
+ String ext = path.substring(path.lastIndexOf('.') + 1);
- if (scriptResource != null) {
- SlingScript script = getScript(scriptResource, null);
- if (script == null) {
- log.error("Cannot handle script {} for path {}",
- scriptResource.getURI(), path);
- } else {
- log.debug("Returning script {} for path {}",
- scriptResource.getURI(), path);
- }
- } else {
- log.error("No resource found at " + path);
+ ScriptEngine engine = getScriptEngineManager().getEngineByExtension(
+ ext);
+ if (engine != null) {
+ // unchecked cast
+ return (AdapterType) new DefaultSlingScript(resource, engine);
}
return null;
@@ -253,23 +195,6 @@
// ---------- SCR integration
----------------------------------------------
protected void activate(ComponentContext context) {
-
- Object pathProp = context.getProperties().get(PROP_SCRIPT_PATH);
- if (pathProp instanceof String[]) {
- scriptPath = (String[]) pathProp;
- } else if (pathProp instanceof Vector<?>) {
- Vector<?> pathVector = (Vector<?>) pathProp;
- List<String> pathList = new ArrayList<String>();
- for (Object entry : pathVector) {
- if (entry != null) {
- pathList.add(entry.toString());
- }
- }
- scriptPath = pathList.toArray(new String[pathList.size()]);
- } else {
- scriptPath = new String[] { "/" };
- }
-
context.getBundleContext().addBundleListener(this);
Bundle[] bundles = context.getBundleContext().getBundles();
@@ -301,21 +226,4 @@
scriptEngineManager = null;
}
- // ---------- inner class
--------------------------------------------------
-
- private SlingScript getScript(Resource resource, String baseName) {
- String path = resource.getURI();
- String name = path.substring(path.lastIndexOf('/') + 1);
-
- if (baseName == null || name.startsWith(baseName)) {
- String ext = name.substring(baseName.length());
- ScriptEngine engine =
getScriptEngineManager().getEngineByExtension(
- ext);
- if (engine != null) {
- return new DefaultSlingScript(resource, engine);
- }
- }
-
- return null;
- }
}