Title: [waffle-scm] [68] trunk: initial checkin of JRuby support for Waffle.
Revision
68
Author
mward
Date
2007-05-22 22:24:33 -0500 (Tue, 22 May 2007)

Log Message

initial checkin of JRuby support for Waffle.

Modified Paths


Added Paths

Diff

Modified: trunk/core/core.iml (67 => 68)

--- trunk/core/core.iml	2007-05-22 15:07:28 UTC (rev 67)
+++ trunk/core/core.iml	2007-05-23 03:24:33 UTC (rev 68)
@@ -131,6 +131,33 @@
         <SOURCES />
       </library>
     </orderEntry>
+    <orderEntry type="module-library">
+      <library>
+        <CLASSES>
+          <root url="" />
+        </CLASSES>
+        <JAVADOC />
+        <SOURCES />
+      </library>
+    </orderEntry>
+    <orderEntry type="module-library">
+      <library>
+        <CLASSES>
+          <root url="" />
+        </CLASSES>
+        <JAVADOC />
+        <SOURCES />
+      </library>
+    </orderEntry>
+    <orderEntry type="module-library">
+      <library>
+        <CLASSES>
+          <root url="" />
+        </CLASSES>
+        <JAVADOC />
+        <SOURCES />
+      </library>
+    </orderEntry>
     <orderEntryProperties />
   </component>
 </module>

Modified: trunk/core/pom.xml (67 => 68)

--- trunk/core/pom.xml	2007-05-22 15:07:28 UTC (rev 67)
+++ trunk/core/pom.xml	2007-05-23 03:24:33 UTC (rev 68)
@@ -28,6 +28,10 @@
       <artifactId>paranamer</artifactId>
     </dependency>
     <dependency>
+      <groupId>org.jruby</groupId>
+      <artifactId>jruby</artifactId>
+    </dependency>
+    <dependency>
       <groupId>velocity</groupId>
       <artifactId>velocity</artifactId>
     </dependency>

Modified: trunk/core/src/main/java/org/codehaus/waffle/WaffleComponentRegistry.java (67 => 68)

--- trunk/core/src/main/java/org/codehaus/waffle/WaffleComponentRegistry.java	2007-05-22 15:07:28 UTC (rev 67)
+++ trunk/core/src/main/java/org/codehaus/waffle/WaffleComponentRegistry.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -16,6 +16,7 @@
 import org.codehaus.waffle.action.ActionMethodResponseHandler;
 import org.codehaus.waffle.action.ArgumentResolver;
 import org.codehaus.waffle.action.MethodDefinitionFinder;
+import org.codehaus.waffle.action.MethodNameResolver;
 import org.codehaus.waffle.bind.BindErrorMessageResolver;
 import org.codehaus.waffle.bind.DataBinder;
 import org.codehaus.waffle.context.ContextContainerFactory;
@@ -53,6 +54,8 @@
 
     MethodDefinitionFinder getMethodDefinitionFinder();
 
+    MethodNameResolver getMethodNameResolver();
+
     TypeConverter getTypeConverter();
 
     Validator getValidator();

Modified: trunk/core/src/main/java/org/codehaus/waffle/action/RequestParameterMethodNameResolver.java (67 => 68)

--- trunk/core/src/main/java/org/codehaus/waffle/action/RequestParameterMethodNameResolver.java	2007-05-22 15:07:28 UTC (rev 67)
+++ trunk/core/src/main/java/org/codehaus/waffle/action/RequestParameterMethodNameResolver.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -1,35 +1,35 @@
-/*****************************************************************************
- * Copyright (C) 2005,2006 Michael Ward                                      *
- * All rights reserved.                                                      *
- * ------------------------------------------------------------------------- *
- * The software in this package is published under the terms of the BSD      *
- * style license a copy of which has been included with this distribution in *
- * the LICENSE.txt file.                                                     *
- *                                                                           *
- * Original code by: Michael Ward                                            *
- *****************************************************************************/
-package org.codehaus.waffle.action;
-
-import javax.servlet.http.HttpServletRequest;
-
-/**
- * Implementation of method name resolver which returns the value of a configurable action parameter key,
- * which defaults to 'method'.
- * 
- * @author Michael Ward
- */
-public class RequestParameterMethodNameResolver implements MethodNameResolver {
-    private String actionParameterKey = "method";
-
-    public RequestParameterMethodNameResolver() {
-        // default
-    }
-
-    public RequestParameterMethodNameResolver(RequestParameterMethodNameResolverConfig configuration) {
-        this.actionParameterKey = configuration.getMethodParameterKey();
-    }
-
-    public String resolve(HttpServletRequest request) {
-        return request.getParameter(actionParameterKey);
-    }
-}
+/*****************************************************************************
+ * Copyright (C) 2005,2006 Michael Ward                                      *
+ * All rights reserved.                                                      *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by: Michael Ward                                            *
+ *****************************************************************************/
+package org.codehaus.waffle.action;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * Implementation of method name resolver which returns the value of a configurable action parameter key,
+ * which defaults to 'method'.
+ * 
+ * @author Michael Ward
+ */
+public class RequestParameterMethodNameResolver implements MethodNameResolver {
+    private String methodParameterKey = "method";
+
+    public RequestParameterMethodNameResolver() {
+        // default
+    }
+
+    public RequestParameterMethodNameResolver(RequestParameterMethodNameResolverConfig configuration) {
+        this.methodParameterKey = configuration.getMethodParameterKey();
+    }
+
+    public String resolve(HttpServletRequest request) {
+        return request.getParameter(methodParameterKey);
+    }
+}

