~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to test/org/hibernate/test/dynamicentity/interceptor/ProxyInterceptor.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hibernate.test.dynamicentity.interceptor;
 
2
 
 
3
import org.hibernate.EmptyInterceptor;
 
4
import org.hibernate.EntityMode;
 
5
import org.hibernate.test.dynamicentity.Company;
 
6
import org.hibernate.test.dynamicentity.Customer;
 
7
import org.hibernate.test.dynamicentity.ProxyHelper;
 
8
import org.hibernate.test.dynamicentity.DataProxyHandler;
 
9
 
 
10
import java.io.Serializable;
 
11
import java.lang.reflect.InvocationHandler;
 
12
import java.lang.reflect.Proxy;
 
13
 
 
14
/**
 
15
 * Our custom {@link org.hibernate.Interceptor} impl which performs the
 
16
 * interpretation of entity-name -> proxy instance and vice-versa.
 
17
 *
 
18
 * @author <a href="mailto:steve@hibernate.org">Steve Ebersole </a>
 
19
 */
 
20
public class ProxyInterceptor extends EmptyInterceptor {
 
21
 
 
22
        /**
 
23
         * The callback from Hibernate to determine the entity name given
 
24
         * a presumed entity instance.
 
25
         *
 
26
         * @param object The presumed entity instance.
 
27
         * @return The entity name (pointing to the proper entity mapping).
 
28
         */
 
29
        public String getEntityName(Object object) {
 
30
                String entityName = ProxyHelper.extractEntityName( object );
 
31
                if ( entityName == null ) {
 
32
                        entityName = super.getEntityName( object );
 
33
                }
 
34
                return entityName;
 
35
        }
 
36
 
 
37
        /**
 
38
         * The callback from Hibernate in order to build an instance of the
 
39
         * entity represented by the given entity name.  Here, we build a
 
40
         * {@link Proxy} representing the entity.
 
41
         *
 
42
         * @param entityName The entity name for which to create an instance.  In our setup,
 
43
         * this is the interface name.
 
44
         * @param entityMode The entity mode in which to create an instance.  Here, we are only
 
45
         * interestes in custom behavior for the POJO entity mode.
 
46
         * @param id The identifier value for the given entity.
 
47
         * @return The instantiated instance.
 
48
         */
 
49
        public Object instantiate(String entityName, EntityMode entityMode, Serializable id) {
 
50
                if ( entityMode == EntityMode.POJO ) {
 
51
                        if ( Customer.class.getName().equals( entityName ) ) {
 
52
                                return ProxyHelper.newCustomerProxy( id );
 
53
                        }
 
54
                        else if ( Company.class.getName().equals( entityName ) ) {
 
55
                                return ProxyHelper.newCompanyProxy( id );
 
56
                        }
 
57
                }
 
58
                return super.instantiate( entityName, entityMode, id );
 
59
        }
 
60
 
 
61
}