View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      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  /**
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  }