1 18 19 package org.apache.tools.ant.taskdefs.rmic; 20 21 import java.io.File ; 22 import java.util.Random ; 23 import java.util.Vector ; 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 37 public abstract class DefaultRmicAdapter implements RmicAdapter { 38 39 private Rmic attributes; 40 private FileNameMapper mapper; 41 private static final Random RAND = new Random (); 42 43 public static final String RMI_STUB_SUFFIX = "_Stub"; 44 45 public static final String RMI_SKEL_SUFFIX = "_Skel"; 46 47 public static final String RMI_TIE_SUFFIX = "_Tie"; 48 49 public static final String STUB_COMPAT = "-vcompat"; 50 51 public static final String STUB_1_1 = "-v1.1"; 52 53 public static final String STUB_1_2 = "-v1.2"; 54 55 58 public DefaultRmicAdapter() { 59 } 60 61 65 public void setRmic(final Rmic attributes) { 66 this.attributes = attributes; 67 mapper = new RmicFileNameMapper(); 68 } 69 70 74 public Rmic getRmic() { 75 return attributes; 76 } 77 78 82 protected String getStubClassSuffix() { 83 return RMI_STUB_SUFFIX; 84 } 85 86 90 protected String getSkelClassSuffix() { 91 return RMI_SKEL_SUFFIX; 92 } 93 94 98 protected String getTieClassSuffix() { 99 return RMI_TIE_SUFFIX; 100 } 101 102 119 public FileNameMapper getMapper() { 120 return mapper; 121 } 122 123 127 public Path getClasspath() { 128 return getCompileClasspath(); 129 } 130 131 135 protected Path getCompileClasspath() { 136 Path classpath = new Path(attributes.getProject()); 137 classpath.setLocation(attributes.getBase()); 140 141 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 164 protected Commandline setupRmicCommand() { 165 return setupRmicCommand(null); 166 } 167 168 174 protected Commandline setupRmicCommand(String [] 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 String stubVersion = attributes.getStubVersion(); 198 String 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 attributes.log("Unknown stub option " + stubVersion); 210 } 212 } 213 if (stubOption == null 216 && !attributes.getIiop() 217 && !attributes.getIdl()) { 218 stubOption = STUB_COMPAT; 219 } 220 if (stubOption != null) { 221 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 263 protected void logAndAddFilesToCompile(Commandline cmd) { 264 Vector compileList = attributes.getCompileList(); 265 266 attributes.log("Compilation " + cmd.describeArguments(), 267 Project.MSG_VERBOSE); 268 269 StringBuffer niceSourceList = new StringBuffer ("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 arg = (String ) 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 301 private class RmicFileNameMapper implements FileNameMapper { 302 303 RmicFileNameMapper() { 304 } 305 306 309 public void setFrom(String s) { 310 } 311 314 public void setTo(String s) { 315 } 316 317 public String [] mapFileName(String 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 return null; 325 } 326 327 String base = name.substring(0, name.length() - 6); 329 330 String classname = base.replace(File.separatorChar, '.'); 331 if (attributes.getVerify() 332 && !attributes.isValidRmiRemote(classname)) { 333 return null; 334 } 335 336 344 String [] target = new String [] {name + ".tmp." + RAND.nextLong()}; 345 346 if (!attributes.getIiop() && !attributes.getIdl()) { 347 if ("1.2".equals(attributes.getStubVersion())) { 349 target = new String [] { 350 base + getStubClassSuffix() + ".class" 351 }; 352 } else { 353 target = new String [] { 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 dirname = ""; 362 365 int index = -1; 366 if (lastSlash == -1) { 367 index = 0; 369 } else { 370 index = lastSlash + 1; 371 dirname = base.substring(0, index); 372 } 373 374 String filename = base.substring(index); 375 376 try { 377 Class c = attributes.getLoader().loadClass(classname); 378 379 if (c.isInterface()) { 380 target = new String [] { 382 dirname + "_" + filename + getStubClassSuffix() 383 + ".class" 384 }; 385 } else { 386 390 Class interf = attributes.getRemoteInterface(c); 391 String iName = interf.getName(); 392 String iDir = ""; 393 int iIndex = -1; 394 int lastDot = iName.lastIndexOf("."); 395 if (lastDot == -1) { 396 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 [] { 405 dirname + "_" + filename + getTieClassSuffix() 406 + ".class", 407 iDir + "_" + iName.substring(iIndex) 408 + getStubClassSuffix() + ".class" 409 }; 410 } 411 } catch (ClassNotFoundException e) { 412 attributes.log("Unable to verify class " + classname 413 + ". It could not be found.", 414 Project.MSG_WARN); 415 } catch (NoClassDefFoundError e) { 416 attributes.log("Unable to verify class " + classname 417 + ". It is not defined.", Project.MSG_WARN); 418 } catch (Throwable 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 |