Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 415   Methods: 16
NCLOC: 217   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
DefaultRmicAdapter.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.taskdefs.rmic;
 56   
 
 57   
 import java.io.File;
 58   
 import java.util.Random;
 59   
 import java.util.Vector;
 60   
 import org.apache.tools.ant.Project;
 61   
 import org.apache.tools.ant.taskdefs.Rmic;
 62   
 import org.apache.tools.ant.types.Commandline;
 63   
 import org.apache.tools.ant.types.Path;
 64   
 import org.apache.tools.ant.util.FileNameMapper;
 65   
 import org.apache.tools.ant.util.JavaEnvUtils;
 66   
 
 67   
 /**
 68   
  * This is the default implementation for the RmicAdapter interface.
 69   
  * Currently, this is a cut-and-paste of the original rmic task and
 70   
  * DefaultCopmpilerAdapter.
 71   
  *
 72   
  * @author duncan@x180.com
 73   
  * @author ludovic.claude@websitewatchers.co.uk
 74   
  * @author David Maclean <a href="mailto:david@cm.co.za">david@cm.co.za</a>
 75   
  * @author Stefan Bodewig 
 76   
  * @author <a href="tokamoto@rd.nttdata.co.jp">Takashi Okamoto</a>
 77   
  * @since Ant 1.4
 78   
  */
 79   
 public abstract class DefaultRmicAdapter implements RmicAdapter {
 80   
 
 81   
     private Rmic attributes;
 82   
     private FileNameMapper mapper;
 83   
     private static final Random rand = new Random();
 84   
 
 85  0
     public DefaultRmicAdapter() {
 86   
     }
 87   
 
 88  0
     public void setRmic(Rmic attributes) {
 89  0
         this.attributes = attributes;
 90  0
         mapper = new RmicFileNameMapper();
 91   
     }
 92   
 
 93  0
     public Rmic getRmic() {
 94  0
         return attributes;
 95   
     }
 96   
 
 97  0
     protected String getStubClassSuffix() {
 98  0
         return "_Stub";
 99   
     }        
 100   
 
 101  0
     protected String getSkelClassSuffix() {
 102  0
         return "_Skel";
 103   
     }        
 104   
 
 105  0
     protected String getTieClassSuffix() {
 106  0
         return "_Tie";
 107   
     }        
 108   
 
 109   
     /**
 110   
      * This implementation returns a mapper that may return up to two
 111   
      * file names.
 112   
      *
 113   
      * <ul>
 114   
      *   <li>for JRMP it will return *_getStubClassSuffix (and
 115   
      *   *_getSkelClassSuffix if JDK 1.1 is used)</li>
 116   
      *
 117   
      *   <li>for IDL it will return a random name, causing &lt;rmic&gt; to
 118   
      *     always recompile.</li>
 119   
      *
 120   
      *   <li>for IIOP it will return _*_getStubClassSuffix for
 121   
      *   interfaces and _*_getStubClassSuffix for non-interfaces (and
 122   
      *   determine the interface and create _*_Stub from that).</li>
 123   
      * </ul>
 124   
      */
 125  0
     public FileNameMapper getMapper() {
 126  0
         return mapper;
 127   
     }
 128   
 
 129   
     /**
 130   
      * The CLASSPATH this rmic process will use.
 131   
      */
 132  0
     public Path getClasspath() {
 133  0
         return getCompileClasspath();
 134   
     }
 135   
 
 136   
     /**
 137   
      * Builds the compilation classpath.
 138   
      */
 139  0
     protected Path getCompileClasspath() {
 140  0
         Path classpath = new Path(attributes.getProject());
 141   
         // add dest dir to classpath so that previously compiled and
 142   
         // untouched classes are on classpath
 143  0
         classpath.setLocation(attributes.getBase());
 144   
 
 145   
         // Combine the build classpath with the system classpath, in an 
 146   
         // order determined by the value of build.sysclasspath
 147   
         
 148  0
         Path cp = attributes.getClasspath();
 149  0
         if (cp == null) {
 150  0
             cp = new Path(attributes.getProject());
 151   
         }
 152  0
         if (attributes.getIncludeantruntime()) {
 153  0
             classpath.addExisting(cp.concatSystemClasspath("last"));
 154   
         } else {
 155  0
             classpath.addExisting(cp.concatSystemClasspath("ignore"));
 156   
         }
 157   
 
 158  0
         if (attributes.getIncludejavaruntime()) {
 159  0
             classpath.addJavaRuntime();
 160   
         }
 161  0
         return classpath;
 162   
     }
 163   
 
 164   
     /**
 165   
      * setup rmic argument for rmic.
 166   
      */
 167  0
     protected Commandline setupRmicCommand() {
 168  0
         return setupRmicCommand(null);
 169   
     }
 170   
 
 171   
     /**
 172   
      * setup rmic argument for rmic.
 173   
      *
 174   
      * @param options additional parameters needed by a specific
 175   
      *                implementation.
 176   
      */
 177  0
     protected Commandline setupRmicCommand(String[] options) {
 178  0
         Commandline cmd = new Commandline();
 179   
 
 180  0
         if (options != null) {
 181  0
             for (int i = 0; i < options.length; i++) {
 182  0
                 cmd.createArgument().setValue(options[i]);
 183   
             }
 184   
         }
 185   
 
 186  0
         Path classpath = getCompileClasspath();
 187   
 
 188  0
         cmd.createArgument().setValue("-d");
 189  0
         cmd.createArgument().setFile(attributes.getBase());
 190   
 
 191  0
         if (attributes.getExtdirs() != null) {
 192  0
             if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
 193   
                 /*
 194   
                  * XXX - This doesn't mix very well with build.systemclasspath,
 195   
                  */
 196  0
                 classpath.addExtdirs(attributes.getExtdirs());
 197   
             } else {
 198  0
                 cmd.createArgument().setValue("-extdirs");
 199  0
                 cmd.createArgument().setPath(attributes.getExtdirs());
 200   
             }
 201   
         }
 202   
 
 203  0
         cmd.createArgument().setValue("-classpath");
 204  0
         cmd.createArgument().setPath(classpath);
 205   
 
 206  0
         String stubVersion = attributes.getStubVersion();
 207  0
         if (null != stubVersion) {
 208  0
             if ("1.1".equals(stubVersion)) {
 209  0
                 cmd.createArgument().setValue("-v1.1");
 210  0
             } else if ("1.2".equals(stubVersion)) {
 211  0
                 cmd.createArgument().setValue("-v1.2");
 212   
             } else {
 213  0
                 cmd.createArgument().setValue("-vcompat");
 214   
             }
 215   
         }
 216   
 
 217  0
         if (null != attributes.getSourceBase()) {
 218  0
             cmd.createArgument().setValue("-keepgenerated");
 219   
         }
 220   
 
 221  0
         if (attributes.getIiop()) {
 222  0
             attributes.log("IIOP has been turned on.", Project.MSG_INFO);
 223  0
             cmd.createArgument().setValue("-iiop");
 224  0
             if (attributes.getIiopopts() != null) {
 225  0
                 attributes.log("IIOP Options: " + attributes.getIiopopts(),
 226   
                                Project.MSG_INFO);
 227  0
                 cmd.createArgument().setValue(attributes.getIiopopts());
 228   
             }
 229   
         }
 230   
 
 231  0
         if (attributes.getIdl())  {
 232  0
             cmd.createArgument().setValue("-idl");
 233  0
             attributes.log("IDL has been turned on.", Project.MSG_INFO);
 234  0
             if (attributes.getIdlopts() != null) {
 235  0
                 cmd.createArgument().setValue(attributes.getIdlopts());
 236  0
                 attributes.log("IDL Options: " + attributes.getIdlopts(),
 237   
                                Project.MSG_INFO);
 238   
             }
 239   
         }
 240   
 
 241  0
         if (attributes.getDebug()) {
 242  0
             cmd.createArgument().setValue("-g");
 243   
         }
 244   
 
 245  0
         cmd.addArguments(attributes.getCurrentCompilerArgs());
 246   
 
 247  0
         logAndAddFilesToCompile(cmd);
 248  0
         return cmd;
 249   
      }
 250   
 
 251   
     /**
 252   
      * Logs the compilation parameters, adds the files to compile and logs the 
 253   
      * &qout;niceSourceList&quot;
 254   
      */
 255  0
     protected void logAndAddFilesToCompile(Commandline cmd) {
 256  0
         Vector compileList = attributes.getCompileList();
 257   
 
 258  0
         attributes.log("Compilation " + cmd.describeArguments(),
 259   
                        Project.MSG_VERBOSE);
 260   
 
 261  0
         StringBuffer niceSourceList = new StringBuffer("File");
 262  0
         if (compileList.size() != 1) {
 263  0
             niceSourceList.append("s");
 264   
         }
 265  0
         niceSourceList.append(" to be compiled:");
 266   
 
 267  0
         for (int i = 0; i < compileList.size(); i++) {
 268  0
             String arg = (String) compileList.elementAt(i);
 269  0
             cmd.createArgument().setValue(arg);
 270  0
             niceSourceList.append("    " + arg);
 271   
         }
 272   
 
 273  0
         attributes.log(niceSourceList.toString(), Project.MSG_VERBOSE);
 274   
     }
 275   
 
 276   
     /**
 277   
      * Mapper that may return up to two file names.
 278   
      *
 279   
      * <ul>
 280   
      *   <li>for JRMP it will return *_getStubClassSuffix (and
 281   
      *   *_getSkelClassSuffix if JDK 1.1 is used)</li>
 282   
      *
 283   
      *   <li>for IDL it will return a random name, causing <rmic> to
 284   
      *     always recompile.</li>
 285   
      *
 286   
      *   <li>for IIOP it will return _*_getStubClassSuffix for
 287   
      *   interfaces and _*_getStubClassSuffix for non-interfaces (and
 288   
      *   determine the interface and create _*_Stub from that).</li>
 289   
      * </ul>
 290   
      */
 291   
     private class RmicFileNameMapper implements FileNameMapper {
 292   
 
 293  0
         RmicFileNameMapper() {}
 294   
 
 295   
         /**
 296   
          * Empty implementation.
 297   
          */
 298  0
         public void setFrom(String s) {}
 299   
         /**
 300   
          * Empty implementation.
 301   
          */
 302  0
         public void setTo(String s) {}
 303   
 
 304  0
         public String[] mapFileName(String name) {
 305  0
             if (name == null
 306   
                 || !name.endsWith(".class")
 307   
                 || name.endsWith(getStubClassSuffix() + ".class") 
 308   
                 || name.endsWith(getSkelClassSuffix() + ".class") 
 309   
                 || name.endsWith(getTieClassSuffix() + ".class")) {
 310   
                 // Not a .class file or the one we'd generate
 311  0
                 return null;
 312   
             }
 313   
 
 314   
             // we know that name.endsWith(".class")
 315  0
             String base = name.substring(0, name.length() - 6);
 316   
 
 317  0
             String classname = base.replace(File.separatorChar, '.');
 318  0
             if (attributes.getVerify() &&
 319   
                 !attributes.isValidRmiRemote(classname)) {
 320  0
                 return null;
 321   
             }
 322   
 
 323   
             /*
 324   
              * fallback in case we have trouble loading the class or
 325   
              * don't know how to handle it (there is no easy way to
 326   
              * know what IDL mode would generate.
 327   
              *
 328   
              * This is supposed to make Ant always recompile the
 329   
              * class, as a file of that name should not exist.
 330   
              */
 331  0
             String[] target = new String[] {name + ".tmp." + rand.nextLong()};
 332   
 
 333  0
             if (!attributes.getIiop() && !attributes.getIdl()) {
 334   
                 // JRMP with simple naming convention
 335  0
                 if ("1.2".equals(attributes.getStubVersion())) {
 336  0
                     target = new String[] {
 337   
                         base + getStubClassSuffix() + ".class"
 338   
                     };
 339   
                 } else {
 340  0
                     target = new String[] {
 341   
                         base + getStubClassSuffix() + ".class",
 342   
                         base + getSkelClassSuffix() + ".class",
 343   
                     };
 344   
                 }
 345  0
             } else if (!attributes.getIdl()) {
 346  0
                 int lastSlash = base.lastIndexOf(File.separatorChar);
 347   
 
 348  0
                 String dirname = "";
 349   
                 /*
 350   
                  * I know, this is not necessary, but I prefer it explicit (SB)
 351   
                  */
 352  0
                 int index = -1;
 353  0
                 if (lastSlash == -1) {
 354   
                     // no package
 355  0
                     index = 0;
 356   
                 } else {
 357  0
                     index = lastSlash + 1;
 358  0
                     dirname = base.substring(0, index);
 359   
                 }
 360   
 
 361  0
                 String filename = base.substring(index);
 362   
 
 363  0
                 try {
 364  0
                     Class c = attributes.getLoader().loadClass(classname);
 365   
 
 366  0
                     if (c.isInterface()) {
 367   
                         // only stub, no tie
 368  0
                         target = new String[] {
 369   
                             dirname + "_" + filename + getStubClassSuffix() 
 370   
                             + ".class"
 371   
                         };
 372   
                     } else {
 373   
                         /*
 374   
                          * stub is derived from implementation, 
 375   
                          * tie from interface name.
 376   
                          */
 377  0
                         Class interf = attributes.getRemoteInterface(c);
 378  0
                         String iName = interf.getName();
 379  0
                         String iDir = "";
 380  0
                         int iIndex = -1;
 381  0
                         int lastDot = iName.lastIndexOf(".");
 382  0
                         if (lastDot == -1) {
 383   
                             // no package
 384  0
                             iIndex = 0;
 385   
                         } else {
 386  0
                             iIndex = lastDot + 1;
 387  0
                             iDir = iName.substring(0, iIndex);
 388  0
                             iDir = iDir.replace('.', File.separatorChar);
 389   
                         }
 390   
                         
 391  0
                         target = new String[] {
 392   
                             dirname + "_" + filename + getTieClassSuffix() 
 393   
                             + ".class",
 394   
                             iDir + "_" + iName.substring(iIndex) 
 395   
                             + getStubClassSuffix() + ".class"
 396   
                         };
 397   
                     }
 398   
                 } catch (ClassNotFoundException e) {
 399  0
                     attributes.log("Unable to verify class " + classname
 400   
                                    + ". It could not be found.", 
 401   
                                    Project.MSG_WARN);
 402   
                 } catch (NoClassDefFoundError e) {
 403  0
                     attributes.log("Unable to verify class " + classname
 404   
                                    + ". It is not defined.", Project.MSG_WARN);
 405   
                 } catch (Throwable t) {
 406  0
                     attributes.log("Unable to verify class " + classname
 407   
                                    + ". Loading caused Exception: "
 408   
                                    + t.getMessage(), Project.MSG_WARN);
 409   
                 }
 410   
             }
 411  0
             return target;
 412   
         }
 413   
     }
 414   
 }
 415