Added: trunk/core/src/main/java/org/codehaus/waffle/bind/RubyDataBinder.java (0 => 68)

--- trunk/core/src/main/java/org/codehaus/waffle/bind/RubyDataBinder.java	                        (rev 0)
+++ trunk/core/src/main/java/org/codehaus/waffle/bind/RubyDataBinder.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -0,0 +1,23 @@
+package org.codehaus.waffle.bind;
+
+import ognl.TypeConverter;
+import org.codehaus.waffle.validation.ErrorsContext;
+import org.jruby.runtime.builtin.IRubyObject;
+
+import javax.servlet.http.HttpServletRequest;
+
+public class RubyDataBinder extends OgnlDataBinder {
+
+    // needs to bind, request, session and parameter to instance variables
+    public RubyDataBinder(TypeConverter typeConverter, BindErrorMessageResolver bindErrorMessageResolver) {
+        super(typeConverter, bindErrorMessageResolver);
+    }
+
+    public void bind(HttpServletRequest request, ErrorsContext errorsContext, Object model) {
+        if(model instanceof IRubyObject) {
+            // does nothing for the moment
+        } else {
+            super.bind(request, errorsContext, model);
+        }
+    }
+}

Modified: trunk/core/src/main/java/org/codehaus/waffle/context/pico/PicoContextContainerFactory.java (67 => 68)

--- trunk/core/src/main/java/org/codehaus/waffle/context/pico/PicoContextContainerFactory.java	2007-05-22 15:07:28 UTC (rev 67)
+++ trunk/core/src/main/java/org/codehaus/waffle/context/pico/PicoContextContainerFactory.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -55,7 +55,7 @@
             HttpSession session = request.getSession();
             PicoContextContainer sessionContextContainer = (PicoContextContainer) session.getAttribute(Constants.SESSION_CONTAINER_KEY);
             if (sessionContextContainer == null) {
-                throw new RuntimeException("Waffle: Possible Tomcat deployment OR Configuration error - read http://waffle.sourceforge.net/tomcat_deployment_issue.html");
+                throw new RuntimeException("Waffle: Possible Tomcat deployment OR Configuration error - read http://waffle.codehaus.org/tomcat_deployment_issue.html");
             }
             MutablePicoContainer delegate = sessionContextContainer.getDelegate();
 

Modified: trunk/core/src/main/java/org/codehaus/waffle/context/pico/PicoWaffleComponentRegistry.java (67 => 68)

--- trunk/core/src/main/java/org/codehaus/waffle/context/pico/PicoWaffleComponentRegistry.java	2007-05-22 15:07:28 UTC (rev 67)
+++ trunk/core/src/main/java/org/codehaus/waffle/context/pico/PicoWaffleComponentRegistry.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -10,12 +10,7 @@
  *****************************************************************************/
 package org.codehaus.waffle.context.pico;
 
-import java.util.Enumeration;
-
-import javax.servlet.ServletContext;
-
 import ognl.TypeConverter;
-
 import org.codehaus.waffle.WaffleComponentRegistry;
 import org.codehaus.waffle.WaffleException;
 import org.codehaus.waffle.action.ActionMethodExecutor;
@@ -26,6 +21,8 @@
 import org.codehaus.waffle.action.HierarchicalArgumentResolver;
 import org.codehaus.waffle.action.InterceptingActionMethodExecutor;
 import org.codehaus.waffle.action.MethodDefinitionFinder;
+import org.codehaus.waffle.action.MethodNameResolver;
+import org.codehaus.waffle.action.RequestParameterMethodNameResolver;
 import org.codehaus.waffle.bind.BindErrorMessageResolver;
 import org.codehaus.waffle.bind.DataBinder;
 import org.codehaus.waffle.bind.DefaultBindErrorMessageResolver;
@@ -51,6 +48,9 @@
 import org.picocontainer.defaults.ConstructorInjectionComponentAdapter;
 import org.picocontainer.defaults.DefaultPicoContainer;
 
