From: <fc...@us...> - 2007-10-31 18:00:26
|
Revision: 490 http://openutils.svn.sourceforge.net/openutils/?rev=490&view=rev Author: fcarone Date: 2007-10-31 11:00:30 -0700 (Wed, 31 Oct 2007) Log Message: ----------- [maven-release-plugin] copy for tag openutils-spring-rmibernate-1.0.3 Added Paths: ----------- tags/openutils-spring-rmibernate-1.0.3/ tags/openutils-spring-rmibernate-1.0.3/pom.xml tags/openutils-spring-rmibernate-1.0.3/src/main/java/it/openutils/spring/rmibernate/server/aspects/SerializationInterceptor.java Removed Paths: ------------- tags/openutils-spring-rmibernate-1.0.3/pom.xml tags/openutils-spring-rmibernate-1.0.3/src/main/java/it/openutils/spring/rmibernate/server/aspects/SerializationInterceptor.java Copied: tags/openutils-spring-rmibernate-1.0.3 (from rev 487, trunk/openutils-spring-rmibernate) Deleted: tags/openutils-spring-rmibernate-1.0.3/pom.xml =================================================================== --- trunk/openutils-spring-rmibernate/pom.xml 2007-10-31 11:05:02 UTC (rev 487) +++ tags/openutils-spring-rmibernate-1.0.3/pom.xml 2007-10-31 18:00:30 UTC (rev 490) @@ -1,122 +0,0 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils</artifactId> - <version>3</version> - <relativePath>..</relativePath> - </parent> - <artifactId>openutils-spring-rmibernate</artifactId> - <name>openutils base Spring-Hibernate RMI remote lazy loading support</name> - <version>1.0.3-SNAPSHOT</version> - <description>openutils base Spring-Hibernate RMI remote lazy loading support</description> - <properties> - <spring.version>2.0.6</spring.version> - </properties> - <dependencies> - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-log4j12</artifactId> - <version>1.4.1</version> - </dependency> - <dependency> - <!-- replaces commons-logging --> - <groupId>org.slf4j</groupId> - <artifactId>jcl104-over-slf4j</artifactId> - <version>1.4.1</version> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-hibernate3</artifactId> - <version>${spring.version}</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-remoting</artifactId> - <version>${spring.version}</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-aop</artifactId> - <version>${spring.version}</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.hibernate</groupId> - <artifactId>hibernate</artifactId> - <version>3.2.4.ga</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - <exclusion> - <groupId>cglib</groupId> - <artifactId>cglib</artifactId> - </exclusion> - <exclusion> - <groupId>asm</groupId> - <artifactId>asm</artifactId> - </exclusion> - <exclusion> - <groupId>asm</groupId> - <artifactId>asm-attrs</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>commons-beanutils</groupId> - <artifactId>commons-beanutils</artifactId> - <version>1.7.0</version> - <exclusions> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>commons-lang</groupId> - <artifactId>commons-lang</artifactId> - <version>2.3</version> - </dependency> - <dependency> - <groupId>commons-collections</groupId> - <artifactId>commons-collections</artifactId> - <version>3.1</version> - </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>3.8.1</version> - <optional>true</optional> - </dependency> - <dependency> - <groupId>cglib</groupId> - <artifactId>cglib-nodep</artifactId> - <version>2.1_3</version> - </dependency> - <dependency> - <groupId>asm</groupId> - <artifactId>asm</artifactId> - <version>2.2.3</version> - </dependency> - </dependencies> -</project> \ No newline at end of file Copied: tags/openutils-spring-rmibernate-1.0.3/pom.xml (from rev 489, trunk/openutils-spring-rmibernate/pom.xml) =================================================================== --- tags/openutils-spring-rmibernate-1.0.3/pom.xml (rev 0) +++ tags/openutils-spring-rmibernate-1.0.3/pom.xml 2007-10-31 18:00:30 UTC (rev 490) @@ -0,0 +1,128 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils</artifactId> + <version>3</version> + <relativePath>..</relativePath> + </parent> + <artifactId>openutils-spring-rmibernate</artifactId> + <name>openutils base Spring-Hibernate RMI remote lazy loading support</name> + <version>1.0.3</version> + <description>openutils base Spring-Hibernate RMI remote lazy loading support</description> + <properties> + <spring.version>2.0.6</spring.version> + </properties> + <dependencies> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <version>1.4.1</version> + </dependency> + <dependency> + <!-- replaces commons-logging --> + <groupId>org.slf4j</groupId> + <artifactId>jcl104-over-slf4j</artifactId> + <version>1.4.1</version> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-hibernate3</artifactId> + <version>${spring.version}</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-remoting</artifactId> + <version>${spring.version}</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-aop</artifactId> + <version>${spring.version}</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate</artifactId> + <version>3.2.4.ga</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + <exclusion> + <groupId>cglib</groupId> + <artifactId>cglib</artifactId> + </exclusion> + <exclusion> + <groupId>asm</groupId> + <artifactId>asm</artifactId> + </exclusion> + <exclusion> + <groupId>asm</groupId> + <artifactId>asm-attrs</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>commons-beanutils</groupId> + <artifactId>commons-beanutils</artifactId> + <version>1.7.0</version> + <exclusions> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.3</version> + </dependency> + <dependency> + <groupId>commons-collections</groupId> + <artifactId>commons-collections</artifactId> + <version>3.1</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>3.8.1</version> + <optional>true</optional> + </dependency> + <dependency> + <groupId>cglib</groupId> + <artifactId>cglib-nodep</artifactId> + <version>2.1_3</version> + </dependency> + <dependency> + <groupId>asm</groupId> + <artifactId>asm</artifactId> + <version>2.2.3</version> + </dependency> + </dependencies> + + <scm> + <connection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-spring-rmibernate-1.0.3</connection> + <developerConnection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-spring-rmibernate-1.0.3</developerConnection> + <url>http://openutils.svn.sourceforge.net/viewcvs.cgi/openutils/tags/openutils-spring-rmibernate-1.0.3</url> + </scm> +</project> \ No newline at end of file Deleted: tags/openutils-spring-rmibernate-1.0.3/src/main/java/it/openutils/spring/rmibernate/server/aspects/SerializationInterceptor.java =================================================================== --- trunk/openutils-spring-rmibernate/src/main/java/it/openutils/spring/rmibernate/server/aspects/SerializationInterceptor.java 2007-10-31 11:05:02 UTC (rev 487) +++ tags/openutils-spring-rmibernate-1.0.3/src/main/java/it/openutils/spring/rmibernate/server/aspects/SerializationInterceptor.java 2007-10-31 18:00:30 UTC (rev 490) @@ -1,374 +0,0 @@ -package it.openutils.spring.rmibernate.server.aspects; - -import it.openutils.spring.rmibernate.server.aspects.util.EntitySerializer; -import it.openutils.spring.rmibernate.shared.LazyReference; -import it.openutils.spring.rmibernate.shared.managers.HibernateLazyService; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import net.sf.cglib.proxy.Callback; -import net.sf.cglib.proxy.CallbackFilter; -import net.sf.cglib.proxy.Enhancer; -import net.sf.cglib.proxy.Factory; -import net.sf.cglib.proxy.MethodInterceptor; -import net.sf.cglib.proxy.MethodProxy; -import net.sf.cglib.proxy.NoOp; - -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.MapUtils; -import org.apache.commons.collections.SetUtils; -import org.apache.commons.collections.Transformer; -import org.hibernate.EntityMode; -import org.hibernate.SessionFactory; -import org.hibernate.collection.PersistentCollection; -import org.hibernate.collection.PersistentSet; -import org.hibernate.proxy.HibernateProxy; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.util.ReflectionUtils; - - -/** - * Proxy object with {@link EntitySerializer} and Intercept writeReplace calls - * @author mmolaschi - * @version $Id$ - */ -public class SerializationInterceptor implements MethodInterceptor -{ - - private static ThreadLocal<List<Object>> processed = new ThreadLocal<List<Object>>(); - - private static ThreadLocal<HibernateLazyService> hibernateLazyService = new ThreadLocal<HibernateLazyService>(); - - private static Map<Class, Object> proxies = Collections.synchronizedMap(new HashMap<Class, Object>()); - - /** - * log - */ - private static Logger log = LoggerFactory.getLogger(SerializationInterceptor.class); - - /** - * non proxied object - */ - private Object original; - - /** - * hibernate sessionfactory - */ - private SessionFactory sessionFactory; - - /** - * Constructor - * @param o object to be proxied - * @param sessionFactory hibernate sessionfactory - */ - private SerializationInterceptor(Object o, SessionFactory sessionFactory) - { - this.original = o; - this.sessionFactory = sessionFactory; - } - - /** - * Cleans the processed beans cache - */ - public static void clean() - { - processed.set(null); - } - - /** - * Get proxy object intercepting writeReplace calls - * @param o object to be proxied - * @param sessionFactory hibernate sessionfactory - * @return proxied object - */ - public static Object getEnhancedObject(Object o, SessionFactory sessionFactory) - { - return getEnhancedObject(o, sessionFactory, null); - } - - /** - * Get proxy object intercepting writeReplace calls - * @param o object to be proxied - * @param sessionFactory hibernate sessionfactory - * @param lazyRef reference to a lazy field - * @return proxied object - */ - public static Object getEnhancedObject(Object o, SessionFactory sessionFactory, LazyReference lazyRef) - { - if (o == null) - { - return null; - } - - if (!(o instanceof HibernateProxy) - && !(o instanceof PersistentSet) - && processed.get() != null - && processed.get().contains(o)) - { - return o; - } - - if (o.getClass().getName().startsWith("java.") && !((o instanceof Collection) || (o instanceof Map))) - { - return o; - } - - // check if there is an empty constructor - try - { - o.getClass().getConstructor(new Class[]{}); - } - catch (NoSuchMethodException ex) - { - return o; - } - - // check if object can be subclassed - if (Modifier.isFinal(o.getClass().getModifiers())) - { - return o; - } - - try - { - // get class - Class clazz = o.getClass(); - - // if it is an hibernateproxy get superclass - if (o instanceof HibernateProxy) - { - clazz = o.getClass().getSuperclass(); - if (clazz == null) - { - clazz = o.getClass().getInterfaces()[0]; - } - } - - Callback callback = null; - - // if this is a lazy field user lazyreferenceaspect - if (lazyRef != null) - { - LazyReferenceAspect lra = new LazyReferenceAspect(); - lra.setLazyReference(lazyRef); - callback = lra; - } - else - { - // user default interceptor - callback = new SerializationInterceptor(o, sessionFactory); - } - - synchronized (proxies) - { - if (proxies.containsKey(clazz)) - { - Factory proxy = (Factory) proxies.get(clazz); - return proxy.newInstance(new Callback[]{callback, NoOp.INSTANCE }); - } - } - // create proxy to listen on writeReplace method calls - Object proxy = Enhancer.create(clazz, new Class[]{EntitySerializer.class }, new CallbackFilter() - { - - public int accept(Method method) - { - if (method.getName().equals("writeReplace")) - { - return 0; - } - else - { - return 1; - } - } - - }, new Callback[]{callback, NoOp.INSTANCE }); - - // store proxy - proxies.put(clazz, ((Factory) proxy).newInstance(new Callback[]{ - EmptyMethodInterceptor.INSTANCE, - NoOp.INSTANCE })); - return proxy; - } - catch (Throwable t) - { - log.warn(t.getMessage(), t); - return o; - } - } - - /** - * {@inheritDoc} - */ - @SuppressWarnings("unchecked") - public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable - { - try - { - if (original == null) - { - return null; - } - - if (original.getClass().getName().startsWith("java.") - && !(original instanceof Collection) - || (original instanceof Map)) - { - return original; - } - - if (processed.get() == null) - { - processed.set(new ArrayList<Object>()); - } - - // add to processed objects - processed.get().add(original); - - if (original.getClass().isPrimitive()) - { - log.debug("Non enhancing primitive type: {}", original.getClass().getName()); - return original; - } - else if (original.getClass().isArray()) - { - // replace array entries with proxied ones - Object[] array = (Object[]) original; - for (int i = 0; i < array.length; i++) - { - array[i] = SerializationInterceptor.getEnhancedObject(array[i], sessionFactory); - } - return array; - } - else if (original instanceof Set) - { - // replace set entries with proxied ones - Set set = (Set) original; - Set transformed = SetUtils.transformedSet(new HashSet(), transformer(sessionFactory)); - transformed.addAll(set); - return transformed; - } - else if (original instanceof Collection) - { - // replace collection entries with proxied ones - Collection collection = (Collection) original; - CollectionUtils.transform(collection, transformer(sessionFactory)); - return collection; - } - else if (original instanceof Map) - { - // replace map entries with proxied ones - Map map = (Map) original; - Map transformed = MapUtils.transformedMap(new HashMap(), transformer(sessionFactory), transformer(sessionFactory)); - transformed.putAll(map); - return transformed; - } - else - { - // cycle on bean fields - ReflectionUtils.doWithFields(original.getClass(), new ReflectionUtils.FieldCallback() - { - - /** - * {@inheritDoc} - */ - public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException - { - // set field accessible - field.setAccessible(true); - - // get original value - Object oldValue = field.get(original); - - // get new value - Object newValue = null; - - if (oldValue instanceof HibernateProxy || oldValue instanceof PersistentCollection) - { - // if this field is a hibernate proxy or a persistent collection store reference - LazyReference lazyRef = new LazyReference(); - lazyRef.setFieldClassName(field.getType().getName()); - lazyRef.setClassName(original.getClass().getName()); - lazyRef.setFieldName(field.getName()); - // load id - lazyRef.setId(sessionFactory.getClassMetadata(original.getClass()).getIdentifier( - original, - EntityMode.POJO)); - - // get proxy for lazy - newValue = SerializationInterceptor.getEnhancedObject(oldValue, sessionFactory, lazyRef); - } - else - { - // get default proxy - newValue = SerializationInterceptor.getEnhancedObject(oldValue, sessionFactory); - } - - // if there is a variation store it - if (newValue != oldValue) - { - field.set(original, newValue); - } - } - - }); - } - - return original; - - } - catch (Throwable t) - { - log.error(t.getMessage(), t); - return original; - } - } - - /** - * Get transformer for collection, map or set injection of proxied objects - * @param factory hibernate session factory - * @return transformer - */ - private Transformer transformer(SessionFactory factory) - { - return new Transformer() - { - - public Object transform(Object input) - { - return SerializationInterceptor.getEnhancedObject(input, sessionFactory); - } - - }; - } - - /** - * Sets the hibernateLazyService. - * @param hibernateLazyService the hibernateLazyService to set - */ - public static void setHibernateLazyService(HibernateLazyService hibernateLazyService) - { - SerializationInterceptor.hibernateLazyService.set(hibernateLazyService); - } - - /** - * Returns the hibernateLazyService. - * @return the hibernateLazyService - */ - public static HibernateLazyService getHibernateLazyService() - { - return hibernateLazyService.get(); - } -} Copied: tags/openutils-spring-rmibernate-1.0.3/src/main/java/it/openutils/spring/rmibernate/server/aspects/SerializationInterceptor.java (from rev 488, trunk/openutils-spring-rmibernate/src/main/java/it/openutils/spring/rmibernate/server/aspects/SerializationInterceptor.java) =================================================================== --- tags/openutils-spring-rmibernate-1.0.3/src/main/java/it/openutils/spring/rmibernate/server/aspects/SerializationInterceptor.java (rev 0) +++ tags/openutils-spring-rmibernate-1.0.3/src/main/java/it/openutils/spring/rmibernate/server/aspects/SerializationInterceptor.java 2007-10-31 18:00:30 UTC (rev 490) @@ -0,0 +1,413 @@ +package it.openutils.spring.rmibernate.server.aspects; + +import it.openutils.spring.rmibernate.server.aspects.util.EntitySerializer; +import it.openutils.spring.rmibernate.shared.LazyReference; +import it.openutils.spring.rmibernate.shared.managers.HibernateLazyService; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import net.sf.cglib.proxy.Callback; +import net.sf.cglib.proxy.CallbackFilter; +import net.sf.cglib.proxy.Enhancer; +import net.sf.cglib.proxy.Factory; +import net.sf.cglib.proxy.MethodInterceptor; +import net.sf.cglib.proxy.MethodProxy; +import net.sf.cglib.proxy.NoOp; + +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.ListUtils; +import org.apache.commons.collections.MapUtils; +import org.apache.commons.collections.SetUtils; +import org.apache.commons.collections.Transformer; +import org.hibernate.EntityMode; +import org.hibernate.SessionFactory; +import org.hibernate.collection.PersistentCollection; +import org.hibernate.collection.PersistentSet; +import org.hibernate.proxy.HibernateProxy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.ReflectionUtils; +import org.apache.commons.lang.builder.HashCodeBuilder; + + +/** + * Proxy object with {@link EntitySerializer} and Intercept writeReplace calls + * @author mmolaschi + * @version $Id$ + */ +public class SerializationInterceptor implements MethodInterceptor +{ + + private static ThreadLocal<List<Object>> processed = new ThreadLocal<List<Object>>(); + + private static ThreadLocal<HibernateLazyService> hibernateLazyService = new ThreadLocal<HibernateLazyService>(); + + private static Map<Class, Object> proxies = Collections.synchronizedMap(new HashMap<Class, Object>()); + + /** + * log + */ + private static Logger log = LoggerFactory.getLogger(SerializationInterceptor.class); + + /** + * non proxied object + */ + private Object original; + + /** + * hibernate sessionfactory + */ + private SessionFactory sessionFactory; + + /** + * Constructor + * @param o object to be proxied + * @param sessionFactory hibernate sessionfactory + */ + private SerializationInterceptor(Object o, SessionFactory sessionFactory) + { + this.original = o; + this.sessionFactory = sessionFactory; + } + + /** + * Cleans the processed beans cache + */ + public static void clean() + { + processed.set(null); + } + + /** + * Get proxy object intercepting writeReplace calls + * @param o object to be proxied + * @param sessionFactory hibernate sessionfactory + * @return proxied object + */ + public static Object getEnhancedObject(Object o, SessionFactory sessionFactory) + { + return getEnhancedObject(o, sessionFactory, null); + } + + /** + * Get proxy object intercepting writeReplace calls + * @param o object to be proxied + * @param sessionFactory hibernate sessionfactory + * @param lazyRef reference to a lazy field + * @return proxied object + */ + public static Object getEnhancedObject(Object o, SessionFactory sessionFactory, LazyReference lazyRef) + { + if (o == null) + { + return null; + } + + if (!(o instanceof HibernateProxy) + && !(o instanceof PersistentSet) + && processed.get() != null + && processed.get().contains(o)) + { + return o; + } + + if (o.getClass().getName().startsWith("java.") && !((o instanceof Collection) || (o instanceof Map))) + { + return o; + } + + // check if there is an empty constructor + try + { + o.getClass().getConstructor(new Class[]{}); + } + catch (NoSuchMethodException ex) + { + return o; + } + + // check if object can be subclassed + if (Modifier.isFinal(o.getClass().getModifiers())) + { + return o; + } + + try + { + // get class + Class clazz = o.getClass(); + + // if it is an hibernateproxy get superclass + if (o instanceof HibernateProxy) + { + clazz = o.getClass().getSuperclass(); + if (clazz == null) + { + clazz = o.getClass().getInterfaces()[0]; + } + } + + Callback callback = null; + + // if this is a lazy field user lazyreferenceaspect + if (lazyRef != null) + { + LazyReferenceAspect lra = new LazyReferenceAspect(); + lra.setLazyReference(lazyRef); + callback = lra; + } + else + { + // user default interceptor + callback = new SerializationInterceptor(o, sessionFactory); + } + + synchronized (proxies) + { + if (proxies.containsKey(clazz)) + { + Factory proxy = (Factory) proxies.get(clazz); + return proxy.newInstance(new Callback[]{callback, NoOp.INSTANCE }); + } + } + // create proxy to listen on writeReplace method calls + Object proxy = Enhancer.create(clazz, new Class[]{EntitySerializer.class }, new CallbackFilter() + { + + public int accept(Method method) + { + if (method.getName().equals("writeReplace")) + { + return 0; + } + if (method.getName().equals("hashCode")) + { + return 0; + } + else + { + return 1; + } + } + + }, new Callback[]{callback, NoOp.INSTANCE }); + + // store proxy + proxies.put(clazz, ((Factory) proxy).newInstance(new Callback[]{ + EmptyMethodInterceptor.INSTANCE, + NoOp.INSTANCE })); + return proxy; + } + catch (Throwable t) + { + log.warn(t.getMessage(), t); + return o; + } + } + + /** + * {@inheritDoc} + */ + @SuppressWarnings("unchecked") + public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable + { + if (method.getName().equals("hashCode")) + { + return hashCode(); + } + try + { + if (original == null) + { + return null; + } + + if (original.getClass().getName().startsWith("java.") + && !(original instanceof Collection) + || (original instanceof Map)) + { + return original; + } + + if (processed.get() == null) + { + processed.set(new ArrayList<Object>()); + } + + // add to processed objects + processed.get().add(original); + + if (original.getClass().isPrimitive()) + { + log.debug("Non enhancing primitive type: {}", original.getClass().getName()); + return original; + } + else if (original.getClass().isArray()) + { + // replace array entries with proxied ones + Object[] array = (Object[]) original; + for (int i = 0; i < array.length; i++) + { + array[i] = SerializationInterceptor.getEnhancedObject(array[i], sessionFactory); + } + return array; + } + else if (original instanceof Set) + { + // replace set entries with proxied ones + Set set = (Set) original; + Set transformed = SetUtils.transformedSet(new HashSet(), transformer(sessionFactory)); + transformed.addAll(set); + Set result = new HashSet(); + result.addAll(transformed); + return result; + } + else if (original instanceof List) + { + List list = (List) original; + List transformed = ListUtils.transformedList(new ArrayList(), transformer(sessionFactory)); + transformed.addAll(list); + List result = new ArrayList(); + result.addAll(transformed); + return result; + } + else if (original instanceof Collection) + { + // replace collection entries with proxied ones + Collection collection = (Collection) original; + CollectionUtils.transform(collection, transformer(sessionFactory)); + return collection; + } + else if (original instanceof Map) + { + // replace map entries with proxied ones + Map map = (Map) original; + Map transformed = MapUtils.transformedMap( + new HashMap(), + transformer(sessionFactory), + transformer(sessionFactory)); + transformed.putAll(map); + Map result = new HashMap(); + result.putAll(transformed); + return result; + } + else + { + // cycle on bean fields + ReflectionUtils.doWithFields(original.getClass(), new ReflectionUtils.FieldCallback() + { + + /** + * {@inheritDoc} + */ + public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException + { + // set field accessible + field.setAccessible(true); + + // get original value + Object oldValue = field.get(original); + + // get new value + Object newValue = null; + + if (oldValue instanceof HibernateProxy || oldValue instanceof PersistentCollection) + { + // if this field is a hibernate proxy or a persistent collection store reference + LazyReference lazyRef = new LazyReference(); + lazyRef.setFieldClassName(field.getType().getName()); + lazyRef.setClassName(original.getClass().getName()); + lazyRef.setFieldName(field.getName()); + // load id + lazyRef.setId(sessionFactory.getClassMetadata(original.getClass()).getIdentifier( + original, + EntityMode.POJO)); + + // get proxy for lazy + newValue = SerializationInterceptor.getEnhancedObject(oldValue, sessionFactory, lazyRef); + } + else + { + // get default proxy + newValue = SerializationInterceptor.getEnhancedObject(oldValue, sessionFactory); + } + + // if there is a variation store it + if (newValue != oldValue) + { + field.set(original, newValue); + } + } + + }); + } + + return original; + + } + catch (Throwable t) + { + log.error(t.getMessage(), t); + return original; + } + } + + /** + * Get transformer for collection, map or set injection of proxied objects + * @param factory hibernate session factory + * @return transformer + */ + private Transformer transformer(SessionFactory factory) + { + return new Transformer() + { + + public Object transform(Object input) + { + return SerializationInterceptor.getEnhancedObject(input, sessionFactory); + } + + }; + } + + /** + * Sets the hibernateLazyService. + * @param hibernateLazyService the hibernateLazyService to set + */ + public static void setHibernateLazyService(HibernateLazyService hibernateLazyService) + { + SerializationInterceptor.hibernateLazyService.set(hibernateLazyService); + } + + /** + * Returns the hibernateLazyService. + * @return the hibernateLazyService + */ + public static HibernateLazyService getHibernateLazyService() + { + return hibernateLazyService.get(); + } + + /** + * {@inheritDoc} + */ + @Override + public int hashCode() + { + return new HashCodeBuilder(-469032761, 855711273) + .appendSuper(super.hashCode()) + .append(this.original) + .toHashCode(); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |