Search in sources :

Example 16 with FileResource

use of org.apache.tools.ant.types.resources.FileResource in project ant by apache.

the class ArchiveScanner method scanme.

/**
 * if the datetime of the archive did not change since
 * lastScannedResource was initialized returns immediately else if
 * the archive has not been scanned yet, then all the zip entries
 * are put into the appropriate tables.
 */
private void scanme() {
    if (!src.isExists() && !errorOnMissingArchive) {
        return;
    }
    // do not use a FileResource b/c it pulls File info from the filesystem:
    Resource thisresource = new Resource(src.getName(), src.isExists(), src.getLastModified());
    // spare scanning again and again
    if (lastScannedResource != null && lastScannedResource.getName().equals(thisresource.getName()) && lastScannedResource.getLastModified() == thisresource.getLastModified()) {
        return;
    }
    init();
    fileEntries.clear();
    dirEntries.clear();
    matchFileEntries.clear();
    matchDirEntries.clear();
    fillMapsFromArchive(src, encoding, fileEntries, matchFileEntries, dirEntries, matchDirEntries);
    // record data about the last scanned resource
    lastScannedResource = thisresource;
}
Also used : FileResource(org.apache.tools.ant.types.resources.FileResource)

Example 17 with FileResource

use of org.apache.tools.ant.types.resources.FileResource in project ant by apache.

the class ExecuteOn method runExec.

/**
 * Run the specified Execute object.
 * @param exe the Execute instance representing the external process.
 * @throws BuildException on error
 */
@Override
protected void runExec(Execute exe) throws BuildException {
    int totalFiles = 0;
    int totalDirs = 0;
    boolean haveExecuted = false;
    try {
        Vector<String> fileNames = new Vector<>();
        Vector<File> baseDirs = new Vector<>();
        for (AbstractFileSet fs : filesets) {
            String currentType = type;
            if (fs instanceof DirSet) {
                if (!FileDirBoth.DIR.equals(type)) {
                    log("Found a nested dirset but type is " + type + ". " + "Temporarily switching to type=\"dir\" on the assumption that you really did mean <dirset> not <fileset>.", Project.MSG_DEBUG);
                    currentType = FileDirBoth.DIR;
                }
            }
            File base = fs.getDir(getProject());
            DirectoryScanner ds = fs.getDirectoryScanner(getProject());
            if (!FileDirBoth.DIR.equals(currentType)) {
                for (String value : getFiles(base, ds)) {
                    totalFiles++;
                    fileNames.add(value);
                    baseDirs.add(base);
                }
            }
            if (!FileDirBoth.FILE.equals(currentType)) {
                for (String value : getDirs(base, ds)) {
                    totalDirs++;
                    fileNames.add(value);
                    baseDirs.add(base);
                }
            }
            if (fileNames.isEmpty() && skipEmpty) {
                logSkippingFileset(currentType, ds, base);
                continue;
            }
            if (!parallel) {
                for (String srcFile : fileNames) {
                    String[] command = getCommandline(srcFile, base);
                    log(Commandline.describeCommand(command), Project.MSG_VERBOSE);
                    exe.setCommandline(command);
                    if (redirectorElement != null) {
                        setupRedirector();
                        redirectorElement.configure(redirector, srcFile);
                    }
                    if (redirectorElement != null || haveExecuted) {
                        // need to reset the stream handler to restart
                        // reading of pipes;
                        // go ahead and do it always w/ nested redirectors
                        exe.setStreamHandler(redirector.createHandler());
                    }
                    runExecute(exe);
                    haveExecuted = true;
                }
                fileNames.clear();
                baseDirs.clear();
            }
        }
        if (resources != null) {
            for (Resource res : resources) {
                if (!res.isExists() && ignoreMissing) {
                    continue;
                }
                File base = null;
                String name = res.getName();
                FileProvider fp = res.as(FileProvider.class);
                if (fp != null) {
                    FileResource fr = ResourceUtils.asFileResource(fp);
                    base = fr.getBaseDir();
                    if (base == null) {
                        name = fr.getFile().getAbsolutePath();
                    }
                }
                if (restrict(new String[] { name }, base).length == 0) {
                    continue;
                }
                if ((!res.isDirectory() || !res.isExists()) && !FileDirBoth.DIR.equals(type)) {
                    totalFiles++;
                } else if (res.isDirectory() && !FileDirBoth.FILE.equals(type)) {
                    totalDirs++;
                } else {
                    continue;
                }
                baseDirs.add(base);
                fileNames.add(name);
                if (!parallel) {
                    String[] command = getCommandline(name, base);
                    log(Commandline.describeCommand(command), Project.MSG_VERBOSE);
                    exe.setCommandline(command);
                    if (redirectorElement != null) {
                        setupRedirector();
                        redirectorElement.configure(redirector, name);
                    }
                    if (redirectorElement != null || haveExecuted) {
                        // need to reset the stream handler to restart
                        // reading of pipes;
                        // go ahead and do it always w/ nested redirectors
                        exe.setStreamHandler(redirector.createHandler());
                    }
                    runExecute(exe);
                    haveExecuted = true;
                    fileNames.clear();
                    baseDirs.clear();
                }
            }
        }
        if (parallel && (!fileNames.isEmpty() || !skipEmpty)) {
            runParallel(exe, fileNames, baseDirs);
            haveExecuted = true;
        }
        if (haveExecuted) {
            log("Applied " + cmdl.getExecutable() + " to " + totalFiles + " file" + (totalFiles != 1 ? "s" : "") + " and " + totalDirs + " director" + (totalDirs != 1 ? "ies" : "y") + ".", verbose ? Project.MSG_INFO : Project.MSG_VERBOSE);
        }
    } catch (IOException e) {
        throw new BuildException("Execute failed: " + e, e, getLocation());
    } finally {
        // close the output file if required
        logFlush();
        redirector.setAppendProperties(false);
        redirector.setProperties();
    }
}
Also used : AbstractFileSet(org.apache.tools.ant.types.AbstractFileSet) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) IOException(java.io.IOException) DirSet(org.apache.tools.ant.types.DirSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) FileProvider(org.apache.tools.ant.types.resources.FileProvider) BuildException(org.apache.tools.ant.BuildException) Vector(java.util.Vector) File(java.io.File)

Example 18 with FileResource

use of org.apache.tools.ant.types.resources.FileResource in project ant by apache.

the class SignJar method execute.

/**
 * sign the jar(s)
 *
 * @throws BuildException on errors
 */
@Override
public void execute() throws BuildException {
    // validation logic
    final boolean hasJar = jar != null;
    final boolean hasSignedJar = signedjar != null;
    final boolean hasDestDir = destDir != null;
    final boolean hasMapper = mapper != null;
    if (!hasJar && !hasResources()) {
        throw new BuildException(ERROR_NO_SOURCE);
    }
    if (null == alias) {
        throw new BuildException(ERROR_NO_ALIAS);
    }
    if (null == storepass) {
        throw new BuildException(ERROR_NO_STOREPASS);
    }
    if (hasDestDir && hasSignedJar) {
        throw new BuildException(ERROR_TODIR_AND_SIGNEDJAR);
    }
    if (hasResources() && hasSignedJar) {
        throw new BuildException(ERROR_SIGNEDJAR_AND_PATHS);
    }
    // we can change implementation details later
    if (!hasDestDir && hasMapper) {
        throw new BuildException(ERROR_MAPPER_WITHOUT_DEST);
    }
    beginExecution();
    try {
        // special case single jar handling with signedjar attribute set
        if (hasJar && hasSignedJar) {
            // single jar processing
            signOneJar(jar, signedjar);
            // return here.
            return;
        }
        // the rest of the method treats single jar like
        // a nested path with one file
        Path sources = createUnifiedSourcePath();
        // set up our mapping policy
        FileNameMapper destMapper = hasMapper ? mapper : new IdentityMapper();
        // deal with the paths
        for (Resource r : sources) {
            FileResource fr = ResourceUtils.asFileResource(r.as(FileProvider.class));
            // calculate our destination directory; it is either the destDir
            // attribute, or the base dir of the fileset (for in situ updates)
            File toDir = hasDestDir ? destDir : fr.getBaseDir();
            // determine the destination filename via the mapper
            String[] destFilenames = destMapper.mapFileName(fr.getName());
            if (destFilenames == null || destFilenames.length != 1) {
                // we only like simple mappers.
                throw new BuildException(ERROR_BAD_MAP + fr.getFile());
            }
            File destFile = new File(toDir, destFilenames[0]);
            signOneJar(fr.getFile(), destFile);
        }
    } finally {
        endExecution();
    }
}
Also used : Path(org.apache.tools.ant.types.Path) IdentityMapper(org.apache.tools.ant.util.IdentityMapper) FileProvider(org.apache.tools.ant.types.resources.FileProvider) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) FileResource(org.apache.tools.ant.types.resources.FileResource) BuildException(org.apache.tools.ant.BuildException) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) File(java.io.File)

