Example usage for org.apache.commons.vfs2 FileType FOLDER

List of usage examples for org.apache.commons.vfs2 FileType FOLDER

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileType FOLDER.

Prototype

FileType FOLDER

To view the source code for org.apache.commons.vfs2 FileType FOLDER.

Click Source Link

Document

A folder.

Usage

From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignManager.java

protected ModuleFile getFileContainerFile(String name) throws FileSystemException, WGDesignSyncException {

    FileObject folder = getFilesFolder();
    List<String> paths = WGUtils.deserializeCollection(name, ":");
    boolean found = false;

    for (String path : paths) {
        FileObject fileContainer = findMatchingChild(folder, path, Collections.singleton(""), true);
        if (fileContainer != null && fileContainer.getType().equals(FileType.FOLDER)
                && isValidDesignFile(fileContainer)) {
            found = true;//ww  w .  ja  va  2 s .  c o m
            folder = fileContainer;
        } else {
            found = false;
            break;
        }
    }

    if (found == true) {
        return new ModuleFile(folder, WGUtils.serializeCollection(paths.subList(0, paths.size() - 1), ":"), "",
                WGDocument.TYPE_FILECONTAINER);
    } else {
        return null;
    }

}

From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignProvider.java

private static void determineChangedResources(int resourceType, FileObject categoryFolder, FileObject source,
        FileObject target, FileObject baseFolder, String sourceEncoding, String targetEncoding,
        OverlayStatus status, Logger log, DesignFileValidator validator)
        throws WGDesignSyncException, NoSuchAlgorithmException, IOException {

    for (FileObject sourceFile : source.getChildren()) {
        if (!isValidDesignFile(sourceFile, validator)) {
            continue;
        }/*  www.  j a  v a2s .  com*/

        FileObject targetFile = target.resolveFile(sourceFile.getName().getBaseName());
        String resourcePath = baseFolder.getName().getRelativeName(targetFile.getName());

        if (sourceFile.getType().equals(FileType.FOLDER)) {
            if (!targetFile.exists()) {
                status.getNewFolders().add(targetFile.getName().getPath());
            } else if (targetFile.getType().equals(FileType.FILE)) {
                throw new WGDesignSyncException("Unable to apply overlay. Folder '"
                        + baseFolder.getName().getRelativeName(targetFile.getName())
                        + " already exists as file. Delete it to enable overlay management again");
            }
            determineChangedResources(resourceType, categoryFolder, sourceFile, targetFile, baseFolder,
                    sourceEncoding, targetEncoding, status, log, validator);
        } else if (sourceFile.getType().equals(FileType.FILE)) {

            // File does not exist.
            if (!targetFile.exists()) {

                // Was it once deployed?
                ResourceData originalHash = status.getOverlayData().getOverlayResources().get(resourcePath);
                if (originalHash == null) { // No, so it must be new in base
                    status.addChangedResource(resourceType, sourceFile, targetFile, categoryFolder,
                            OverlayStatus.ChangeType.NEW, resourcePath, null);
                } else { // Yes, Check if the base file changed since deployment
                    String newHash = MD5HashingInputStream
                            .getStreamHash(sourceFile.getContent().getInputStream());
                    if (newHash.equals(originalHash.getMd5Hash())) { // Nope. So this is no change. The overlay just chose not to use the file
                        continue;
                    } else { // Yes, so it is indeed a conflict
                        status.addChangedResource(resourceType, sourceFile, targetFile, categoryFolder,
                                OverlayStatus.ChangeType.CONFLICT, resourcePath, null);
                    }
                }

            }

            // File does exist: Determine if is updated in base since the overlay file was deployed
            else {

                ResourceData originalHash = status.getOverlayData().getOverlayResources().get(resourcePath);
                if (originalHash == null) {
                    if (!status.isUpdatedBaseDesign()) {
                        log.info("There is no information about the original deployment state of  resource "
                                + resourcePath
                                + ". Setting original deployment state now to the current base version.");
                        OverlayData.ResourceData resource = new OverlayData.ResourceData();
                        resource.setMd5Hash(
                                MD5HashingInputStream.getStreamHash(sourceFile.getContent().getInputStream()));
                        status.getOverlayData().setOverlayResource(resourcePath, resource);
                    } else {
                        log.info("Cannot update overlay resource " + resourcePath
                                + " as there is no information of its original deployment state.");
                    }
                    continue;
                }

                // First determine if the resource really changed from what was distributed
                String newHash = MD5HashingInputStream.getStreamHash(sourceFile.getContent().getInputStream());
                if (newHash.equals(originalHash.getMd5Hash())) {
                    continue;
                }

                // Determine if the target file is the same as was distributed. If not then it was user modified, so it is a conflict
                String currentHash = MD5HashingInputStream
                        .getStreamHash(targetFile.getContent().getInputStream());
                if (!currentHash.equals(originalHash.getMd5Hash())) {
                    status.addChangedResource(resourceType, sourceFile, targetFile, categoryFolder,
                            OverlayStatus.ChangeType.CONFLICT, resourcePath, null);
                }

                // It is a normal change
                else {
                    status.addChangedResource(resourceType, sourceFile, targetFile, categoryFolder,
                            OverlayStatus.ChangeType.CHANGED, resourcePath, null);
                }
            }
        }
    }

}