+import javax.servlet.ServletContext;
+import java.util.Enumeration;
+
 /**
  * This allows Waffle to have a pluggable architecture.
  *
@@ -77,8 +77,9 @@
         register(ControllerNameResolver.class, DefaultControllerNameResolver.class, servletContext);
         register(DataBinder.class, OgnlDataBinder.class, servletContext);
         register(DispatchAssistant.class, DefaultDispatchAssistant.class, servletContext);
+        register(MessageResources.class, DefaultMessageResources.class, servletContext);
         register(MethodDefinitionFinder.class, AnnotatedMethodDefinitionFinder.class, servletContext);
-        register(MessageResources.class, DefaultMessageResources.class, servletContext);
+        register(MethodNameResolver.class, RequestParameterMethodNameResolver.class, servletContext);
         register(TypeConverter.class, OgnlTypeConverter.class, servletContext);
         register(Validator.class, DefaultValidator.class, servletContext);
         register(ViewDispatcher.class, DefaultViewDispatcher.class, servletContext);
@@ -210,6 +211,10 @@
         return locateByType(MethodDefinitionFinder.class);
     }
 
+    public MethodNameResolver getMethodNameResolver() {
+        return locateByType(MethodNameResolver.class);
+    }
+
     public TypeConverter getTypeConverter() {
         return locateByType(TypeConverter.class);
     }

Added: trunk/core/src/main/java/org/codehaus/waffle/context/pico/RubyAwarePicoContextContainerFactory.java (0 => 68)

--- trunk/core/src/main/java/org/codehaus/waffle/context/pico/RubyAwarePicoContextContainerFactory.java	                        (rev 0)
+++ trunk/core/src/main/java/org/codehaus/waffle/context/pico/RubyAwarePicoContextContainerFactory.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -0,0 +1,24 @@
+package org.codehaus.waffle.context.pico;
+
+import org.codehaus.waffle.i18n.MessageResources;
+import org.codehaus.waffle.context.ContextContainer;
+import org.picocontainer.MutablePicoContainer;
+import org.jruby.Ruby;
+
+public class RubyAwarePicoContextContainerFactory extends PicoContextContainerFactory {
+
+    public RubyAwarePicoContextContainerFactory(MessageResources messageResources) {
+        super(messageResources);
+    }
+
+    public ContextContainer buildApplicationContextContainer() {
+        ContextContainer contextContainer = super.buildApplicationContextContainer();
+
+        // Register RubyRuntime at Application level
+        MutablePicoContainer picoContainer = (MutablePicoContainer) contextContainer.getDelegate();
+        picoContainer.registerComponentInstance(Ruby.class, Ruby.getDefaultInstance());
+
+        return contextContainer;
+    }
+    
+}

Modified: trunk/core/src/main/java/org/codehaus/waffle/controller/DefaultControllerDefinitionFactory.java (67 => 68)

--- trunk/core/src/main/java/org/codehaus/waffle/controller/DefaultControllerDefinitionFactory.java	2007-05-22 15:07:28 UTC (rev 67)
+++ trunk/core/src/main/java/org/codehaus/waffle/controller/DefaultControllerDefinitionFactory.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -10,12 +10,12 @@
  *****************************************************************************/
 package org.codehaus.waffle.controller;
 
+import static org.codehaus.waffle.Constants.CONTROLLER_KEY;
 import org.codehaus.waffle.WaffleException;
-import org.codehaus.waffle.Constants;
+import org.codehaus.waffle.action.MethodDefinition;
+import org.codehaus.waffle.action.MethodDefinitionFinder;
 import org.codehaus.waffle.context.ContextContainer;
 import org.codehaus.waffle.context.RequestLevelContainer;
