001: /*
002: * Created on 17-Dec-2005
003: */
004: package uk.org.ponder.rsac;
005:
006: import java.util.ArrayList;
007: import java.util.HashMap;
008: import java.util.Map;
009:
010: import org.springframework.aop.framework.ProxyFactoryBean;
011: import org.springframework.beans.factory.BeanFactory;
012: import org.springframework.beans.factory.BeanInitializationException;
013:
014: import uk.org.ponder.beanutil.WriteableBeanLocator;
015: import uk.org.ponder.beanutil.support.ConcreteWBL;
016: import uk.org.ponder.springutil.TLABPostProcessor;
017: import uk.org.ponder.stringutil.StringList;
018: import uk.org.ponder.util.UniversalRuntimeException;
019:
020: class PerRequestInfo {
021: // HashMap beans = new HashMap();
022: int cbeans = 0;
023: ConcreteWBL beans = new ConcreteWBL(); // the raw bean container
024: WriteableBeanLocator requestwbl; // "active" container with lazy-init
025: ArrayList postprocessors = new ArrayList();
026: StringList todestroy = new StringList();
027: // a cached BeanFactory corresponding to the lazy container, for any
028: // BeanFactoryAware beans
029: BeanFactory blfactory;
030: TLABPostProcessor tlabpp;
031: // the container of RSACLazyTargetSources, permanent in this ThreadLocal.
032: Map lazysources;
033: Map seedbeans = new HashMap();
034:
035: public void clear() {
036: cbeans = 0;
037: // we now know that all of this stuff is actually SLOWER than throwing the
038: // whole entry away. But we NEED to cache the lazytargets, so what the
039: // heck...
040: beans.clear();
041: todestroy.clear();
042: postprocessors.clear();
043: }
044:
045: public PerRequestInfo(final RSACBeanLocatorImpl rsacbl,
046: StringList lazysources, TLABPostProcessor tlabpp) {
047:
048: requestwbl = new WriteableBeanLocator() {
049: public Object locateBean(String beanname) {
050: return rsacbl.getBean(PerRequestInfo.this , beanname,
051: false);
052: }
053:
054: public boolean remove(String beanname) {
055: return beans.remove(beanname);
056: }
057:
058: public void set(String beanname, Object toset) {
059: seedbeans.put(beanname, toset);
060: beans.set(beanname, toset);
061: }
062: };
063:
064: HashMap this lazies = new HashMap();
065: blfactory = new RSACBeanFactory(rsacbl, requestwbl);
066: for (int i = 0; i < lazysources.size(); ++i) {
067: String lazysource = lazysources.stringAt(i);
068: try {
069: ProxyFactoryBean pfb = new ProxyFactoryBean();
070:
071: Class beanclass = rsacbl.getBeanClass(lazysource);
072: if (beanclass == null) {
073: throw UniversalRuntimeException
074: .accumulate(
075: new BeanInitializationException(""),
076: "Unable to determine the class of bean "
077: + lazysource
078: + " which has bean marked as (very) lazy");
079: }
080: // NB - report as bug! This proxies NOTHING if the supplied class
081: // is not a concrete class.
082: RSACLazyTargetSource rlts = new RSACLazyTargetSource(
083: rsacbl, this , beanclass, lazysource);
084: if (beanclass.isInterface()) {
085: pfb.setInterfaces(new Class[] { beanclass });
086: } else {
087: pfb.setProxyTargetClass(true);
088: }
089: pfb.setTargetSource(rlts);
090: pfb.setBeanFactory(blfactory);
091:
092: this lazies.put(lazysource, pfb);
093: } catch (Exception e) {
094: throw UniversalRuntimeException.accumulate(e,
095: "Error constructing Bridge proxy for bean name "
096: + lazysource);
097: }
098: }
099: this.lazysources = thislazies;
100: this.tlabpp = tlabpp.copy();
101: this.tlabpp.setRSACBeanLocator(rsacbl);
102: }
103: }
|