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:
019: package org.apache.tools.ant.taskdefs.rmic;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.Task;
023: import org.apache.tools.ant.util.ClasspathUtils;
024:
025: import java.util.Locale;
026:
027: /**
028: * Creates the necessary rmic adapter, given basic criteria.
029: *
030: * @since 1.4
031: */
032: public final class RmicAdapterFactory {
033: /** The error message to be used when the compiler cannot be found. */
034: public static final String ERROR_UNKNOWN_COMPILER = "Class not found: ";
035:
036: /** The error message to be used when the class is not an rmic adapter. */
037: public static final String ERROR_NOT_RMIC_ADAPTER = "Class of unexpected Type: ";
038:
039: /** If the compiler has this name use a default compiler. */
040: public static final String DEFAULT_COMPILER = "default";
041:
042: /** This is a singleton -- can't create instances!! */
043: private RmicAdapterFactory() {
044: }
045:
046: /**
047: * Based on the parameter passed in, this method creates the necessary
048: * factory desired.
049: *
050: * <p>The current mapping for rmic names are as follows:</p>
051: * <ul><li>sun = SUN's rmic
052: * <li>kaffe = Kaffe's rmic
053: * <li><i>a fully quallified classname</i> = the name of a rmic
054: * adapter
055: * <li>weblogic = weblogic compiler
056: * <li>forking = Sun's RMIC by forking a new JVM
057: * </ul>
058: *
059: * @param rmicType either the name of the desired rmic, or the
060: * full classname of the rmic's adapter.
061: * @param task a task to log through.
062: * @return the compiler adapter
063: * @throws BuildException if the rmic type could not be resolved into
064: * a rmic adapter.
065: */
066: public static RmicAdapter getRmic(String rmicType, Task task)
067: throws BuildException {
068: //convert to lower case in the English locale,
069: String compiler = rmicType.toLowerCase(Locale.ENGLISH);
070:
071: //handle default specially by choosing the sun or kaffe compiler
072: if (DEFAULT_COMPILER.equals(compiler) || compiler.length() == 0) {
073: compiler = KaffeRmic.isAvailable() ? KaffeRmic.COMPILER_NAME
074: : SunRmic.COMPILER_NAME;
075: }
076: if (SunRmic.COMPILER_NAME.equals(compiler)) {
077: return new SunRmic();
078: } else if (KaffeRmic.COMPILER_NAME.equals(compiler)) {
079: return new KaffeRmic();
080: } else if (WLRmic.COMPILER_NAME.equals(compiler)) {
081: return new WLRmic();
082: } else if (ForkingSunRmic.COMPILER_NAME.equals(compiler)) {
083: return new ForkingSunRmic();
084: } else if (XNewRmic.COMPILER_NAME.equals(compiler)) {
085: return new XNewRmic();
086: }
087: //no match? ask for the non-lower-cased type
088: return resolveClassName(rmicType);
089: }
090:
091: /**
092: * Tries to resolve the given classname into a rmic adapter.
093: * Throws a fit if it can't.
094: *
095: * @param className The fully qualified classname to be created.
096: * @throws BuildException This is the fit that is thrown if className
097: * isn't an instance of RmicAdapter.
098: */
099: private static RmicAdapter resolveClassName(String className)
100: throws BuildException {
101: return (RmicAdapter) ClasspathUtils.newInstance(className,
102: RmicAdapterFactory.class.getClassLoader(),
103: RmicAdapter.class);
104: }
105: }
|