001: /**
002: * InstantJ
003: *
004: * Copyright (C) 2002 Nils Meier
005: * Additional changes (C) 2002 Andy Thomas
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: */package instantj.compile;
018:
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.Map;
023:
024: /**
025: * The abstract Compiler base - it relies on a concrete sub-class
026: * that provides the compilation functionality. Use compile()
027: * to compile source and get a new Class.
028: *
029: * @see instantj.compile.sun.SunSourceCompiler
030: * @author <A href="mailto:nils@meiers.net">Nils Meier</A>
031: */
032: public abstract class Compiler {
033:
034: /** we keep a reference to one instance */
035: private static Compiler instance;
036:
037: /** the compiler key */
038: private static String DEFAULT_COMPILER_KEY = "instantj.compile.compiler";
039:
040: /** the compiler classname */
041: private static String DEFAULT_COMPILER = "instantj.compile.sun.SunSourceCompiler";
042:
043: /**
044: * Access a sub-class instance
045: */
046: private static Compiler getInstance() {
047: // compiler switch?
048: try {
049: String s = System.getProperty(DEFAULT_COMPILER_KEY);
050: if (s != null && s.length() > 0)
051: DEFAULT_COMPILER = s;
052: } catch (Throwable t) {
053: }
054: // create new?
055: if (instance == null)
056: try {
057: instance = (Compiler) Class.forName(DEFAULT_COMPILER)
058: .newInstance();
059: } catch (Throwable t) {
060: throw new CompilerError("Initializing compiler "
061: + DEFAULT_COMPILER + " failed with "
062: + t.getClass().getName() + "/" + t.getMessage());
063: }
064: // done
065: return instance;
066: }
067:
068: /**
069: * Compiles source into a Class
070: * @param source the input to compile
071: * @param verbose whether to make compiler messages verbose
072: * @return the compiled class
073: */
074: public static CompiledClass compile(Source source, boolean verbose)
075: throws CompilationFailedException {
076: return compile(source, verbose, null);
077: }
078:
079: /**
080: * Compiles source into a Class
081: * @param source the input to compile
082: * @param verbose whether to make compiler messages verbose
083: * @param options compiler switches (depending on compiler implementation, 0=none, Integer.MAX_VALUE=all)
084: * @return the compiled class
085: */
086: public static CompiledClass compile(Source source, boolean verbose,
087: List options) throws CompilationFailedException {
088: return compile(source, verbose, options, null);
089: }
090:
091: /**
092: * Compiles source into a Class
093: * @param source the input to compile
094: * @param verbose whether to make compiler messages verbose
095: * @param options compiler switches (depending on compiler implementation, 0=none, Integer.MAX_VALUE=all)
096: * @param deps dependencies to other CompiledClasses
097: * @return the compiled class
098: */
099: public static CompiledClass compile(Source source, boolean verbose,
100: List options, Collection deps)
101: throws CompilationFailedException {
102: // Compile it
103: return getInstance().compileImpl(source, verbose, options,
104: CompiledClass.map(new HashMap(), deps));
105: }
106:
107: /**
108: * Abstract compilation method that has to be provided by the concrete type
109: */
110: protected abstract CompiledClass compileImpl(Source source,
111: boolean verbose, List options, Map deps)
112: throws CompilationFailedException;
113:
114: } //Compiler
|