1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.taxeditor.singlesource;
|
5
|
|
6
|
import java.lang.reflect.Constructor;
|
7
|
import java.lang.reflect.InvocationTargetException;
|
8
|
import java.util.ArrayList;
|
9
|
|
10
|
import javax.naming.OperationNotSupportedException;
|
11
|
|
12
|
import org.apache.log4j.Logger;
|
13
|
|
14
|
/**
|
15
|
* @author Lutz Suhrbier
|
16
|
*
|
17
|
*/
|
18
|
public class ImplementationLoader {
|
19
|
private static final Logger logger = Logger.getLogger(ImplementationLoader.class);
|
20
|
|
21
|
public static Object newInstance(final Class<?> type, final Object... parameter) {
|
22
|
String className = type.getName() + "Impl";
|
23
|
Object result = null;
|
24
|
logger.debug("Loading class " + className);
|
25
|
Class clazz;
|
26
|
try {
|
27
|
clazz = type.getClassLoader().loadClass(className);
|
28
|
if( clazz == null )
|
29
|
logger.error("Class not loaded " + className + ".");
|
30
|
else
|
31
|
logger.debug("Class loaded " + clazz.getName() + ".");
|
32
|
|
33
|
if (parameter.length > 0) {
|
34
|
ArrayList<Class<?>> parameterList = new ArrayList<Class<?>>();
|
35
|
for (Object o : parameter) {
|
36
|
parameterList.add(o.getClass());
|
37
|
}
|
38
|
Class<?>[] parameterTypes = (Class<?>[]) parameterList.toArray(new Class<?>[parameter.length]);
|
39
|
Constructor constructor = getPossibleConstructor(clazz, parameterTypes);
|
40
|
if (constructor == null) {
|
41
|
logger.error("No constructor found for class " + className +".");
|
42
|
logger.error("ParameterTypes:");
|
43
|
for (Class<?> c: parameterTypes) {
|
44
|
logger.debug(c.getName());
|
45
|
}
|
46
|
}
|
47
|
else
|
48
|
logger.debug("Constructor " + constructor + " found for class " + className);
|
49
|
|
50
|
result = constructor.newInstance(parameter);
|
51
|
} else
|
52
|
result = clazz.newInstance();
|
53
|
} catch (IllegalArgumentException e) {
|
54
|
logger.error("ImplementationLoader.newInstance(): " + e.getMessage());
|
55
|
} catch (InstantiationException e) {
|
56
|
logger.error("ImplementationLoader.newInstance(): " + e.getMessage());
|
57
|
} catch (IllegalAccessException e) {
|
58
|
logger.error("ImplementationLoader.newInstance(): " + e.getMessage());
|
59
|
} catch (InvocationTargetException e) {
|
60
|
logger.error("ImplementationLoader.newInstance(): " + e.getMessage());
|
61
|
} catch (ClassNotFoundException e) {
|
62
|
logger.error("ImplementationLoader.newInstance(): " + e.getMessage());
|
63
|
}
|
64
|
return result;
|
65
|
}
|
66
|
|
67
|
public static Constructor getPossibleConstructor(Class motherClass, Class[] pars) {
|
68
|
|
69
|
Constructor[] ctors = motherClass.getConstructors();
|
70
|
for (Constructor c : ctors) {
|
71
|
Class[] cpars = c.getParameterTypes();
|
72
|
if (cpars.length != pars.length)
|
73
|
continue;
|
74
|
boolean found = true;
|
75
|
|
76
|
for (int i = 0; i < cpars.length; ++i) {
|
77
|
if (!cpars[i].isAssignableFrom(pars[i])) {
|
78
|
found = false;
|
79
|
break;
|
80
|
}
|
81
|
}
|
82
|
|
83
|
if (found)
|
84
|
return c;
|
85
|
}
|
86
|
return null;
|
87
|
}
|
88
|
}
|