01: package net.sourceforge.jaxor.impl;
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.util.JavaBean;
08: import net.sourceforge.jaxor.util.SystemException;
09:
10: import java.lang.reflect.Constructor;
11: import java.lang.reflect.InvocationTargetException;
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: 9:13:14 PM
20: *
21: * Last Checkin: $Author: mrettig $
22: * Date: $Date: 2004/03/03 03:59:19 $
23: * Revision: $Revision: 1.5 $
24: */
25: public class InstanceFactoryImpl implements InstanceFactory {
26:
27: private final Map _finderCache = new HashMap();
28:
29: public EntityInterface createEntity(Class clzz) {
30: JavaBean bean = new JavaBean(clzz);
31: return (EntityInterface) bean.getWrappedObject();
32: }
33:
34: public FinderAdapter createFinder(Class clzz, JaxorContext context) {
35: FinderAdapter adapter = (FinderAdapter) _finderCache.get(clzz);
36: if (adapter == null) {
37: adapter = createFinderObject(clzz, context);
38: _finderCache.put(clzz, adapter);
39: }
40: return adapter;
41: }
42:
43: public Object createListImpl(List list, Class clzz) {
44: return createListUsingReflection(clzz, list);
45: }
46:
47: public static Object createListUsingReflection(Class clzz, List list) {
48: try {
49: Constructor conn = clzz
50: .getConstructor(new Class[] { List.class });
51: return conn.newInstance(new Object[] { list });
52: } catch (NoSuchMethodException e) {
53: throw new SystemException(e);
54: } catch (SecurityException e) {
55: throw new SystemException(e);
56: } catch (InstantiationException e) {
57: throw new SystemException(e);
58: } catch (IllegalAccessException e) {
59: throw new SystemException(e);
60: } catch (IllegalArgumentException e) {
61: throw new SystemException(e);
62: } catch (InvocationTargetException e) {
63: throw new SystemException(e);
64: }
65: }
66:
67: public FinderAdapter createFinderObject(Class clzz,
68: JaxorContext context) {
69: try {
70: Constructor conn = clzz
71: .getConstructor(new Class[] { JaxorContext.class });
72: return (FinderAdapter) conn
73: .newInstance(new Object[] { context });
74: } catch (NoSuchMethodException e) {
75: throw new SystemException(e);
76: } catch (SecurityException e) {
77: throw new SystemException(e);
78: } catch (InstantiationException e) {
79: throw new SystemException(e);
80: } catch (IllegalAccessException e) {
81: throw new SystemException(e);
82: } catch (IllegalArgumentException e) {
83: throw new SystemException(e);
84: } catch (InvocationTargetException e) {
85: throw new SystemException(e);
86: }
87: }
88:
89: }
|