-import org.codehaus.waffle.action.MethodDefinition;
-import org.codehaus.waffle.action.MethodDefinitionFinder;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -23,7 +23,7 @@
 /**
  * Default implementation of the controller definition factory which uses the context container to look up the
  * controller objected registered.
- * 
+ *
  * @author Michael Ward
  * @author Mauro Talevi
  * @todo (mt) Rename to ContextContainerControllerDefinitionFactory?
@@ -37,11 +37,7 @@
         this.controllerNameResolver = controllerNameResolver;
     }
 
-    /**
-     * Retrieves the controller definition from the context container via the WaffleRequestFilter
-     * @see org.codehaus.waffle.context.WaffleRequestFilter
-     */
-    public ControllerDefinition getControllerDefinition(HttpServletRequest request, HttpServletResponse response) {
+    protected Object findController(String name, HttpServletRequest request) {
         ContextContainer requestLevelContainer = RequestLevelContainer.get();
 
         if (requestLevelContainer == null) {
@@ -50,18 +46,34 @@
             throw new WaffleException(error);
         }
 
-        String name = controllerNameResolver.findControllerName(request);
         Object controller = requestLevelContainer.getComponentInstance(name);
-        if ( controller == null ){
+        if (controller == null) {
             String error = "No controller configured for the specified path: '"
                     + request.getRequestURI() + "' (controller name='" + name + "') "
-                + "Please ensure that controller '" + name + "' was registered in the Registrar.";
-            throw new WaffleException(error);            
+                    + "Please ensure that controller '" + name + "' was registered in the Registrar.";
+            throw new WaffleException(error);
         }
-        MethodDefinition methodDefinition = methodDefinitionFinder.find(controller, request, response);
 
+        return controller;
+    }
+
+    protected MethodDefinition findMethodDefinition(Object controller, HttpServletRequest request, HttpServletResponse response) {
+        return methodDefinitionFinder.find(controller, request, response);
+    }
+
+    /**
+     * Retrieves the controller definition from the context container via the WaffleRequestFilter
+     *
+     * @see org.codehaus.waffle.context.WaffleRequestFilter
+     */
+    public ControllerDefinition getControllerDefinition(HttpServletRequest request, HttpServletResponse response) {
+        String name = controllerNameResolver.findControllerName(request);
+
+        Object controller = findController(name, request);
+        MethodDefinition methodDefinition = findMethodDefinition(controller, request, response);
+
         // set the controller to the request so it can be accessed from the view
-        request.setAttribute(Constants.CONTROLLER_KEY, controller);
+        request.setAttribute(CONTROLLER_KEY, controller);
         return new ControllerDefinition(name, controller, methodDefinition);
     }
 

Added: trunk/core/src/main/java/org/codehaus/waffle/controller/RubyController.java (0 => 68)

--- trunk/core/src/main/java/org/codehaus/waffle/controller/RubyController.java	                        (rev 0)
+++ trunk/core/src/main/java/org/codehaus/waffle/controller/RubyController.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -0,0 +1,29 @@
+package org.codehaus.waffle.controller;
+
+import org.jruby.runtime.builtin.IRubyObject;
+import org.jruby.javasupport.JavaUtil;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * This is a wrapper for the underlying ruby script
+ */
+public class RubyController {
+    private final IRubyObject rubyObject;
+    private String methodName;
+
+    public RubyController(IRubyObject rubyObject) {
+        this.rubyObject = rubyObject;
+    }
+
+    public void setMethodName(String methodName) {
+        this.methodName = methodName;
+    }
+
+    public Object execute(HttpServletRequest request, HttpServletResponse response) {
+        IRubyObject result = rubyObject.callMethod(rubyObject.getRuntime().getCurrentContext(), methodName);
+
+        return JavaUtil.convertRubyToJava(result);
+    }
+}

Added: trunk/core/src/main/java/org/codehaus/waffle/controller/RubyControllerDefinitionFactory.java (0 => 68)

--- trunk/core/src/main/java/org/codehaus/waffle/controller/RubyControllerDefinitionFactory.java	                        (rev 0)
+++ trunk/core/src/main/java/org/codehaus/waffle/controller/RubyControllerDefinitionFactory.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -0,0 +1,59 @@
+package org.codehaus.waffle.controller;
+
+import org.codehaus.waffle.WaffleException;
+import org.codehaus.waffle.action.MethodDefinition;
+import org.codehaus.waffle.action.MethodDefinitionFinder;
+import org.codehaus.waffle.action.MethodNameResolver;
+import org.jruby.runtime.builtin.IRubyObject;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.lang.reflect.Method;
+
+public class RubyControllerDefinitionFactory extends DefaultControllerDefinitionFactory {
+    private final MethodNameResolver methodNameResolver;
+    private static final Method executeMethod;
+
+    static {
+        try {
+            executeMethod = RubyController.class.getMethod("execute", HttpServletRequest.class, HttpServletResponse.class);
+        } catch (NoSuchMethodException e) {
+            throw new WaffleException("FATAL: Waffle's RubyController does not define an execute() method.");
+        }
+    }
+
+    public RubyControllerDefinitionFactory(MethodDefinitionFinder methodDefinitionFinder,
+                                           ControllerNameResolver controllerNameResolver,
+                                           MethodNameResolver methodNameResolver) {
+        super(methodDefinitionFinder, controllerNameResolver);
+        this.methodNameResolver = methodNameResolver;
+    }
+
+    protected Object findController(String name, HttpServletRequest request) {
+        Object controller = super.findController(name, request);
+
+        if (controller instanceof IRubyObject) {
+            return new RubyController((IRubyObject) controller);
+        }
+
+        return controller;
+    }
+
+    protected MethodDefinition findMethodDefinition(Object controller, HttpServletRequest request, HttpServletResponse response) {
+        if (controller instanceof RubyController) {
+            RubyController rubyController = (RubyController) controller;
+            String methodName = methodNameResolver.resolve(request);
+
+            if(methodName == null || methodName.equals("")) {
+                methodName = "index"; // default to the index method
+            }
+
+            rubyController.setMethodName(methodName);
+            MethodDefinition methodDefinition = new MethodDefinition(executeMethod);
+            methodDefinition.addMethodArgument(request);
+            methodDefinition.addMethodArgument(response);
+            return methodDefinition;
+        }
+        return super.findMethodDefinition(controller, request, response);
+    }
+}

Added: trunk/core/src/main/java/org/codehaus/waffle/registrar/pico/RubyScriptComponentAdapter.java (0 => 68)

--- trunk/core/src/main/java/org/codehaus/waffle/registrar/pico/RubyScriptComponentAdapter.java	                        (rev 0)
+++ trunk/core/src/main/java/org/codehaus/waffle/registrar/pico/RubyScriptComponentAdapter.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -0,0 +1,44 @@
+package org.codehaus.waffle.registrar.pico;
+
+import org.picocontainer.PicoVisitor;
+import org.picocontainer.ComponentAdapter;
+import org.picocontainer.PicoContainer;
+import org.picocontainer.PicoIntrospectionException;
+import org.picocontainer.PicoInitializationException;
+import org.jruby.Ruby;
+import org.jruby.runtime.builtin.IRubyObject;
+
+public class RubyScriptComponentAdapter implements ComponentAdapter {
+    private Object componentKey;
+    private final String rubyScript;
+
+    public RubyScriptComponentAdapter(Object componentKey, String rubyScript) {
+        this.componentKey = componentKey;
+        this.rubyScript = rubyScript;
+    }
+
+    public Object getComponentKey() {
+        return componentKey;
+    }
+
+    public Class getComponentImplementation() {
+        return IRubyObject.class;
+    }
+
+    public Object getComponentInstance(PicoContainer picoContainer) throws PicoInitializationException, PicoIntrospectionException {
+        Ruby runtime = (Ruby) picoContainer.getComponentInstance(Ruby.class);
+        runtime.evalScript(rubyScript);
+
+        // TODO RUBY: mixin custom module!
+
+        return runtime.evalScript(componentKey + ".new"); // TODO RUBY: convert name to Camelize
+    }
+
+    public void verify(PicoContainer picoContainer) throws PicoIntrospectionException {
+        // do nothing!
+    }
+
+    public void accept(PicoVisitor picoVisitor) {
+        // do nothing!
+    }
+}

Modified: trunk/core/src/test/java/org/codehaus/waffle/context/pico/PicoWaffleComponentRegistryTest.java (67 => 68)

--- trunk/core/src/test/java/org/codehaus/waffle/context/pico/PicoWaffleComponentRegistryTest.java	2007-05-22 15:07:28 UTC (rev 67)
+++ trunk/core/src/test/java/org/codehaus/waffle/context/pico/PicoWaffleComponentRegistryTest.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -1,11 +1,16 @@
 package org.codehaus.waffle.context.pico;
 
+import ognl.DefaultTypeConverter;
+import ognl.TypeConverter;
 import org.codehaus.waffle.WaffleComponentRegistry;
-import org.codehaus.waffle.controller.DefaultControllerNameResolver;
-import org.codehaus.waffle.controller.ControllerDefinitionFactory;
-import org.codehaus.waffle.controller.DefaultControllerDefinitionFactory;
-import org.codehaus.waffle.controller.ControllerNameResolver;
-import org.codehaus.waffle.action.*;
+import org.codehaus.waffle.action.ActionMethodExecutor;
+import org.codehaus.waffle.action.ActionMethodResponseHandler;
+import org.codehaus.waffle.action.ArgumentResolver;
+import org.codehaus.waffle.action.DefaultActionMethodResponseHandler;
+import org.codehaus.waffle.action.InterceptingActionMethodExecutor;
+import org.codehaus.waffle.action.MethodDefinitionFinder;
+import org.codehaus.waffle.action.RequestParameterMethodNameResolver;
+import org.codehaus.waffle.action.MethodNameResolver;
 import org.codehaus.waffle.bind.BindErrorMessageResolver;
 import org.codehaus.waffle.bind.DataBinder;
 import org.codehaus.waffle.bind.DefaultBindErrorMessageResolver;
@@ -13,15 +18,19 @@
 import org.codehaus.waffle.bind.OgnlTypeConverter;
 import org.codehaus.waffle.context.AbstractContextContainerFactory;
 import org.codehaus.waffle.context.ContextContainerFactory;
-import org.codehaus.waffle.context.pico.PicoWaffleComponentRegistry;
+import org.codehaus.waffle.controller.ControllerDefinitionFactory;
+import org.codehaus.waffle.controller.ControllerNameResolver;
+import org.codehaus.waffle.controller.DefaultControllerDefinitionFactory;
+import org.codehaus.waffle.controller.DefaultControllerNameResolver;
 import org.codehaus.waffle.i18n.DefaultMessageResources;
 import org.codehaus.waffle.i18n.MessageResources;
-import org.codehaus.waffle.testmodel.StubControllerDefinitionFactory;
 import org.codehaus.waffle.testmodel.StubActionMethodExecutor;
 import org.codehaus.waffle.testmodel.StubActionMethodResponseHandler;
 import org.codehaus.waffle.testmodel.StubArgumentResolver;
 import org.codehaus.waffle.testmodel.StubBindErrorMessageResolver;
 import org.codehaus.waffle.testmodel.StubContextContainerFactory;
+import org.codehaus.waffle.testmodel.StubControllerDefinitionFactory;
+import org.codehaus.waffle.testmodel.StubControllerNameResolver;
 import org.codehaus.waffle.testmodel.StubDataBinder;
 import org.codehaus.waffle.testmodel.StubDispatchAssistant;
 import org.codehaus.waffle.testmodel.StubMessageResources;
@@ -29,7 +38,7 @@
 import org.codehaus.waffle.testmodel.StubValidator;
 import org.codehaus.waffle.testmodel.StubViewDispatcher;
 import org.codehaus.waffle.testmodel.StubViewResolver;
-import org.codehaus.waffle.testmodel.StubControllerNameResolver;
+import org.codehaus.waffle.testmodel.StubMethodNameResolver;
 import org.codehaus.waffle.validation.DefaultValidator;
 import org.codehaus.waffle.validation.Validator;
 import org.codehaus.waffle.view.DefaultDispatchAssistant;
@@ -38,8 +47,6 @@
 import org.codehaus.waffle.view.DispatchAssistant;
 import org.codehaus.waffle.view.ViewDispatcher;
 import org.codehaus.waffle.view.ViewResolver;
-import ognl.DefaultTypeConverter;
-import ognl.TypeConverter;
 import org.jmock.Mock;
 import org.jmock.MockObjectTestCase;
 import org.picocontainer.MutablePicoContainer;
@@ -85,7 +92,7 @@
         mockServletContext.expects(once())
                 .method("getInitParameterNames")
                 .will(returnValue(EMPTY_ENUMERATION));
-        mockServletContext.expects(exactly(15))
+        mockServletContext.expects(exactly(16))
                 .method("getInitParameter")
                 .will(returnValue(null));
         ServletContext servletContext = (ServletContext) mockServletContext.proxy();
@@ -99,6 +106,7 @@
         assertTrue(componentRegistry.getDispatchAssistant() instanceof DefaultDispatchAssistant);
         assertTrue(componentRegistry.getActionMethodExecutor() instanceof InterceptingActionMethodExecutor);
         assertTrue(componentRegistry.getActionMethodResponseHandler() instanceof DefaultActionMethodResponseHandler);
+        assertTrue(componentRegistry.getMethodNameResolver() instanceof RequestParameterMethodNameResolver);
         assertTrue(componentRegistry.getMessageResources() instanceof DefaultMessageResources);
         assertTrue(componentRegistry.getViewDispatcher() instanceof DefaultViewDispatcher);
         assertTrue(componentRegistry.getTypeConverter() instanceof OgnlTypeConverter);
@@ -156,6 +164,9 @@
         mockServletContext.expects(once()).method("getInitParameter")
                 .with(eq(MethodDefinitionFinder.class.getName()))
                 .will(returnValue(StubMethodDefinitionFinder.class.getName()));
+        mockServletContext.expects(once()).method("getInitParameter")
+                .with(eq(MethodNameResolver.class.getName()))
+                .will(returnValue(StubMethodNameResolver.class.getName()));
 
         ServletContext servletContext = (ServletContext) mockServletContext.proxy();
         WaffleComponentRegistry componentRegistry = new PicoWaffleComponentRegistry(servletContext);
@@ -169,6 +180,7 @@
         assertTrue(componentRegistry.getDispatchAssistant() instanceof StubDispatchAssistant);
         assertTrue(componentRegistry.getActionMethodExecutor() instanceof StubActionMethodExecutor);
         assertTrue(componentRegistry.getMethodDefinitionFinder() instanceof StubMethodDefinitionFinder);
+        assertTrue(componentRegistry.getMethodNameResolver() instanceof StubMethodNameResolver);
         assertTrue(componentRegistry.getActionMethodResponseHandler() instanceof StubActionMethodResponseHandler);
         assertTrue(componentRegistry.getMessageResources() instanceof StubMessageResources);
         assertTrue(componentRegistry.getTypeConverter() instanceof DefaultTypeConverter);
@@ -185,7 +197,7 @@
         mockServletContext.expects(once())
                 .method("getInitParameterNames")
                 .will(returnValue(Collections.enumeration(names)));
-        mockServletContext.expects(exactly(15))
+        mockServletContext.expects(exactly(16))
                 .method("getInitParameter")
                 .will(returnValue(null));
         mockServletContext.expects(once())
@@ -209,7 +221,7 @@
         mockServletContext.expects(once())
                 .method("getInitParameterNames")
                 .will(returnValue(Collections.enumeration(names)));
-        mockServletContext.expects(exactly(15))
+        mockServletContext.expects(exactly(16))
                 .method("getInitParameter")
                 .will(returnValue(null));
         mockServletContext.expects(once())

Added: trunk/core/src/test/java/org/codehaus/waffle/context/pico/RubyAwarePicoContextContainerFactoryTest.java (0 => 68)

--- trunk/core/src/test/java/org/codehaus/waffle/context/pico/RubyAwarePicoContextContainerFactoryTest.java	                        (rev 0)
+++ trunk/core/src/test/java/org/codehaus/waffle/context/pico/RubyAwarePicoContextContainerFactoryTest.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -0,0 +1,18 @@
+package org.codehaus.waffle.context.pico;
+
+import org.jmock.MockObjectTestCase;
+import org.codehaus.waffle.context.ContextContainer;
+import org.picocontainer.MutablePicoContainer;
+import org.picocontainer.PicoContainer;
+import org.jruby.Ruby;
+
+public class RubyAwarePicoContextContainerFactoryTest extends MockObjectTestCase {
+
+    public void testBuildApplicationContextContainer() {
+        RubyAwarePicoContextContainerFactory factory = new RubyAwarePicoContextContainerFactory(null);
+        ContextContainer contextContainer = factory.buildApplicationContextContainer();
+        PicoContainer picoContainer = (MutablePicoContainer)contextContainer.getDelegate();
+        assertNotNull(picoContainer.getComponentInstance(Ruby.class));
+    }
+
+}

Modified: trunk/core/src/test/java/org/codehaus/waffle/controller/DefaultControllerDefinitionFactoryTest.java (67 => 68)

--- trunk/core/src/test/java/org/codehaus/waffle/controller/DefaultControllerDefinitionFactoryTest.java	2007-05-22 15:07:28 UTC (rev 67)
+++ trunk/core/src/test/java/org/codehaus/waffle/controller/DefaultControllerDefinitionFactoryTest.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -1,11 +1,7 @@
 package org.codehaus.waffle.controller;
 
-import org.codehaus.waffle.WaffleException;
 import org.codehaus.waffle.Constants;
-import org.codehaus.waffle.controller.DefaultControllerDefinitionFactory;
-import org.codehaus.waffle.controller.ControllerDefinitionFactory;
-import org.codehaus.waffle.controller.DefaultControllerNameResolver;
-import org.codehaus.waffle.controller.ControllerDefinition;
+import org.codehaus.waffle.WaffleException;
 import org.codehaus.waffle.action.MethodDefinition;
 import org.codehaus.waffle.action.MethodDefinitionFinder;
 import org.codehaus.waffle.context.RequestLevelContainer;
@@ -90,7 +86,6 @@
         }        
     }
 
