THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
No Format |
---|
import java.util.*; import org.apache.commons.betwixt.expression.*; import org.apache.commons.betwixt.strategy.*; import net.sf.cglib.proxy.*; /** * * @author Jesse Sweetland */ public class IdentityIdStoringStrategy extends IdStoringStrategy { private class Reference { private int _identity; private String _id; private Object _entity; public int getIdentity() { return _identity; } public String getId() { return _id; } public Object getEntity() { return _entity; } public Reference(Object entity, String id) { _identity = System.identityHashCode(entity); _entity = entity; _id = id; } } private HashMap<Integer, Reference> _identityRefs = new HashMap<Integer, Reference>(); private HashMap<String, Reference> _idRefs = new HashMap<String, Reference>(); private boolean _reset = false; public boolean isReset() { return _reset; } public IdentityIdStoringStrategy() { System.out.println("Boo"); } public void setReference(Context context, Object bean, String id) { Reference if(_reset) doReset(); Reference ref = new Reference(bean, id); _identityRefs.put(ref.getIdentity(), ref); _idRefs.put(ref.getId(), ref); } public String getReferenceFor(Context context, Object bean) { int identity = System.identityHashCode(bean); Reference ref = _identityRefs.get(identity); return ref == null ? null : ref.getId(); } public Object getReferenced(Context context, String id) { Reference ref = _idRefs.get(id); return ref == null ? null : ref.getEntity(); } public void reset() { // DODelay NOTthe CLEARactual VALUES!reset until Wethis need these references afterinstance IdentityIdStoringStrategy // unmarshallingis hasused completedagain. to resolveThe references. } } |
ForwardReferenceChainedBeanCreator.java
No Format |
---|
import net.sf.cglib.proxy.*; import org.apache.commons.betwixt.*; import org.apache.commons.betwixt.io.read.*; /** * * @author Jesse Sweetland */ public class ForwardReferenceChainedBeanCreator implements ChainedBeanCreator { needs to persist as long as possible // after unmarshalling so that the forward reference proxies can resolve. public Object create(ElementMapping elementMapping, ReadContext// context,Called BeanCreationChainwhen chain)unmarshalling { is complete. Set a flag to if(context.getMapIDs()) {trigger cleanup // if and when this Stringinstance idrefis = elementMapping.getAttributes().getValue("idref"); used again. This flag also serves as an if(idref != null){ // indicator to ForwardReferenceInvocationHandler that unmarshalling is context.getLog().trace("Found IDREF"); // complete, meaning that if a proxy target does not resolve then it Object bean// =never context.getBean( idref );will. _reset = true; } if(bean != nullprivate void doReset() { _identityRefs.clear(); if(context.getLog().isTraceEnabled()) {_idRefs.clear(); } } |
ForwardReferenceChainedBeanCreator.java
No Format |
---|
import net.sf.cglib.proxy.*; import org.apache.commons.betwixt.*; import org.apache.commons.betwixt.io.read.*; /** * * @author Jesse Sweetland */ public class ForwardReferenceChainedBeanCreator implements ChainedBeanCreator { public Object create(ElementMapping elementMapping, ReadContext context, BeanCreationChain chain) { context.getLog().trace("Matched bean " + bean); }if(context.getMapIDs()) { String idref return bean= elementMapping.getAttributes().getValue("idref"); if(idref != null){ } context.getLog().trace("Found IDREF"); ClassObject beanClassbean = null context.getBean( idref ); ElementDescriptor descriptor if(bean != elementMapping.getDescriptor();null) { if(descriptor != null(context.getLog().isTraceEnabled()) { // check for polymorphism context.getLog().trace("Matched bean " + bean); if (descriptor.isPolymorphic()) {} return bean; beanClass = context.getXMLIntrospector().getPolymorphicReferenceResolver() } .resolveType(elementMapping, context); Class beanClass = null; } ElementDescriptor descriptor = elementMapping.getDescriptor(); if(beanClass == null) { if(descriptor != null) { // created based on implementation class // check for polymorphism beanClass =if (descriptor.getImplementationClassisPolymorphic()); { } beanClass = context.getXMLIntrospector().getPolymorphicReferenceResolver() } if.resolveType(beanClass == null) {elementMapping, context); // create based on type} if(beanClass == elementMapping.getType();null) { } // created based on implementation class if(beanClass != null) { beanClass = descriptor.getImplementationClass(); } } if(beanClass == null) { // create based on type beanClass = elementMapping.getType(); } if(beanClass != null) { return Enhancer.create(beanClass, new Class[0], new ForwardReferenceInvocationHandler(beanClass, idref, context)); } } } return chain.create(elementMapping, context); } } |
ForwardReferenceInvocationHandler.java
No Format |
---|
import java.util.*; import java.lang.reflect.Method; import net.sf.cglib.proxy.*; import org.apache.commons.betwixt.io.read.*; import org.apache.commons.betwixt.strategy.*; /** * * @author jessesw */ public class ForwardReferenceInvocationHandler implements InvocationHandler { return Enhancer.create(beanClass, new Class[0], new ForwardReferenceInvocationHandler(beanClass, idref, context));private class QueuedCall { public } Method method; public Object[] }args; } returnpublic chain.create(elementMapping, context); QueuedCall(Method method, Object[] args) { } } |
ForwardReferenceInvocationHandler.java
No Format |
---|
import java.util.*; import java.lang.reflect.Method; import net.sf.cglib.proxy.*; import org.apache.commons.betwixt.io.read.*; import org.apache.commons.betwixt.strategy.*; /** * * @author jessesw */ public class ForwardReferenceInvocationHandler implements InvocationHandler { this.method = method; this.args = args; } } private Class _type; private String _id; private ReadContext _context; private class QueuedCall {IdStoringStrategy _references; private List<QueuedCall> _queue = public Method methodnew ArrayList<QueuedCall>(); private Object _target; public Object[] args; public Object getTarget() { public QueuedCall(Method method, Object[] argsif(_target == null) { this.method_target = method_references.getReferenced(_context, _id); this.argsif((_target = args;= null) && isFinishedUnmarshalling(_references)) { } } throw new private Class _type; private String _idRuntimeException("IDREF " + _id + " can not be resolved"); private ReadContext _context; private IdStoringStrategy _references;} private List<QueuedCall> _queue} = new ArrayList<QueuedCall>(); private Objectreturn _target; } public Object getTarget() { public ForwardReferenceInvocationHandler(Class type, if(_target == nullString id, ReadContext context) { _targettype = _references.getReferenced(_context, _id)type; } _id = id; return _target; } context = context; public ForwardReferenceInvocationHandler(Class type, String id,_references ReadContext= context.getIdMappingStrategy() {; } _typepublic = type; _id = id; Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object _contexttarget = contextgetTarget(); _referencesif(target == context.getIdMappingStrategy();null) { } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable if("getClass".equals(method.getName())) { Object target = getTarget(); return _type; if(target == null) { } else if("getClasshashCode".equals(method.getName())) { return _type; System.identityHashCode(proxy); } else { } else if("hashCode".equals(method.getName())) {_queue.add(new QueuedCall(method, args)); return System.identityHashCode(proxy)null; } else { } else { _queue.add(new QueuedCall(method, args)); for(QueuedCall call: _queue) { return nullcall.method.invoke(target, call.args); } } else { _queue.clear(); return for(QueuedCall call: _queue) { method.invoke(target, args); } } private call.method.invoke(target, call.args);boolean isFinishedUnmarshalling(IdStoringStrategy _references) { if(_references instanceof IdentityIdStoringStrategy) }{ return method.invoke(target, args((IdentityIdStoringStrategy)_references).isReset(); } return false; } } |
To use, set the custom IdStoringStrategy and insert the chained bean creator:
...