alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Struts example source code file (ProxyObjectFactory.java)

This example Struts source code file (ProxyObjectFactory.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Struts tags/keywords

exception, invocationhandler, invocationhandler, map, method, object, object, objectfactory, override, proxyinvocationproxy, proxyinvocationproxy, proxyobjectfactory, proxyobjectfactory, reflection, throwable, util

The Struts ProxyObjectFactory.java source code

package com.opensymphony.xwork2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;

/**
 * ObjectFactory that returns a FooProxy in the buildBean if the clazz is FooAction 
 */
public class ProxyObjectFactory extends ObjectFactory {

    /**
     * It returns an instance of the bean except if the class is FooAction. 
     * In this case, it returns a FooProxy of it.
     */
    @Override
    public Object buildBean(Class clazz, Map<String, Object> extraContext)
        throws Exception {
        Object bean = super.buildBean(clazz, extraContext);
        if(clazz.equals(ProxyInvocationAction.class)) {
            return Proxy.newProxyInstance(bean.getClass()
                .getClassLoader(), bean.getClass().getInterfaces(),
                new ProxyInvocationProxy(bean));

        }
        return bean;
    }
    
    /**
     * Simple proxy that just invokes the method on the target on the invoke method
     */
    public class ProxyInvocationProxy implements InvocationHandler {

        private Object target;

        public ProxyInvocationProxy(Object target) {
            this.target = target;
        }

        public Object invoke(Object proxy, Method m, Object[] args)
            throws Throwable {
            return m.invoke(target, args);
        }
    }
}

Other Struts examples (source code examples)

Here is a short list of links related to this Struts ProxyObjectFactory.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.