KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
22 import java.util.Random JavaDoc;
23 import java.util.Vector JavaDoc;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.taskdefs.Rmic;
26 import org.apache.tools.ant.types.Commandline;
27 import org.apache.tools.ant.types.Path;
28 import org.apache.tools.ant.util.FileNameMapper;
29
30 /**
31  * This is the default implementation for the RmicAdapter interface.
32  * Currently, this is a cut-and-paste of the original rmic task and
33  * DefaultCopmpilerAdapter.
34  *
35  * @since Ant 1.4
36  */

37 public abstract class DefaultRmicAdapter implements RmicAdapter {
38
39     private Rmic attributes;
40     private FileNameMapper mapper;
41     private static final Random JavaDoc RAND = new Random JavaDoc();
42     /** suffix denoting a stub file */
43     public static final String JavaDoc RMI_STUB_SUFFIX = "_Stub";
44     /** suffix denoting a skel file */
45     public static final String JavaDoc RMI_SKEL_SUFFIX = "_Skel";
46     /** suffix denoting a tie file */
47     public static final String JavaDoc RMI_TIE_SUFFIX = "_Tie";
48     /** arg for compat */
49     public static final String JavaDoc STUB_COMPAT = "-vcompat";
50     /** arg for 1.1 */
51     public static final String JavaDoc STUB_1_1 = "-v1.1";
52     /** arg for 1.2 */
53     public static final String JavaDoc STUB_1_2 = "-v1.2";
54
55     /**
56      * Default constructor
57      */

58     public DefaultRmicAdapter() {
59     }
60
61     /**
62      * Sets Rmic attributes
63      * @param attributes the rmic attributes
64      */

65     public void setRmic(final Rmic attributes) {
66         this.attributes = attributes;
67         mapper = new RmicFileNameMapper();
68     }
69
70     /**
71      * Get the Rmic attributes
72      * @return the attributes as a Rmic taskdef
73      */

74     public Rmic getRmic() {
75         return attributes;
76     }
77
78     /**
79      * Gets the stub class suffix
80      * @return the stub suffix "_Stub"
81      */

82     protected String JavaDoc getStubClassSuffix() {
83         return RMI_STUB_SUFFIX;
84     }
85
86     /**
87      * Gets the skeleton class suffix
88      * @return the skeleton suffix "_Skel"
89      */

90     protected String JavaDoc getSkelClassSuffix() {
91         return RMI_SKEL_SUFFIX;
92     }
93
94     /**
95      * Gets the tie class suffix
96      * @return the tie suffix "_Tie"
97      */

98     protected String JavaDoc getTieClassSuffix() {
99         return RMI_TIE_SUFFIX;
100     }
101
102     /**
103      * This implementation returns a mapper that may return up to two
104      * file names.
105      *
106      * <ul>
107      * <li>for JRMP it will return *_getStubClassSuffix (and
108      * *_getSkelClassSuffix if JDK 1.1 is used)</li>
109      *
110      * <li>for IDL it will return a random name, causing &lt;rmic&gt; to
111      * always recompile.</li>
112      *
113      * <li>for IIOP it will return _*_getStubClassSuffix for
114      * interfaces and _*_getStubClassSuffix for non-interfaces (and
115      * determine the interface and create _*_Stub from that).</li>
116      * </ul>
117      * @return a <code>FileNameMapper</code>
118      */

119     public FileNameMapper getMapper() {
120         return mapper;
121     }
122
123     /**
124      * Gets the CLASSPATH this rmic process will use.
125      * @return the classpath
126      */

127     public Path getClasspath() {
128         return getCompileClasspath();
129     }
130
131     /**
132      * Builds the compilation classpath.
133      * @return the classpath
134      */

135     protected Path getCompileClasspath() {
136         Path classpath = new Path(attributes.getProject());
137         // add dest dir to classpath so that previously compiled and
138
// untouched classes are on classpath
139
classpath.setLocation(attributes.getBase());
140
141         // Combine the build classpath with the system classpath, in an
142
// order determined by the value of build.sysclasspath
143

144         Path cp = attributes.getClasspath();
145         if (cp == null) {
146             cp = new Path(attributes.getProject());
147         }
148         if (attributes.getIncludeantruntime()) {
149             classpath.addExisting(cp.concatSystemClasspath("last"));
150         } else {
151             classpath.addExisting(cp.concatSystemClasspath("ignore"));
152         }
153
154         if (attributes.getIncludejavaruntime()) {
155             classpath.addJavaRuntime();
156         }
157         return classpath;
158     }
159
160     /**
161      * Setup rmic argument for rmic.
162      * @return the command line
163      */

164     protected Commandline setupRmicCommand() {
165         return setupRmicCommand(null);
166     }
167
168     /**
169      * Setup rmic argument for rmic.
170      * @param options additional parameters needed by a specific
171      * implementation.
172      * @return the command line
173      */

174     protected Commandline setupRmicCommand(String JavaDoc[] options) {
175         Commandline cmd = new Commandline();
176
177         if (options != null) {
178             for (int i = 0; i < options.length; i++) {
179                 cmd.createArgument().setValue(options[i]);
180             }
181         }
182
183         Path classpath = getCompileClasspath();
184
185         cmd.createArgument().setValue("-d");
186         cmd.createArgument().setFile(attributes.getBase());
187
188         if (attributes.getExtdirs() != null) {
189             cmd.createArgument().setValue("-extdirs");
190             cmd.createArgument().setPath(attributes.getExtdirs());
191         }
192
193         cmd.createArgument().setValue("-classpath");
194         cmd.createArgument().setPath(classpath);
195
196         //handle the many different stub options.
197
String JavaDoc stubVersion = attributes.getStubVersion();
198         //default is compatibility
199
String JavaDoc stubOption = null;
200         if (null != stubVersion) {
201             if ("1.1".equals(stubVersion)) {
202                 stubOption = STUB_1_1;
203             } else if ("1.2".equals(stubVersion)) {
204                 stubOption = STUB_1_2;
205             } else if ("compat".equals(stubVersion)) {
206                 stubOption = STUB_COMPAT;
207             } else {
208                 //anything else
209
attributes.log("Unknown stub option " + stubVersion);
210                 //do nothing with the value? or go -v+stubVersion??
211
}
212         }
213         //for java1.5+, we generate compatible stubs, that is, unless
214
//the caller asked for IDL or IIOP support.
215
if (stubOption == null
216             && !attributes.getIiop()
217             && !attributes.getIdl()) {
218             stubOption = STUB_COMPAT;
219         }
220         if (stubOption != null) {
221             //set the non-null stubOption
222
cmd.createArgument().setValue(stubOption);
223         }
224         if (null != attributes.getSourceBase()) {
225             cmd.createArgument().setValue("-keepgenerated");
226         }
227
228         if (attributes.getIiop()) {
229             attributes.log("IIOP has been turned on.", Project.MSG_INFO);
230             cmd.createArgument().setValue("-iiop");
231             if (attributes.getIiopopts() != null) {
232                 attributes.log("IIOP Options: " + attributes.getIiopopts(),
233                                Project.MSG_INFO);
234                 cmd.createArgument().setValue(attributes.getIiopopts());
235             }
236         }
237
238         if (attributes.getIdl()) {
239             cmd.createArgument().setValue("-idl");
240             attributes.log("IDL has been turned on.", Project.MSG_INFO);
241             if (attributes.getIdlopts() != null) {
242                 cmd.createArgument().setValue(attributes.getIdlopts());
243                 attributes.log("IDL Options: " + attributes.getIdlopts(),
244                                Project.MSG_INFO);
245             }
246         }
247
248         if (attributes.getDebug()) {
249             cmd.createArgument().setValue("-g");
250         }
251
252         cmd.addArguments(attributes.getCurrentCompilerArgs());
253
254         logAndAddFilesToCompile(cmd);
255         return cmd;
256      }
257
258     /**
259      * Logs the compilation parameters, adds the files to compile and logs the
260      * &quot;niceSourceList&quot;
261      * @param cmd the commandline args
262      */

263     protected void logAndAddFilesToCompile(Commandline cmd) {
264         Vector JavaDoc compileList = attributes.getCompileList();
265
266         attributes.log("Compilation " + cmd.describeArguments(),
267                        Project.MSG_VERBOSE);
268
269         StringBuffer JavaDoc niceSourceList = new StringBuffer JavaDoc("File");
270         int cListSize = compileList.size();
271         if (cListSize != 1) {
272             niceSourceList.append("s");
273         }
274         niceSourceList.append(" to be compiled:");
275
276         for (int i = 0; i < cListSize; i++) {
277             String JavaDoc arg = (String JavaDoc) compileList.elementAt(i);
278             cmd.createArgument().setValue(arg);
279             niceSourceList.append(" ");
280             niceSourceList.append(arg);
281         }
282
283         attributes.log(niceSourceList.toString(), Project.MSG_VERBOSE);
284     }
285
286     /**
287      * Mapper that may return up to two file names.
288      *
289      * <ul>
290      * <li>for JRMP it will return *_getStubClassSuffix (and
291      * *_getSkelClassSuffix if JDK 1.1 is used)</li>
292      *
293      * <li>for IDL it will return a random name, causing <rmic> to
294      * always recompile.</li>
295      *
296      * <li>for IIOP it will return _*_getStubClassSuffix for
297      * interfaces and _*_getStubClassSuffix for non-interfaces (and
298      * determine the interface and create _*_Stub from that).</li>
299      * </ul>
300      */

301     private class RmicFileNameMapper implements FileNameMapper {
302
303         RmicFileNameMapper() {
304         }
305
306         /**
307          * Empty implementation.
308          */

309         public void setFrom(String JavaDoc s) {
310         }
311         /**
312          * Empty implementation.
313          */

314         public void setTo(String JavaDoc s) {
315         }
316
317         public String JavaDoc[] mapFileName(String JavaDoc name) {
318             if (name == null
319                 || !name.endsWith(".class")
320                 || name.endsWith(getStubClassSuffix() + ".class")
321                 || name.endsWith(getSkelClassSuffix() + ".class")
322                 || name.endsWith(getTieClassSuffix() + ".class")) {
323                 // Not a .class file or the one we'd generate
324
return null;
325             }
326
327             // we know that name.endsWith(".class")
328
String JavaDoc base = name.substring(0, name.length() - 6);
329
330             String JavaDoc classname = base.replace(File.separatorChar, '.');
331             if (attributes.getVerify()
332                 && !attributes.isValidRmiRemote(classname)) {
333                 return null;
334             }
335
336             /*
337              * fallback in case we have trouble loading the class or
338              * don't know how to handle it (there is no easy way to
339              * know what IDL mode would generate.
340              *
341              * This is supposed to make Ant always recompile the
342              * class, as a file of that name should not exist.
343              */

344             String JavaDoc[] target = new String JavaDoc[] {name + ".tmp." + RAND.nextLong()};
345
346             if (!attributes.getIiop() && !attributes.getIdl()) {
347                 // JRMP with simple naming convention
348
if ("1.2".equals(attributes.getStubVersion())) {
349                     target = new String JavaDoc[] {
350                         base + getStubClassSuffix() + ".class"
351                     };
352                 } else {
353                     target = new String JavaDoc[] {
354                         base + getStubClassSuffix() + ".class",
355                         base + getSkelClassSuffix() + ".class",
356                     };
357                 }
358             } else if (!attributes.getIdl()) {
359                 int lastSlash = base.lastIndexOf(File.separatorChar);
360
361                 String JavaDoc dirname = "";
362                 /*
363                  * I know, this is not necessary, but I prefer it explicit (SB)
364                  */

365                 int index = -1;
366                 if (lastSlash == -1) {
367                     // no package
368
index = 0;
369                 } else {
370                     index = lastSlash + 1;
371                     dirname = base.substring(0, index);
372                 }
373
374                 String JavaDoc filename = base.substring(index);
375
376                 try {
377                     Class JavaDoc c = attributes.getLoader().loadClass(classname);
378
379                     if (c.isInterface()) {
380                         // only stub, no tie
381
target = new String JavaDoc[] {
382                             dirname + "_" + filename + getStubClassSuffix()
383                             + ".class"
384                         };
385                     } else {
386                         /*
387                          * stub is derived from implementation,
388                          * tie from interface name.
389                          */

390                         Class JavaDoc interf = attributes.getRemoteInterface(c);
391                         String JavaDoc iName = interf.getName();
392                         String JavaDoc iDir = "";
393                         int iIndex = -1;
394                         int lastDot = iName.lastIndexOf(".");
395                         if (lastDot == -1) {
396                             // no package
397
iIndex = 0;
398                         } else {
399                             iIndex = lastDot + 1;
400                             iDir = iName.substring(0, iIndex);
401                             iDir = iDir.replace('.', File.separatorChar);
402                         }
403
404                         target = new String JavaDoc[] {
405                             dirname + "_" + filename + getTieClassSuffix()
406                             + ".class",
407                             iDir + "_" + iName.substring(iIndex)
408                             + getStubClassSuffix() + ".class"
409                         };
410                     }
411                 } catch (ClassNotFoundException JavaDoc e) {
412                     attributes.log("Unable to verify class " + classname
413                                    + ". It could not be found.",
414                                    Project.MSG_WARN);
415                 } catch (NoClassDefFoundError JavaDoc e) {
416                     attributes.log("Unable to verify class " + classname
417                                    + ". It is not defined.", Project.MSG_WARN);
418                 } catch (Throwable JavaDoc t) {
419                     attributes.log("Unable to verify class " + classname
420                                    + ". Loading caused Exception: "
421                                    + t.getMessage(), Project.MSG_WARN);
422                 }
423             }
424             return target;
425         }
426     }
427 }
428
Popular Tags