Example usage for org.apache.commons.vfs2 FileObject exists

List of usage examples for org.apache.commons.vfs2 FileObject exists

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject exists.

Prototype

boolean exists() throws FileSystemException;

Source Link

Document

Determines if this file exists.

Usage

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

private void determineEncryption() throws WGDesignSyncException {
    try {//from   w w w .ja  v a 2 s .  c  o m
        FileObject file = getBaseFolder().resolveFile(DesignDirectory.OBFUSCATE_FLAGFILE);
        if (file.exists()) {
            _cipher = new DESEncrypter();
            _cipher.initObfuscation();
        }
    } catch (Exception e) {
        throw new WGDesignSyncException("Exception inizializing deobfuscation", e);
    }

}

From source file:fr.cls.atoll.motu.library.misc.vfs.VFSManager.java

/**
 * Delete the file repsented by the file parameter.
 * /* w  w w  .  j a va2  s  . c om*/
 * @param file the file
 * 
 * @return true, if successful
 * @throws MotuException
 */
public boolean deleteFile(FileObject file) throws MotuException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("deleteFile(FileObject) - entering");
    }

    boolean deleted = false;
    try {

        if (file.exists()) {
            if (file.getType() != FileType.FILE) {
                throw new MotuException(String.format("Delete file '%s' is rejected: it is a folder. ",
                        file.getName().toString()));
            }

            deleted = file.delete();
        }
    } catch (FileSystemException e) {
        LOG.error("deleteFile(FileObject)", e);

        // throw new MotuException(String.format("Unable to copy file '%s' to '%s'",
        // foSrc.getURL().toString(), foDest.getURL().toString()), e);
        throw new MotuException(String.format("Unable to delete '%s'", file.getName().toString()), e);
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("deleteFile(FileObject) - exiting");
    }
    return deleted;
}

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

protected void updateConfiguration()
        throws IOException, WGDesignSyncException, FileSystemException, InvalidCSConfigVersionException {

    boolean configUpdated = false;

    // Import config files
    FileObject designFile = getSyncInfoFile();
    long currentDesignFileTimestamp = designFile.getContent().getLastModifiedTime();
    if (_designFileTimestamp == null || currentDesignFileTimestamp > _designFileTimestamp.longValue()) {
        _designFileTimestamp = new Long(currentDesignFileTimestamp);
        importSyncInfo(designFile);//from  ww  w  .  ja v  a2  s .  com
        configUpdated = true;
    }

    FileObject csconfigFile = getFilesFolder().resolveFile("system/csconfig.xml");
    if (csconfigFile.exists()) {
        long currentCsconfigTimestamp = csconfigFile.getContent().getLastModifiedTime();
        if (_csconfigTimestamp == null || currentCsconfigTimestamp > _csconfigTimestamp.longValue()) {
            _csconfigTimestamp = new Long(currentCsconfigTimestamp);
            importCSConfig(csconfigFile);
            configUpdated = true;
        }
    }

    // Load configuration settings
    if (configUpdated) {
        determineDesignEncoding();
        determineEncryption();
        determineSyncDefaults();
    }
}

From source file:fr.cls.atoll.motu.library.misc.vfs.VFSManager.java

/**
 * Refresh ftp cache.//from  ww w. j a v  a2 s  . c o  m
 * 
 * @param fileObject the file object
 * @throws FileSystemException
 */
public void refreshFtpCache(FileObject fileObject) throws FileSystemException {

    if (fileObject == null) {
        return;
    }

    if (!(fileObject.getFileSystem() instanceof FtpFileSystem)) {
        return;
    }
    if (fileObject.exists()) {
        return;
    }
    FileObject fileObjectParent = fileObject.getParent();
    if (fileObjectParent == null) {
        return;
    }

    fileObjectParent.getType(); // force to attach : needed for force refresh
    fileObjectParent.refresh();

}

From source file:fr.cls.atoll.motu.library.misc.vfs.VFSManager.java

