Skip to content

Instantly share code, notes, and snippets.

@gastaldi
Created March 14, 2011 21:43
Show Gist options
  • Save gastaldi/869946 to your computer and use it in GitHub Desktop.
Save gastaldi/869946 to your computer and use it in GitHub Desktop.
package org.jboss.seam.jcr;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
import org.jboss.seam.jcr.annotations.JcrEventListener;
import org.jboss.seam.jcr.events.JcrCDIEventListener;
public class JcrRepositoryInvocationHandler implements InvocationHandler
{
private JcrCDIEventListener eventListener;
private InjectionPoint injectionPoint;
public JcrRepositoryInvocationHandler(InjectionPoint ip, JcrCDIEventListener eventListener)
{
this.injectionPoint = ip;
this.eventListener = eventListener;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
Object obj = method.invoke(proxy, args);
if (obj instanceof Session)
{
Session session = (Session) obj;
registerListener(injectionPoint, session);
}
return obj;
}
/**
* Registers this {@link EventListener} into an {@link ObservationManager}
* using the supplied config
*
* @param ip - the injection point.
* @param beanManager - the CDI bean manager.
* @param session - the session to be configured.
* @throws RepositoryException
*/
private void registerListener(InjectionPoint ip, Session session) throws RepositoryException
{
JcrEventListener ann = ip.getAnnotated().getAnnotation(JcrEventListener.class);
if (ann != null)
{
session.getWorkspace().getObservationManager().addEventListener(eventListener, ann.eventTypes(), ann.absPath(), ann.deep(), ann.uuid(), ann.nodeTypeName(), ann.noLocal());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment