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 java.io.IOException;
022: import java.io.OutputStream;
023: import java.lang.reflect.Constructor;
024: import java.lang.reflect.Method;
025: import org.apache.tools.ant.BuildException;
026: import org.apache.tools.ant.Project;
027: import org.apache.tools.ant.taskdefs.LogOutputStream;
028: import org.apache.tools.ant.types.Commandline;
029:
030: /**
031: * The implementation of the rmic for SUN's JDK.
032: *
033: * @since Ant 1.4
034: */
035: public class SunRmic extends DefaultRmicAdapter {
036:
037: /**
038: * name of the class
039: */
040: public static final String RMIC_CLASSNAME = "sun.rmi.rmic.Main";
041:
042: /**
043: * the name of this adapter for users to select
044: */
045: public static final String COMPILER_NAME = "sun";
046:
047: /**
048: * name of the executable
049: */
050: public static final String RMIC_EXECUTABLE = "rmic";
051: /** Error message to use with the sun rmic is not the classpath. */
052: public static final String ERROR_NO_RMIC_ON_CLASSPATH = "Cannot use SUN rmic, as it is not "
053: + "available. A common solution is to "
054: + "set the environment variable "
055: + "JAVA_HOME or CLASSPATH.";
056: /** Error message to use when there is an error starting the sun rmic compiler */
057: public static final String ERROR_RMIC_FAILED = "Error starting SUN rmic: ";
058:
059: /**
060: * Run the rmic compiler.
061: * @return true if the compilation succeeded
062: * @throws BuildException on error
063: */
064: public boolean execute() throws BuildException {
065: getRmic().log("Using SUN rmic compiler", Project.MSG_VERBOSE);
066: Commandline cmd = setupRmicCommand();
067:
068: // Create an instance of the rmic, redirecting output to
069: // the project log
070: LogOutputStream logstr = new LogOutputStream(getRmic(),
071: Project.MSG_WARN);
072:
073: try {
074: Class c = Class.forName(RMIC_CLASSNAME);
075: Constructor cons = c.getConstructor(new Class[] {
076: OutputStream.class, String.class });
077: Object rmic = cons.newInstance(new Object[] { logstr,
078: "rmic" });
079:
080: Method doRmic = c.getMethod("compile",
081: new Class[] { String[].class });
082: Boolean ok = (Boolean) doRmic.invoke(rmic,
083: (new Object[] { cmd.getArguments() }));
084: return ok.booleanValue();
085: } catch (ClassNotFoundException ex) {
086: throw new BuildException(ERROR_NO_RMIC_ON_CLASSPATH,
087: getRmic().getLocation());
088: } catch (Exception ex) {
089: if (ex instanceof BuildException) {
090: throw (BuildException) ex;
091: } else {
092: throw new BuildException(ERROR_RMIC_FAILED, ex,
093: getRmic().getLocation());
094: }
095: } finally {
096: try {
097: logstr.close();
098: } catch (IOException e) {
099: throw new BuildException(e);
100: }
101: }
102: }
103: }
|