From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignManager.java

private ModuleFile resolveModuleFile(FileObject folder, String name, String category, int type)
        throws FileSystemException {

    if (folder == null || !folder.exists() || !folder.getType().equals(FileType.FOLDER)) {
        return null;
    }//from w w  w .  j  a va  2  s .co  m

    String prefix;
    String baseName;
    String fullName;
    String suffix = getFileStandardSuffix(type, category);
    int lastColon = name.lastIndexOf(":");
    if (lastColon != -1) {
        prefix = name.substring(0, lastColon);
        baseName = name.substring(lastColon + 1);
        fullName = baseName + suffix;
    } else {
        prefix = "";
        baseName = name;
        fullName = name + suffix;
    }

    // Find correct folder
    if (!prefix.equals("")) {
        List<String> path = WGUtils.deserializeCollection(prefix, ":");
        Iterator<String> elems = path.iterator();
        while (elems.hasNext()) {
            String elem = (String) elems.next();
            FileObject matchingChild = findMatchingChild(folder, elem, Collections.singleton(""), true);

            // Use matching child as next folder
            if (matchingChild != null && matchingChild.getType().equals(FileType.FOLDER)) {
                folder = matchingChild;
            }

            // Exit if we did not find a matching folder
            else {
                return null;
            }

        }
    }

    // Find the valid suffixes for the type: Standard plus registered conversions
    Set<String> suffixes = new HashSet<String>();
    suffixes.add(getFileStandardSuffix(type, category));
    for (ModuleDefinition def : _core.getModuleRegistry()
            .getModulesForType(DesignResourceConversionModuleType.class).values()) {
        DesignResourceConversionProperties props = (DesignResourceConversionProperties) def.getProperties();
        if (props.getDesignType() == type
                && (props.getCodeType() == null || props.getCodeType().equals(category))) {
            for (String sfx : props.getSuffixes()) {
                suffixes.add("." + sfx);
            }
        }
    }

    // Find the file
    FileObject file = findMatchingChild(folder, baseName, suffixes, false);
    if (file != null) {
        return new ModuleFile(file, prefix, category, type);
    } else {
        return null;
    }

}

From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignProvider.java

