1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jci.compilers;
19
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.apache.commons.jci.utils.ConversionUtils;
24
25
26 /**
27 * Creates JavaCompilers
28 *
29 * TODO use META-INF discovery mechanism
30 *
31 * @author tcurdt
32 */
33 public final class JavaCompilerFactory {
34
35 /**
36 * @deprecated will be remove after the next release, please create an instance yourself
37 */
38 private static final JavaCompilerFactory INSTANCE = new JavaCompilerFactory();
39
40 private final Map classCache = new HashMap();
41
42 /**
43 * @deprecated will be remove after the next release, please create an instance yourself
44 */
45 public static JavaCompilerFactory getInstance() {
46 return JavaCompilerFactory.INSTANCE;
47 }
48
49 /**
50 * Tries to guess the class name by convention. So for compilers
51 * following the naming convention
52 *
53 * org.apache.commons.jci.compilers.SomeJavaCompiler
54 *
55 * you can use the short-hands "some"/"Some"/"SOME". Otherwise
56 * you have to provide the full class name. The compiler is
57 * getting instanciated via (cached) reflection.
58 *
59 * @param pHint
60 * @return JavaCompiler or null
61 */
62 public JavaCompiler createCompiler(final String pHint) {
63
64 final String className;
65 if (pHint.indexOf('.') < 0) {
66 className = "org.apache.commons.jci.compilers." + ConversionUtils.toJavaCasing(pHint) + "JavaCompiler";
67 } else {
68 className = pHint;
69 }
70
71 Class clazz = (Class) classCache.get(className);
72
73 if (clazz == null) {
74 try {
75 clazz = Class.forName(className);
76 classCache.put(className, clazz);
77 } catch (ClassNotFoundException e) {
78 clazz = null;
79 }
80 }
81
82 if (clazz == null) {
83 return null;
84 }
85
86 try {
87 return (JavaCompiler) clazz.newInstance();
88 } catch (Throwable t) {
89 return null;
90 }
91 }
92
93 }