01: package net.sourceforge.jaxor.example.pico;
02:
03: import net.sourceforge.jaxor.api.EntityInterface;
04: import net.sourceforge.jaxor.api.FinderAdapter;
05: import net.sourceforge.jaxor.api.InstanceFactory;
06: import net.sourceforge.jaxor.api.JaxorContext;
07: import net.sourceforge.jaxor.impl.InstanceFactoryImpl;
08: import org.picocontainer.MutablePicoContainer;
09: import org.picocontainer.PicoContainer;
10: import org.picocontainer.extras.ThrowawayPicoInstantiator;
11:
12: import java.util.HashMap;
13: import java.util.Map;
14: import java.util.List;
15:
16: /**
17: * Created By: Mike
18: * Date: Jan 15, 2004
19: * Time: 10:25:34 PM
20: *
21: * Last Checkin: $Author: mrettig $
22: * Date: $Date: 2004/03/03 03:59:18 $
23: * Revision: $Revision: 1.9 $
24: */
25: public class PicoInstanceFactory implements InstanceFactory {
26:
27: private final Map _finderCache = new HashMap();
28: private final MutablePicoContainer _container;
29:
30: public PicoInstanceFactory(MutablePicoContainer container) {
31: _container = container;
32: }
33:
34: public Object createListImpl(List list, Class listImplClass) {
35: return InstanceFactoryImpl.createListUsingReflection(
36: listImplClass, list);
37: }
38:
39: public EntityInterface createEntity(Class clzz) {
40: //Will create a new entity every time
41: // Entities don't need to be managed by the container we just need the container to resolve dependencies
42: return (EntityInterface) new ThrowawayPicoInstantiator(
43: _container, clzz).getInstance();
44: }
45:
46: public FinderAdapter createFinder(Class clzz, JaxorContext context) {
47: FinderAdapter finder = (FinderAdapter) _finderCache.get(clzz);
48: if (finder == null) {
49: finder = (FinderAdapter) new ThrowawayPicoInstantiator(
50: _container, clzz).getInstance();
51: _finderCache.put(clzz, finder);
52: }
53: return finder;
54: }
55:
56: public PicoContainer getContainer() {
57: return _container;
58: }
59:
60: }
|