private static void determineChangedFileContainerResources(FileObject categoryFolder, FileObject source,
        FileObject target, FileObject baseFolder, String sourceEncoding, String targetEncoding,
        OverlayStatus status, Logger log, DesignFileValidator validator)
        throws WGDesignSyncException, NoSuchAlgorithmException, IOException {

    String targetResourcePath = baseFolder.getName().getRelativeName(target.getName());
    Set<String> involvedFiles = new HashSet<String>();

    // Completely new folder is a new file container
    if (!target.exists()) {

        status.addChangedResource(WGDocument.TYPE_FILECONTAINER, source, target, categoryFolder, ChangeType.NEW,
                targetResourcePath, null);
        // Als add all subfolders
        for (FileObject sourceFile : source.getChildren()) {
            if (sourceFile.getType().equals(FileType.FOLDER)) {
                FileObject targetFile = target.resolveFile(sourceFile.getName().getBaseName());
                determineChangedFileContainerResources(categoryFolder, sourceFile, targetFile, baseFolder,
                        sourceEncoding, targetEncoding, status, log, validator);
            }/*from   w  ww .j  av a  2s.c o  m*/
        }
        return;
    }

    if (target.getType().equals(FileType.FILE)) {
        throw new WGDesignSyncException(
                "Unable to apply overlay. Folder '" + baseFolder.getName().getRelativeName(target.getName())
                        + " already exists as file. Delete it to enable overlay management again");
    }

    // Determine change type by iterating through source child files and compare with target
    boolean overlayChanged = false;
    boolean baseChanged = false;
    boolean directConflict = false;

    for (FileObject sourceFile : source.getChildren()) {
        if (!isValidDesignFile(sourceFile, validator)) {
            continue;
        }

        FileObject targetFile = target.resolveFile(sourceFile.getName().getBaseName());
        String fileResourcePath = baseFolder.getName().getRelativeName(targetFile.getName());

        if (sourceFile.getType().equals(FileType.FOLDER)) {
            // Descend onto subcontainer
            determineChangedFileContainerResources(categoryFolder, sourceFile, targetFile, baseFolder,
                    sourceEncoding, targetEncoding, status, log, validator);
        } else if (sourceFile.getType().equals(FileType.FILE)) {

            // File does not exist. Look if it was once deployed.
            if (!targetFile.exists()) {

                ResourceData resourceData = status.getOverlayData().getOverlayResources().get(fileResourcePath);
                if (resourceData != null) {
                    // Did already exist. But did it have the same content?
                    String newHash = MD5HashingInputStream
                            .getStreamHash(sourceFile.getContent().getInputStream());
                    if (newHash.equals(resourceData.getMd5Hash())) {
                        overlayChanged = true;
                        involvedFiles.add(sourceFile.getName().getBaseName() + " (removed in overlay)");
                    } else {
                        baseChanged = true;
                        overlayChanged = true;
                        directConflict = true;
                        involvedFiles.add(
                                sourceFile.getName().getBaseName() + " (removed in overlay, changed in base)");
                    }

                }

                // Did not yet exist. It is a new file in the base version.
                else {
                    baseChanged = true;
                    involvedFiles.add(sourceFile.getName().getBaseName() + " (new in base)");
                }

            }

            // File does exist: Determine if is updated in base since the overlay file was deployed
            else {

                ResourceData originalHash = status.getOverlayData().getOverlayResources().get(fileResourcePath);
                if (originalHash == null) {
                    log.warn("There is no information about the original deployment state of resource '"
                            + fileResourcePath + "' so its change status cannot be determined.");
                    OverlayData.ResourceData resource = new OverlayData.ResourceData();
                    resource.setMd5Hash(
                            MD5HashingInputStream.getStreamHash(sourceFile.getContent().getInputStream()));
                    status.getOverlayData().setOverlayResource(fileResourcePath, resource);
                    continue;
                }

                // First determine if the resource really changed from what was distributed
                String newHash = MD5HashingInputStream.getStreamHash(sourceFile.getContent().getInputStream());
                if (newHash.equals(originalHash.getMd5Hash())) {
                    continue;
                }

                // Determine if the target file is the same as was distributed. If not then it was user modified, so it is a conflict
                String currentHash = MD5HashingInputStream
                        .getStreamHash(targetFile.getContent().getInputStream());
                if (!currentHash.equals(originalHash.getMd5Hash())) {
                    overlayChanged = true;
                    baseChanged = true;
                    directConflict = true;
                    involvedFiles.add(sourceFile.getName().getBaseName() + " (changed in base and overlay)");
                    break;
                }

                // It is a normal change
                else {
                    baseChanged = true;
                    involvedFiles.add(sourceFile.getName().getBaseName() + " (changed in base)");
                }
            }
        }
    }

    // Test the target files. Files may have been added there, or files from the previous base version may still be present that got deleted in the new base version.
    if (!baseChanged || !overlayChanged) {
        for (FileObject targetFile : target.getChildren()) {
            FileObject sourceFile = source.resolveFile(targetFile.getName().getBaseName());
            if (!sourceFile.exists()) {

                // Look if it was deployed with the previous base version.
                String fileResourcePath = baseFolder.getName().getRelativeName(targetFile.getName());
                ResourceData resourceData = status.getOverlayData().getOverlayResources().get(fileResourcePath);
                if (resourceData != null) {
                    // Was deployed. But with same contents?
                    String targetHash = MD5HashingInputStream
                            .getStreamHash(targetFile.getContent().getInputStream());
                    if (targetHash.equals(resourceData.getMd5Hash())) {
                        // This is a file that was from previous base version and got removed in the new base version.
                        involvedFiles.add(targetFile.getName().getBaseName() + " (removed from base)");
                        baseChanged = true;
                    }

                    // File got removed in new base version, updated in overlay. Conflict.
                    else {
                        baseChanged = true;
                        overlayChanged = true;
                        directConflict = true;
                        involvedFiles.add(targetFile.getName().getBaseName()
                                + " (removed from base, changed in overlay)");
                        break;
                    }
                } else {
                    involvedFiles.add(targetFile.getName().getBaseName() + " (new in overlay)");
                    overlayChanged = true;
                }
            }
        }
    }

    // Determine change type based on the found changes
    ChangeType changeType = null;
    if (baseChanged) {
        if (overlayChanged) {
            changeType = ChangeType.CONFLICT;
        } else {
            changeType = ChangeType.CHANGED;
        }
    }

    if (changeType != null) {
        status.addChangedResource(WGDocument.TYPE_FILECONTAINER, source, target, categoryFolder, changeType,
                targetResourcePath, involvedFiles);
    }

}