Example 19 with FileResource

use of org.apache.tools.ant.types.resources.FileResource in project ant by apache.

the class Tar method check.

/**
 * Checks whether the archive is out-of-date with respect to the resources
 * of the given collection.
 *
 * <p>Also checks that either all collections only contain file
 * resources or this class supports non-file collections.</p>
 *
 * <p>And - in case of file-collections - ensures that the archive won't
 * contain itself.</p>
 *
 * @param rc the resource collection to check
 * @return whether the archive is up-to-date
 * @since Ant 1.7
 */
protected boolean check(final ResourceCollection rc) {
    boolean upToDate = true;
    if (isFileFileSet(rc)) {
        final FileSet fs = (FileSet) rc;
        upToDate = check(fs.getDir(getProject()), getFileNames(fs));
    } else if (!rc.isFilesystemOnly() && !supportsNonFileResources()) {
        throw new BuildException("only filesystem resources are supported");
    } else if (rc.isFilesystemOnly()) {
        final Set<File> basedirs = new HashSet<>();
        final Map<File, List<String>> basedirToFilesMap = new HashMap<>();
        for (final Resource res : rc) {
            final FileResource r = ResourceUtils.asFileResource(res.as(FileProvider.class));
            File base = r.getBaseDir();
            if (base == null) {
                base = Copy.NULL_FILE_PLACEHOLDER;
            }
            basedirs.add(base);
            List<String> files = basedirToFilesMap.get(base);
            if (files == null) {
                files = new Vector<>();
                basedirToFilesMap.put(base, files);
            }
            if (base == Copy.NULL_FILE_PLACEHOLDER) {
                files.add(r.getFile().getAbsolutePath());
            } else {
                files.add(r.getName());
            }
        }
        for (final File base : basedirs) {
            final File tmpBase = base == Copy.NULL_FILE_PLACEHOLDER ? null : base;
            final List<String> files = basedirToFilesMap.get(base);
            upToDate &= check(tmpBase, files);
        }
    } else {
        // non-file resources
        for (Resource r : rc) {
            upToDate = archiveIsUpToDate(r);
        }
    }
    return upToDate;
}
Also used : FileSet(org.apache.tools.ant.types.FileSet) ArchiveFileSet(org.apache.tools.ant.types.ArchiveFileSet) HashMap(java.util.HashMap) ArchiveResource(org.apache.tools.ant.types.resources.ArchiveResource) FileResource(org.apache.tools.ant.types.resources.FileResource) TarResource(org.apache.tools.ant.types.resources.TarResource) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) FileProvider(org.apache.tools.ant.types.resources.FileProvider) List(java.util.List) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) HashSet(java.util.HashSet)

Example 20 with FileResource

use of org.apache.tools.ant.types.resources.FileResource in project ant by apache.

the class PermissionUtilsTest method detectsFileTypeOfDirectoryFromResource.

@Test
public void detectsFileTypeOfDirectoryFromResource() throws IOException {
    File f = File.createTempFile("ant", ".tst");
    f.delete();
    f.mkdirs();
    f.deleteOnExit();
    assertEquals(PermissionUtils.FileType.DIR, PermissionUtils.FileType.of(new FileResource(f)));
}
Also used : FileResource(org.apache.tools.ant.types.resources.FileResource) File(java.io.File) Test(org.junit.Test)

Aggregations

FileResource (org.apache.tools.ant.types.resources.FileResource)48 File (java.io.File)29 BuildException (org.apache.tools.ant.BuildException)25 Resource (org.apache.tools.ant.types.Resource)23 IOException (java.io.IOException)15 FileProvider (org.apache.tools.ant.types.resources.FileProvider)10 Test (org.junit.Test)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Liquibase (liquibase.Liquibase)6 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)6 FileSet (org.apache.tools.ant.types.FileSet)6 Contexts (liquibase.Contexts)5 LiquibaseException (liquibase.exception.LiquibaseException)5 BufferedReader (java.io.BufferedReader)4 Reader (java.io.Reader)4 Writer (java.io.Writer)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Iterator (java.util.Iterator)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4