/**
 * Delete all descendents of this file that match a selector.
 * /*ww  w . ja  v  a  2s . co  m*/
 * @param file the file
 * @param selector the selector
 * 
 * @return true, if successful
 * @throws MotuException
 */
public boolean delete(FileObject file, FileSelector selector) throws MotuException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("delete(FileObject, FileSelector) - entering");
    }

    int deleted = 0;
    try {
        if (file.exists()) {
            deleted = file.delete(selector);
        }
    } catch (FileSystemException e) {
        LOG.error("delete(FileObject, FileSelector)", e);

        // throw new MotuException(String.format("Unable to copy file '%s' to '%s'",
        // foSrc.getURL().toString(), foDest.getURL().toString()), e);
        throw new MotuException(String.format("Unable to delete '%s'", file.getName().toString()), e);
    }
    boolean returnboolean = (deleted > 0);
    if (LOG.isDebugEnabled()) {
        LOG.debug("delete(FileObject, FileSelector) - exiting");
    }
    return returnboolean;
}

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

public WGDocumentCore createDesignDocument(int type, String name, String mediaKey)
        throws WGAuthorisationException, WGCreationException {

    try {/*from ww  w .  j  a v a2s .c o  m*/
        switch (type) {

        case WGDocument.TYPE_TML: {
            FileObject newTml = getTmlFolder()
                    .resolveFile(mediaKey.toLowerCase() + "/" + name.replace(":", "/") + ".tml");
            if (newTml.exists()) {
                throw new WGCreationException(
                        "Document already exists: " + (new WGDocumentKey(type, name, mediaKey)).toString());
            }
            newTml.createFile();
            break;
        }

        case WGDocument.TYPE_CSSJS: {
            String suffix = DesignDirectory.getScriptInformation(mediaKey).getSuffix();
            FileObject newScript = getScriptTypeFolder(mediaKey)
                    .resolveFile(mediaKey.toLowerCase() + "/" + name.replace(":", "/") + suffix);
            if (newScript.exists()) {
                throw new WGCreationException(
                        "Document already exists: " + (new WGDocumentKey(type, name, mediaKey)).toString());
            }
            newScript.createFile();
            break;
        }

        case WGDocument.TYPE_FILECONTAINER: {
            FileObject newFC = getFilesFolder().resolveFile(name.replace(":", "/"));
            if (newFC.exists()) {
                throw new WGCreationException(
                        "Document already exists: " + (new WGDocumentKey(type, name, mediaKey)).toString());
            }
            newFC.createFolder();
            break;
        }

        }

        clearCache();
        return getDesignObject(type, name, mediaKey);
    } catch (Exception e) {
        throw new WGCreationException(
                "Exception creating design document " + (new WGDocumentKey(type, name, mediaKey)).toString(),
                e);
    }

}

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

protected void fetchFileSystem(WGACore core) throws WGDesignSyncException, IOException {

    // Dont run if already fetched
    if (_fsResources != null) {
        return;/*  ww  w .  ja va 2 s.  c o m*/
    }

    FileObject baseFolder = null;

    try {
        baseFolder = _fsManager.resolveFile(_designPath);
        if (!baseFolder.exists() || !baseFolder.getType().equals(FileType.FOLDER)) {
            throw new WGDesignSyncException(
                    "Directory location " + _designPath + " either does not exist or is not directory");
        }
    } catch (FileSystemException e) {
        throw new WGDesignSyncException("Unparseable VFS URI: " + _designPath, e);
    }

    _fsResources = new FileSystemResources(baseFolder);
}

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

