    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements.  See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache License, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License.  You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the License for the specific language governing permissions and
    015     * limitations under the License.
    016     */
    017
    018    package org.apache.commons.jci.compilers;
    019
    020    import java.util.HashMap;
    021    import java.util.Map;
    022
    023    import org.apache.commons.jci.utils.ConversionUtils;
    024
    025
    026    /**
    027     * Creates JavaCompilers
    028     *
    029     * TODO use META-INF discovery mechanism
    030     *
    031     * @author tcurdt
    032     */
    033    public final class JavaCompilerFactory {
    034
    035        /**
    036         * @deprecated will be remove after the next release, please create an instance yourself
    037         */
    038        @Deprecated
    039        private static final JavaCompilerFactory INSTANCE = new JavaCompilerFactory();
    040
    041        private final Map<String, Class<?>> classCache = new HashMap<String, Class<?>>();
    042
    043        /**
    044         * @deprecated will be remove after the next release, please create an instance yourself
    045         */
    046        @Deprecated
    047        public static JavaCompilerFactory getInstance() {
    048            return JavaCompilerFactory.INSTANCE;
    049        }
    050
    051        /**
    052         * Tries to guess the class name by convention. So for compilers
    053         * following the naming convention
    054         *
    055         *   org.apache.commons.jci.compilers.SomeJavaCompiler
    056         *
    057         * you can use the short-hands "some"/"Some"/"SOME". Otherwise
    058         * you have to provide the full class name. The compiler is
    059         * getting instanciated via (cached) reflection.
    060         *
    061         * @param pHint
    062         * @return JavaCompiler or null
    063         */
    064        public JavaCompiler createCompiler(final String pHint) {
    065
    066            final String className;
    067            if (pHint.indexOf('.') < 0) {
    068                className = "org.apache.commons.jci.compilers." + ConversionUtils.toJavaCasing(pHint) + "JavaCompiler";
    069            } else {
    070                className = pHint;
    071            }
    072
    073            Class<?> clazz = classCache.get(className);
    074
    075            if (clazz == null) {
    076                try {
    077                    clazz = Class.forName(className);
    078                    classCache.put(className, clazz);
    079                } catch (ClassNotFoundException e) {
    080                    clazz = null;
    081                }
    082            }
    083
    084            if (clazz == null) {
    085                return null;
    086            }
    087
    088            try {
    089                return (JavaCompiler) clazz.newInstance();
    090            } catch (Throwable t) {
    091                return null;
    092            }
    093        }
    094
    095    }