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      @Deprecated
39      private static final JavaCompilerFactory INSTANCE = new JavaCompilerFactory();
40  
41      private final Map<String, Class<?>> classCache = new HashMap<String, Class<?>>();
42      
43      /**
44       * @deprecated will be remove after the next release, please create an instance yourself
45       */
46      @Deprecated
47      public static JavaCompilerFactory getInstance() {
48          return JavaCompilerFactory.INSTANCE;
49      }
50  
51      /**
52       * Tries to guess the class name by convention. So for compilers
53       * following the naming convention
54       * 
55       *   org.apache.commons.jci.compilers.SomeJavaCompiler
56       *   
57       * you can use the short-hands "some"/"Some"/"SOME". Otherwise
58       * you have to provide the full class name. The compiler is
59       * getting instanciated via (cached) reflection.
60       * 
61       * @param pHint
62       * @return JavaCompiler or null
63       */
64      public JavaCompiler createCompiler(final String pHint) {
65          
66          final String className;
67          if (pHint.indexOf('.') < 0) {
68              className = "org.apache.commons.jci.compilers." + ConversionUtils.toJavaCasing(pHint) + "JavaCompiler";
69          } else {
70              className = pHint;
71          }
72          
73          Class<?> clazz = classCache.get(className);
74          
75          if (clazz == null) {
76              try {
77                  clazz = Class.forName(className);
78                  classCache.put(className, clazz);
79              } catch (ClassNotFoundException e) {
80                  clazz = null;
81              }
82          }
83  
84          if (clazz == null) {
85              return null;
86          }
87          
88          try {
89              return (JavaCompiler) clazz.newInstance();
90          } catch (Throwable t) {
91              return null;
92          }
93      }
94      
95  }