From source file:de.innovationgate.utils.WGUtils.java

/**
 * Returns a VFS file object for a folder. If the  folder does not exist it is created.
 * @param parent The parent folder of the retrieved folder
 * @param name The name of the folder to retrieve
 * @return The folder/* ww w .  j ava2  s.  c om*/
 * @throws IOException
 */
public static FileObject getOrCreateFolder(FileObject parent, String name) throws IOException {
    if (!parent.getType().equals(FileType.FOLDER)) {
        throw new IllegalArgumentException("Parent file is no folder: " + parent.getName().getPathDecoded());
    }

    FileObject folder = parent.resolveFile(name);
    if (!folder.exists()) {
        if (!folder.getFileSystem().hasCapability(Capability.CREATE)) {
            throw new IOException("File system of file " + folder.getURL().toString() + " is read only");
        }
        folder.createFolder();
    }
    if (!folder.getType().equals(FileType.FOLDER)) {
        throw new IllegalArgumentException("There is already a file of this name: " + name);
    }
    return folder;
}

From source file:de.innovationgate.utils.WGUtils.java

/**
 * Resolves an eventually present directory link file (variant with Commons VFS file objects). Use this with folders that either may be used themselves or that contain a directory link pointing to the directory to use.
 * @param file The directory that might contain a directory link file.
 * @return Either the path that an available directory link file points to or the given directory itself again.
 *///from   w  w w .jav a  2s  . c  o  m
public static FileObject resolveDirLink(FileObject file) throws FileSystemException {

    if (file != null && file.exists()) {
        if (file.getType().equals(FileType.FOLDER)) {
            FileObject link = file.resolveFile(DIRLINK_FILE);
            if (link.exists()) {
                // dir link present resolve

                Document doc;
                try {
                    InputStream fileInputStream = link.getContent().getInputStream();
                    String linkLocation = readDirLinkLocation(fileInputStream);
                    if (linkLocation != null) {
                        if (linkLocation.startsWith("../")) {
                            return file.resolveFile(linkLocation);
                        } else {
                            return file.getFileSystem().resolveFile(linkLocation);
                        }
                    }
                } catch (Exception e) {
                    Logger.getLogger("wga.utils")
                            .error("Unable to resolve dir link. '" + link.getName().getPath() + "'.", e);
                }
            }
        }
    }
    // no dir link or file does not exist - just return
    return file;

}

From source file:org.aludratest.service.file.FileInfo.java

/** Tells if the file is a directory. 
 *  @return true if a directory is referred, otherwise false */
public boolean isDirectory() {
    try {//from   w  w  w. j  av  a2  s.c  o  m
        return (file.getType() == FileType.FOLDER);
    } catch (FileSystemException e) {
        throw new TechnicalException("Error checking file type", e);
    }
}

From source file:org.aludratest.service.file.impl.AbstractFileAction.java

/** Tells if the given path represents a directory.
 * @param filePath filePath the path of the file to check
 * @return true if it's a directory, otherwise false */
public boolean isDirectory(String filePath) {
    File.verifyFilePath(filePath);
    try {//from   ww  w  . j a va  2 s .c o  m
        boolean result = getFileObject(filePath).getType() == FileType.FOLDER;
        logger.debug("{} is {}", filePath, (result ? "a folder" : "not a folder"));
        return result;
    } catch (IOException e) {
        throw new TechnicalException("Error accessing file or folder", e);
    }
}

From source file:org.aludratest.service.file.impl.FileActionImpl.java

/** Tells if the given path represents a directory. */
@Override//from  www . j  av  a2s.  co  m
public boolean isDirectory(String filePath) {
    FileUtil.verifyFilePath(filePath);
    try {
        boolean result = getFileObject(filePath).getType() == FileType.FOLDER;
        LOGGER.debug("{} is {}", filePath, (result ? "a folder" : "not a folder"));
        return result;
    } catch (FileSystemException e) {
        throw new TechnicalException("Error accessing file or folder", e);
    }
}

From source file:org.aludratest.service.file.impl.FileInfoImpl.java

/** Tells if the file is a directory.
 * @return true if a directory is referred, otherwise false */
@Override/*www .  j a v a2  s. c  o m*/
public boolean isDirectory() {
    try {
        return (file.getType() == FileType.FOLDER);
    } catch (FileSystemException e) {
        throw new TechnicalException("Error checking file type", e);
    }
}