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

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

Introduction

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

Prototype

void createFolder() throws FileSystemException;

Source Link

Document

Creates this folder, if it does not exist.

Usage

From source file:org.eobjects.datacleaner.user.DataCleanerHome.java

private static void copyIfNonExisting(FileObject candidate, FileSystemManager manager, String filename)
        throws FileSystemException {
    FileObject file = candidate.resolveFile(filename);
    if (file.exists()) {
        logger.info("File already exists in DATACLEANER_HOME: " + filename);
        return;// w  ww.  j a v  a 2  s . c  om
    }
    FileObject parentFile = file.getParent();
    if (!parentFile.exists()) {
        parentFile.createFolder();
    }

    final ResourceManager resourceManager = ResourceManager.get();
    final URL url = resourceManager.getUrl("datacleaner-home/" + filename);

    InputStream in = null;
    OutputStream out = null;
    try {
        in = url.openStream();
        out = file.getContent().getOutputStream();

        FileHelper.copy(in, out);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        FileHelper.safeClose(in, out);
    }
}

From source file:org.esupportail.portlet.filemanager.services.vfs.VfsAccessImpl.java

@Override
public String createFile(String parentPath, String title, String type,
        SharedUserPortletParameters userParameters) {
    try {//from   w w  w  .ja v a 2 s. c  o  m
        FileObject parent = cd(parentPath, userParameters);
        FileObject child = parent.resolveFile(title);
        if (!child.exists()) {
            if ("folder".equals(type)) {
                child.createFolder();
                log.info("folder " + title + " created");
            } else {
                child.createFile();
                log.info("file " + title + " created");
            }
            return child.getName().getPath();
        } else {
            log.info("file " + title + " already exists !");
        }
    } catch (FileSystemException e) {
        log.info("can't create file because of FileSystemException : " + e.getMessage(), e);
    }
    return null;
}

From source file:org.kalypso.commons.io.VFSUtilities.java

/**
 * This function will copy one directory to another one. If the destination base directory does not exist, it will be
 * created./*from w  ww .  j a va 2s .com*/
 *
 * @param source
 *          The source directory.
 * @param destination
 *          The destination directory.
 * @param overwrite
 *          If set, always overwrite existing and newer files
 */
public static void copyDirectoryToDirectory(final FileObject source, final FileObject destination,
        final boolean overwrite) throws IOException {
    if (!FileType.FOLDER.equals(source.getType()))
        throw new IllegalArgumentException(
                Messages.getString("org.kalypso.commons.io.VFSUtilities.3") + source.getURL()); //$NON-NLS-1$

    if (destination.exists()) {
        if (!FileType.FOLDER.equals(destination.getType()))
            throw new IllegalArgumentException(
                    Messages.getString("org.kalypso.commons.io.VFSUtilities.4") + destination.getURL()); //$NON-NLS-1$
    } else {
        KalypsoCommonsDebug.DEBUG.printf("Creating directory '%s'...%", destination.getName()); //$NON-NLS-1$
        destination.createFolder();
    }

    final FileObject[] children = source.getChildren();
    for (final FileObject child : children) {
        if (FileType.FILE.equals(child.getType())) {
            /* Need a destination file with the same name as the source file. */
            final FileObject destinationFile = destination.resolveFile(child.getName().getBaseName());

            /* Copy ... */
            copyFileTo(child, destinationFile, overwrite);
        } else if (FileType.FOLDER.equals(child.getType())) {
            /* Need the same name for destination directory, as the source directory has. */
            final FileObject destinationDir = destination.resolveFile(child.getName().getBaseName());

            /* Copy ... */
            KalypsoCommonsDebug.DEBUG.printf("Copy directory %s to %s ...", child.getName(), //$NON-NLS-1$
                    destinationDir.getName());
            copyDirectoryToDirectory(child, destinationDir, overwrite);
        } else {
            KalypsoCommonsDebug.DEBUG.printf("Could not determine the file type ...%n"); //$NON-NLS-1$
        }
    }
}

From source file:org.kalypso.commons.io.VFSUtilities.java

/**
 * This function creates a temporary directory, which has a unique file name.
 *
 * @param prefix//  w  w  w.  j a  v a  2  s. co  m
 *          This prefix will be used for the temporary directory.
 * @param parentDir
 *          The parent directory. In it the new directory will be created.
 * @return The new unique directory.
 */
public static FileObject createTempDirectory(final String prefix, final FileObject parentDir,
        final FileSystemManager fsManager) throws FileSystemException {
    while (true) {
        final String dirParent = parentDir.getURL().toExternalForm();
        final String dirName = prefix + String.valueOf(System.currentTimeMillis());

        final FileObject newDir = fsManager.resolveFile(dirParent + "/" + dirName); //$NON-NLS-1$
        if (newDir.exists()) {
            continue;
        }

        KalypsoCommonsDebug.DEBUG.printf("Creating folder %s ...%n", newDir.getName().getPath()); //$NON-NLS-1$
        newDir.createFolder();
        return newDir;
    }
}

