KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > property > PropertyAccessorFactory


1 //$Id: PropertyAccessorFactory.java,v 1.6 2005/02/19 12:58:23 oneovthafew Exp $
2
package org.hibernate.property;
3
4 import java.util.Map JavaDoc;
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 /**
15  * A factory for building/retrieving PropertyAccessor instances.
16  *
17  * @author Gavin King
18  * @author Steve Ebersole
19  */

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     //TODO: ideally we need the construction of PropertyAccessor to take the following:
28
// 1) EntityMode
29
// 2) EntityMode-specific data (i.e., the classname for pojo entities)
30
// 3) Property-specific data based on the EntityMode (i.e., property-name or dom4j-node-name)
31
// The easiest way, with the introduction of the new runtime-metamodel classes, would be the
32
// the following predicates:
33
// 1) PropertyAccessorFactory.getPropertyAccessor() takes references to both a
34
// org.hibernate.metadata.EntityModeMetadata and org.hibernate.metadata.Property
35
// 2) What is now termed a "PropertyAccessor" stores any values needed from those two
36
// pieces of information
37
// 3) Code can then simply call PropertyAccess.getGetter() with no parameters; likewise with
38
// PropertyAccessor.getSetter()
39

40     /**
41      * Retrieves a PropertyAccessor instance based on the given property definition and
42      * entity mode.
43      *
44      * @param property The property for which to retrieve an accessor.
45      * @param mode The mode for the resulting entity.
46      * @return An appropriate accessor.
47      * @throws MappingException
48      */

49     public static PropertyAccessor getPropertyAccessor(Property property, EntityMode mode) throws MappingException {
50         //TODO: this is temporary in that the end result will probably not take a Property reference per-se.
51
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             //TODO: passing null here, because this method is not really used for DOM4J at the moment
59
// but it is still a bug, if we don't get rid of this!
60
return getDom4jPropertyAccessor( property.getAccessorPropertyName( mode ), property.getType(), null );
61         }
62         else {
63             throw new MappingException( "Unknown entity mode [" + mode + "]" );
64         }
65     } /**
66      * Retreives a PropertyAccessor specific for a PojoRepresentation with the given access strategy.
67      *
68      * @param pojoAccessorStrategy The access strategy.
69      * @return An appropriate accessor.
70      */

71     private static PropertyAccessor getPojoPropertyAccessor(String JavaDoc 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 JavaDoc nodeName, Type type, SessionFactoryImplementor factory)
91     throws MappingException {
92         //TODO: need some caching scheme? really comes down to decision
93
// regarding amount of state (if any) kept on PropertyAccessors
94
return new Dom4jAccessor( nodeName, type, factory );
95     }
96
97     private static PropertyAccessor resolveCustomAccessor(String JavaDoc accessorName) {
98         Class JavaDoc accessorClass;
99         try {
100             accessorClass = ReflectHelper.classForName(accessorName);
101         }
102         catch (ClassNotFoundException JavaDoc cnfe) {
103             throw new MappingException("could not find PropertyAccessor class: " + accessorName, cnfe);
104         }
105         try {
106             return (PropertyAccessor) accessorClass.newInstance();
107         }
108         catch (Exception JavaDoc e) {
109             throw new MappingException("could not instantiate PropertyAccessor class: " + accessorName, e);
110         }
111     }
112
113     private PropertyAccessorFactory() {}
114
115     // todo : this eventually needs to be removed
116
public static PropertyAccessor getPropertyAccessor(Class JavaDoc optionalClass, String JavaDoc type) throws MappingException {
117         if ( type==null ) type = optionalClass==null || optionalClass==Map JavaDoc.class ? "map" : "property";
118         return getPropertyAccessor(type);
119     }
120
121     // todo : this eventually needs to be removed
122
public static PropertyAccessor getPropertyAccessor(String JavaDoc 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