You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2008/02/27 18:09:33 UTC
svn commit: r631650 - in /incubator/sling/trunk:
api/src/main/java/org/apache/sling/api/scripting/
scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/
scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/
Author: cziegeler
Date: Wed Feb 27 09:09:32 2008
New Revision: 631650
URL: http://svn.apache.org/viewvc?rev=631650&view=rev
Log:
IN PROGRESS - issue SLING-279: ServiceLocator clean up
https://issues.apache.org/jira/browse/SLING-279
Add functionality of ServiceLocator to SlingScriptHelper.
Ensure that SlingScriptHelper is always available as SLING binding.
Clean up validation of bindings a little bit.
Modified:
incubator/sling/trunk/api/src/main/java/org/apache/sling/api/scripting/SlingScriptHelper.java
incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/ScriptHelper.java
incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScript.java
Modified: incubator/sling/trunk/api/src/main/java/org/apache/sling/api/scripting/SlingScriptHelper.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/api/src/main/java/org/apache/sling/api/scripting/SlingScriptHelper.java?rev=631650&r1=631649&r2=631650&view=diff
==============================================================================
--- incubator/sling/trunk/api/src/main/java/org/apache/sling/api/scripting/SlingScriptHelper.java (original)
+++ incubator/sling/trunk/api/src/main/java/org/apache/sling/api/scripting/SlingScriptHelper.java Wed Feb 27 09:09:32 2008
@@ -23,6 +23,7 @@
import org.apache.sling.api.SlingIOException;
import org.apache.sling.api.SlingServletException;
import org.apache.sling.api.request.RequestDispatcherOptions;
+import org.apache.sling.api.services.InvalidServiceFilterSyntaxException;
/**
* The <code>SlingScriptHelper</code> interface defines the API of a helper
@@ -53,7 +54,7 @@
/**
* Same as {@link #include(String,RequestDispatcherOptions)}, but using
* empty options.
- *
+ *
* @throws SlingIOException Wrapping a <code>IOException</code> thrown
* while handling the include.
* @throws SlingServletException Wrapping a <code>ServletException</code>
@@ -65,7 +66,7 @@
* Helper method to include the result of processing the request for the
* given <code>path</code> and <code>requestDispatcherOptions</code>.
* This method is intended to be implemented as follows:
- *
+ *
* <pre>
* RequestDispatcher dispatcher = getRequest().getRequestDispatcher(path,
* "option:xyz");
@@ -73,13 +74,13 @@
* dispatcher.include(getRequest(), getResponse());
* }
* </pre>
- *
+ *
* <p>
* This method creates a <code>RequestDispatcherOptions</code> object by
* calling the
* {@link RequestDispatcherOptions#RequestDispatcherOptions(String)}
* constructor.
- *
+ *
* @param path The path to the resource to include.
* @param requestDispatcherOptions influence the rendering of the included
* Resource
@@ -96,7 +97,7 @@
* Helper method to include the result of processing the request for the
* given <code>path</code> and <code>options</code>. This method is
* intended to be implemented as follows:
- *
+ *
* <pre>
* RequestDispatcherOptions opts = new RequestDispatcherOptions();
* opts.put("option", "xyz");
@@ -105,7 +106,7 @@
* dispatcher.include(getRequest(), getResponse());
* }
* </pre>
- *
+ *
* @param path The path to the resource to include.
* @param options influence the rendering of the included Resource
* @throws SlingIOException Wrapping a <code>IOException</code> thrown
@@ -117,4 +118,29 @@
*/
void include(String path, RequestDispatcherOptions options);
+ /**
+ * Lookup a single service
+ *
+ * @param serviceName The name (interface) of the service.
+ * @return The service instance, or null if the service is not available.
+ */
+ <ServiceType> ServiceType getService(Class<ServiceType> type);
+
+ /**
+ * Lookup one or several services
+ *
+ * @param serviceName The name (interface) of the service.
+ * @param filter An optional filter (LDAP-like, see OSGi spec)
+ * @return The services object or null.
+ * @throws InvalidServiceFilterSyntaxException If the <code>filter</code>
+ * string is not a valid OSGi service filter string.
+ */
+ <ServiceType> ServiceType[] getServices(Class<ServiceType> serviceType,
+ String filter);
+
+ /**
+ * Dispose the helper.
+ * This method can be used to clean up the script helper after the script is run.
+ */
+ void dispose();
}
Modified: incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/ScriptHelper.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/ScriptHelper.java?rev=631650&r1=631649&r2=631650&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/ScriptHelper.java (original)
+++ incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/ScriptHelper.java Wed Feb 27 09:09:32 2008
@@ -19,6 +19,11 @@
package org.apache.sling.scripting.resolver;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
@@ -30,8 +35,12 @@
import org.apache.sling.api.request.RequestDispatcherOptions;
import org.apache.sling.api.scripting.SlingScript;
import org.apache.sling.api.scripting.SlingScriptHelper;
+import org.apache.sling.api.services.InvalidServiceFilterSyntaxException;
import org.apache.sling.scripting.resolver.impl.helper.OnDemandReaderRequest;
import org.apache.sling.scripting.resolver.impl.helper.OnDemandWriterResponse;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
/**
* Simple script helper providing access to the (wrapped) response, the
@@ -47,8 +56,27 @@
private final SlingHttpServletResponse response;
- public ScriptHelper(SlingScript script, SlingHttpServletRequest request,
+ protected final BundleContext bundleContext;
+
+ /**
+ * The list of references - we don't need to synchronize this as we are
+ * running in one single request.
+ */
+ protected final List<ServiceReference> references = new ArrayList<ServiceReference>();
+
+ /** A map of found services. */
+ protected final Map<String, Object> services = new HashMap<String, Object>();
+
+ public ScriptHelper(BundleContext ctx, SlingScript script) {
+ this.bundleContext = ctx;
+ this.request = null;
+ this.response = null;
+ this.script = script;
+ }
+
+ public ScriptHelper(BundleContext ctx, SlingScript script, SlingHttpServletRequest request,
SlingHttpServletResponse response) {
+ this.bundleContext = ctx;
this.script = script;
this.request = new OnDemandReaderRequest(request);
this.response = new OnDemandWriterResponse(response);
@@ -95,5 +123,74 @@
throw new SlingServletException(se);
}
}
+ }
+
+ /**
+ * @see org.apache.sling.api.scripting.SlingScriptHelper#getService(java.lang.Class)
+ */
+ @SuppressWarnings("unchecked")
+ public <ServiceType> ServiceType getService(Class<ServiceType> type) {
+ // TODO - we should make sure in the constructor that bundle context is not null
+ if ( this.bundleContext == null ) {
+ throw new IllegalAccessError("getService() can't be invoked without a bundle context.");
+ }
+ ServiceType service = (ServiceType) this.services.get(type.getName());
+ if (service == null) {
+ final ServiceReference ref = this.bundleContext.getServiceReference(type.getName());
+ if (ref != null) {
+ this.references.add(ref);
+ service = (ServiceType) this.bundleContext.getService(ref);
+ this.services.put(type.getName(), service);
+ }
+ }
+ return service;
+ }
+
+ /**
+ * @see org.apache.sling.api.scripting.SlingScriptHelper#getServices(java.lang.Class, java.lang.String)
+ */
+ @SuppressWarnings("unchecked")
+ public <ServiceType> ServiceType[] getServices(
+ Class<ServiceType> serviceType, String filter)
+ throws InvalidServiceFilterSyntaxException {
+ // TODO - we should make sure in the constructor that bundle context is not null
+ if ( this.bundleContext == null ) {
+ throw new IllegalAccessError("getService() can't be invoked without a bundle context.");
+ }
+ try {
+ final ServiceReference[] refs = this.bundleContext.getServiceReferences(
+ serviceType.getName(), filter);
+ ServiceType[] result = null;
+ if (refs != null) {
+ final List<ServiceType> objects = new ArrayList<ServiceType>();
+ for (int i = 0; i < refs.length; i++) {
+ this.references.add(refs[i]);
+ final ServiceType service = (ServiceType) this.bundleContext.getService(refs[i]);
+ if (service != null) {
+ objects.add(service);
+ }
+ }
+ if (objects.size() > 0) {
+ result = (ServiceType[]) objects.toArray();
+ }
+ }
+ return result;
+ } catch (InvalidSyntaxException ise) {
+ throw new InvalidServiceFilterSyntaxException(filter,
+ "Invalid filter syntax", ise);
+ }
+ }
+
+ /**
+ * @see org.apache.sling.api.scripting.SlingScriptHelper#dispose()
+ */
+ public void dispose() {
+ final Iterator<ServiceReference> i = this.references.iterator();
+ while (i.hasNext()) {
+ final ServiceReference ref = i.next();
+ this.bundleContext.ungetService(ref);
+ }
+ this.references.clear();
+ this.services.clear();
}
}
Modified: incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScript.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScript.java?rev=631650&r1=631649&r2=631650&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScript.java (original)
+++ incubator/sling/trunk/scripting/resolver/src/main/java/org/apache/sling/scripting/resolver/impl/DefaultSlingScript.java Wed Feb 27 09:09:32 2008
@@ -96,8 +96,9 @@
String scriptName = getScriptResource().getPath();
+ Bindings bindings = null;
try {
- Bindings bindings = verifySlingBindings(scriptName, props);
+ bindings = verifySlingBindings(scriptName, props);
ScriptContext ctx = new SimpleScriptContext();
ctx.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
@@ -127,6 +128,13 @@
Throwable cause = (se.getCause() == null) ? se : se.getCause();
throw new ScriptEvaluationException(scriptName, se.getMessage(),
cause);
+ } finally {
+ if ( bindings != null ) {
+ final SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SLING);
+ if ( helper != null ) {
+ helper.dispose();
+ }
+ }
}
}
@@ -236,7 +244,23 @@
Bindings bindings = new SimpleBindings();
- SlingHttpServletRequest request = slingBindings.getRequest();
+ final SlingHttpServletRequest request = slingBindings.getRequest();
+
+ // check sling object
+ Object slingObject = slingBindings.get(SLING);
+ if (slingObject == null) {
+
+ if ( request != null ) {
+ slingObject = new ScriptHelper(null, this, request, slingBindings.getResponse());
+ } else {
+ slingObject = new ScriptHelper(null, this);
+ }
+ } else if (!(slingObject instanceof SlingScriptHelper) ) {
+ throw fail(scriptName, SLING, "Wrong type");
+ }
+ final SlingScriptHelper sling = (SlingScriptHelper)slingObject;
+ bindings.put(SLING, sling);
+
if (request != null) {
//throw fail(scriptName, REQUEST, "Missing or wrong type");
@@ -255,16 +279,8 @@
throw fail(scriptName, OUT, "Wrong type");
}
- SlingScriptHelper sling;
-
- Object slingObject = slingBindings.get(SLING);
- if (slingObject == null) {
-
- sling = new ScriptHelper(this, request, response);
-
- } else if (slingObject instanceof SlingScriptHelper) {
-
- sling = (SlingScriptHelper) slingObject;
+ // if there is a provided sling script helper, check arguments
+ if (slingBindings.get(SLING) != null) {
if (sling.getRequest() != request) {
throw fail(scriptName, REQUEST,
@@ -287,15 +303,9 @@
throw fail(scriptName, OUT,
"Not the same as writer of the SlingScriptHelper response");
}
-
- } else {
-
- throw fail(scriptName, SLING, "Wrong type");
-
}
// set base variables when executing inside a request
- bindings.put(SLING, sling);
bindings.put(REQUEST, sling.getRequest());
bindings.put(READER, sling.getRequest().getReader());
bindings.put(RESPONSE, sling.getResponse());