From source file:org.kalypso.commons.io.VFSUtilities.java

/**
 * Moves the complete content of one directory into another.
 *
 * @throws IOException/*from   w ww. j  a  va2 s .c o m*/
 *           If the move failed.
 */
public static void moveContents(final File sourceDir, final File dest) throws IOException {
    final FileSystemManager vfsManager = VFSUtilities.getManager();
    final FileObject source = vfsManager.toFileObject(sourceDir);
    final FileObject destDir = vfsManager.toFileObject(dest);

    final FileObject[] findFiles = source.findFiles(new AllFileSelector());
    // Might happen, if source does not exists... shouldn't we check this?
    if (findFiles == null)
        return;

    for (final FileObject fileObject : findFiles) {
        if (FileType.FILE.equals(fileObject.getType())) {
            final String relPath = source.getName().getRelativeName(fileObject.getName());
            final FileObject destFile = destDir.resolveFile(relPath, NameScope.DESCENDENT_OR_SELF);
            final FileObject folder = destFile.getParent();
            folder.createFolder();
            fileObject.moveTo(destFile);
        }
    }
}

From source file:org.kalypso.project.database.server.trigger.TriggerHelper.java

public static void handleBean(final KalypsoProjectBean bean, final IConfigurationElement element)
        throws Exception {
    /* resolve trigger extension class */
    final String pluginid = element.getContributor().getName();
    final Bundle bundle = Platform.getBundle(pluginid);
    final Class<?> triggerClass = bundle.loadClass(element.getAttribute("class")); //$NON-NLS-1$
    final Constructor<?> constructor = triggerClass.getConstructor();

    final IProjectDatabaseTrigger trigger = (IProjectDatabaseTrigger) constructor.newInstance();

    final FileSystemManager manager = VFSUtilities.getManager();

    /* resolve global dir */
    final String urlGlobalPath = System.getProperty(IProjectDataBaseServerConstant.SERVER_GLOBAL_DATA_PATH);
    final FileObject folderGlobal = manager.resolveFile(urlGlobalPath);
    if (!folderGlobal.exists()) {
        folderGlobal.createFolder();
    }// w ww . ja va 2s .c  om

    /* global "global" dir */
    final FileObject destinationCommonFolder = folderGlobal.resolveFile(COMMON_FOLDER);
    if (!destinationCommonFolder.exists()) {
        destinationCommonFolder.createFolder();
    }

    /* global project dir */
    final FileObject destinationProjectFolder = folderGlobal.resolveFile(bean.getUnixName());
    if (!destinationProjectFolder.exists()) {
        destinationProjectFolder.createFolder();
    }

    trigger.handleCommonData(bean, destinationCommonFolder);
    trigger.handleProjectData(bean, destinationProjectFolder);
}

From source file:org.mycore.backend.filesystem.MCRCStoreVFS.java

@Override
public void init(String storeId) {
    super.init(storeId);

    uri = MCRConfiguration.instance().getString(storeConfigPrefix + "URI");
    String check = MCRConfiguration.instance().getString(storeConfigPrefix + "StrictHostKeyChecking", "no");

    try {/*from w ww  .ja  va2s  . c  om*/
        fsManager = VFS.getManager();

        opts = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, check);

        FileObject baseDir = getBase();

        // Create a folder, if it does not exist or throw an
        // exception, if baseDir is not a folder
        baseDir.createFolder();

        if (!baseDir.isWriteable()) {
            String msg = "Content store base directory is not writeable: " + uri;
            throw new MCRConfigurationException(msg);
        }
    } catch (FileSystemException ex) {
        throw new MCRException(ex.getCode(), ex);
    }
}

From source file:org.obiba.opal.core.runtime.DefaultOpalRuntime.java

private void ensureFolder(String path) throws FileSystemException {
    FileObject folder = getFileSystem().getRoot().resolveFile(path);
    folder.createFolder();
}

From source file:org.obiba.opal.core.service.ProjectsServiceImpl.java

@NotNull
@Override//from w  w  w.j  a  v a2 s  . co m
public FileObject getProjectDirectory(@NotNull Project project)
        throws NoSuchIdentifiersMappingException, FileSystemException {
    FileObject projectDir = opalRuntime.getFileSystem().getRoot().resolveFile(PROJECTS_DIR)
            .resolveFile(project.getName());
    projectDir.createFolder();
    return projectDir;
}

From source file:org.obiba.opal.core.service.SubjectProfileServiceImpl.java

private void ensureUserHomeExists(String username) {
    try {// ww  w . j av  a  2  s  . c o  m
        if (!opalRuntime.hasFileSystem())
            return;
        FileObject home = opalRuntime.getFileSystem().getRoot().resolveFile("/home/" + username);
        if (!home.exists()) {
            log.info("Creating user home: /home/{}", username);
            home.createFolder();
        }
    } catch (FileSystemException e) {
        log.error("Failed creating user home.", e);
    }
}