01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.drools.commons.jci.compilers;
19:
20: import java.util.HashMap;
21: import java.util.Map;
22:
23: import org.drools.util.ClassUtils;
24:
25: /**
26: * Creates JavaCompilers
27: *
28: * TODO use META-INF discovery mechanism
29: *
30: * @author tcurdt
31: */
32: public final class JavaCompilerFactory {
33:
34: /**
35: * @deprecated will be remove after the next release, please create an instance yourself
36: */
37: private static final JavaCompilerFactory INSTANCE = new JavaCompilerFactory();
38:
39: private final Map classCache = new HashMap();
40:
41: /**
42: * @deprecated will be remove after the next release, please create an instance yourself
43: */
44: public static JavaCompilerFactory getInstance() {
45: return JavaCompilerFactory.INSTANCE;
46: }
47:
48: /**
49: * Tries to guess the class name by convention. So for compilers
50: * following the naming convention
51: *
52: * org.apache.commons.jci.compilers.SomeJavaCompiler
53: *
54: * you can use the short-hands "some"/"Some"/"SOME". Otherwise
55: * you have to provide the full class name. The compiler is
56: * getting instanciated via (cached) reflection.
57: *
58: * @param pHint
59: * @return JavaCompiler or null
60: */
61: public JavaCompiler createCompiler(final String pHint) {
62:
63: final String className;
64: if (pHint.indexOf('.') < 0) {
65: className = "org.drools.commons.jci.compilers."
66: + ClassUtils.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: }
|