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

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

Introduction

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

Prototype

void createFile() throws FileSystemException;

Source Link

Document

Creates this file, if it does not exist.

Usage

From source file:org.pentaho.hadoop.shim.HadoopConfigurationLocatorTest.java

@BeforeClass
public static void setup() throws Exception {
    // Create a test hadoop configuration "a"
    FileObject ramRoot = VFS.getManager().resolveFile(HADOOP_CONFIGURATIONS_PATH);
    FileObject aConfigFolder = ramRoot.resolveFile("a");
    if (aConfigFolder.exists()) {
        aConfigFolder.delete(new AllFileSelector());
    }//  w w  w  .j a  v  a  2 s  . c o m
    aConfigFolder.createFolder();

    assertEquals(FileType.FOLDER, aConfigFolder.getType());

    // Create the properties file for the configuration as hadoop-configurations/a/config.properties
    configFile = aConfigFolder.resolveFile("config.properties");
    Properties p = new Properties();
    p.setProperty("name", "Test Configuration A");
    p.setProperty("classpath", "");
    p.setProperty("ignore.classes", "");
    p.setProperty("library.path", "");
    p.setProperty("required.classes", HadoopConfigurationLocatorTest.class.getName());
    p.store(configFile.getContent().getOutputStream(), "Test Configuration A");
    configFile.close();

    // Create the implementation jar
    FileObject implJar = aConfigFolder.resolveFile("a-config.jar");
    implJar.createFile();

    // Use ShrinkWrap to create the jar and write it out to VFS
    JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "a-configuration.jar")
            .addAsServiceProvider(HadoopShim.class, MockHadoopShim.class).addClass(MockHadoopShim.class);
    archive.as(ZipExporter.class).exportTo(implJar.getContent().getOutputStream());
}

From source file:org.pentaho.hadoop.shim.HadoopConfigurationLocatorTest.java

@Test(expected = ConfigurationException.class)
public void createConfigurationLoader_root_not_a_folder() throws Exception {
    HadoopConfigurationLocator locator = new HadoopConfigurationLocator();
    // Try to create a configuration based on a file, not a folder
    FileObject buildProperties = VFS.getManager().resolveFile("ram:///test.file");
    buildProperties.createFile();
    assertEquals(FileType.FILE, buildProperties.getType());
    locator.createConfigurationLoader(buildProperties, null, null, new ShimProperties());
}

From source file:org.pentaho.vfs.ui.VfsBrowser.java

public boolean renameItem(TreeItem ti, String newName) throws FileSystemException {
    FileObject file = (FileObject) ti.getData();
    FileObject newFileObject = file.getParent().resolveFile(newName);

    if (file.canRenameTo(newFileObject)) {
        if (!newFileObject.exists()) {
            newFileObject.createFile();
        } else {// w  w w .  j  a  va2s. c  o m
            return false;
        }
        file.moveTo(newFileObject);
        ti.setText(newName);
        ti.setData(newFileObject);
        return true;
    } else {
        return false;
    }

}

From source file:org.pentaho.vfs.util.VfsHelper.java

public FileObject saveFileAs(String uri, InputStream is) throws FileSystemException, IOException {
    if (fsManager != null) {
        FileObject savedFile = fsManager.resolveFile(uri);
        if (!savedFile.exists()) {
            savedFile.createFile();
        }// ww w .  java2s  . co  m
        IOUtils.copy(is, savedFile.getContent().getOutputStream());
        return savedFile;
    }
    throw new FileSystemException(Messages.getString("VfsHelper.operationFailed")); //$NON-NLS-1$
}

From source file:org.sitemap4j.dom4j.Dom4jSiteMapIndexWriterTest.java

@Test
public void test() throws Exception {
    // Given//from w w  w  .  jav  a2 s .  co  m
    final MutableSiteMapIndex siteMapIndex = ArrayListMutableSiteMapIndex.create();
    siteMapIndex.add(SiteMapLocation.create(getRandomUrl()));
    siteMapIndex.add(SiteMapLocation.create(getRandomUrl(), new DateTime(DateTimeZone.UTC)));

    final FileObject siteMapIndexFile = VFS.getManager().resolveFile("ram:///sitemap-index.xml");
    siteMapIndexFile.createFile();
    final OutputStream outputStream = siteMapIndexFile.getContent().getOutputStream();

    // When
    final SiteMapIndexWriter writer = Dom4jSiteMapIndexWriter.forSiteMapIndex(siteMapIndex);
    writer.write(outputStream);

    // Then
    final SiteMapIndexReader siteMapIndexReader = Dom4jSiteMapIndexReader.create();
    assertEquals(siteMapIndex, siteMapIndexReader
            .read(RawSiteMap.fromInputStream(siteMapIndexFile.getContent().getInputStream())));
}

