org.exolab.castor.mapping.loader

Class AbstractMappingLoader

Implemented Interfaces:
MappingLoader
Known Direct Subclasses:
JDOMappingLoader, XMLMappingLoader

public abstract class AbstractMappingLoader
extends AbstractMappingLoader2

Assists in the construction of descriptors. Can be used as a mapping resolver to the engine. Engines will implement their own mapping scheme typically by extending this class.
Version:
$Revision: 7276 $ $Date: 2006-04-10 16:39:24 -0600 (Mon, 10 Apr 2006) $
Authors:
Assaf Arkin
Keith Visco

Nested Class Summary

class
AbstractMappingLoader.TypeInfoReference
A class used to by the createFieldHandler method in order to save the reference of the TypeInfo that was used.

Field Summary

protected static Class[]
EMPTY_ARGS
Empty array of class types used for reflection.
protected static Class[]
STRING_ARG
The string argument for the valueOf method, used for introspection when searching for type-safe enumeration style classes.
protected static String
VALUE_OF
Factory method name for type-safe enumerations.

Constructor Summary

AbstractMappingLoader(ClassLoader loader)
Constructs a new mapping helper.

Method Summary

protected void
checkFieldNameDuplicates(FieldDescriptor[] fields, Class cls)
Checks all given fields for name equality and throws a MappingException if at least two fields have the same name.
protected abstract ClassDescriptor
createClassDescriptor(ClassMapping clsMap)
protected void
createClassDescriptors(MappingRoot mapping)
protected AbstractFieldDescriptor
createFieldDesc(Class javaClass, FieldMapping fieldMap)
Creates a single field descriptor.
protected AbstractFieldDescriptor[]
createFieldDescriptors(ClassMapping clsMap, Class javaClass)
Create field descriptors.
protected FieldHandler
createFieldHandler(Class javaClass, Class fldType, FieldMapping fldMap, AbstractMappingLoader.TypeInfoReference typeInfoRef)
Creates the FieldHandler for the given FieldMapping.
protected void
createFieldHandlers(MappingRoot mapping)
Load field handler definitions, check for duplicate definitions and instantiate the appropriate FieldHandler implementations.
protected FieldDescriptor[]
divideFieldDescriptors(FieldDescriptor[] fields, String[] ids, FieldDescriptor[] identities)
static Method
findAccessor(Class javaClass, String methodName, Class fieldType, boolean getMethod)
Returns the named accessor.
protected ClassDescriptor
getDepended(ClassMapping clsMap, Class javaClass)
Gets the ClassDescriptor the given classMapping depends on.
protected ClassDescriptor
getExtended(ClassMapping clsMap, Class javaClass)
Gets the ClassDescriptor the given classMapping extends.
protected int
getIdColumnIndex(FieldDescriptor field, String[] ids)
Finds the index in the given idColumnNames that has the same name as the given field.
static String[]
getIdentityColumnNames(String[] ids, ClassMapping clsMap)
Returns a list of column names that are part of the identity.
InternalContext
getInternalContext()
protected ClassMapping
getOrigin(ClassMapping clsMap)
Gets the top-most (i.e.
String
getSourceType()
protected TypeInfo
getTypeInfo(Class fieldType, CollectionHandler colHandler, FieldMapping fieldMap)
protected static boolean
isPrimitive(Class type)
Returns true if the given class should be treated as a primitive type
abstract void
loadMapping(MappingRoot mapping, Object param)
Loads the mapping from the specified mapping object if not loaded previously.
protected abstract void
resolveRelations(ClassDescriptor clsDesc)
protected Class
resolveType(String typeName)
Returns the Java class for the named type.
void
setInternalContext(InternalContext internalContext)

Methods inherited from class org.exolab.castor.mapping.loader.AbstractMappingLoader2

addDescriptor, clear, descriptorIterator, getClassLoader, getDescriptor, isAllowRedefinition, loadMapping, setAllowRedefinitions, setClassLoader

Field Details

EMPTY_ARGS

protected static final Class[] EMPTY_ARGS
Empty array of class types used for reflection.

STRING_ARG

protected static final Class[] STRING_ARG
The string argument for the valueOf method, used for introspection when searching for type-safe enumeration style classes.

VALUE_OF

protected static final String VALUE_OF
Factory method name for type-safe enumerations.

Constructor Details

AbstractMappingLoader

protected AbstractMappingLoader(ClassLoader loader)
Constructs a new mapping helper. This constructor is used by a derived class.
Parameters:
loader - The class loader to use, null for the default

Method Details

checkFieldNameDuplicates

protected final void checkFieldNameDuplicates(FieldDescriptor[] fields,
                                              Class cls)
            throws MappingException
Checks all given fields for name equality and throws a MappingException if at least two fields have the same name.
Parameters:
fields - The fields to be checked.
cls - Class that is checked (this is used for generating the exception).
Throws:
MappingException - If at least two fields have the same name.

createClassDescriptor

protected abstract ClassDescriptor createClassDescriptor(ClassMapping clsMap)
            throws MappingException

createClassDescriptors

protected final void createClassDescriptors(MappingRoot mapping)
            throws MappingException

createFieldDesc

protected AbstractFieldDescriptor createFieldDesc(Class javaClass,
                                                  FieldMapping fieldMap)
            throws MappingException
Creates a single field descriptor. The field mapping is used to create a new stock FieldDescriptor. Implementations may extend this class to create a more suitable descriptor.
Parameters:
javaClass - The class to which the field belongs.
fieldMap - The field mapping information.
Returns:
The field descriptor.
Throws:
MappingException - The field or its accessor methods are not found, not accessible, not of the specified type, etc.

createFieldDescriptors

protected final AbstractFieldDescriptor[] createFieldDescriptors(ClassMapping clsMap,
                                                                 Class javaClass)
            throws MappingException
Create field descriptors. The class mapping information is used to create descriptors for all the fields in the class, except for container fields. Implementations may extend this method to create more suitable descriptors, or create descriptors only for a subset of the fields.
Parameters:
clsMap - The class to which the fields belong.
javaClass - The field mappings.
Throws:
MappingException - An exception indicating why mapping for the class cannot be created.

createFieldHandler

protected final FieldHandler createFieldHandler(Class javaClass,
                                                Class fldType,
                                                FieldMapping fldMap,
                                                AbstractMappingLoader.TypeInfoReference typeInfoRef)
            throws MappingException
Creates the FieldHandler for the given FieldMapping.
Parameters:
javaClass - the class type of the parent of the field.
fldType - the Java class type for the field.
fldMap - the field mapping.
Returns:
the newly created FieldHandler.

createFieldHandlers

protected void createFieldHandlers(MappingRoot mapping)
            throws MappingException
Load field handler definitions, check for duplicate definitions and instantiate the appropriate FieldHandler implementations.
Parameters:
mapping - Mapping to load field handler definitions from.
Throws:
MappingException - If mapping contains more then one field handler definition with same name.

divideFieldDescriptors

protected final FieldDescriptor[] divideFieldDescriptors(FieldDescriptor[] fields,
                                                         String[] ids,
                                                         FieldDescriptor[] identities)

findAccessor

public static final Method findAccessor(Class javaClass,
                                        String methodName,
                                        Class fieldType,
                                        boolean getMethod)
            throws MappingException
Returns the named accessor. Uses reflection to return the named accessor and check the return value or parameter type, if specified.
Parameters:
javaClass - The class to which the field belongs.
methodName - The name of the accessor method.
fieldType - The type of the field if known, or null.
getMethod - True if get method, false if set method.
Returns:
The method, null if not found.
Throws:
MappingException - The method is not accessible or is not of the specified type.

getDepended

protected final ClassDescriptor getDepended(ClassMapping clsMap,
                                            Class javaClass)
            throws MappingException
Gets the ClassDescriptor the given classMapping depends on.
Parameters:
clsMap - The ClassMapping to find the required ClassDescriptor for.
javaClass - The name of the class that is checked (this is used for generating the exception).
Returns:
The ClassDescriptor the given ClassMapping depends on or null if the given ClassMapping does not depend on any.
Throws:
MappingException - If the given ClassMapping depends on another ClassMapping but its descriptor could not be found.

getExtended

protected final ClassDescriptor getExtended(ClassMapping clsMap,
                                            Class javaClass)
            throws MappingException
Gets the ClassDescriptor the given classMapping extends.
Parameters:
clsMap - The ClassMapping to find the required descriptor for.
javaClass - The name of the class that is checked (this is used for generating the exception).
Returns:
The ClassDescriptor the given ClassMapping extends or null if the given ClassMapping does not extend any.
Throws:
MappingException - If the given ClassMapping extends another ClassMapping but its descriptor could not be found.

getIdColumnIndex

protected int getIdColumnIndex(FieldDescriptor field,
                               String[] ids)
Finds the index in the given idColumnNames that has the same name as the given field.
Parameters:
field - The FieldDescriptor to find the column index for.
ids - The id columnNames available.
Returns:
The index of the id column name that matches the given field's name or -1 if no such id column name exists.

getIdentityColumnNames

public static final String[] getIdentityColumnNames(String[] ids,
                                                    ClassMapping clsMap)
Returns a list of column names that are part of the identity.
Parameters:
ids - Known identity names.
clsMap - Class mapping.
Returns:
List of identity column names.

getInternalContext

public InternalContext getInternalContext()

getOrigin

protected final ClassMapping getOrigin(ClassMapping clsMap)
Gets the top-most (i.e. without any further 'extends') extends of the given classMapping.
Parameters:
clsMap - The ClassMapping to get the origin for.
Returns:
The top-most extends of the given ClassMapping or the ClassMapping itself if it does not extend any other ClassMapping.

getSourceType

public final String getSourceType()
Specified by:
getSourceType in interface MappingLoader

getTypeInfo

protected TypeInfo getTypeInfo(Class fieldType,
                               CollectionHandler colHandler,
                               FieldMapping fieldMap)
            throws MappingException

isPrimitive

protected static final boolean isPrimitive(Class type)
Returns true if the given class should be treated as a primitive type
Returns:
true if the given class should be treated as a primitive type

loadMapping

public abstract void loadMapping(MappingRoot mapping,
                                 Object param)
            throws MappingException
Loads the mapping from the specified mapping object if not loaded previously.
Parameters:
mapping - The mapping information.
param - Arbitrary parameter that can be used by subclasses.
Throws:
MappingException - The mapping file is invalid.

resolveRelations

protected abstract void resolveRelations(ClassDescriptor clsDesc)

resolveType

protected final Class resolveType(String typeName)
            throws MappingException
Returns the Java class for the named type. The type name can be one of the accepted short names (e.g. integer) or the full Java class name (e.g. java.lang.Integer). If the short name is used, the primitive type might be returned.

setInternalContext

public void setInternalContext(InternalContext internalContext)

Intalio Inc. (C) 1999-2008. All rights reserved http://www.intalio.com