Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] CGLIB Proxy Using Wrapper Policy

Hi All,
Just to recap – I was trying to create a wrapper as CGLIB proxy around the
JPA entity. My strategy to wrap is
1.	The callback/interceptor class contain the original JPA entity
2.	For the all the methods call on the JPA entity is intercepted and
delegated to the oringinal JPA entity
3.	For the methods call not in the JPA entity are handled by the
interceptor/callback or could be passed to some other class.
4.	I want to create CGLIB proxy around the entire graph of the object.
When I tried to query with wrapper policy I got the following exception
Exception Description: Missing descriptor for [class
readwrite.model.EmployeeAddress$$EnhancerByCGLIB$$b60b0c9b].  Verify that
the descriptor has been properly registered with the Session.
When I debugged further, I found that CGLIB proxying is failing in
AbstractSession.getDescriptor(proxy)

   public ClassDescriptor getDescriptor(Object domainObject) {
        if (domainObject == null) {
            return null;
        }
        //Bug#3947714  Check and trigger the proxy here
        if (this.project.hasProxyIndirection()) {
            return
getDescriptor(ProxyIndirectionPolicy.getValueFromProxy(domainObject).getClass());
        }
        return getDescriptor(domainObject.getClass());
    }

Now the domainObject is my case was CGLIB proxy but when it tries to get the
class descriptor it fails.
For supporting the CGLIB proxying there should be code like
if (Enhancer.isEnhanced(attributeValue.getClass())) {
	Factory factory = (Factory) attributeValue;
	WrappedObject wo = (WrappedObject) factory.getCallback(0);
--- --- --
}

I also observed that I only get exception for 1 to 1 relationship for which
eclipselink fires ReadObjectQuery. 
How I soloved the problem
1.	In the session customizer, I added mine indirection policy for 1 to 1
relationship I did the following
public class MyCustomizer implements SessionCustomizer {

	@SuppressWarnings("serial")
	public void customize(Session session) throws Exception {
		Map, ClassDescriptor> mapClassDesc = session.getDescriptors();
		Set> keys = mapClassDesc.keySet();
		for (Class<?> key : keys) {
			ClassDescriptor cd = mapClassDesc.get(key);
			Vector vDbMappings = cd.getMappings();
			for (DatabaseMapping databaseMapping : vDbMappings) {
				if (databaseMapping instanceof OneToOneMapping) {
					OneToOneMapping oneToOneMapping = (OneToOneMapping) databaseMapping;
// 	ADDED MY INDIRECTION POILICY TO HANDLE 
					oneToOneMapping.setIndirectionPolicy(new
CglibAwareNoIndirectionPolicy());
				} 
			}
			
			cd.setWrapperPolicy(new WrapperPolicyImpl());
		}
	}
}

My indireciton policy – CglibAwareNoIndirectionPolicy looks like
public class CglibAwareNoIndirectionPolicy extends NoIndirectionPolicy {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8135612685536491399L;

	@Override
	public Object cloneAttribute(Object attributeValue, Object original,
			Object clone, UnitOfWorkImpl unitOfWork,
			boolean buildDirectlyFromRow) {
		
		if (Enhancer.isEnhanced(attributeValue.getClass())) {
			Factory factory = (Factory) attributeValue;
			WrappedObject wo = (WrappedObject) factory.getCallback(0);
			Object retObj = super.cloneAttribute(wo.getOrginalObject(), original,
clone,
					unitOfWork, buildDirectlyFromRow);
			Object proxyObj =
unitOfWork.getDescriptor(retObj.getClass()).getWrapperPolicy().wrapObject(retObj,
unitOfWork);
			return proxyObj;
		}		
		
		return super.cloneAttribute(attributeValue, original, clone,
				unitOfWork, buildDirectlyFromRow);
	}

}


I am also uploading my project. It will awsome if ecliplink team can verify
my research.
Waiting eagerly for response 

Regards,
GM
Oracle
http://old.nabble.com/file/p28785834/wrapperpolicy.zip wrapperpolicy.zip 
-- 
View this message in context: http://old.nabble.com/CGLIB-Proxy-Using-Wrapper-Policy-tp28780712p28785834.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.

Back to the top