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.Project;
023: import org.apache.tools.ant.taskdefs.ExecuteJava;
024: import org.apache.tools.ant.types.Commandline;
025:
026: /**
027: * The implementation of the rmic for Kaffe
028: *
029: * @since Ant 1.4
030: */
031: public class KaffeRmic extends DefaultRmicAdapter {
032: // sorted by newest Kaffe version first
033: private static final String[] RMIC_CLASSNAMES = new String[] {
034: "gnu.classpath.tools.rmi.rmic.RMIC",
035: // pre Kaffe 1.1.5
036: "gnu.java.rmi.rmic.RMIC",
037: // pre Kaffe 1.1.2
038: "kaffe.rmi.rmic.RMIC", };
039:
040: /**
041: * the name of this adapter for users to select
042: */
043: public static final String COMPILER_NAME = "kaffe";
044:
045: /** {@inheritDoc} */
046: public boolean execute() throws BuildException {
047: getRmic().log("Using Kaffe rmic", Project.MSG_VERBOSE);
048: Commandline cmd = setupRmicCommand();
049:
050: Class c = getRmicClass();
051: if (c == null) {
052: StringBuffer buf = new StringBuffer(
053: "Cannot use Kaffe rmic, as it"
054: + " is not available. None" + " of ");
055: for (int i = 0; i < RMIC_CLASSNAMES.length; i++) {
056: if (i != 0) {
057: buf.append(", ");
058: }
059:
060: buf.append(RMIC_CLASSNAMES[i]);
061: }
062: buf
063: .append(" have been found. A common solution is to set the"
064: + " environment variable JAVA_HOME or CLASSPATH.");
065: throw new BuildException(buf.toString(), getRmic()
066: .getLocation());
067: }
068:
069: cmd.setExecutable(c.getName());
070: if (!c.getName().equals(
071: RMIC_CLASSNAMES[RMIC_CLASSNAMES.length - 1])) {
072: // only supported since Kaffe 1.1.2
073: cmd.createArgument().setValue("-verbose");
074: getRmic().log(Commandline.describeCommand(cmd));
075: }
076: ExecuteJava ej = new ExecuteJava();
077: ej.setJavaCommand(cmd);
078: return ej.fork(getRmic()) == 0;
079: }
080:
081: /**
082: * test for kaffe being on the system
083: * @return true if kaffe is on the current classpath
084: */
085: public static boolean isAvailable() {
086: return getRmicClass() != null;
087: }
088:
089: /**
090: * tries to load Kaffe RMIC and falls back to the older class name
091: * if necessary.
092: *
093: * @return null if neither class can get loaded.
094: */
095: private static Class getRmicClass() {
096: for (int i = 0; i < RMIC_CLASSNAMES.length; i++) {
097: try {
098: return Class.forName(RMIC_CLASSNAMES[i]);
099: } catch (ClassNotFoundException cnfe) {
100: // Ignore
101: }
102: }
103: return null;
104: }
105: }
|