-    
     public void testMissingRequestLevelContainerThrowsException() throws NoSuchMethodException {
         RequestLevelContainer.set(null);
         // Mock HttpServletRequest
@@ -101,10 +96,10 @@
         Mock mockResponse = mock(HttpServletResponse.class);
         HttpServletResponse response = (HttpServletResponse) mockResponse.proxy();
 
-        ControllerDefinitionFactory controllerDefinitionFactory = new DefaultControllerDefinitionFactory(null, null);
+        DefaultControllerDefinitionFactory controllerDefinitionFactory = new DefaultControllerDefinitionFactory(null, null);
 
         try {
-            controllerDefinitionFactory.getControllerDefinition(httpRequest, response).getController();
+            controllerDefinitionFactory.findController("foobar", httpRequest);
             fail("WaffleException should have been thrown when no request level container exists");
         } catch (WaffleException expected) {
             // expected

Added: trunk/core/src/test/java/org/codehaus/waffle/controller/RubyControllerDefinitionFactoryTest.java (0 => 68)

--- trunk/core/src/test/java/org/codehaus/waffle/controller/RubyControllerDefinitionFactoryTest.java	                        (rev 0)
+++ trunk/core/src/test/java/org/codehaus/waffle/controller/RubyControllerDefinitionFactoryTest.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -0,0 +1,38 @@
+package org.codehaus.waffle.controller;
+
+import org.codehaus.waffle.context.ContextContainer;
+import org.codehaus.waffle.context.RequestLevelContainer;
+import org.jmock.Mock;
+import org.jmock.MockObjectTestCase;
+import org.jruby.Ruby;
+import org.jruby.javasupport.JavaUtil;
+import org.jruby.runtime.builtin.IRubyObject;
+
+public class RubyControllerDefinitionFactoryTest extends MockObjectTestCase {
+
+    public void testFindControllerHandlesIRubyObject() {
+        RubyControllerDefinitionFactory factory = new RubyControllerDefinitionFactory(null, null, null);
+
+        Mock mockContextContainer = mock(ContextContainer.class);
+        IRubyObject rubyObject = JavaUtil.convertJavaToRuby(Ruby.getDefaultInstance(), "Hello From Ruby");
+        mockContextContainer.expects(once()).method("getComponentInstance").with(eq("foobar")).will(returnValue(rubyObject));
+        ContextContainer contextContainer = (ContextContainer) mockContextContainer.proxy();
+        RequestLevelContainer.set(contextContainer);
+
+        Object controller = factory.findController("foobar", null);
+        assertTrue(controller instanceof RubyController);
+    }
+
+    public void testFindControllerHandlesNonRubyObjects() {
+        RubyControllerDefinitionFactory factory = new RubyControllerDefinitionFactory(null, null, null);
+
+        Mock mockContextContainer = mock(ContextContainer.class);
+        mockContextContainer.expects(once()).method("getComponentInstance").with(eq("foobar")).will(returnValue("Pojo"));
+        ContextContainer contextContainer = (ContextContainer) mockContextContainer.proxy();
+        RequestLevelContainer.set(contextContainer);
+
+        Object controller = factory.findController("foobar", null);
+        assertFalse(controller instanceof RubyController);
+        assertEquals("Pojo", controller);
+    }
+}

Added: trunk/core/src/test/java/org/codehaus/waffle/controller/RubyControllerTest.java (0 => 68)

--- trunk/core/src/test/java/org/codehaus/waffle/controller/RubyControllerTest.java	                        (rev 0)
+++ trunk/core/src/test/java/org/codehaus/waffle/controller/RubyControllerTest.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -0,0 +1,23 @@
+package org.codehaus.waffle.controller;
+
+import org.jmock.MockObjectTestCase;
+import org.jruby.Ruby;
+
+public class RubyControllerTest extends MockObjectTestCase {
+
+    public void testExecute() {
+        String script =
+                "class Foo\n" +
+                "  def my_method\n" +
+                "    'Hello World'\n" +
+                "  end\n" +
+                "end\n";
+
+        Ruby runtime = Ruby.getDefaultInstance();
+        runtime.evalScript(script);
+
+        RubyController rubyController = new RubyController(runtime.evalScript("Foo.new"));
+        rubyController.setMethodName("my_method");
+        assertEquals("Hello World", rubyController.execute(null, null));
+    }
+}

Added: trunk/core/src/test/java/org/codehaus/waffle/registrar/pico/RubyScriptComponentAdapterTest.java (0 => 68)

--- trunk/core/src/test/java/org/codehaus/waffle/registrar/pico/RubyScriptComponentAdapterTest.java	                        (rev 0)
+++ trunk/core/src/test/java/org/codehaus/waffle/registrar/pico/RubyScriptComponentAdapterTest.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -0,0 +1,40 @@
+package org.codehaus.waffle.registrar.pico;
+
+import org.jmock.MockObjectTestCase;
+import org.picocontainer.ComponentAdapter;
+import org.picocontainer.MutablePicoContainer;
+import org.picocontainer.defaults.DefaultPicoContainer;
+import org.jruby.runtime.builtin.IRubyObject;
+import org.jruby.Ruby;
+import org.jruby.javasupport.JavaUtil;
+
+public class RubyScriptComponentAdapterTest extends MockObjectTestCase {
+
+    public void testComponentKeyAndImplementationMethods() {
+        ComponentAdapter componentAdapter = new RubyScriptComponentAdapter("Foobar", "'ruby script'");
+        assertEquals("Foobar", componentAdapter.getComponentKey());
+        assertEquals(IRubyObject.class, componentAdapter.getComponentImplementation());
+    }
+
+    public void testGetComponentInstance() {
+        Ruby runtime = Ruby.getDefaultInstance();
+        runtime.evalScript("$my_global = 'Waffle'\n");
+
+        String script =
+                "class Foobar\n" +
+                "  def execute\n" +
+                "    \"JRuby and #{$my_global}\"\n" +
+                "  end\n" +
+                "end";
+
+        ComponentAdapter componentAdapter = new RubyScriptComponentAdapter("Foobar", script);
+        MutablePicoContainer picoContainer = new DefaultPicoContainer();
+        picoContainer.registerComponentInstance(Ruby.class, runtime);
+
+        IRubyObject instance = (IRubyObject) componentAdapter.getComponentInstance(picoContainer);
+
+        // call a method on the ruby instance ... enuring it was instantiated and that the runtime was set
+        IRubyObject response = instance.callMethod(runtime.getCurrentContext(), "execute");
+        assertEquals("JRuby and Waffle", JavaUtil.convertRubyToJava(response));
+    }
+}

Added: trunk/core/src/test/java/org/codehaus/waffle/testmodel/StubMethodNameResolver.java (0 => 68)

--- trunk/core/src/test/java/org/codehaus/waffle/testmodel/StubMethodNameResolver.java	                        (rev 0)
+++ trunk/core/src/test/java/org/codehaus/waffle/testmodel/StubMethodNameResolver.java	2007-05-23 03:24:33 UTC (rev 68)
@@ -0,0 +1,11 @@
+package org.codehaus.waffle.testmodel;
+
+import org.codehaus.waffle.action.MethodNameResolver;
+
+import javax.servlet.http.HttpServletRequest;
+
+public class StubMethodNameResolver implements MethodNameResolver {
+    public String resolve(HttpServletRequest request) {
+        return null;
+    }
+}

Modified: trunk/examples/freemarker-example/freemarker-example.iml (67 => 68)

--- trunk/examples/freemarker-example/freemarker-example.iml	2007-05-22 15:07:28 UTC (rev 67)
+++ trunk/examples/freemarker-example/freemarker-example.iml	2007-05-23 03:24:33 UTC (rev 68)
@@ -161,6 +161,11 @@
       <attribute name="URI" value="/WEB-INF/lib/standard-1.1.2.jar" />
       <url>jar://$M2_REPOSITORY$/taglibs/standard/1.1.2/standard-1.1.2.jar!/</url>
     </containerElement>
+    <containerElement type="library" level="module">
+      <attribute name="method" value="0" />
+      <attribute name="URI" value="/WEB-INF/lib/jruby.jar" />
+      <url>jar:///usr/local/jruby-1.0.0RC1/lib/jruby.jar!/</url>
+    </containerElement>
     <containerElement type="library" name="Groovy from GroovyJ 0.1.8 (Demetra)" level="application">
       <attribute name="method" value="0" />
       <attribute name="URI" value="/WEB-INF/lib" />

Modified: trunk/pom.xml (67 => 68)

--- trunk/pom.xml	2007-05-22 15:07:28 UTC (rev 67)
+++ trunk/pom.xml	2007-05-23 03:24:33 UTC (rev 68)
@@ -15,7 +15,7 @@
   <modules>
     <module>skin</module>
     <module>core</module>
-    <module>extensions/webcontainer</module>
+    <!--<module>extensions/webcontainer</module>-->
   </modules>
 
   <dependencyManagement>
@@ -37,6 +37,12 @@
         <scope>provided</scope>
       </dependency>
       <dependency>
+        <groupId>org.jruby</groupId>
+        <artifactId>jruby</artifactId>
+        <version>1.0RC2</version>
+        <scope>provided</scope>
+      </dependency>
+      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>servlet-api</artifactId>
         <version>2.4</version>


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to