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:org.pentaho.di.job.entries.sftp.JobEntrySFTPIT.java

@Test
public void getFile_WhenDestinationIsSetViaVariable() throws Exception {
    final String localDir = TestUtils.createTempDir();
    KettleVFS.getFileObject(localDir).createFolder();

    final String myVar = "my-var";

    final String sftpDir = "job-entry-sftp-test";
    final String fileName = "file.txt";

    uploadFile(sftpDir, fileName);/*from www  .  j  a va2s  .co m*/

    JobEntrySFTP job = new JobEntrySFTP();
    job.setVariable(myVar, localDir);

    Job parent = mock(Job.class);
    when(parent.isStopped()).thenReturn(false);
    job.setParentJob(parent);
    job.setLogLevel(LogLevel.NOTHING);

    job.setUserName(server.getUsername());
    job.setPassword(server.getPassword());
    job.setServerName("localhost");
    job.setServerPort(Integer.toString(server.getPort()));
    job.setScpDirectory(sftpDir);
    job.setTargetDirectory(String.format("${%s}", myVar));

    job.execute(new Result(), 1);

    FileObject downloaded = KettleVFS.getFileObject(localDir + "/" + fileName);
    assertTrue(downloaded.exists());
    downloaded.delete();
}

From source file:org.pentaho.di.job.entries.sftp.SFTPClientIT.java

@Test
public void getFile() throws Exception {
    final byte[] data = "getFile()".getBytes();

    channel.connect();// w w  w.  ja  v a2s.co m
    channel.put(new ByteArrayInputStream(data), "downloaded.txt");

    client.get(KettleVFS.getFileObject("ram://downloaded.txt"), "downloaded.txt");

    FileObject downloaded = KettleVFS.getFileObject("ram://downloaded.txt");
    assertTrue(downloaded.exists());
    assertTrue(IOUtils.contentEquals(downloaded.getContent().getInputStream(), new ByteArrayInputStream(data)));
}

From source file:org.pentaho.di.job.entries.zipfile.JobEntryZipFileIT.java

@Test
public void processFile_ReturnsTrue_OnSuccess() throws Exception {
    final String zipPath = "ram://pdi-15013.zip";
    final String content = "temp file";
    final File tempFile = createTempFile(content);
    tempFile.deleteOnExit();//  w w w  . ja v  a2s . c om
    try {
        Result result = new Result();
        JobEntryZipFile entry = new JobEntryZipFile();
        assertTrue(entry.processRowFile(new Job(), result, zipPath, null, null, tempFile.getAbsolutePath(),
                null, false));
    } finally {
        tempFile.delete();
    }

    FileObject zip = KettleVFS.getFileObject(zipPath);
    assertTrue("Zip archive should be created", zip.exists());

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    IOUtils.copy(zip.getContent().getInputStream(), os);

    ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(os.toByteArray()));
    ZipEntry entry = zis.getNextEntry();
    assertEquals("Input file should be put into the archive", tempFile.getName(), entry.getName());

    os.reset();
    IOUtils.copy(zis, os);
    assertEquals("File's content should be equal to original", content, new String(os.toByteArray()));
}

From source file:org.pentaho.di.plugins.examples.step.YamlInputMeta.java

/**
 * Since the exported transformation that runs this will reside in a ZIP file, we can't reference files relatively. So
 * what this does is turn the name of files into absolute paths OR it simply includes the resource in the ZIP file.
 * For now, we'll simply turn it into an absolute path and pray that the file is on a shared drive or something like
 * that./*from   w  ww  .java2s .c om*/
 *
 * @param space
 *          the variable space to use
 * @param definitions
 * @param resourceNamingInterface
 * @param repository
 *          The repository to optionally load other resources from (to be converted to XML)
 * @param metaStore
 *          the metaStore in which non-kettle metadata could reside.
 *
 * @return the filename of the exported resource
 */
public String exportResources(VariableSpace space, Map<String, ResourceDefinition> definitions,
        ResourceNamingInterface resourceNamingInterface, Repository repository, IMetaStore metaStore)
        throws KettleException {
    try {
        // The object that we're modifying here is a copy of the original!
        // So let's change the filename from relative to absolute by grabbing the file object...
        // In case the name of the file comes from previous steps, forget about this!
        //
        List<String> newFilenames = new ArrayList<String>();

        if (!isInFields()) {
            FileInputList fileList = getFiles(space);
            if (fileList.getFiles().size() > 0) {
                for (FileObject fileObject : fileList.getFiles()) {
                    // From : ${Internal.Transformation.Filename.Directory}/../foo/bar.xml
                    // To : /home/matt/test/files/foo/bar.xml
                    //
                    // If the file doesn't exist, forget about this effort too!
                    //
                    if (fileObject.exists()) {
                        // Convert to an absolute path and add it to the list.
                        //
                        newFilenames.add(fileObject.getName().getPath());
                    }
                }

                // Still here: set a new list of absolute filenames!
                //
                fileName = newFilenames.toArray(new String[newFilenames.size()]);
                fileMask = new String[newFilenames.size()]; // all null since converted to absolute path.
                fileRequired = new String[newFilenames.size()]; // all null, turn to "Y" :
                for (int i = 0; i < newFilenames.size(); i++) {
                    fileRequired[i] = "Y";
                }
            }
        }
        return null;
    } catch (Exception e) {
        throw new KettleException(e);
    }
}

From source file:org.pentaho.di.plugins.fileopensave.providers.vfs.VFSFileProvider.java

