1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2008 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.core.aop;
18
19 import java.util.Map;
20
21 import org.apache.commons.collections.map.AbstractReferenceMap;
22 import org.apache.commons.collections.map.ReferenceIdentityMap;
23 import org.springframework.aop.framework.ProxyFactory;
24 import org.springframework.beans.factory.FactoryBean;
25
26 /**
27 * A generic {@link FactoryBean} that wraps another factory and proxies the beans generated by that factory.
28 *
29 * @svnLink $Revision: 3875 $;$Date: 2009-08-04 14:35:53 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/core/aop/GenericProxiedFactoryBean.java $
30 *
31 * @author Stefan Wismer (SWI)
32 */
33 public class GenericProxiedFactoryBean implements FactoryBean {
34
35 /**
36 * A cache to store original and wrapped beans.
37 */
38 private static Map<Object, Object> s_proxyCache;
39
40 /**
41 * The wrapped {@link FactoryBean}.
42 */
43 private final FactoryBean m_factoryBean;
44
45 /**
46 * The proxy factory to wrap objects generated by the wrapped factory.
47 */
48 private final ProxyFactory m_proxyFactory;
49
50 /**
51 * @param factoryBean the {@link FactoryBean} to wrap
52 * @param proxyFactory The proxy factory to wrap objects generated by the wrapped factory
53 */
54 @SuppressWarnings("unchecked")
55 public GenericProxiedFactoryBean(FactoryBean factoryBean, ProxyFactory proxyFactory) {
56 m_factoryBean = factoryBean;
57 m_proxyFactory = proxyFactory;
58
59 s_proxyCache = new ReferenceIdentityMap(AbstractReferenceMap.WEAK, AbstractReferenceMap.HARD);
60 }
61
62 /**
63 * @param original the object to wrap
64 * @return the wrapped object
65 */
66 protected Object wrap(Object original) {
67 if (s_proxyCache.containsKey(original)) {
68 return s_proxyCache.get(original);
69 } else {
70 // create proxy
71 m_proxyFactory.setTarget(original);
72 Object wrapped = m_proxyFactory.getProxy();
73
74 s_proxyCache.put(original, wrapped);
75 return wrapped;
76 }
77 }
78
79 /** {@inheritDoc} */
80 public Object getObject() throws Exception {
81 return wrap(m_factoryBean.getObject());
82 }
83
84 /** {@inheritDoc} */
85 public Class<?> getObjectType() {
86 return m_factoryBean.getObjectType();
87 }
88
89 /** {@inheritDoc} */
90 public boolean isSingleton() {
91 return m_factoryBean.isSingleton();
92 }
93 }