KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > rmic > SunRmic


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.rmic;
20
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.lang.reflect.Constructor JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Project;
27 import org.apache.tools.ant.taskdefs.LogOutputStream;
28 import org.apache.tools.ant.types.Commandline;
29
30 /**
31  * The implementation of the rmic for SUN's JDK.
32  *
33  * @since Ant 1.4
34  */

35 public class SunRmic extends DefaultRmicAdapter {
36
37     /**
38      * name of the class
39      */

40     public static final String JavaDoc RMIC_CLASSNAME = "sun.rmi.rmic.Main";
41
42     /**
43      * the name of this adapter for users to select
44      */

45     public static final String JavaDoc COMPILER_NAME = "sun";
46
47     /**
48      * name of the executable
49      */

50     public static final String JavaDoc RMIC_EXECUTABLE = "rmic";
51     /** Error message to use with the sun rmic is not the classpath. */
52     public static final String JavaDoc ERROR_NO_RMIC_ON_CLASSPATH = "Cannot use SUN rmic, as it is not "
53                                          + "available. A common solution is to "
54                                          + "set the environment variable "
55                                          + "JAVA_HOME or CLASSPATH.";
56     /** Error message to use when there is an error starting the sun rmic compiler */
57     public static final String JavaDoc ERROR_RMIC_FAILED = "Error starting SUN rmic: ";
58
59     /**
60      * Run the rmic compiler.
61      * @return true if the compilation succeeded
62      * @throws BuildException on error
63      */

64     public boolean execute() throws BuildException {
65         getRmic().log("Using SUN rmic compiler", Project.MSG_VERBOSE);
66         Commandline cmd = setupRmicCommand();
67
68         // Create an instance of the rmic, redirecting output to
69
// the project log
70
LogOutputStream logstr = new LogOutputStream(getRmic(),
71                                                      Project.MSG_WARN);
72
73         try {
74             Class JavaDoc c = Class.forName(RMIC_CLASSNAME);
75             Constructor JavaDoc cons
76                 = c.getConstructor(new Class JavaDoc[] {OutputStream JavaDoc.class, String JavaDoc.class});
77             Object JavaDoc rmic = cons.newInstance(new Object JavaDoc[] {logstr, "rmic"});
78
79             Method JavaDoc doRmic = c.getMethod("compile",
80                                         new Class JavaDoc [] {String JavaDoc[].class});
81             Boolean JavaDoc ok =
82                 (Boolean JavaDoc) doRmic.invoke(rmic,
83                                        (new Object JavaDoc[] {cmd.getArguments()}));
84             return ok.booleanValue();
85         } catch (ClassNotFoundException JavaDoc ex) {
86             throw new BuildException(ERROR_NO_RMIC_ON_CLASSPATH,
87                                      getRmic().getLocation());
88         } catch (Exception JavaDoc ex) {
89             if (ex instanceof BuildException) {
90                 throw (BuildException) ex;
91             } else {
92                 throw new BuildException(ERROR_RMIC_FAILED,
93                                          ex, getRmic().getLocation());
94             }
95         } finally {
96             try {
97                 logstr.close();
98             } catch (IOException JavaDoc e) {
99                 throw new BuildException(e);
100             }
101         }
102     }
103 }
104
Popular Tags