KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > tuple > TuplizerLookup


1 //$Id: TuplizerLookup.java,v 1.9 2005/07/11 21:47:03 steveebersole Exp $
2
package org.hibernate.tuple;
3
4 import java.io.Serializable JavaDoc;
5
6 import org.hibernate.EntityMode;
7 import org.hibernate.HibernateException;
8 import org.hibernate.util.ReflectHelper;
9 import org.hibernate.mapping.Component;
10 import org.hibernate.mapping.PersistentClass;
11
12 /**
13  * Stores references to the tuplizers available for a
14  * "tuplizable thing" (i.e., an entity or component).
15  *
16  * @author Gavin King
17  */

18 public class TuplizerLookup implements Serializable JavaDoc {
19
20     private static final Class JavaDoc[] ENTITY_TUP_CTOR_SIG = new Class JavaDoc[] { EntityMetamodel.class, PersistentClass.class };
21     private static final Class JavaDoc[] COMPONENT_TUP_CTOR_SIG = new Class JavaDoc[] { Component.class };
22
23     private final Tuplizer pojoTuplizer;
24     private final Tuplizer dynamicMapTuplizer;
25     private final Tuplizer dom4jTuplizer;
26
27     /**
28      * TuplizerLookup constructor.
29      *
30      * @param pojoTuplizer The POJO-based tuplizer.
31      * @param dynamicMapTuplizer The java.util.Map-based tuplizer.
32      * @param dom4jTuplizer The org.dom4j.Element-based tuplizer.
33      */

34     private TuplizerLookup(Tuplizer pojoTuplizer, Tuplizer dynamicMapTuplizer, Tuplizer dom4jTuplizer) {
35         this.pojoTuplizer = pojoTuplizer;
36         this.dynamicMapTuplizer = dynamicMapTuplizer;
37         this.dom4jTuplizer = dom4jTuplizer;
38     }
39
40     /**
41      * Generate a TuplizerLookup based on the given entity mapping and metamodel
42      * definitions.
43      *
44      * @param mappedEntity The entity mapping definition.
45      * @param em The entity metamodel definition.
46      * @return A TuplizerLookup containing the appropriate Tuplizers.
47      */

48     public static TuplizerLookup create(PersistentClass mappedEntity, EntityMetamodel em) {
49         // Build the dynamic-map tuplizer...
50
Tuplizer dynamicMapTuplizer = null;
51         String JavaDoc tuplizerImpl = mappedEntity.getTuplizerImplClassName( EntityMode.MAP );
52         if ( tuplizerImpl == null ) {
53             dynamicMapTuplizer = new DynamicMapEntityTuplizer( em, mappedEntity );
54         }
55         else {
56             dynamicMapTuplizer = buildEntityTuplizer( tuplizerImpl, mappedEntity, em );
57         }
58
59         // then the pojo tuplizer, using the dynamic-map tuplizer if no pojo representation is available
60
Tuplizer pojoTuplizer = null;
61         if ( mappedEntity.hasPojoRepresentation() ) {
62             tuplizerImpl = mappedEntity.getTuplizerImplClassName( EntityMode.POJO );
63             if ( tuplizerImpl == null ) {
64                 pojoTuplizer = new PojoEntityTuplizer( em, mappedEntity );
65             }
66             else {
67                 pojoTuplizer = buildEntityTuplizer( tuplizerImpl, mappedEntity, em );
68             }
69         }
70         else {
71             pojoTuplizer = dynamicMapTuplizer;
72         }
73
74         // then dom4j tuplizer, if dom4j representation is available
75
Tuplizer dom4jTuplizer = null;
76         if ( mappedEntity.hasDom4jRepresentation() ) {
77             tuplizerImpl = mappedEntity.getTuplizerImplClassName( EntityMode.DOM4J );
78             if ( tuplizerImpl == null ) {
79                 dom4jTuplizer = new Dom4jEntityTuplizer( em, mappedEntity );
80             }
81             else {
82                 dom4jTuplizer = buildEntityTuplizer( tuplizerImpl, mappedEntity, em );
83             }
84         }
85         else {
86             dom4jTuplizer = null;
87         }
88
89         return new TuplizerLookup( pojoTuplizer, dynamicMapTuplizer, dom4jTuplizer );
90     }
91
92     private static EntityTuplizer buildEntityTuplizer(String JavaDoc className, PersistentClass pc, EntityMetamodel em) {
93         try {
94             Class JavaDoc implClass = ReflectHelper.classForName( className );
95             return ( EntityTuplizer ) implClass.getConstructor( ENTITY_TUP_CTOR_SIG ).newInstance( new Object JavaDoc[] { em, pc } );
96         }
97         catch( Throwable JavaDoc t ) {
98             throw new HibernateException( "Could not build tuplizer [" + className + "]", t );
99         }
100     }
101
102     /**
103      * Generate a TuplizerLookup based on the given component mapping definition.
104      *
105      * @param component The component mapping definition.
106      * @return A TuplizerLookup containing the appropriate Tuplizers.
107      */

108     public static TuplizerLookup create(Component component) {
109         PersistentClass owner = component.getOwner();
110
111         // Build the dynamic-map tuplizer...
112
Tuplizer dynamicMapTuplizer = null;
113         String JavaDoc tuplizerImpl = component.getTuplizerImplClassName( EntityMode.MAP );
114         if ( tuplizerImpl == null ) {
115             dynamicMapTuplizer = new DynamicMapComponentTuplizer( component );
116         }
117         else {
118             dynamicMapTuplizer = buildComponentTuplizer( tuplizerImpl, component );
119         }
120
121         // then the pojo tuplizer, using the dynamic-map tuplizer if no pojo representation is available
122
Tuplizer pojoTuplizer = null;
123         if ( owner.hasPojoRepresentation() && component.hasPojoRepresentation() ) {
124             tuplizerImpl = component.getTuplizerImplClassName( EntityMode.POJO );
125             if ( tuplizerImpl == null ) {
126                 pojoTuplizer = new PojoComponentTuplizer( component );
127             }
128             else {
129                 pojoTuplizer = buildComponentTuplizer( tuplizerImpl, component );
130             }
131         }
132         else {
133             pojoTuplizer = dynamicMapTuplizer;
134         }
135
136         // then dom4j tuplizer, if dom4j representation is available
137
Tuplizer dom4jTuplizer = null;
138         if ( owner.hasDom4jRepresentation() ) {
139             tuplizerImpl = component.getTuplizerImplClassName( EntityMode.DOM4J );
140             if ( tuplizerImpl == null ) {
141                 dom4jTuplizer = new Dom4jComponentTuplizer( component );
142             }
143             else {
144                 dom4jTuplizer = buildComponentTuplizer( tuplizerImpl, component );
145             }
146         }
147         else {
148             dom4jTuplizer = null;
149         }
150
151         return new TuplizerLookup( pojoTuplizer, dynamicMapTuplizer, dom4jTuplizer );
152     }
153
154     private static ComponentTuplizer buildComponentTuplizer(String JavaDoc tuplizerImpl, Component component) {
155         try {
156             Class JavaDoc implClass = ReflectHelper.classForName( tuplizerImpl );
157             return ( ComponentTuplizer ) implClass.getConstructor( COMPONENT_TUP_CTOR_SIG ).newInstance( new Object JavaDoc[] { component } );
158         }
159         catch( Throwable JavaDoc t ) {
160             throw new HibernateException( "Could not build tuplizer [" + tuplizerImpl + "]", t );
161         }
162
163     }
164
165     /**
166      * Given a supposed instance of an entity/component, guess its entity mode.
167      *
168      * @param object The supposed instance of the entity/component.
169      * @return The guessed entity mode.
170      */

171     public EntityMode guessEntityMode(Object JavaDoc object) {
172         if ( pojoTuplizer != null && pojoTuplizer.isInstance(object) ) {
173             return EntityMode.POJO;
174         }
175
176         if ( dom4jTuplizer != null && dom4jTuplizer.isInstance(object) ) {
177             return EntityMode.DOM4J;
178         }
179
180         if ( dynamicMapTuplizer != null && dynamicMapTuplizer.isInstance(object) ) {
181             return EntityMode.MAP;
182         }
183
184         return null; // or should we throw an exception?
185
}
186
187     /**
188      * Locate the contained tuplizer responsible for the given entity-mode. If
189      * no such tuplizer is defined on this lookup, then return null.
190      *
191      * @param entityMode The entity-mode for which the client wants a tuplizer.
192      * @return The tuplizer, or null if not found.
193      */

194     public Tuplizer getTuplizerOrNull(EntityMode entityMode) {
195         Tuplizer rtn = null;
196         if ( EntityMode.POJO == entityMode ) {
197             rtn = pojoTuplizer;
198         }
199         else if ( EntityMode.DOM4J == entityMode ) {
200             rtn = dom4jTuplizer;
201         }
202         else if ( EntityMode.MAP == entityMode ) {
203             rtn = dynamicMapTuplizer;
204         }
205
206         return rtn;
207     }
208
209     /**
210      * Locate the contained tuplizer responsible for the given entity-mode. If
211      * no such tuplizer is defined on this lookup, then an exception is thrown.
212      *
213      * @param entityMode The entity-mode for which the client wants a tuplizer.
214      * @return The tuplizer.
215      * @throws HibernateException Unable to locate the requested tuplizer.
216      */

217     public Tuplizer getTuplizer(EntityMode entityMode) {
218         Tuplizer rtn = getTuplizerOrNull( entityMode );
219
220         if ( rtn == null ) {
221             throw new HibernateException( "No tuplizer found for entity-mode [" + entityMode + "]");
222         }
223
224         return rtn;
225     }
226
227 }
228
Popular Tags