[jboss-dev-forums] [Design the new POJO MicroContainer] - Re: Aspect integration to bootstrap classes
scott.stark@jboss.org
do-not-reply at jboss.com
Mon Jan 8 12:55:48 EST 2007
Or do what Adrian did in the org.jboss.test.managed.mock.MockTest of the managed project:
| public void testMock() throws Exception
| {
| MockDataSourceManagedObject mock = new MockDataSourceManagedObject();
|
| ManagedObject mo = WrapperAdvice.wrapManagedObject(mock);
| ...
|
| package org.jboss.managed.plugins.advice;
|
| import java.util.Set;
|
| import org.jboss.aop.joinpoint.Invocation;
| import org.jboss.aop.proxy.container.AOPProxyFactoryParameters;
| import org.jboss.aop.proxy.container.GeneratedAOPProxyFactory;
| import org.jboss.logging.Logger;
| import org.jboss.managed.api.Fields;
| import org.jboss.managed.api.ManagedObject;
| import org.jboss.managed.api.ManagedProperty;
|
| /**
| * WrapperAdvice, intercepts methods that produce objects
| * that require proxies.
| *
| * @author <a href="adrian at jboss.com">Adrian Brock</a>
| * @version $Revision: 59258 $
| */
| public class WrapperAdvice
| {
| private static Logger log = Logger.getLogger(WrapperAdvice.class);
|
| /**
| * Wrap a managed object
| *
| * @param managedObject the managed object
| * @return the managed object wrapper
| */
| public static ManagedObject wrapManagedObject(ManagedObject managedObject)
| {
| return createProxy(managedObject, ManagedObject.class);
| }
|
| /**
| * Wrap a managed property
| *
| * @param managedProperty the managed property
| * @return the managed property wrapper
| */
| public static ManagedProperty wrapManagedProperty(ManagedProperty managedProperty)
| {
| return createProxy(managedProperty, ManagedProperty.class);
| }
|
| /**
| * Wrap fields
| *
| * @param fields the fields
| * @return the fields wrapper
| */
| public static Fields wrapFields(Fields fields)
| {
| return createProxy(fields, Fields.class);
| }
|
| /**
| * Wrap a returned managed object
| *
| * @param invocation the invocation
| * @return the wrapped managed object
| * @throws Throwable for any error
| */
| public ManagedObject wrapManagedObject(Invocation invocation) throws Throwable
| {
| ManagedObject result = (ManagedObject) invocation.invokeNext();
| return wrapManagedObject(result);
| }
|
| /**
| * Wrap a returned managed property
| *
| * @param invocation the invocation
| * @return the wrapped managed property
| * @throws Throwable for any error
| */
| public ManagedProperty wrapManagedProperty(Invocation invocation) throws Throwable
| {
| ManagedProperty result = (ManagedProperty) invocation.invokeNext();
| return wrapManagedProperty(result);
| }
|
| /**
| * Wrap a returned managed property set
| *
| * @param invocation the invocation
| * @return the wrapped managed property set
| * @throws Throwable for any error
| */
| @SuppressWarnings("unchecked")
| public Set<ManagedProperty> wrapManagedPropertySet(Invocation invocation) throws Throwable
| {
| Set<ManagedProperty> result = (Set<ManagedProperty>) invocation.invokeNext();
| return new WrapperSet<ManagedProperty>(result, ManagedProperty.class);
| }
|
| /**
| * Wrap fields
| *
| * @param invocation the invocation
| * @return the wrapped managed property
| * @throws Throwable for any error
| */
| public Fields wrapFields(Invocation invocation) throws Throwable
| {
| Fields result = (Fields) invocation.invokeNext();
| return wrapFields(result);
| }
|
| /**
| * Create a proxy
| *
| * @param <T> the expected type
| * @param target the target
| * @param interfaceClass the interface class
| * @return the proxy
| */
| static <T> T createProxy(T target, Class<T> interfaceClass)
| {
| if (target == null)
| return null;
|
| GeneratedAOPProxyFactory proxyFactory = new GeneratedAOPProxyFactory();
| AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
| params.setInterfaces(new Class[] { interfaceClass });
| params.setObjectAsSuperClass(true);
| params.setTarget(target);
| Object proxy = proxyFactory.createAdvisedProxy(params);
| if( log.isTraceEnabled() )
| log.trace("Created proxy: "+proxy.getClass()+"@"+System.identityHashCode(proxy)+" target: "+target.getClass());
| return interfaceClass.cast(proxy);
| }
| }
|
|
which uses the WrapperAdvice to bootstrap a aop proxy so that subsequent access to the ManagedProperty are proxied. This is what I was planning on using in the ManagedObjectBuilder that created the top-level MnagedObject of a deployment.
This works right?
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3999129#3999129
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3999129
More information about the jboss-dev-forums
mailing list