private FileObject findMatchingChild(FileObject folder, String baseName, Set<String> suffixes,
        boolean exactMatch) throws FileSystemException {
    FileObject matchingChild = null;

    // First try fast straight fetch using lowercase file
    for (String suffix : suffixes) {
        FileObject straightChild = folder.resolveFile(baseName.toLowerCase() + suffix.toLowerCase());
        if (straightChild.exists()) {
            return straightChild;
        }//www . j av a  2  s.  c om
    }

    // Secondly we iterate over all children and to a case-insensitive
    // equals
    FileObject[] children = folder.getChildren();

    for (String suffix : suffixes) {
        for (FileObject child : children) {
            if (exactMatch) {
                if (child.getName().getBaseName().equalsIgnoreCase(baseName + suffix)) {
                    matchingChild = child;
                    break;
                }
            } else {
                String fileName = child.getName().getBaseName().toLowerCase();
                String elementName = baseName.toLowerCase() + suffix.toLowerCase();
                if (fileName.startsWith(elementName) && fileName.indexOf(".", elementName.length()) == -1) {
                    matchingChild = child;
                    break;
                }
            }
        }
    }

    return matchingChild;
}

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

private void importCSConfig(FileObject csconfigFile)
        throws FileSystemException, IOException, WGDesignSyncException, InvalidCSConfigVersionException {
    if (csconfigFile.exists()) {
        _csconfig = CSConfig.load(csconfigFile);
    }//from  w ww. ja v a 2s  .  c  o  m

}

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

protected FileObject initialDeployTMLModule(WGTMLModule mod) throws IOException, InstantiationException,
        IllegalAccessException, WGAPIException, WGDesignSyncException {

    // Find/create media key folder
    FileObject mediaKeyFolder = getTmlFolder().resolveFile(mod.getMediaKey());
    if (!mediaKeyFolder.exists()) {
        _log.info("Creating media key folder " + getRelativePath(mediaKeyFolder));
        try {/*ww  w  .ja  va  2  s  .co m*/
            mediaKeyFolder.createFolder();
        } catch (FileSystemException e) {
            throw new WGInitialDeployException(
                    "Could not create media key folder '" + mediaKeyFolder.getName().getPathDecoded() + "'");
        }
    }

    // Eventually create intermediate directories
    List<String> path = WGUtils.deserializeCollection(mod.getName(), ":", true);
    String localName = (String) path.get(path.size() - 1);
    FileObject currentDir = mediaKeyFolder;
    for (int i = 0; i < path.size() - 1; i++) {
        currentDir = currentDir.resolveFile((String) path.get(i));
        if (!currentDir.exists()) {
            _log.info("Creating tml category directory " + getRelativePath(currentDir));
            try {
                currentDir.createFolder();
            } catch (FileSystemException e) {
                throw new WGInitialDeployException(
                        "Could not create tml category folder '" + getRelativePath(currentDir) + "'", e);
            }
        } else if (!currentDir.getType().equals(FileType.FOLDER)) {
            throw new WGInitialDeployException(
                    "Cannot deploy " + mod.getDocumentKey() + " to sync folder because the directory name '"
                            + path.get(i) + "' is already used by another file");

        }
    }

    // Create code file
    FileObject tmlCodeFile = currentDir.resolveFile(localName + DesignDirectory.SUFFIX_TML);
    _log.info("Creating tml module file " + getRelativePath(tmlCodeFile));
    try {
        tmlCodeFile.createFile();
    } catch (FileSystemException e) {
        throw new WGInitialDeployException(
                "Could not create tml code file '" + getRelativePath(tmlCodeFile) + "'");
    }
    Writer writer = createWriter(tmlCodeFile);
    writer.write(mod.getCode());
    writer.close();

    // Create metadata file
    TMLMetadata metaData = new TMLMetadata(mod);
    FileObject metadataDir = currentDir.resolveFile(DesignDirectory.NAME_METADATADIR);
    if (!metadataDir.exists()) {
        _log.info("Creating tml metadata directory " + getRelativePath(metadataDir));
        try {
            metadataDir.createFolder();
        } catch (FileSystemException e) {
            throw new WGInitialDeployException(
                    "Could not create metadata folder '" + metadataDir.getName().getPathDecoded() + "'");
        }
    }

    FileObject metadataFile = metadataDir.resolveFile(localName + DesignDirectory.SUFFIX_METADATA);
    _log.info("Creating tml metadata file " + getRelativePath(metadataFile));
    writer = createWriter(metadataFile);
    writer.write(_xstream.toXML(metaData.getInfo()));
    writer.close();

    return tmlCodeFile;
}