From: <fc...@us...> - 2007-10-22 17:15:25
|
Revision: 478 http://openutils.svn.sourceforge.net/openutils/?rev=478&view=rev Author: fcarone Date: 2007-10-22 10:15:30 -0700 (Mon, 22 Oct 2007) Log Message: ----------- [maven-release-plugin] copy for tag openutils-spring-rmibernate-1.0.1 Added Paths: ----------- tags/openutils-spring-rmibernate-1.0.1/ tags/openutils-spring-rmibernate-1.0.1/pom.xml tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/client/aspects/HibernateLazyLoaderAspect.java tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/server/aspects/HibernateLazyRmiInterceptor.java tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/server/aspects/SerializationInterceptor.java tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/shared/LazyReference.java Removed Paths: ------------- tags/openutils-spring-rmibernate-1.0.1/pom.xml tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/client/aspects/HibernateLazyLoaderAspect.java tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/server/aspects/HibernateLazyRmiInterceptor.java tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/server/aspects/SerializationInterceptor.java tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/shared/LazyReference.java Copied: tags/openutils-spring-rmibernate-1.0.1 (from rev 475, trunk/openutils-spring-rmibernate) Deleted: tags/openutils-spring-rmibernate-1.0.1/pom.xml =================================================================== --- trunk/openutils-spring-rmibernate/pom.xml 2007-10-17 17:06:38 UTC (rev 475) +++ tags/openutils-spring-rmibernate-1.0.1/pom.xml 2007-10-22 17:15:30 UTC (rev 478) @@ -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.1-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.1/pom.xml (from rev 477, trunk/openutils-spring-rmibernate/pom.xml) =================================================================== --- tags/openutils-spring-rmibernate-1.0.1/pom.xml (rev 0) +++ tags/openutils-spring-rmibernate-1.0.1/pom.xml 2007-10-22 17:15:30 UTC (rev 478) @@ -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.1</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.1</connection> + <developerConnection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-spring-rmibernate-1.0.1</developerConnection> + <url>http://openutils.svn.sourceforge.net/viewcvs.cgi/openutils/tags/openutils-spring-rmibernate-1.0.1</url> + </scm> +</project> \ No newline at end of file Deleted: tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/client/aspects/HibernateLazyLoaderAspect.java =================================================================== --- trunk/openutils-spring-rmibernate/src/main/java/it/openutils/spring/rmibernate/client/aspects/HibernateLazyLoaderAspect.java 2007-10-17 17:06:38 UTC (rev 475) +++ tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/client/aspects/HibernateLazyLoaderAspect.java 2007-10-22 17:15:30 UTC (rev 478) @@ -1,75 +0,0 @@ -package it.openutils.spring.rmibernate.client.aspects; - -import it.openutils.spring.rmibernate.client.HibernateRmiProxyFactoryBean; -import it.openutils.spring.rmibernate.client.pagination.Paginator; -import it.openutils.spring.rmibernate.shared.managers.HibernateLazyService; - -import java.io.Serializable; - -import net.sf.cglib.proxy.LazyLoader; - - -/** - * Aspect that does remote lazy loading on cglib proxy - * @author mmolaschi - * @version $Id: $ - */ -public class HibernateLazyLoaderAspect implements LazyLoader, Serializable -{ - - private static ThreadLocal<Paginator> paginator = new ThreadLocal<Paginator>(); - - private String className; - - private String fieldName; - - private Serializable id; - - /** - * Constructor - * @param className parent class name - * @param fieldName field to be lazy loaded - * @param id id of current entity - */ - public HibernateLazyLoaderAspect(String className, String fieldName, Serializable id) - { - this.className = className; - this.fieldName = fieldName; - this.id = id; - } - - /** - * Set pagination for next calls in current thread - * @param from starting row number - * @param size max number of rows - */ - public static void setPagination(int from, int size) - { - paginator.set(new Paginator(from, size)); - } - - /** - * Disable pagination for next calls in current thread - */ - public static void resetPagination() - { - paginator.set(null); - } - - /** - * {@inheritDoc} - */ - public Object loadObject() throws Exception - { - // Load from remote - HibernateLazyService hls = HibernateRmiProxyFactoryBean.hibernateLazyServiceTL.get(); - - if (paginator.get() != null) - { - return hls.invoke(className, fieldName, id, paginator.get().getFrom(), paginator.get().getSize()); - } - - return hls.invoke(className, fieldName, id); - } - -} Copied: tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/client/aspects/HibernateLazyLoaderAspect.java (from rev 476, trunk/openutils-spring-rmibernate/src/main/java/it/openutils/spring/rmibernate/client/aspects/HibernateLazyLoaderAspect.java) =================================================================== --- tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/client/aspects/HibernateLazyLoaderAspect.java (rev 0) +++ tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/client/aspects/HibernateLazyLoaderAspect.java 2007-10-22 17:15:30 UTC (rev 478) @@ -0,0 +1,81 @@ +package it.openutils.spring.rmibernate.client.aspects; + +import it.openutils.spring.rmibernate.client.HibernateRmiProxyFactoryBean; +import it.openutils.spring.rmibernate.client.pagination.Paginator; +import it.openutils.spring.rmibernate.shared.managers.HibernateLazyService; + +import java.io.Serializable; + +import net.sf.cglib.proxy.LazyLoader; + + +/** + * Aspect that does remote lazy loading on cglib proxy + * @author mmolaschi + * @version $Id: $ + */ +public class HibernateLazyLoaderAspect implements LazyLoader, Serializable +{ + + /** + * UID + */ + private static final long serialVersionUID = -365731708075101363L; + + private static ThreadLocal<Paginator> paginator = new ThreadLocal<Paginator>(); + + private String className; + + private String fieldName; + + private Serializable id; + + private HibernateLazyService hibernateLazyService; + + /** + * Constructor + * @param className parent class name + * @param fieldName field to be lazy loaded + * @param id id of current entity + */ + public HibernateLazyLoaderAspect(String className, String fieldName, Serializable id, HibernateLazyService hibernateLazyService) + { + this.className = className; + this.fieldName = fieldName; + this.id = id; + this.hibernateLazyService = hibernateLazyService; + } + + /** + * Set pagination for next calls in current thread + * @param from starting row number + * @param size max number of rows + */ + public static void setPagination(int from, int size) + { + paginator.set(new Paginator(from, size)); + } + + /** + * Disable pagination for next calls in current thread + */ + public static void resetPagination() + { + paginator.set(null); + } + + /** + * {@inheritDoc} + */ + public Object loadObject() throws Exception + { + // Load from remote + if (paginator.get() != null) + { + return hibernateLazyService.invoke(className, fieldName, id, paginator.get().getFrom(), paginator.get().getSize()); + } + + return hibernateLazyService.invoke(className, fieldName, id); + } + +} Deleted: tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/server/aspects/HibernateLazyRmiInterceptor.java =================================================================== --- trunk/openutils-spring-rmibernate/src/main/java/it/openutils/spring/rmibernate/server/aspects/HibernateLazyRmiInterceptor.java 2007-10-17 17:06:38 UTC (rev 475) +++ tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/server/aspects/HibernateLazyRmiInterceptor.java 2007-10-22 17:15:30 UTC (rev 478) @@ -1,58 +0,0 @@ -package it.openutils.spring.rmibernate.server.aspects; - -import org.aopalliance.intercept.MethodInterceptor; -import org.aopalliance.intercept.MethodInvocation; -import org.hibernate.SessionFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -/** - * Main server interceptor on rmi calls - * @author mmolaschi - * @version $Id: $ - */ -public class HibernateLazyRmiInterceptor implements MethodInterceptor -{ - - /** - * log - */ - protected static final Logger logger = LoggerFactory.getLogger(HibernateLazyRmiInterceptor.class); - - private SessionFactory sessionFactory; - - /** - * Contructor - * @param sessionFactory hibernate session factory - */ - public HibernateLazyRmiInterceptor(SessionFactory sessionFactory) - { - this.sessionFactory = sessionFactory; - } - - /** - * {@inheritDoc} - */ - public Object invoke(MethodInvocation invocation) throws Throwable - { - try - { - Object retVal = invocation.proceed(); - - SerializationInterceptor.clean(); - // Proxy class for serialization work - return SerializationInterceptor.getEnhancedObject(retVal, sessionFactory); - } - catch (Throwable ex) - { - if (logger.isInfoEnabled()) - { - logger.info("Processing of " + " remote call resulted in exception: ", ex); - } - - throw ex; - } - } - -} Copied: tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/server/aspects/HibernateLazyRmiInterceptor.java (from rev 476, trunk/openutils-spring-rmibernate/src/main/java/it/openutils/spring/rmibernate/server/aspects/HibernateLazyRmiInterceptor.java) =================================================================== --- tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/server/aspects/HibernateLazyRmiInterceptor.java (rev 0) +++ tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/server/aspects/HibernateLazyRmiInterceptor.java 2007-10-22 17:15:30 UTC (rev 478) @@ -0,0 +1,63 @@ +package it.openutils.spring.rmibernate.server.aspects; + +import it.openutils.spring.rmibernate.shared.managers.HibernateLazyService; + +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; +import org.hibernate.SessionFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Main server interceptor on rmi calls + * @author mmolaschi + * @version $Id: $ + */ +public class HibernateLazyRmiInterceptor implements MethodInterceptor +{ + + /** + * log + */ + protected static final Logger logger = LoggerFactory.getLogger(HibernateLazyRmiInterceptor.class); + + private SessionFactory sessionFactory; + + /** + * Contructor + * @param sessionFactory hibernate session factory + */ + public HibernateLazyRmiInterceptor(SessionFactory sessionFactory) + { + this.sessionFactory = sessionFactory; + } + + /** + * {@inheritDoc} + */ + public Object invoke(MethodInvocation invocation) throws Throwable + { + logger.debug("Catched invocation: {}", invocation); + try + { + Object retVal = invocation.proceed(); + + SerializationInterceptor.clean(); + // Proxy class for serialization work + Object result = SerializationInterceptor.getEnhancedObject(retVal, sessionFactory); + logger.debug("Returning enhanced object {}", result); + return result; + } + catch (Throwable ex) + { + if (logger.isInfoEnabled()) + { + logger.info("Processing of " + " remote call resulted in exception: ", ex); + } + + throw ex; + } + } + +} Deleted: tags/openutils-spring-rmibernate-1.0.1/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-17 17:06:38 UTC (rev 475) +++ tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/server/aspects/SerializationInterceptor.java 2007-10-22 17:15:30 UTC (rev 478) @@ -1,334 +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 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.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 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; - } - - 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; - } - - // 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 (processed.get() == null) - { - processed.set(new ArrayList<Object>()); - } - - // add to processed objects - processed.get().add(original); - - if (original.getClass().isPrimitive()) - { - 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 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; - MapUtils.transformedMap(map, transformer(sessionFactory), transformer(sessionFactory)); - return map; - } - else if (original instanceof Set) - { - // replace set entries with proxied ones - Set set = (Set) original; - SetUtils.transformedSet(set, transformer(sessionFactory)); - return set; - } - 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); - } - - }; - } - -} Copied: tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/server/aspects/SerializationInterceptor.java (from rev 476, trunk/openutils-spring-rmibernate/src/main/java/it/openutils/spring/rmibernate/server/aspects/SerializationInterceptor.java) =================================================================== --- tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/server/aspects/SerializationInterceptor.java (rev 0) +++ tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/server/aspects/SerializationInterceptor.java 2007-10-22 17:15:30 UTC (rev 478) @@ -0,0 +1,376 @@ +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.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 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; + MapUtils.transformedMap(map, transformer(sessionFactory), transformer(sessionFactory)); + return map; + } + else if (original instanceof Set) + { + // replace set entries with proxied ones + Set set = (Set) original; + SetUtils.transformedSet(set, transformer(sessionFactory)); + return set; + } + 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(); + } +} Deleted: tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/shared/LazyReference.java =================================================================== --- trunk/openutils-spring-rmibernate/src/main/java/it/openutils/spring/rmibernate/shared/LazyReference.java 2007-10-17 17:06:38 UTC (rev 475) +++ tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/shared/LazyReference.java 2007-10-22 17:15:30 UTC (rev 478) @@ -1,117 +0,0 @@ -package it.openutils.spring.rmibernate.shared; - -import it.openutils.spring.rmibernate.client.aspects.HibernateLazyLoaderAspect; - -import java.io.ObjectStreamException; -import java.io.Serializable; - -import net.sf.cglib.proxy.Enhancer; - - -/** - * Store lazy reference - * @author mmolaschi - * @version $Id: $ - */ -public class LazyReference implements Serializable -{ - - private Serializable id; - - private String fieldName; - - private String className; - - private String fieldClassName; - - /** - * default constructor - */ - public LazyReference() - { - - } - - /** - * Constructor - * @param className className - * @param fieldName fieldName - * @param fieldClassName fieldClassName - * @param id id of current entity - */ - public LazyReference(String className, String fieldName, String fieldClassName, Serializable id) - { - this.className = className; - this.fieldName = fieldName; - this.fieldClassName = fieldClassName; - this.id = id; - } - - /** - * Called on deserialization, creates proxy for remote lazy loading - * @return proxy for remote lazy loading - * @throws ObjectStreamException exception reading - * @throws ClassNotFoundException class not found - */ - @SuppressWarnings("unchecked") - Object readResolve() throws ObjectStreamException, ClassNotFoundException - { - // get proxy superclass - Class superclass = Class.forName(fieldClassName); - Class[] interfaces = null; - // if superclass is an interface add it to interfaces - if (superclass.isInterface()) - { - interfaces = new Class[]{superclass}; - } - // return proxy - return Enhancer.create( - superclass, - interfaces, - new HibernateLazyLoaderAspect(className, fieldName, id)); - - } - - public String getClassName() - { - return className; - } - - public void setClassName(String className) - { - this.className = className; - } - - public Serializable getId() - { - return id; - } - - public void setId(Serializable id) - { - this.id = id; - } - - public String getFieldName() - { - return fieldName; - } - - public void setFieldName(String fieldName) - { - this.fieldName = fieldName; - } - - - public String getFieldClassName() - { - return fieldClassName; - } - - - public void setFieldClassName(String fieldClassName) - { - this.fieldClassName = fieldClassName; - } - -} Copied: tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/shared/LazyReference.java (from rev 476, trunk/openutils-spring-rmibernate/src/main/java/it/openutils/spring/rmibernate/shared/LazyReference.java) =================================================================== --- tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/shared/LazyReference.java (rev 0) +++ tags/openutils-spring-rmibernate-1.0.1/src/main/java/it/openutils/spring/rmibernate/shared/LazyReference.java 2007-10-22 17:15:30 UTC (rev 478) @@ -0,0 +1,123 @@ +package it.openutils.spring.rmibernate.shared; + +import it.openutils.spring.rmibernate.client.HibernateRmiProxyFactoryBean; +import it.openutils.spring.rmibernate.client.aspects.HibernateLazyLoaderAspect; + +import java.io.ObjectStreamException; +import java.io.Serializable; + +import net.sf.cglib.proxy.Enhancer; + + +/** + * Store lazy reference + * @author mmolaschi + * @version $Id: $ + */ +public class LazyReference implements Serializable +{ + + /** + * UID + */ + private static final long serialVersionUID = 421364413345638150L; + + private Serializable id; + + private String fieldName; + + private String className; + + private String fieldClassName; + + /** + * default constructor + */ + public LazyReference() + { + + } + + /** + * Constructor + * @param className className + * @param fieldName fieldName + * @param fieldClassName fieldClassName + * @param id id of current entity + */ + public LazyReference(String className, String fieldName, String fieldClassName, Serializable id) + { + this.className = className; + this.fieldName = fieldName; + this.fieldClassName = fieldClassName; + this.id = id; + } + + /** + * Called on deserialization, creates proxy for remote lazy loading + * @return proxy for remote lazy loading + * @throws ObjectStreamException exception reading + * @throws ClassNotFoundException class not found + */ + @SuppressWarnings("unchecked") + Object readResolve() throws ObjectStreamException, ClassNotFoundException + { + // get proxy superclass + Class superclass = Class.forName(fieldClassName); + Class[] interfaces = null; + // if superclass is an interface add it to interfaces + if (superclass.isInterface()) + { + interfaces = new Class[]{superclass}; + } + // return proxy + return Enhancer.create( + superclass, + interfaces, + new HibernateLazyLoaderAspect(className, fieldName, id, HibernateRmiProxyFactoryBean.hibernateLazyServiceTL.get())); + + } + + public String getClassName() + { + return className; + } + + public void setClassName(String className) + { + this.className = className; + } + + public Serializable getId() + { + return id; + } + + public void setId(Serializable id) + { + this.id = id; + } + + public String getFieldName() + { + return fieldName; + } + + public void setFieldName(String fieldName) + { + this.fieldName = fieldName; + } + + + public String getFieldClassName() + { + return fieldClassName; + } + + + public void setFieldClassName(String fieldClassName) + { + this.fieldClassName = fieldClassName; + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |