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

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

Introduction

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

Prototype

FileName getName();

Source Link

Document

Returns the name of this file.

Usage

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

/**
 * Delete the file repsented by the file parameter.
 * /*from   w w w  .j  a  v a 2s .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.FileSystemDesignProvider.java

public static void createDowngradeFiles(FileSystemDesignProvider originalDesignProvider, OverlayData data,
        FileObject targetFolder, String targetEncoding, Logger log) throws Exception {

    for (Map.Entry<String, ResourceData> resourceEntry : data.getOverlayResources().entrySet()) {

        boolean changed = false;
        FileObject targetFile = targetFolder.resolveFile(resourceEntry.getKey());
        if (targetFile.exists()) {
            InputStream in = new BufferedInputStream(targetFile.getContent().getInputStream(), 4096);
            MD5HashingOutputStream out = new MD5HashingOutputStream(new NullOutputStream());
            resourceInToOut(in, targetEncoding, out, targetEncoding);
            if (!out.getHash().equals(resourceEntry.getValue().getMd5Hash())) {
                changed = true;//from  w w  w. j  a  va 2s. c  om
            }
        } else {
            changed = true;
        }

        if (changed == true) {
            String basePath = getOverlayResourcePathInBaseDesign(resourceEntry.getKey());
            FileObject sourceFile = originalDesignProvider.getBaseFolder().resolveFile(basePath);
            if (sourceFile.exists()) {
                FileObject conflictFile = createConflictFile(targetFile);
                log.info("Writing downgrade file of modified overlay resource " + resourceEntry.getKey()
                        + " to conflict file: "
                        + targetFolder.getName().getRelativeName(conflictFile.getName()));
                InputStream in = new BufferedInputStream(sourceFile.getContent().getInputStream(), 4096);
                OutputStream out = new BufferedOutputStream(conflictFile.getContent().getOutputStream(), 4096);
                resourceInToOut(in, originalDesignProvider.getFileEncoding(), out, targetEncoding);
            } else {
                log.warn("Overlay resource '" + resourceEntry.getKey()
                        + "' does not exist any more in the current base design version.");
            }
        }

    }

}

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

/**
 * Copy file./*from w  ww  . j  a  v a2  s . c om*/
 * 
 * @param from the from
 * @param to the to
 * 
 * @throws MotuException the motu exception
 */
public void copyFile(FileObject from, FileObject to) throws MotuException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("copyFile(FileObject, FileObject) - entering");
    }

    try {
        if ((to.exists()) && (to.getType() == FileType.FOLDER)) {
            throw new MotuException(String.format(
                    "File copy from '%s' to '%s' is rejected: the destination already exists and is a folder. You were about to loose all of the content of '%s' ",
                    from.getName().toString(), to.getName().toString(), to.getName().toString()));
        }
        this.copyFrom(from, to, Selectors.SELECT_ALL);
    } catch (MotuException e) {
        LOG.error("copyFile(FileObject, FileObject)", e);

        throw e;
    } catch (Exception e) {
        LOG.error("copyFile(FileObject, 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 copy file '%s' to '%s'", from.getName().toString(),
                to.getName().toString()), e);
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("copyFile(FileObject, FileObject) - exiting");
    }
}

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

protected FileObject initialDeployFileContainer(WGFileContainer con) throws IOException, InstantiationException,
        IllegalAccessException, WGAPIException, WGDesignSyncException {

    // Create container folder
    FileObject containerFolder = getFilesFolder().resolveFile(con.getName());
    _log.info("Creating file container folder " + getRelativePath(containerFolder));
    try {/*from   w  ww. j a v  a 2s .  c  o  m*/
        containerFolder.createFolder();
    } catch (FileSystemException e) {
        throw new WGInitialDeployException(
                "Could not create file container folder '" + containerFolder.getName().getPathDecoded() + "'",
                e);
    }

    // Create metadata file
    FCMetadata metaData = new FCMetadata(con);
    FileObject metadataFile = containerFolder
            .resolveFile(AbstractDesignFile.FILECONTAINER_METADATA_FILENAME + DesignDirectory.SUFFIX_METADATA);
    _log.info("Creating file container metadata file " + getRelativePath(metadataFile));
    Writer writer = createWriter(metadataFile);
    writer.write(_xstream.toXML(metaData.getInfo()));
    writer.close();

    // Create contained files
    Iterator fileNames = con.getFileNames().iterator();
    String fileName;
    FileObject file;
    while (fileNames.hasNext()) {
        fileName = (String) fileNames.next();
        InputStream in = con.getFileData(fileName);
        file = containerFolder.resolveFile(fileName);
        _log.info("Creating file container file " + getRelativePath(file));
        try {
            file.createFile();
        } catch (FileSystemException e) {
            throw new WGInitialDeployException(
                    "Could not create container file '" + getRelativePath(file) + "'", e);
        }
        OutputStream out = file.getContent().getOutputStream();
        WGUtils.inToOut(in, out, 2048);
        in.close();
        out.close();
    }

    return containerFolder;

}

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

