Skip to content

Commit

Permalink
Merge pull request #367 from caelum/ot-noinvokedyn
Browse files Browse the repository at this point in the history
Mirror instead invoke dynamic, since uses less memory and cpu
  • Loading branch information
garcia-jj committed Mar 20, 2014
2 parents de9ee26 + 280b083 commit fb85d8c
Show file tree
Hide file tree
Showing 18 changed files with 19 additions and 286 deletions.
6 changes: 0 additions & 6 deletions vraptor-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@
<artifactId>xstream</artifactId>
<version>1.4.7</version>
</dependency>

<dependency>
<groupId>com.headius</groupId>
<artifactId>invokebinder</artifactId>
<version>1.2</version>
</dependency>
<!-- /mandatory -->

<!-- [cdi] -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,17 @@
import java.lang.reflect.Method;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import net.vidageek.mirror.dsl.Mirror;
import net.vidageek.mirror.list.dsl.Matcher;
import net.vidageek.mirror.list.dsl.MirrorList;
import br.com.caelum.vraptor.InterceptionException;
import br.com.caelum.vraptor.reflection.MethodExecutor;
import br.com.caelum.vraptor.reflection.MethodExecutorException;

import com.google.common.base.Throwables;

@ApplicationScoped
public class StepInvoker {

private final MethodExecutor methodExecutor;

/**
* @deprecated CDI eyes only
*/
protected StepInvoker() {
this(null);
}

@Inject
public StepInvoker(MethodExecutor methodExecutor) {
this.methodExecutor = methodExecutor;
}

private class InvokeMatcher implements Matcher<Method> {

private Class<? extends Annotation> step;
Expand Down Expand Up @@ -62,8 +45,8 @@ public Object tryToInvoke(Object interceptor, Method stepMethod, Object... param

private Object invokeMethod(Object interceptor, Method stepMethod, Object... params) {
try {
return methodExecutor.invoke(stepMethod, interceptor, params);
} catch (MethodExecutorException e) {
return new Mirror().on(interceptor).invoke().method(stepMethod).withArgs(params);
} catch (Exception e) {
// we dont wanna wrap it if it is a simple controller business logic
// exception
Throwables.propagateIfInstanceOf(e.getCause(), ApplicationLogicException.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import javax.enterprise.event.Observes;
import javax.inject.Inject;

import net.vidageek.mirror.dsl.Mirror;

import org.slf4j.Logger;

import br.com.caelum.vraptor.InterceptionException;
Expand All @@ -35,9 +37,6 @@
import br.com.caelum.vraptor.events.EndOfInterceptorStack;
import br.com.caelum.vraptor.events.MethodExecuted;
import br.com.caelum.vraptor.events.ReadyToExecuteMethod;
import br.com.caelum.vraptor.interceptor.ApplicationLogicException;
import br.com.caelum.vraptor.reflection.MethodExecutor;
import br.com.caelum.vraptor.reflection.MethodExecutorException;
import br.com.caelum.vraptor.validator.ValidationException;
import br.com.caelum.vraptor.validator.Validator;

Expand All @@ -55,7 +54,6 @@ public class ExecuteMethod {

private final MethodInfo methodInfo;
private final Validator validator;
private final MethodExecutor methodExecutor;

private Event<MethodExecuted> methodExecutedEvent;
private Event<ReadyToExecuteMethod> readyToExecuteMethod;
Expand All @@ -64,21 +62,19 @@ public class ExecuteMethod {
* @deprecated CDI eyes only
*/
protected ExecuteMethod() {
this(null, null, null, null, null);
this(null, null, null, null);
}

@Inject
public ExecuteMethod(MethodInfo methodInfo, Validator validator, MethodExecutor methodExecutor,
public ExecuteMethod(MethodInfo methodInfo, Validator validator,
Event<MethodExecuted> methodExecutedEvent, Event<ReadyToExecuteMethod> readyToExecuteMethod) {
this.methodInfo = methodInfo;
this.validator = validator;
this.methodExecutor = methodExecutor;
this.methodExecutedEvent = methodExecutedEvent;
this.readyToExecuteMethod = readyToExecuteMethod;
}

public void execute(@Observes EndOfInterceptorStack event) {

try {
ControllerMethod method = event.getControllerMethod();
readyToExecuteMethod.fire(new ReadyToExecuteMethod(method));
Expand All @@ -87,7 +83,7 @@ public void execute(@Observes EndOfInterceptorStack event) {

log.debug("Invoking {}", reflectionMethod);
Object instance = event.getControllerInstance();
Object result = methodExecutor.invoke(reflectionMethod, instance , parameters);
Object result = new Mirror().on(instance).invoke().method(reflectionMethod).withArgs(parameters);

if (validator.hasErrors()) { // method should have thrown
// ValidationException
Expand All @@ -109,9 +105,6 @@ public void execute(@Observes EndOfInterceptorStack event) {
methodExecutedEvent.fire(new MethodExecuted(method, methodInfo));
} catch (IllegalArgumentException e) {
throw new InterceptionException(e);
} catch (MethodExecutorException e) {
throwIfNotValidationException(e,
new ApplicationLogicException("your controller raised an exception", e.getCause()));
} catch (Exception e) {
throwIfNotValidationException(e, new InterceptionException(e));
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private <T extends View> T instanceView(Class<T> view){
public String getAcceptFormat() {
return "xml";
}
}, this, new MockInstanceImpl<Serialization>(this.serialization)));
}, this, new MockInstanceImpl<>(this.serialization)));
}

return proxifier.proxify(view, returnOnFinalMethods(view));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import br.com.caelum.vraptor.cache.CacheStore;
import br.com.caelum.vraptor.cache.DefaultCacheStore;
import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.interceptor.AspectStyleInterceptorHandler;
import br.com.caelum.vraptor.interceptor.CustomAcceptsExecutor;
import br.com.caelum.vraptor.interceptor.Interceptor;
Expand All @@ -28,7 +27,7 @@ public class DefaultInterceptorHandlerFactoryTest {

private DefaultInterceptorHandlerFactory factory;

private StepInvoker stepInvoker = Factories.createStepInvoker();
private StepInvoker stepInvoker = new StepInvoker();

private @Mock SimpleInterceptorStack simpleStack;
private @Mock InterceptorAcceptsExecutor acceptsExecutor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
package br.com.caelum.vraptor.factory;

import java.lang.invoke.MethodHandle;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;

import br.com.caelum.vraptor.cache.CacheStore;
import br.com.caelum.vraptor.cache.DefaultCacheStore;
import br.com.caelum.vraptor.cache.LRUCacheStore;
import br.com.caelum.vraptor.http.Parameter;
import br.com.caelum.vraptor.http.ParameterNameProvider;
import br.com.caelum.vraptor.http.ParanamerNameProvider;
import br.com.caelum.vraptor.interceptor.StepInvoker;
import br.com.caelum.vraptor.reflection.DefaultMethodExecutor;
import br.com.caelum.vraptor.reflection.MethodExecutor;
import br.com.caelum.vraptor.reflection.MethodHandleFactory;

public class Factories {

public static StepInvoker createStepInvoker(){
return new StepInvoker(Factories.createMethodExecutor());
}

public static ParameterNameProvider createParameterNameProvider() {
CacheStore<AccessibleObject, Parameter[]> cache = new DefaultCacheStore<>();
return new ParanamerNameProvider(cache);
}

public static MethodExecutor createMethodExecutor(){
MethodHandleFactory methodHandleFactory = new MethodHandleFactory();
LRUCacheStore<Method, MethodHandle> cache = new LRUCacheStore<>(500);
return new DefaultMethodExecutor(cache,methodHandleFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class AcceptsNeedReturnBooleanValidationRuleTest {

@Before
public void setUp() {
stepInvoker = new StepInvoker(null);
stepInvoker = new StepInvoker();
validationRule = new AcceptsNeedReturnBooleanValidationRule(stepInvoker);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import br.com.caelum.vraptor.controller.ControllerInstance;
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.core.InterceptorStack;
import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.interceptor.example.AcceptsInterceptor;
import br.com.caelum.vraptor.interceptor.example.AcceptsWithoutArgsInterceptor;
import br.com.caelum.vraptor.interceptor.example.AlwaysAcceptsAspectInterceptor;
Expand Down Expand Up @@ -50,7 +49,7 @@ public class AspectStyleInterceptorHandlerTest {
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
stepInvoker = Factories.createStepInvoker();
stepInvoker = new StepInvoker();
}

@Test
Expand Down Expand Up @@ -249,12 +248,12 @@ private AspectStyleInterceptorHandler newAspectStyleInterceptorHandler(Class<?>
acceptsExecutor = new InterceptorAcceptsExecutor(parametersResolver, stepInvoker);

customAcceptsExecutor = new CustomAcceptsExecutor(
new MockInstanceImpl<ControllerMethod>(controllerMethod),
new MockInstanceImpl<ControllerInstance>(controllerInstance),
new MockInstanceImpl<>(controllerMethod),
new MockInstanceImpl<>(controllerInstance),
stepInvoker, new CustomAcceptsVerifier(container));

interceptorExecutor = new InterceptorExecutor(stepInvoker, parametersResolver,
new MockInstanceImpl<SimpleInterceptorStack>(simpleInterceptorStack));
new MockInstanceImpl<>(simpleInterceptorStack));

return new AspectStyleInterceptorHandler(interceptorClass, stepInvoker, container, customAcceptsExecutor,
acceptsExecutor, interceptorExecutor);
Expand Down

0 comments on commit fb85d8c

Please sign in to comment.