From source file:org.sitemap4j.dom4j.Dom4jSiteMapWriterTest.java

@Test
public void test() throws Exception {
    // Given//  w ww.ja  va2  s  . c om
    final FileObject siteMapFile = VFS.getManager().resolveFile("ram:///sitemap.xml");
    siteMapFile.createFile();
    final OutputStream outputStream = siteMapFile.getContent().getOutputStream();

    final MutableSiteMap siteMap = ArrayListMutableSiteMap.create();
    siteMap.add(getSiteMapUrl().build());
    siteMap.add(getSiteMapUrl().build());

    // When
    final SiteMapWriter writer = Dom4jSiteMapWriter.forSiteMap(siteMap);
    writer.write(outputStream);

    // Then             
    final SiteMapReader siteMapReader = Dom4jSiteMapReader.create();

    assertEquals(siteMap,
            siteMapReader.read(RawSiteMap.fromInputStream(siteMapFile.getContent().getInputStream())));
}

From source file:org.wso2.carbon.connector.FileAppend.java

/**
 * @param destination    Location if the file
 * @param content        Content that is going to be added
 * @param encoding       Encoding type//from   w w  w  . java2 s  . co  m
 * @param messageContext The message context that is generated for processing the file
 * @return true/false
 */
private boolean appendFile(String destination, String content, String encoding, MessageContext messageContext) {
    OutputStream out = null;
    boolean resultStatus = false;
    FileObject fileObj = null;
    StandardFileSystemManager manager = null;
    try {
        manager = FileConnectorUtils.getManager();
        fileObj = manager.resolveFile(destination, FileConnectorUtils.init(messageContext));
        if (!fileObj.exists()) {
            fileObj.createFile();
        }
        out = fileObj.getContent().getOutputStream(true);
        if (StringUtils.isEmpty(encoding)) {
            IOUtils.write(content, out, DEFAULT_ENCODING);
        } else {
            IOUtils.write(content, out, encoding);
        }
        resultStatus = true;

        if (log.isDebugEnabled()) {
            log.debug("File appending completed. " + destination);
        }
    } catch (IOException e) {
        handleException("Error while appending a file.", e, messageContext);
    } finally {
        try {
            if (fileObj != null) {
                //close the file object
                fileObj.close();
            }
        } catch (FileSystemException e) {
            log.error("Error while closing FileObject: " + e.getMessage(), e);
        }
        try {
            if (out != null) {
                //close the output stream
                out.close();
            }
        } catch (IOException e) {
            log.error("Error while closing OutputStream: " + e.getMessage(), e);
        }
        if (manager != null) {
            //close the StandardFileSystemManager
            manager.close();
        }
    }
    return resultStatus;
}

From source file:org.wso2.carbon.connector.FileAppendConnector.java

/**
 * Add the content into file./* w  w w.j av  a2 s  . c  om*/
 *
 * @param messageContext The message context that is generated for processing the file.
 * @return true, if the content is successfully appended.
 * @throws FileSystemException On error parsing the file name, determining if the file exists and creating the
 * file.
 */
private boolean appendFile(MessageContext messageContext) throws FileSystemException {
    String destination = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.NEW_FILE_LOCATION);
    String content = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.CONTENT);
    String encoding = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.ENCODING);
    if (StringUtils.isEmpty(encoding)) {
        encoding = FileConstants.DEFAULT_ENCODING;
    }
    FileSystemOptions opts = FileConnectorUtils.init(messageContext);
    OutputStream out = null;
    FileObject fileObj = null;
    StandardFileSystemManager manager = FileConnectorUtils.getManager();
    try {
        fileObj = manager.resolveFile(destination, opts);
        if (!fileObj.exists()) {
            fileObj.createFile();
        }
        // True, if the content should be appended.
        out = fileObj.getContent().getOutputStream(true);
        IOUtils.write(content, out, encoding);
        if (log.isDebugEnabled()) {
            log.debug("File appending completed. " + destination);
        }
    } catch (IOException e) {
        throw new SynapseException("Error while appending content", e);
    } finally {
        try {
            if (fileObj != null) {
                // close the file object
                fileObj.close();
            }
        } catch (FileSystemException e) {
            log.error("Error while closing FileObject", e);
        }
        try {
            if (out != null) {
                // close the output stream
                out.close();
            }
        } catch (IOException e) {
            log.error("Error while closing OutputStream", e);
        }
        // close the StandardFileSystemManager
        manager.close();
    }
    return true;
}

