All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dynamicextensionsalfresco.proxy.ServiceInvocationHandler Maven / Gradle / Ivy

Go to download

Adds an OSGi container to alfresco repository supporting dynamic code reloading, classpath isolation and a bunch of other useful features

There is a newer version: 3.1.0
Show newest version
package com.github.dynamicextensionsalfresco.proxy;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * AOP or JDK Proxy compliant wrapper for the service {@link Tracker}
 *
 * @author Laurent Van der Linden
 */
public class ServiceInvocationHandler implements MethodInterceptor, InvocationHandler {
	private final Tracker tracker;

	public ServiceInvocationHandler(Tracker tracker) {
		this.tracker = tracker;
	}

	@Override
	public Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable {
		if (method.getDeclaringClass().equals(FilterModel.class)) {
			return method.invoke(tracker.getFilterModel(), args);
		} else {
			return tracker.invokeUsing(new ServiceInvoker() {
				@Override
				public Object invokeService(Object service) throws Throwable {
					try {
						return method.invoke(service, args);
					} catch (IllegalAccessException e) {
						throw new RuntimeException(e);
					} catch (InvocationTargetException e) {
						throw e.getTargetException();
					}
				}
			});
		}
	}

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		return invoke(null, invocation.getMethod(), invocation.getArguments());
	}
}