1 package ar.com.jiji.kaya.spring;
2
3 import ar.com.jiji.kaya.KayaException;
4
5 /**
6 * Una implementacion de <code>DaoInstantiator</code> que crea objetos con
7 * refleccion. Chequea que la clase implemente la interfaz y luego crea un objeto
8 * de esa clase.
9 *
10 * @author lparra
11 *
12 */
13 public class SimpleDaoInstantiator implements DaoInstantiator {
14
15 public Object instantiate(String clazzImpl, String interfaceClass)
16 throws ClassNotFoundException, KayaException,
17 InstantiationException, IllegalAccessException {
18 Class c;
19 c = Class.forName(clazzImpl);
20 Class[] interfaces = c.getInterfaces();
21 Class interfaceClazz = Class.forName(interfaceClass);
22
23
24 int pos = -1;
25 for (int i = 0; i < interfaces.length && pos == -1; i++) {
26 if (interfaces[i].equals(interfaceClazz))
27 pos = i;
28 }
29
30 if (pos != -1)
31 return c.newInstance();
32
33 throw new KayaException("dao " + clazzImpl
34 + " does not implement interface " + interfaceClass);
35 }
36
37 }