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: package org.apache.harmony.tools.ijh.anttask;
019:
020: import java.io.File;
021:
022: import org.apache.tools.ant.BuildException;
023: import org.apache.tools.ant.launch.Locator;
024: import org.apache.tools.ant.taskdefs.ExecuteJava;
025: import org.apache.tools.ant.taskdefs.optional.Javah;
026: import org.apache.tools.ant.taskdefs.optional.javah.JavahAdapter;
027: import org.apache.tools.ant.types.Commandline;
028: import org.apache.tools.ant.types.Path;
029:
030: /**
031: * Adapter to org.apache.harmony.tools.ijh.Main.
032: *
033: * This class depends on Apache Ant tool 1.6.5 or later.
034: * Please see http://ant.apache.org for more information about this tool.
035: */
036: public class Adapter implements JavahAdapter {
037:
038: /**
039: * Runs our implementation of the <code>javah</code>.
040: *
041: * @param javah is the Javah task.
042: * @return <code>true</code> if there is no error; Otherwise
043: * <code>false</code>.
044: */
045: public boolean compile(Javah javah) throws BuildException {
046: Class clss = null;
047:
048: // Try to load the main class of the tool.
049: try {
050: clss = Class.forName("org.apache.harmony.tools.ijh.Main");
051: } catch (ClassNotFoundException e) {
052: throw new BuildException("Can't load a class", e, javah
053: .getLocation());
054: }
055:
056: // Prepare a command to launch the tool.
057: Commandline cmd = setupCommand(javah);
058: cmd.setExecutable(clss.getName());
059:
060: // Prepare a java command.
061: ExecuteJava java = new ExecuteJava();
062: java.setJavaCommand(cmd);
063:
064: // Find a file or a jar which represents the required class.
065: File file = Locator.getClassSource(clss);
066: if (file != null) {
067: // The found file should be included into the class path.
068: Path classpath = new Path(javah.getProject(), file
069: .getPath());
070:
071: // Try to load BCEL's ClassPath utility class.
072: try {
073: clss = Class.forName("org.apache.bcel.util.ClassPath");
074: } catch (ClassNotFoundException e) {
075: throw new BuildException("Can't load BCEL", e, javah
076: .getLocation());
077: }
078:
079: // Find a file or a jar which represents the required class.
080: file = Locator.getClassSource(clss);
081: if (file != null) {
082: // Add the found file to the class path.
083: classpath.append(new Path(javah.getProject(), file
084: .getPath()));
085: }
086:
087: // Set the class path.
088: java.setClasspath(classpath);
089: }
090:
091: // Run the java command.
092: return java.fork(javah) == 0;
093: }
094:
095: /**
096: * Prepare a command line that includes all parameters.
097: *
098: * @param javah is the Javah task.
099: * @return The prepared command line.
100: */
101: private Commandline setupCommand(Javah javah) {
102: Commandline cmd = new Commandline();
103:
104: // Add a destination directory if any.
105: if (javah.getDestdir() != null) {
106: cmd.createArgument().setValue("-d");
107: cmd.createArgument().setFile(javah.getDestdir());
108: }
109:
110: // Add an output file if any.
111: if (javah.getOutputfile() != null) {
112: cmd.createArgument().setValue("-o");
113: cmd.createArgument().setFile(javah.getOutputfile());
114: }
115:
116: // Add a class path if any.
117: if (javah.getClasspath() != null) {
118: cmd.createArgument().setValue("-classpath");
119: cmd.createArgument().setPath(javah.getClasspath());
120: }
121:
122: // Add a verbose flag if any.
123: if (javah.getVerbose()) {
124: cmd.createArgument().setValue("-verbose");
125: }
126:
127: // Add a boot class path if any.
128: Path bootClasspath = new Path(javah.getProject());
129: if (javah.getBootclasspath() != null) {
130: bootClasspath.append(javah.getBootclasspath());
131: }
132: if (bootClasspath.size() > 0) {
133: cmd.createArgument().setValue("-bootclasspath");
134: cmd.createArgument().setPath(bootClasspath);
135: }
136:
137: // Add settings given as nested arg elements.
138: cmd.addArguments(javah.getCurrentArgs());
139:
140: // Add and log the parameters and source files.
141: javah.logAndAddFiles(cmd);
142: return cmd;
143: }
144: }
|