
hudson.maven.agent.ComponentInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of maven-interceptor Show documentation
Show all versions of maven-interceptor Show documentation
Plexus module that intercepts invocations of key Maven components
so that Hudson can monitor what's going on in Maven.
The newest version!
/*******************************************************************************
*
* Copyright (c) 2004-2009 Oracle Corporation.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*
* Kohsuke Kawaguchi
*
*
*******************************************************************************/
package hudson.maven.agent;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
/**
* Base class for intercepting components through its interfaces.
*
* @author Kohsuke Kawaguchi
*/
abstract class ComponentInterceptor implements InvocationHandler {
protected T delegate;
/**
* By default, all the methods are simply delegated to the intercepted component.
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
return method.invoke(delegate,args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
}
public T wrap(T o) {
if(delegate !=null)
// can't intercept two objects simultaneously
throw new IllegalStateException();
delegate = o;
return (T)Proxy.newProxyInstance(o.getClass().getClassLoader(),
getImplementedInterfaces(o), this);
}
private static Class[] getImplementedInterfaces(Object o) {
Stack s = new Stack();
s.push(o.getClass());
Set interfaces = new HashSet();
while(!s.isEmpty()) {
Class c = s.pop();
for (Class intf : c.getInterfaces()) {
interfaces.add(intf);
s.push(intf);
}
Class sc = c.getSuperclass();
if(sc!=null)
s.push(sc);
}
return interfaces.toArray(new Class[interfaces.size()]);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy