1 package org.hibernate.property; 3 4 import java.util.Map ; 5 6 import org.hibernate.MappingException; 7 import org.hibernate.EntityMode; 8 import org.hibernate.type.Type; 9 import org.hibernate.engine.SessionFactoryImplementor; 10 import org.hibernate.mapping.Property; 11 import org.hibernate.util.ReflectHelper; 12 import org.hibernate.util.StringHelper; 13 14 20 public final class PropertyAccessorFactory { 21 22 private static final PropertyAccessor BASIC_PROPERTY_ACCESSOR = new BasicPropertyAccessor(); 23 private static final PropertyAccessor DIRECT_PROPERTY_ACCESSOR = new DirectPropertyAccessor(); 24 private static final PropertyAccessor MAP_ACCESSOR = new MapAccessor(); 25 private static final PropertyAccessor EMBEDDED_PROPERTY_ACCESSOR = new EmbeddedPropertyAccessor(); 26 27 40 49 public static PropertyAccessor getPropertyAccessor(Property property, EntityMode mode) throws MappingException { 50 if ( null == mode || EntityMode.POJO.equals( mode ) ) { 52 return getPojoPropertyAccessor( property.getPropertyAccessorName() ); 53 } 54 else if ( EntityMode.MAP.equals( mode ) ) { 55 return getDynamicMapPropertyAccessor(); 56 } 57 else if ( EntityMode.DOM4J.equals( mode ) ) { 58 return getDom4jPropertyAccessor( property.getAccessorPropertyName( mode ), property.getType(), null ); 61 } 62 else { 63 throw new MappingException( "Unknown entity mode [" + mode + "]" ); 64 } 65 } 71 private static PropertyAccessor getPojoPropertyAccessor(String pojoAccessorStrategy) { 72 if ( StringHelper.isEmpty( pojoAccessorStrategy ) || "property".equals( pojoAccessorStrategy ) ) { 73 return BASIC_PROPERTY_ACCESSOR; 74 } 75 else if ( "field".equals( pojoAccessorStrategy ) ) { 76 return DIRECT_PROPERTY_ACCESSOR; 77 } 78 else if ( "embedded".equals( pojoAccessorStrategy ) ) { 79 return EMBEDDED_PROPERTY_ACCESSOR; 80 } 81 else { 82 return resolveCustomAccessor( pojoAccessorStrategy ); 83 } 84 } 85 86 public static PropertyAccessor getDynamicMapPropertyAccessor() throws MappingException { 87 return MAP_ACCESSOR; 88 } 89 90 public static PropertyAccessor getDom4jPropertyAccessor(String nodeName, Type type, SessionFactoryImplementor factory) 91 throws MappingException { 92 return new Dom4jAccessor( nodeName, type, factory ); 95 } 96 97 private static PropertyAccessor resolveCustomAccessor(String accessorName) { 98 Class accessorClass; 99 try { 100 accessorClass = ReflectHelper.classForName(accessorName); 101 } 102 catch (ClassNotFoundException cnfe) { 103 throw new MappingException("could not find PropertyAccessor class: " + accessorName, cnfe); 104 } 105 try { 106 return (PropertyAccessor) accessorClass.newInstance(); 107 } 108 catch (Exception e) { 109 throw new MappingException("could not instantiate PropertyAccessor class: " + accessorName, e); 110 } 111 } 112 113 private PropertyAccessorFactory() {} 114 115 public static PropertyAccessor getPropertyAccessor(Class optionalClass, String type) throws MappingException { 117 if ( type==null ) type = optionalClass==null || optionalClass==Map .class ? "map" : "property"; 118 return getPropertyAccessor(type); 119 } 120 121 public static PropertyAccessor getPropertyAccessor(String type) throws MappingException { 123 if ( type==null || "property".equals(type) ) return BASIC_PROPERTY_ACCESSOR; 124 if ( "field".equals(type) ) return DIRECT_PROPERTY_ACCESSOR; 125 if ( "map".equals(type) ) return MAP_ACCESSOR; 126 if ( "embedded".equals(type) ) return EMBEDDED_PROPERTY_ACCESSOR; 127 128 return resolveCustomAccessor(type); 129 } 130 } 131 | Popular Tags |