/**
 * @param file/*w w  w . j  a va  2 s.  c om*/
 * @param newPath
 * @param overwrite
 * @return
 */
private VFSFile doMove(VFSFile file, String newPath, Boolean overwrite) {
    try {
        FileObject fileObject = KettleVFS.getFileObject(file.getPath(), new Variables(),
                VFSHelper.getOpts(file.getPath(), file.getConnection()));
        FileObject renameObject = KettleVFS.getFileObject(newPath, new Variables(),
                VFSHelper.getOpts(file.getPath(), file.getConnection()));
        if (overwrite) {
            if (renameObject.exists()) {
                renameObject.delete();
            }
        }
        fileObject.moveTo(renameObject);
        if (file instanceof VFSDirectory) {
            return VFSDirectory.create(renameObject.getParent().getPublicURIString(), renameObject,
                    file.getConnection());
        } else {
            return VFSFile.create(renameObject.getParent().getPublicURIString(), renameObject,
                    file.getConnection());
        }
    } catch (KettleFileException | FileSystemException e) {
        return null;
    }
}

From source file:org.pentaho.di.plugins.fileopensave.providers.vfs.VFSFileProvider.java

/**
 * @param dir/*from  ww w  .  j  ava2s .  c  o m*/
 * @param path
 * @return
 * @throws FileException
 */
@Override
public boolean fileExists(VFSFile dir, String path) throws FileException {
    path = sanitizeName(dir, path);
    try {
        FileObject fileObject = KettleVFS.getFileObject(path, new Variables(),
                VFSHelper.getOpts(path, dir.getConnection()));
        return fileObject.exists();
    } catch (KettleFileException | FileSystemException e) {
        throw new FileException();
    }
}

From source file:org.pentaho.di.repository.filerep.KettleFileRepository_DatabaseNames_Test.java

@Test
public void getDatabaseId_ReturnsExactMatch_PriorToCaseInsensitiveMatch() throws Exception {
    final String exact = "databaseExactMatch";
    final String similar = exact.toLowerCase();
    assertNotSame(similar, exact);/*from  w  w  w.  j av a 2 s. c o  m*/

    DatabaseMeta db = saveDatabase(exact);

    // simulate legacy repository - store a DB with a name different only in case
    DatabaseMeta another = new DatabaseMeta();
    another.setName(similar);
    FileObject fileObject = repository.getFileObject(another);
    assertFalse(fileObject.exists());
    // just create it - enough for this case
    fileObject.createFile();
    assertTrue(fileObject.exists());

    ObjectId id = this.repository.getDatabaseID(exact);
    assertEquals(db.getObjectId(), id);
}

From source file:org.pentaho.di.sdk.samples.databases.demo.DemoDatabaseMeta.java

/**
 * @param hostname     ignored in this implementation
 * @param port         ignored in this implementation
 * @param databaseName the directory containing CSV files.
 * /*from w  ww  .ja  v  a  2s.c o m*/
 * @return the connection string based on hostname, port and databasename.
 */
public String getURL(String hostname, String port, String databaseName) throws KettleDatabaseException {
    File dbName = new File(databaseName);
    if (dbName != null && !dbName.exists()) {
        // CSV-JDBC requires local file paths
        // It may be a VFS path, test and convert to local if it is a VFS path
        FileObject vfsObject;
        try {
            vfsObject = KettleVFS.getFileObject(databaseName);
            if (vfsObject != null && vfsObject.exists()) {
                File temp = new File(vfsObject.getURL().getPath());
                if (temp.exists()) {
                    dbName = temp;
                }
            }
        } catch (KettleFileException | FileSystemException e) {
            throw new KettleDatabaseException(e);
        }
    }
    return "jdbc:relique:csv:" + dbName.getPath();
}

From source file:org.pentaho.di.trans.step.filestream.FileStream.java

private String getFilePath(String path) {
    try {//  w w w  .  ja v a2s.  c o m
        final FileObject fileObject = KettleVFS.getFileObject(environmentSubstitute(path));
        if (!fileObject.exists()) {
            throw new FileNotFoundException(path);
        }
        return Paths.get(fileObject.getURL().toURI()).normalize().toString();
    } catch (URISyntaxException | FileNotFoundException | FileSystemException | KettleFileException e) {
        propagate(e);
    }
    return null;
}

From source file:org.pentaho.di.trans.steps.cpythonscriptexecutor.CPythonScriptExecutorData.java

protected static String loadScriptFromFile(String file) throws KettleException {
    FileObject scriptF = KettleVFS.getFileObject(file);

    BufferedReader br = null;//from w w  w .  j a v  a2s  .  c  o m
    StringBuilder b = new StringBuilder();
    try {
        if (!scriptF.exists()) {
            throw new KettleException(
                    BaseMessages.getString(PKG, "RScriptExecutorData.Error.ScriptFileDoesNotExist", file));
        }

        InputStream is = KettleVFS.getInputStream(scriptF);
        InputStreamReader isr = new InputStreamReader(is);
        br = new BufferedReader(isr);

        String line = null;
        while ((line = br.readLine()) != null) {
            b.append(line).append("\n");
        }

        br.close();
        br = null;
    } catch (IOException e) {
        throw new KettleException(e);
    } finally {
        if (br != null) {
            try {
                br.close();
                br = null;
            } catch (IOException e) {
                throw new KettleException(e);
            }
        }
    }

    return b.toString();
}