001: /**
002: * MVEL (The MVFLEX Expression Language)
003: *
004: * Copyright (C) 2007 Christopher Brock, MVFLEX/Valhalla Project and the Codehaus
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: */package org.mvel.optimizers;
019:
020: import org.mvel.CompileException;
021: import org.mvel.optimizers.impl.asm.ASMAccessorOptimizer;
022: import org.mvel.optimizers.impl.refl.ReflectiveAccessorOptimizer;
023:
024: import static java.lang.Thread.currentThread;
025: import java.util.HashMap;
026: import java.util.Map;
027:
028: public class OptimizerFactory {
029: public static String SAFE_REFLECTIVE = "reflective";
030:
031: private static String defaultOptimizer = "ASM";
032: private static final Map<String, AccessorOptimizer> accessorCompilers = new HashMap<String, AccessorOptimizer>();
033:
034: private static ThreadLocal<Class<? extends AccessorOptimizer>> threadOptimizer = new ThreadLocal<Class<? extends AccessorOptimizer>>();
035:
036: static {
037: accessorCompilers.put(SAFE_REFLECTIVE,
038: new ReflectiveAccessorOptimizer());
039: /**
040: * By default, activate the JIT if ASM is present in the classpath
041: */
042: try {
043: currentThread().getContextClassLoader().loadClass(
044: "org.mvel.asm.ClassWriter");
045: accessorCompilers.put("ASM", new ASMAccessorOptimizer());
046: }
047:
048: catch (ClassNotFoundException e) {
049: defaultOptimizer = SAFE_REFLECTIVE;
050: } catch (Throwable e) {
051: e.printStackTrace();
052: System.err
053: .println("[MVEL] Notice: Possible incorrect version of ASM present (3.0 required). "
054: + "Disabling JIT compiler. Reflective Optimizer will be used.");
055: defaultOptimizer = SAFE_REFLECTIVE;
056: }
057:
058: if (Boolean.getBoolean("mvel.disable.jit"))
059: defaultOptimizer = SAFE_REFLECTIVE;
060: }
061:
062: public static AccessorOptimizer getDefaultAccessorCompiler() {
063: try {
064: return accessorCompilers.get(defaultOptimizer).getClass()
065: .newInstance();
066: } catch (Exception e) {
067: throw new CompileException(
068: "unable to instantiate accessor compiler", e);
069: }
070: }
071:
072: public static AccessorOptimizer getAccessorCompiler(String name) {
073: try {
074: return accessorCompilers.get(name).getClass().newInstance();
075: } catch (Exception e) {
076: throw new CompileException(
077: "unable to instantiate accessor compiler", e);
078: }
079: }
080:
081: public static AccessorOptimizer getThreadAccessorOptimizer() {
082: // if (threadOptimizer == null) {
083: // threadOptimizer = new ThreadLocal<Class<? extends AccessorOptimizer>>();
084: // }
085: if (threadOptimizer.get() == null) {
086: threadOptimizer
087: .set(getDefaultAccessorCompiler().getClass());
088: }
089:
090: try {
091: return threadOptimizer.get().newInstance();
092: } catch (Exception e) {
093: throw new CompileException(
094: "unable to instantiate accessor compiler", e);
095: }
096: }
097:
098: public static void setThreadAccessorOptimizer(
099: Class<? extends AccessorOptimizer> optimizer) {
100: // if (threadOptimizer == null) {
101: // threadOptimizer = new ThreadLocal<Class<? extends AccessorOptimizer>>();
102: // }
103: if (optimizer == null)
104: throw new RuntimeException("null optimizer");
105: threadOptimizer.set(optimizer);
106: }
107:
108: public static void setDefaultOptimizer(String name) {
109: try {
110: //noinspection unchecked
111: setThreadAccessorOptimizer(accessorCompilers.get(
112: defaultOptimizer = name).getClass());
113: } catch (Exception e) {
114: throw new CompileException(
115: "unable to instantiate accessor compiler", e);
116:
117: }
118: }
119: }
|