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.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: * 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.apache.commons.jci.compilers."
66: + ConversionUtils.toJavaCasing(pHint)
67: + "JavaCompiler";
68: } else {
69: className = pHint;
70: }
71:
72: Class clazz = (Class) classCache.get(className);
73:
74: if (clazz == null) {
75: try {
76: clazz = Class.forName(className);
77: classCache.put(className, clazz);
78: } catch (ClassNotFoundException e) {
79: clazz = null;
80: }
81: }
82:
83: if (clazz == null) {
84: return null;
85: }
86:
87: try {
88: return (JavaCompiler) clazz.newInstance();
89: } catch (Throwable t) {
90: return null;
91: }
92: }
93:
94: }
|