From source file:org.wso2.carbon.connector.FileCreate.java

/**
 * Create a file with Apache commons/* w w w . j  a va2  s . c o  m*/
 *
 * @param source         Location of the file/folder
 * @param content        Content in a file
 * @param encoding       Encoding type
 * @param messageContext The message context that is generated for processing the file
 * @return Return the status
 */
private boolean createFile(String source, String content, String encoding, MessageContext messageContext) {
    boolean resultStatus = false;
    StandardFileSystemManager manager;
    try {
        OutputStream out = null;
        manager = FileConnectorUtils.getManager();
        if (manager != null) {
            FileObject sourceFile = manager.resolveFile(source, FileConnectorUtils.init(messageContext));
            try {
                if (FileConnectorUtils.isFolder(sourceFile)) {
                    sourceFile.createFolder();
                } else {
                    if (StringUtils.isEmpty(content)) {
                        sourceFile.createFile();
                    } else {
                        FileContent fileContent = sourceFile.getContent();
                        out = fileContent.getOutputStream(true);
                        if (StringUtils.isEmpty(encoding)) {
                            IOUtils.write(content, out, DEFAULT_ENCODING);
                        } else {
                            IOUtils.write(content, out, encoding);
                        }
                    }
                }
                resultStatus = true;
                if (log.isDebugEnabled()) {
                    log.debug("File creation completed with " + source);
                }
            } finally {
                try {
                    if (sourceFile != null) {
                        sourceFile.close();
                    }
                } catch (FileSystemException e) {
                    log.error("Error while closing FileObject: " + e.getMessage(), e);
                }
                try {
                    if (out != null) {
                        out.close();
                    }
                } catch (IOException e) {
                    log.error("Error while closing OutputStream: " + e.getMessage(), e);
                }
                manager.close();
            }
        }
    } catch (IOException e) {
        handleException("Unable to create a file/folder.", e, messageContext);
    }
    return resultStatus;
}

From source file:org.wso2.carbon.connector.FileCreateConnector.java

/**
 * Create a new file/folder./*from ww w  .  j a  v  a2 s  . c  om*/
 *
 * @param messageContext The message context that is generated for processing the file.
 * @return true, if the file/folder is successfully created.
 */
private boolean createFile(MessageContext messageContext) throws FileSystemException {
    String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_LOCATION);
    String content = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.CONTENT);
    String encoding = (String) ConnectorUtils.lookupTemplateParamater(messageContext, FileConstants.ENCODING);
    if (StringUtils.isEmpty(encoding)) {
        encoding = FileConstants.DEFAULT_ENCODING;
    }
    StandardFileSystemManager manager = FileConnectorUtils.getManager();
    FileObject sourceFile = manager.resolveFile(source, FileConnectorUtils.init(messageContext));
    if (FileConnectorUtils.isFolder(sourceFile)) {
        sourceFile.createFolder();
    } else {
        if (StringUtils.isEmpty(content)) {
            sourceFile.createFile();
        } else {
            OutputStream out = sourceFile.getContent().getOutputStream();
            try {
                IOUtils.write(content, out, encoding);
            } catch (IOException e) {
                throw new SynapseException("Error while writing the file content", e);
            } finally {
                try {
                    // close the file object
                    sourceFile.close();
                } catch (FileSystemException e) {
                    log.error("Error while closing FileObject", e);
                }
                try {
                    if (out != null) {
                        out.close();
                    }
                } catch (IOException e) {
                    log.error("Error while closing OutputStream", e);
                }
                // close the StandardFileSystemManager
                manager.close();
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("File creation completed with " + source);
        }
    }
    return true;
}