org.apache.commons.mapper
Class MapperFactory

java.lang.Object
  extended by org.apache.commons.mapper.MapperFactory

public class MapperFactory
extends Object

MapperFactory is responsible for creating mappers based on domain class names or any other String name. This allows a domain class to be a value bean and delegate data storage responsibility to a specific Mapper subclass.

MapperFactory.getMapper() method uses caching to prevent creating mappers multiple times. This means that a Mapper returned from getMapper() will not be a new instance which could cause problems in a multi-threaded environment (the servlet container environment is multi-threaded). Mappers returned by the MapperFactory should not use instance variables to maintain state, rather, they should use local method variables so they are thread-safe.

Note that you do not need to subclass MapperFactory to specify the specific Mapper implementation classes to return from getMapper(). Because Mapper implementation classes are defined in a Map, you can have any combination of Mapper types in use at the same time. For example, you could have EjbPersonMapper using an EJB to persist Person objects and a JdbcOrderMapper using JDBC calls to persist Order objects. You can register MapperFactoryListener implementations with the factory to initialize the various types of Mappers when they are created.

See Also:
Mapper, MapperFactoryListener

Field Summary
protected  ObjectFactory factory
          This factory actually creates the mappers.
 
Constructor Summary
MapperFactory(Map map)
          Create a new MapperFactory with the mappings contained in the given Map.
 
Method Summary
 void addMapperFactoryListener(MapperFactoryListener listener)
          Register a listener with this MapperFactory to receive notifications of events.
 Mapper getMapper(Class mappedClass)
          Factory method for getting the mapper associated with the given class.
 Mapper getMapper(String name)
          Factory method for getting the mapper associated with the given class.
 void removeMapperFactoryListener(MapperFactoryListener listener)
          Unregister a listener from this MapperFactory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

factory

protected ObjectFactory factory
This factory actually creates the mappers.

Constructor Detail

MapperFactory

public MapperFactory(Map map)
Create a new MapperFactory with the mappings contained in the given Map.

Parameters:
map - Any map containing logical names (often domain class names) as the keys and mapper class names as the values.
Throws:
IllegalArgumentException - if there is a problem finding the mapped classes.
Method Detail

getMapper

public Mapper getMapper(Class mappedClass)
Factory method for getting the mapper associated with the given class.

Parameters:
mappedClass - The class whose mapper should be returned
Returns:
Mapper the mapper associated with the given class
Throws:
IllegalArgumentException - if the given Class was not found in the map or there was an error creating an instance of the Mapper. This is usually caused by a missing public no-arg constructor.

getMapper

public Mapper getMapper(String name)
Factory method for getting the mapper associated with the given class.

Parameters:
name - The name of the mapper to be returned.
Returns:
Mapper the mapper associated with the given name.
Throws:
IllegalArgumentException - if the given name was not found in the map or there was an error creating an instance of the Mapper. This is usually caused by a missing public no-arg constructor.

addMapperFactoryListener

public void addMapperFactoryListener(MapperFactoryListener listener)
Register a listener with this MapperFactory to receive notifications of events.

Parameters:
listener -

removeMapperFactoryListener

public void removeMapperFactoryListener(MapperFactoryListener listener)
Unregister a listener from this MapperFactory.

Parameters:
listener -


Copyright © 2001-2010 The Apache Software Foundation. All Rights Reserved.