Author: heiko.braun(a)jboss.com
Date: 2007-11-20 06:48:04 -0500 (Tue, 20 Nov 2007)
New Revision: 5071
Added:
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/InvocationMediator.java
Modified:
stack/native/branches/rest/src/main/java/org/jboss/rs/deployment/ResourceServlet.java
Log:
Introduce invocation mediator
Modified:
stack/native/branches/rest/src/main/java/org/jboss/rs/deployment/ResourceServlet.java
===================================================================
---
stack/native/branches/rest/src/main/java/org/jboss/rs/deployment/ResourceServlet.java 2007-11-20
11:34:38 UTC (rev 5070)
+++
stack/native/branches/rest/src/main/java/org/jboss/rs/deployment/ResourceServlet.java 2007-11-20
11:48:04 UTC (rev 5071)
@@ -25,11 +25,9 @@
import org.jboss.rs.ResourceError;
import org.jboss.rs.ResourceRegistry;
import org.jboss.rs.ResourceRegistryFactory;
-import org.jboss.rs.model.ResourceMethod;
import org.jboss.rs.model.ResourceModel;
-import org.jboss.rs.model.StatefulResourceResolver;
-import org.jboss.rs.model.ResourceLocator;
-import org.jboss.rs.runtime.*;
+import org.jboss.rs.runtime.InvocationMediator;
+import org.jboss.rs.runtime.RuntimeContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
@@ -41,7 +39,6 @@
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
-import java.util.Stack;
/**
* @author Heiko.Braun(a)jboss.com
@@ -95,43 +92,11 @@
RuntimeContext rt = new RuntimeContext(method, uri, rootResources);
parseAcceptHeader(req, rt);
- // locate the resource to be invoked
- StatefulResourceResolver resolver = StatefulResourceResolver.newInstance(rt);
- ResourceMethod resourceMethod = resolver.resolve();
+ // mediate the invocation
+ InvocationMediator mediator = new InvocationMediator(rt);
+ Object result = mediator.invoke();
- // evaluate locator stack
- Object subResourceInstance = null;
- Stack<ResourceLocator> visitedLocators = resolver.getVisitedLocator();
- while(!visitedLocators.isEmpty())
- {
- ResourceLocator loc = visitedLocators.pop();
- rt.setWorkingPath(resolver.getLocatorWorkingPath(loc));
-
- InvocationBuilder builder = new DefaultInvocationBuilder();
- builder.addInvocationModel(loc.getParameterBinding());
- builder.addInvocationModel(loc.getOperationBinding());
- Invocation locatorInvocation = builder.build(rt);
-
- InvocationHandler bridgeInvoker = new DefaultInvocationHandler();
- subResourceInstance = bridgeInvoker.invoke(locatorInvocation);
-
- }
-
- // create an Invocation instance
- InvocationBuilder builder = new DefaultInvocationBuilder();
- rt.setWorkingPath(resolver.getMethodWorkingPath());
-
- if(subResourceInstance!=null)
- builder.addInvocationModel(new PresetInvocationTarget(subResourceInstance));
-
- builder.addInvocationModel( resourceMethod.getParameterBinding() );
- builder.addInvocationModel( resourceMethod.getOperationBinding() );
- Invocation invocation = builder.build(rt);
-
- // invoke it
- InvocationHandler invoker = new DefaultInvocationHandler();
- Object result = invoker.invoke(invocation);
-
+ // write to output stream
if(result instanceof String)
{
res.setContentType("text/plain");
Added:
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/InvocationMediator.java
===================================================================
--- stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/InvocationMediator.java
(rev 0)
+++
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/InvocationMediator.java 2007-11-20
11:48:04 UTC (rev 5071)
@@ -0,0 +1,65 @@
+package org.jboss.rs.runtime;
+
+import org.jboss.rs.model.ResourceLocator;
+import org.jboss.rs.model.StatefulResourceResolver;
+import org.jboss.rs.model.ResourceMethod;
+import org.jboss.rs.ResourceError;
+
+import java.util.Stack;
+
+/**
+ * A resource invocation can go through many locator invocations
+ * before invoking the final resource method. A mediator hides the implementation
+ * details of this process from outermost components.
+ *
+ */
+public class InvocationMediator
+{
+ private RuntimeContext runtimeContext;
+
+ public InvocationMediator(RuntimeContext rt)
+ {
+ this.runtimeContext = rt;
+ }
+
+ public Object invoke() throws ResourceError
+ {
+ StatefulResourceResolver resolver =
StatefulResourceResolver.newInstance(runtimeContext);
+ ResourceMethod resourceMethod = resolver.resolve();
+
+ // evaluate locator stack
+ Object subResourceInstance = null;
+ Stack<ResourceLocator> visitedLocators = resolver.getVisitedLocator();
+ while(!visitedLocators.isEmpty())
+ {
+ ResourceLocator loc = visitedLocators.pop();
+ runtimeContext.setWorkingPath(resolver.getLocatorWorkingPath(loc));
+
+ InvocationBuilder builder = new DefaultInvocationBuilder();
+ builder.addInvocationModel(loc.getParameterBinding());
+ builder.addInvocationModel(loc.getOperationBinding());
+ Invocation locatorInvocation = builder.build(runtimeContext);
+
+ InvocationHandler bridgeInvoker = new DefaultInvocationHandler();
+ subResourceInstance = bridgeInvoker.invoke(locatorInvocation);
+
+ }
+
+ // create an Invocation instance
+ InvocationBuilder builder = new DefaultInvocationBuilder();
+ runtimeContext.setWorkingPath(resolver.getMethodWorkingPath());
+
+ if(subResourceInstance!=null)
+ builder.addInvocationModel(new PresetInvocationTarget(subResourceInstance));
+
+ builder.addInvocationModel( resourceMethod.getParameterBinding() );
+ builder.addInvocationModel( resourceMethod.getOperationBinding() );
+ Invocation invocation = builder.build(runtimeContext);
+
+ // invoke it
+ InvocationHandler invoker = new DefaultInvocationHandler();
+ Object result = invoker.invoke(invocation);
+
+ return result;
+ }
+}
Property changes on:
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/InvocationMediator.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Show replies by date