Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2013 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.springframework.data.mongodb.core.convert;
 
 
 import  org.aopalliance.intercept.MethodInterceptor;
 import  org.aopalliance.intercept.MethodInvocation;
 import  org.springframework.aop.framework.ProxyFactory;
 import  org.springframework.cglib.proxy.Callback;
 import  org.springframework.cglib.proxy.Enhancer;
 import  org.springframework.cglib.proxy.Factory;
 import  org.springframework.cglib.proxy.MethodProxy;
 import  org.springframework.core.SpringVersion;
 import  org.springframework.dao.DataAccessException;
 import  org.springframework.dao.support.PersistenceExceptionTranslator;
 import  org.springframework.util.Assert;
 import  org.springframework.util.ClassUtils;
 import  org.springframework.util.ReflectionUtils;
 import  org.springframework.util.StringUtils;
 
 import com.mongodb.DB;
A DbRefResolver that resolves org.springframework.data.mongodb.core.mapping.DBRefs by delegating to a DbRefResolverCallback than is able to generate lazy loading proxies.

Author(s):
Thomas Darimont
Oliver Gierke
 
 public class DefaultDbRefResolver implements DbRefResolver {
 
 	private static final boolean IS_SPRING_4_OR_BETTER = SpringVersion.getVersion().startsWith("4");
 	private static final boolean OBJENESIS_PRESENT = ClassUtils.isPresent("org.objenesis.Objenesis"null);
 
 	private final MongoDbFactory mongoDbFactory;
 	private final PersistenceExceptionTranslator exceptionTranslator;

Creates a new DefaultDbRefResolver with the given MongoDbFactory.

Parameters:
mongoDbFactory must not be null.
 
 	public DefaultDbRefResolver(MongoDbFactory mongoDbFactory) {
 
 		Assert.notNull(mongoDbFactory"MongoDbFactory translator must not be null!");
 
 		this. = mongoDbFactory;
 		this. = mongoDbFactory.getExceptionTranslator();
 	}
 
 	/*
 	 * (non-Javadoc)
 	 * @see org.springframework.data.mongodb.core.convert.DbRefResolver#resolveDbRef(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty, org.springframework.data.mongodb.core.convert.DbRefResolverCallback)
 	 */
 
 		Assert.notNull(property"Property must not be null!");
 		Assert.notNull(callback"Callback must not be null!");
 
 		if (isLazyDbRef(property)) {
 			return createLazyLoadingProxy(propertycallback);
 		}
 
 		return callback.resolve(property);
 	}
 
 	/* 
 	 * (non-Javadoc)
 	 * @see org.springframework.data.mongodb.core.convert.DbRefResolver#created(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty, org.springframework.data.mongodb.core.mapping.MongoPersistentEntity, java.lang.Object)
 	 */
 			MongoPersistentEntity<?> entityObject id) {
		db = annotation != null && StringUtils.hasText(annotation.db()) ? .getDb(annotation.db()) : db;
		return new DBRef(dbentity.getCollection(), id);
	}

Creates a proxy for the given MongoPersistentProperty using the given DbRefResolverCallback to eventually resolve the value of the property.

Parameters:
property must not be null.
callback must not be null.
Returns:
		ProxyFactory proxyFactory = new ProxyFactory();
		Class<?> propertyType = property.getType();
		for (Class<?> type : propertyType.getInterfaces()) {
			proxyFactory.addInterface(type);
		}
		LazyLoadingInterceptor interceptor = new LazyLoadingInterceptor(propertycallback);
		if (propertyType.isInterface()) {
			proxyFactory.addInterface(propertyType);
			proxyFactory.addAdvice(interceptor);
			return proxyFactory.getProxy();
		}
		proxyFactory.setProxyTargetClass(true);
		proxyFactory.setTargetClass(propertyType);
			proxyFactory.addAdvice(interceptor);
			return proxyFactory.getProxy();
		}
		return ObjenesisProxyEnhancer.enhanceAndGet(proxyFactorypropertyTypeinterceptor);
	}

Parameters:
property
Returns:
	private boolean isLazyDbRef(MongoPersistentProperty property) {
		return property.getDBRef() != null && property.getDBRef().lazy();
	}

A MethodInterceptor that is used within a lazy loading proxy. The property resolving is delegated to a DbRefResolverCallback. The resolving process is triggered by a method invocation on the proxy and is guaranteed to be performed only once.

Author(s):
Thomas Darimont
	static class LazyLoadingInterceptor implements MethodInterceptor, org.springframework.cglib.proxy.MethodInterceptor,
		private final PersistenceExceptionTranslator exceptionTranslator;
		private volatile boolean resolved;
		private Object result;

Creates a new LazyLoadingInterceptor for the given MongoPersistentProperty, PersistenceExceptionTranslator and DbRefResolverCallback.

Parameters:
property must not be null.
callback must not be null.
		public LazyLoadingInterceptor(MongoPersistentProperty property, PersistenceExceptionTranslator exceptionTranslator,
			Assert.notNull(property"Property must not be null!");
			Assert.notNull(exceptionTranslator"Exception translator must not be null!");
			Assert.notNull(callback"Callback must not be null!");
			this. = callback;
			this. = exceptionTranslator;
			this. = property;
		}
		/*
		 * (non-Javadoc)
		 * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
		 */
		public Object invoke(MethodInvocation invocationthrows Throwable {
			return intercept(invocation.getThis(), invocation.getMethod(), invocation.getArguments(), null);
		}
		/* 
		 * (non-Javadoc)
		 * @see org.springframework.cglib.proxy.MethodInterceptor#intercept(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], org.springframework.cglib.proxy.MethodProxy)
		 */
		public Object intercept(Object objMethod methodObject[] args, MethodProxy proxythrows Throwable {
			return ReflectionUtils.isObjectMethod(method) ? method.invoke(objargs) : method.invoke(ensureResolved(), args);
		}
		private Object ensureResolved() {
			if (!) {
				this. = resolve();
				this. = true;
			}
			return this.;
		}
		private void writeObject(ObjectOutputStream outthrows IOException {
		}
		private void readObject(ObjectInputStream inthrows IOException {
			try {
				this. = true// Object is guaranteed to be resolved after serializations
				this. = in.readObject();
catch (ClassNotFoundException e) {
				throw new LazyLoadingException("Could not deserialize result"e);
			}
		}

Returns:
		private synchronized Object resolve() {
			if (!) {
				try {
catch (RuntimeException ex) {
					DataAccessException translatedException = this..translateExceptionIfPossible(ex);
					throw new LazyLoadingException("Unable to lazily resolve DBRef!"translatedException);
				}
			}
			return ;
		}
		public boolean isResolved() {
			return ;
		}
		public Object getResult() {
			return ;
		}
	}

Static class to accomodate optional dependency on Objenesis.

Author(s):
Oliver Gierke
	private static class ObjenesisProxyEnhancer {
		private static final Objenesis OBJENESIS = new ObjenesisStd(true);
		public static Object enhanceAndGet(ProxyFactory proxyFactoryClass<?> type,
				org.springframework.cglib.proxy.MethodInterceptor interceptor) {
			Enhancer enhancer = new Enhancer();
			enhancer.setSuperclass(type);
			enhancer.setCallbackType(org.springframework.cglib.proxy.MethodInterceptor.class);
			Factory factory = (Factory) .newInstance(enhancer.createClass());
			factory.setCallbacks(new Callback[] { interceptor });
			return factory;
		}
	}
New to GrepCode? Check out our FAQ X