001: /*
002: * Created on 27-Feb-2006
003: */
004: package uk.org.ponder.rsac;
005:
006: import org.springframework.aop.TargetSource;
007: import org.springframework.aop.framework.ProxyFactoryBean;
008: import org.springframework.beans.factory.BeanFactory;
009: import org.springframework.beans.factory.BeanFactoryAware;
010: import org.springframework.beans.factory.FactoryBean;
011:
012: /**
013: * An "AOP Alliance" proxy that allows request-scope dependencies to be injected
014: * into application scope. A parent bean definition is in
015: * <code>rsf-config.xml</code> - just inherit from it, and set the
016: * <code>targetBeanName</code> field, preferably using an <code>idref</code>
017: * for link-safety. For example:
018: *
019: * <pre>
020: * <bean id="RSACSafeBeanLocatorProxy" parent="RSACBridgeProxy">
021: * <property name="targetBeanName">
022: * <idref bean="rsacSafeBeanLocator" />
023: * </property>
024: * </bean>
025: * </pre>
026: *
027: * @author Antranig Basman (antranig@caret.cam.ac.uk)
028: *
029: */
030:
031: public class RSACBridgeProxy implements TargetSource, FactoryBean,
032: BeanFactoryAware {
033:
034: private RSACBeanLocatorImpl rsacbl;
035: private String targetbean;
036: private boolean pea = false;
037: private Class targetclass;
038:
039: public void setRSACBeanLocator(RSACBeanLocatorImpl rsacbl) {
040: this .rsacbl = rsacbl;
041: }
042:
043: public void setTargetBeanName(String targetbean) {
044: this .targetbean = targetbean;
045: }
046:
047: public Class getTargetClass() {
048: return targetclass == null ? rsacbl.getBeanClass(targetbean)
049: : targetclass;
050: }
051:
052: public void setTargetClass(Class targetclass) {
053: this .targetclass = targetclass;
054: }
055:
056: public boolean isStatic() {
057: return false;
058: }
059:
060: public void setPea(boolean pea) {
061: this .pea = pea;
062: }
063:
064: public Object getTarget() throws Exception {
065: return rsacbl.getBeanLocator().locateBean(targetbean);
066: }
067:
068: public void releaseTarget(Object target) throws Exception {
069: // apparently no action required here.
070: }
071:
072: Object proxy = null;
073: private BeanFactory beanFactory;
074: Object peaproxy = null;
075:
076: private void createProxy() {
077: Class beanclass = getTargetClass();
078: if (pea) {
079: RSACPeaProxyFactory ppf = new RSACPeaProxyFactory(
080: beanclass, this );
081: proxy = ppf.getProxy();
082: } else {
083: ProxyFactoryBean pfb = new ProxyFactoryBean();
084:
085: if (beanclass.isInterface()) {
086: pfb.setInterfaces(new Class[] { beanclass });
087: } else {
088: pfb.setProxyTargetClass(true);
089: }
090: pfb.setTargetSource(this );
091: pfb.setBeanFactory(beanFactory);
092:
093: proxy = pfb.getObject();
094: }
095: }
096:
097: public Object getObject() throws Exception {
098: if (proxy == null) {
099: createProxy();
100: }
101: return proxy;
102: }
103:
104: public Class getObjectType() {
105: return getTargetClass();
106: }
107:
108: public boolean isSingleton() {
109: return true;
110: }
111:
112: public void setBeanFactory(BeanFactory beanFactory) {
113: this.beanFactory = beanFactory;
114: }
115: }
|