forked from telosys-tools-beta/java8-commons-T312
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplementationProvider_java.vm
85 lines (76 loc) · 2.87 KB
/
ImplementationProvider_java.vm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Created on $today.date ( Time $today.time )
* Generated by $generator.name ( version $generator.version )
*/
package ${target.javaPackageFromFolder($SRC)};
/**
* Utility class designed to provide the implementation of a given interface. <br>
* The implementation class name is determined using a "pattern" that must be given to the constructor.<br>
*
* @author Telosys
*
*/
public class ImplementationProvider {
private final String pattern ;
/**
* Constructor <br>
*
* @param pattern the implementation class name pattern (supposed to contain 2 "%s") <br>
* for example "%s.impl.jdbc.%sImpl" where <br>
* the first "%s" will be replaced by the interface package name <br>
* the second "%s" will be replaced by the interface simple class name <br>
*/
public ImplementationProvider(String pattern) {
super();
this.pattern = pattern;
}
/**
* Returns a service instance implementing the given interface
* @param interfaceClass
* @return
*/
public final <T> T getServiceInstance(Class<T> interfaceClass) {
String serviceImplementationClassName = buildImplementationClassName(interfaceClass) ;
try {
Class<?> serviceImplementationClass = ImplementationProvider.class.getClassLoader().loadClass(serviceImplementationClassName);
return createServiceInstance(interfaceClass, serviceImplementationClass) ;
} catch (ClassNotFoundException e) {
throw new RuntimeException("Cannot load class " + serviceImplementationClassName, e );
}
}
/**
* Builds the service implementation full class name from the given interface class name
* @param interfaceClass
* @return
*/
private final String buildImplementationClassName(Class<?> interfaceClass) {
Package interfacePackage = interfaceClass.getPackage();
String interfacePackageName = interfacePackage != null ? interfacePackage.getName() : "";
String interfaceSimpleName = interfaceClass.getSimpleName();
String result = String.format(pattern, interfacePackageName, interfaceSimpleName);
return result ;
}
/**
* Creates the service instance using the given class
* @param interfaceClass
* @param implementationClass
* @return
*/
@SuppressWarnings("unchecked")
private final <T> T createServiceInstance(Class<T> interfaceClass, Class<?> implementationClass) {
Object instance = null ;
try {
instance = implementationClass.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException("Cannot create instance for class " + implementationClass.getCanonicalName(), e );
} catch (IllegalAccessException e) {
throw new RuntimeException("Cannot create instance for class " + implementationClass.getCanonicalName(), e );
}
if ( interfaceClass.isInstance(instance) ) {
return (T) instance ;
}
else {
throw new RuntimeException("Class " + implementationClass.getCanonicalName() + " does not implement " + interfaceClass.getSimpleName() );
}
}
}