1 package org.hibernate.tuple; 3 4 import java.io.Serializable ; 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 18 public class TuplizerLookup implements Serializable { 19 20 private static final Class [] ENTITY_TUP_CTOR_SIG = new Class [] { EntityMetamodel.class, PersistentClass.class }; 21 private static final Class [] COMPONENT_TUP_CTOR_SIG = new Class [] { Component.class }; 22 23 private final Tuplizer pojoTuplizer; 24 private final Tuplizer dynamicMapTuplizer; 25 private final Tuplizer dom4jTuplizer; 26 27 34 private TuplizerLookup(Tuplizer pojoTuplizer, Tuplizer dynamicMapTuplizer, Tuplizer dom4jTuplizer) { 35 this.pojoTuplizer = pojoTuplizer; 36 this.dynamicMapTuplizer = dynamicMapTuplizer; 37 this.dom4jTuplizer = dom4jTuplizer; 38 } 39 40 48 public static TuplizerLookup create(PersistentClass mappedEntity, EntityMetamodel em) { 49 Tuplizer dynamicMapTuplizer = null; 51 String 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 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 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 className, PersistentClass pc, EntityMetamodel em) { 93 try { 94 Class implClass = ReflectHelper.classForName( className ); 95 return ( EntityTuplizer ) implClass.getConstructor( ENTITY_TUP_CTOR_SIG ).newInstance( new Object [] { em, pc } ); 96 } 97 catch( Throwable t ) { 98 throw new HibernateException( "Could not build tuplizer [" + className + "]", t ); 99 } 100 } 101 102 108 public static TuplizerLookup create(Component component) { 109 PersistentClass owner = component.getOwner(); 110 111 Tuplizer dynamicMapTuplizer = null; 113 String 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 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 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 tuplizerImpl, Component component) { 155 try { 156 Class implClass = ReflectHelper.classForName( tuplizerImpl ); 157 return ( ComponentTuplizer ) implClass.getConstructor( COMPONENT_TUP_CTOR_SIG ).newInstance( new Object [] { component } ); 158 } 159 catch( Throwable t ) { 160 throw new HibernateException( "Could not build tuplizer [" + tuplizerImpl + "]", t ); 161 } 162 163 } 164 165 171 public EntityMode guessEntityMode(Object 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; } 186 187 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 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 |