blob: fa8c6f8fa7a29118e84ae7e4cbc223d25376d300 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.myfaces.extensions.scripting.cdi.compiler;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* <p>This factory chooses and creates an instance of the according compiler
* implementation based on the current JVM for you. If you're using a Java 6
* VM, it will return a compiler using the JSR-199 API, otherwise it will
* return a compiler that uses the JavaC tool.</p>
*/
public class CompilerFactory {
/**
* The logger instance for this class.
*/
private static final Logger logger = Logger.getLogger(CompilerFactory.class.getName());
// ------------------------------------------ Public methods
/**
* <p>Factory method that creates a new Java compiler depending on the
* Java runtime that this application is running on. That means, if the
* Java runtime supports the JSR-199 API (i.e. it's at least a Java 6
* runtime) this API will be used. Otherwise a compiler will be returned
* that tries to use some internal JDK classes.</p>
*
* @return a new Java compiler depending on the Java runtime
*/
public static Compiler createCompiler() {
boolean isJava15 = System.getProperty("java.version").indexOf("1.5.") != -1;
if (!isJava15) {
// If the Java version is not 1.5, it's safe to assume that
// it's at least 1.6 as otherwise you wouldn't even be able
// to load the module (the target VM is set to 1.5 after all).
return Jsr199CompilerLoader.createJsr199Compiler();
} else {
if (logger.isLoggable(Level.WARNING) &&
!System.getProperty("java.vendor").contains("Sun Microsystems")) {
logger.warning("This application is running on a Java runtime that neither supports the JSR-199 API " +
"nor is it distributed by Sun Microsystems. However, the compiler implementation that will " +
"be used depends on internal classes in the package 'com.sun.tools.javac' so compilation " +
"is likely to fail! Be sure that the Java runtime that you're using provides these internal " +
"classes!");
}
return new JavacCompiler();
}
}
/**
* <p>Utility class that loads the JSR-199 compiler implementation. The sole purpose
* of this class is to ensure that the current class loader doesn't try to load the
* class 'Jsr199Compiler' until it's sure that we're running on a JVM 6 platform.</p>
*/
private static class Jsr199CompilerLoader {
/**
* <p>Utility method that creates a new compiler implementation that uses the
* JSR-199 Compiler API.</p>
*
* @return a new compiler implementation that uses the JSR-199 Compiler API
*/
public static Compiler createJsr199Compiler() {
return new Jsr199Compiler();
}
}
}