protected FileObject initialDeployScriptModule(WGCSSJSModule script) throws IOException, InstantiationException,
        IllegalAccessException, WGAPIException, WGDesignSyncException {

    if (script.isMetadataModule()) {
        return null;
    }/* w w  w  .  j a  va 2s. c o  m*/

    ScriptInformation info = DesignDirectory.getScriptInformation(script.getCodeType());
    if (info == null) {
        _log.warn("Cannot deploy unknown script code type: " + script.getCodeType());
        return null;
    }

    // Get script type folder
    FileObject scriptTypeFolder = getScriptTypeFolder(info.getFolder());

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

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

    // Create metadata file
    ScriptMetadata metaData = new ScriptMetadata(script);
    FileObject metadataDir = currentDir.resolveFile(DesignDirectory.NAME_METADATADIR);
    if (!metadataDir.exists()) {
        _log.info("Creating script metadata directory " + getRelativePath(metadataDir));
        try {
            metadataDir.createFolder();
        } catch (FileSystemException e) {
            throw new WGInitialDeployException(
                    "Could not create metadata folder '" + getRelativePath(metadataDir) + "'");
        }
    }
    FileObject metadataFile = metadataDir.resolveFile(localName + DesignDirectory.SUFFIX_METADATA);
    _log.info("Creating script metadata file " + getRelativePath(metadataFile));
    writer = createWriter(metadataFile);
    writer.write(_xstream.toXML(metaData.getInfo()));
    writer.close();

    return codeFile;
}

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

protected List<ModuleFile> getTMLModuleFiles() throws FileSystemException, WGDesignSyncException {
    // Iterate thru media key folders
    FileObject[] mediaKeyFolders = getTmlFolder().getChildren();
    if (mediaKeyFolders == null) {
        throw new WGDesignSyncException("Cannot collect modules from directory '"
                + getTmlFolder().getName().getPathDecoded() + "'. Please verify directory existence.");
    }//from  ww  w . j  a  v  a2 s .  c o  m

    FileObject mediaKeyFolder;
    List<ModuleFile> files = new ArrayList();
    for (int mediaKeyIdx = 0; mediaKeyIdx < mediaKeyFolders.length; mediaKeyIdx++) {
        mediaKeyFolder = mediaKeyFolders[mediaKeyIdx];
        if (!mediaKeyFolder.getType().equals(FileType.FOLDER)) {
            continue;
        }
        if (!isValidDesignFile(mediaKeyFolder)) {
            continue;
        }
        String mediaKey = mediaKeyFolder.getName().getBaseName().toLowerCase();
        files.addAll(getModuleFiles(WGDocument.TYPE_TML, mediaKeyFolder, mediaKey));
    }
    return files;
}

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 {/*from w  w  w .  jav  a2s  . c  o  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;
}

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

/**
 * Delete all descendents of this file that match a selector.
 * //from   w  w  w.  j av a2s  .c  o 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:fr.cls.atoll.motu.library.misc.vfs.VFSManager.java

/**
 * Resolve file./*from  ww w  .j  a  va2s.  co  m*/
 * 
 * @param baseFile the base file
 * @param file the file
 * 
 * @return the file object
 * 
 * @throws MotuException the motu exception
 */
public FileObject resolveFile(FileObject baseFile, final String file) throws MotuException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("resolveFile(FileObject, String) - entering");
    }

    FileObject fileObject = null;
    open();
    if (opts == null) {
        opts = new FileSystemOptions();
    }

    try {

        // setSchemeOpts(baseFile.getName().getScheme());
        setSchemeOpts(baseFile.getURL());

        fileObject = standardFileSystemManager.resolveFile(baseFile, file, opts);

    } catch (FileSystemException e) {
        throw new MotuException(
                String.format("Unable to resolve uri '%s/%s' ", baseFile.getName().toString(), file), e);
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("resolveFile(FileObject, String) - exiting");
    }
    return fileObject;

}

From source file:de.innovationgate.wgpublisher.services.WGACoreServicesImpl.java

public void updateFSDesignResource(RemoteSession session, String path, DataSource content, long lastModified)
        throws WGAServiceException {
    if (!isAdminServiceEnabled()) {
        throw new WGAServiceException("Administrative services are disabled");
    }/*from   ww  w  .  j  a v  a2s . c o m*/

    if (!isAdminSession(session)) {
        throw new WGAServiceException("You need an administrative login to access this service.");
    }

    WGADesignSource source = _core.getDesignManager().getDesignSources()
            .get(WGAConfiguration.UID_DESIGNSOURCE_FILESYSTEM);
    if (source instanceof FileSystemDesignSource) {
        FileSystemDesignSource fsSource = (FileSystemDesignSource) source;
        try {
            fsSource.getDir().refresh();
            FileObject resource = fsSource.getDir().resolveFile(path);

            String basePath = fsSource.getDir().getURL().getPath();
            String resourcePath = resource.getURL().getPath();
            if (!resourcePath.startsWith(basePath)) {
                throw new WGAServiceException(
                        new IllegalArgumentException("Illegal design resource path '" + path + "'."));
            }

            resource.createFile();
            OutputStream out = resource.getContent().getOutputStream();
            InputStream in = content.getInputStream();
            WGUtils.inToOut(in, out, 1024);
            out.close();
            in.close();
            resource.getContent().setLastModifiedTime(lastModified);

            clearDesignFileCache(fsSource, fsSource.getDir().getName().getRelativeName(resource.getName()));

        } catch (FileSystemException e) {
            //            throw new WGAServiceException("Updating FSDesignResource '" + path + "' failed.", e);
        } catch (IOException e) {
            throw new WGAServiceException("Updating FSDesignResource '" + path + "' failed.", e);
        }
    }
}