/** * Move the generated source file(s) to the base directory * * @throws org.apache.tools.ant.BuildException When error copying/removing files. */ private void moveGeneratedFile( File baseDir, File sourceBaseFile, String classname, RmicAdapter adapter) throws BuildException { String classFileName = classname.replace('.', File.separatorChar) + ".class"; String[] generatedFiles = adapter.getMapper().mapFileName(classFileName); for (int i = 0; i < generatedFiles.length; i++) { final String generatedFile = generatedFiles[i]; if (!generatedFile.endsWith(".class")) { // don't know how to handle that - a IDL file doesn't // have a corresponding Java source for example. continue; } String sourceFileName = StringUtils.removeSuffix(generatedFile, ".class") + ".java"; File oldFile = new File(baseDir, sourceFileName); if (!oldFile.exists()) { // no source file generated, nothing to move continue; } File newFile = new File(sourceBaseFile, sourceFileName); try { if (filtering) { FILE_UTILS.copyFile( oldFile, newFile, new FilterSetCollection(getProject().getGlobalFilterSet())); } else { FILE_UTILS.copyFile(oldFile, newFile); } oldFile.delete(); } catch (IOException ioe) { String msg = "Failed to copy " + oldFile + " to " + newFile + " due to " + ioe.getMessage(); throw new BuildException(msg, ioe, getLocation()); } } }
/** * execute by creating an instance of an implementation class and getting to do the work * * @throws org.apache.tools.ant.BuildException if there's a problem with baseDir or RMIC */ @Override public void execute() throws BuildException { try { compileList.clear(); File outputDir = getOutputDir(); if (outputDir == null) { throw new BuildException(ERROR_BASE_NOT_SET, getLocation()); } if (!outputDir.exists()) { throw new BuildException(ERROR_NO_BASE_EXISTS + outputDir, getLocation()); } if (!outputDir.isDirectory()) { throw new BuildException(ERROR_NOT_A_DIR + outputDir, getLocation()); } if (verify) { log("Verify has been turned on.", Project.MSG_VERBOSE); } RmicAdapter adapter = nestedAdapter != null ? nestedAdapter : RmicAdapterFactory.getRmic(getCompiler(), this, createCompilerClasspath()); // now we need to populate the compiler adapter adapter.setRmic(this); Path classpath = adapter.getClasspath(); loader = getProject().createClassLoader(classpath); // scan base dirs to build up compile lists only if a // specific classname is not given if (classname == null) { DirectoryScanner ds = this.getDirectoryScanner(baseDir); String[] files = ds.getIncludedFiles(); scanDir(baseDir, files, adapter.getMapper()); } else { // otherwise perform a timestamp comparison - at least String path = classname.replace('.', File.separatorChar) + ".class"; File f = new File(baseDir, path); if (f.isFile()) { scanDir(baseDir, new String[] {path}, adapter.getMapper()); } else { // Does not exist, so checking whether it is up to // date makes no sense. Compilation will fail // later anyway, but tests expect a certain // output. compileList.add(classname); } } int fileCount = compileList.size(); if (fileCount > 0) { log( "RMI Compiling " + fileCount + " class" + (fileCount > 1 ? "es" : "") + " to " + outputDir, Project.MSG_INFO); if (listFiles) { for (int i = 0; i < fileCount; i++) { log(compileList.get(i).toString()); } } // finally, lets execute the compiler!! if (!adapter.execute()) { throw new BuildException(ERROR_RMIC_FAILED, getLocation()); } } /* * Move the generated source file to the base directory. If * base directory and sourcebase are the same, the generated * sources are already in place. */ if (null != sourceBase && !outputDir.equals(sourceBase) && fileCount > 0) { if (idl) { log("Cannot determine sourcefiles in idl mode, ", Project.MSG_WARN); log("sourcebase attribute will be ignored.", Project.MSG_WARN); } else { for (int j = 0; j < fileCount; j++) { moveGeneratedFile(outputDir, sourceBase, (String) compileList.elementAt(j), adapter); } } } } finally { cleanup(); } }