Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create @UnitOfWork-aware proxies of existing objects

Tags:

I have the following classes:

public FooDAO extends AbstractDAO<Foo> { // Dropwizard DAO
  @Inject FooDAO(SessionFactory sf) { super(sf); }
  public void foo() { /* use SessionFactory */ }
}

public class FooService {
  private final FooDAO fooDAO; // Constructor-injected dependency
  @Inject FooService (FooDAO fooDAO) { this.fooDAO = fooDAO; }

  @UnitOfWork
  public void foo() {
    this.fooDAO.foo();
    System.out.println("I went through FooService.foo()");
  }
}

Now, FooService is not a resource, so Dropwizard doesn't know about it and doesn't automagically proxy it. However the smart guys at Dropwizard made it so I can get a proxy through UnitOfWorkAwareProxyFactory.

I tried doing feeding these proxies to Guice with an interceptor, but I faced an issue because UnitOfWorkAwareProxyFactory only ever creates new instances and never lets me pass existing objects. The thing with new instances is that I don't know the parameters to give it since they're injected by Guice.

How do I create @UnitOfWork-aware proxies of existing objects?

Here's the interceptor I've made so far:

public class UnitOfWorkModule extends AbstractModule {
  @Override protected void configure() {
    UnitOfWorkInterceptor interceptor = new UnitOfWorkInterceptor();
    bindInterceptor(Matchers.any(), Matchers.annotatedWith(UnitOfWork.class), interceptor);
    requestInjection(interceptor);
  }

  private static class UnitOfWorkInterceptor implements MethodInterceptor { 
    @Inject UnitOfWorkAwareProxyFactory proxyFactory;
    Map<Object, Object> proxies = new IdentityHashMap<>();

    @Override public Object invoke(MethodInvocation mi) throws Throwable {
      Object target = proxies.computeIfAbsent(mi.getThis(), x -> createProxy(mi));
      Method method = mi.getMethod();
      Object[] arguments = mi.getArguments();
      return method.invoke(target, arguments);
    }

    Object createProxy(MethodInvocation mi) {
      // here, what to do? proxyFactory will only provide objects where I pass constructor arguments, but... I don't have those!
    }
  }
}

Of course, if Dropwizard (or Guice) offers me a simpler way to do so, which is it?

like image 306
Olivier Grégoire Avatar asked Jul 08 '16 08:07

Olivier Grégoire


1 Answers

As from Dropwizard 1.1: (not yet released, as of August 10, 2016)

public class UnitOfWorkModule extends AbstractModule {

  @Override
  protected void configure() {
    UnitOfWorkInterceptor interceptor = new UnitOfWorkInterceptor();
    bindInterceptor(Matchers.any(), Matchers.annotatedWith(UnitOfWork.class), interceptor);
    requestInjection(interceptor);
  }

  @Provides
  @Singleton
  UnitOfWorkAwareProxyFactory provideUnitOfWorkAwareProxyFactory(HibernateBundle<AlexandriaConfiguration> hibernateBundle) {
    return new UnitOfWorkAwareProxyFactory(hibernateBundle);
  }

  private static class UnitOfWorkInterceptor implements MethodInterceptor {

    @Inject
    UnitOfWorkAwareProxyFactory proxyFactory;

    @Override
    public Object invoke(MethodInvocation mi) throws Throwable {
      UnitOfWorkAspect aspect = proxyFactory.newAspect();
      try {
        aspect.beforeStart(mi.getMethod().getAnnotation(UnitOfWork.class));
        Object result = mi.proceed();
        aspect.afterEnd();
        return result;
      } catch (InvocationTargetException e) {
        aspect.onError();
        throw e.getCause();
      } catch (Exception e) {
        aspect.onError();
        throw e;
      } finally {
        aspect.onFinish();
      }
    }
  }
}
like image 178
Olivier Grégoire Avatar answered Sep 28 '22 04:09

Olivier Grégoire