org.apache.commons.vfs2.provider.local.LocalFile Java Examples

The following examples show how to use org.apache.commons.vfs2.provider.local.LocalFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OraBulkDataOutputTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
public void testOpen() {
  try {
    File tempFile = TempFile.createTempFile( "temp", "test" );
    String tempFilePath = tempFile.getAbsolutePath();
    String dataFileVfsPath = "file:///" + tempFilePath;
    LocalFile tempFileObject = mock( LocalFile.class );

    tempFile.deleteOnExit();

    doReturn( dataFileVfsPath ).when( oraBulkLoaderMeta ).getDataFile();
    doReturn( tempFilePath ).when( variables ).environmentSubstitute( dataFileVfsPath );
    doReturn( tempFileObject ).when( oraBulkDataOutput ).getFileObject( tempFilePath, variables );
    doReturn( tempFilePath ).when( oraBulkDataOutput ).getFilename( tempFileObject );

    oraBulkDataOutput.open( variables, sqlldrProcess );
    oraBulkDataOutput.close();

  } catch ( Exception ex ) {
    fail( "If any exception occurs, this test fails: " + ex );
  }
}
 
Example #2
Source File: CsvInputDialog.java    From hop with Apache License 2.0 6 votes vote down vote up
/**
 * '
 * Returns the {@link InputStream} corresponding to the csv file, or null if the file cannot be read.
 *
 * @return the {@link InputStream} corresponding to the csv file, or null if the file cannot be read
 */
private InputStream getInputStream( final CsvInputMeta meta ) {
  InputStream inputStream = null;
  try {
    final String filename = pipelineMeta.environmentSubstitute( meta.getFilename() );

    final FileObject fileObject = HopVfs.getFileObject( filename );
    if ( !( fileObject instanceof LocalFile ) ) {
      // We can only use NIO on local files at the moment, so that's what we
      // limit ourselves to.
      //
      throw new HopException( BaseMessages.getString( PKG, "CsvInput.Log.OnlyLocalFilesAreSupported" ) );
    }

    inputStream = HopVfs.getInputStream( fileObject );
  } catch ( final Exception e ) {
    logError( BaseMessages.getString( PKG, "CsvInputDialog.ErrorGettingFileDesc.DialogMessage" ), e );
  }
  return inputStream;
}
 
Example #3
Source File: CsvInputDialog.java    From hop with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream getInputStream( final ICsvInputAwareMeta meta ) {
  InputStream inputStream = null;
  try {
    FileObject fileObject = meta.getHeaderFileObject( getPipelineMeta() );
    if ( !( fileObject instanceof LocalFile ) ) {
      // We can only use NIO on local files at the moment, so that's what we limit ourselves to.
      throw new HopException( BaseMessages.getString( "FileInputDialog.Log.OnlyLocalFilesAreSupported" ) );
    }

    inputStream = HopVfs.getInputStream( fileObject );
  } catch ( final Exception e ) {
    logError( BaseMessages.getString( "FileInputDialog.ErrorGettingFileDesc.DialogMessage" ), e );
  }
  return inputStream;
}
 
Example #4
Source File: OraBulkDataOutputTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void testOpen() {
  try {
    File tempFile = TempFile.createTempFile( "temp", "test" );
    String tempFilePath = tempFile.getAbsolutePath();
    String dataFileVfsPath = "file:///" + tempFilePath;
    LocalFile tempFileObject = mock( LocalFile.class );

    tempFile.deleteOnExit();

    doReturn( dataFileVfsPath ).when( oraBulkLoaderMeta ).getDataFile();
    doReturn( tempFilePath ).when( space ).environmentSubstitute( dataFileVfsPath );
    doReturn( tempFileObject ).when( oraBulkDataOutput ).getFileObject( tempFilePath, space );
    doReturn( tempFilePath ).when( oraBulkDataOutput ).getFilename( tempFileObject );

    oraBulkDataOutput.open( space, sqlldrProcess );
    oraBulkDataOutput.close();

  } catch ( Exception ex ) {
    fail( "If any exception occurs, this test fails: " + ex );
  }
}
 
Example #5
Source File: CsvInputDialog.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**'
 * Returns the {@link InputStream} corresponding to the csv file, or null if the file cannot be read.
 * @return the {@link InputStream} corresponding to the csv file, or null if the file cannot be read
 */
private InputStream getInputStream( final CsvInputMeta meta ) {
  InputStream inputStream = null;
  try {
    final String filename = transMeta.environmentSubstitute( meta.getFilename() );

    final FileObject fileObject = KettleVFS.getFileObject( filename );
    if ( !( fileObject instanceof LocalFile ) ) {
      // We can only use NIO on local files at the moment, so that's what we
      // limit ourselves to.
      //
      throw new KettleException( BaseMessages.getString( PKG, "CsvInput.Log.OnlyLocalFilesAreSupported" ) );
    }

    inputStream = KettleVFS.getInputStream( fileObject );
  } catch ( final Exception e ) {
    logError( BaseMessages.getString( PKG, "CsvInputDialog.ErrorGettingFileDesc.DialogMessage" ), e );
  }
  return inputStream;
}
 
Example #6
Source File: CsvInputDialog.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream getInputStream( final CsvInputAwareMeta meta ) {
  InputStream inputStream = null;
  try {
    FileObject fileObject = meta.getHeaderFileObject( getTransMeta() );
    if ( !( fileObject instanceof LocalFile ) ) {
      // We can only use NIO on local files at the moment, so that's what we limit ourselves to.
      throw new KettleException( BaseMessages.getString( "FileInputDialog.Log.OnlyLocalFilesAreSupported" ) );
    }

    inputStream = KettleVFS.getInputStream( fileObject );
  } catch ( final Exception e ) {
    logError( BaseMessages.getString( "FileInputDialog.ErrorGettingFileDesc.DialogMessage" ), e );
  }
  return inputStream;
}
 
Example #7
Source File: ResourceService.java    From spoofax with Apache License 2.0 6 votes vote down vote up
@Override public File localFile(FileObject resource, FileObject dir) {
    if(resource instanceof LocalFile) {
        return FileUtils.toFile(resource);
    }

    final File localDir = localPath(dir);
    if(localDir == null) {
        throw new MetaborgRuntimeException("Replication directory " + dir
            + " is not on the local filesystem, cannot get local file for " + resource);
    }
    try {
        dir.createFolder();

        final FileObject copyLoc;
        if(resource.getType() == FileType.FOLDER) {
            copyLoc = dir;
        } else {
            copyLoc = ResourceUtils.resolveFile(dir, resource.getName().getBaseName());
        }
        copyLoc.copyFrom(resource, new AllFileSelector());

        return localDir;
    } catch(FileSystemException e) {
        throw new MetaborgRuntimeException("Could not get local file for " + resource, e);
    }
}
 
Example #8
Source File: HopVfs.java    From hop with Apache License 2.0 5 votes vote down vote up
public static OutputStream getOutputStream( FileObject fileObject, boolean append ) throws IOException {
  FileObject parent = fileObject.getParent();
  if ( parent != null ) {
    if ( !parent.exists() ) {
      throw new IOException( BaseMessages.getString(
        PKG, "HopVFS.Exception.ParentDirectoryDoesNotExist", getFriendlyURI( parent ) ) );
    }
  }
  try {
    fileObject.createFile();
    FileContent content = fileObject.getContent();
    return content.getOutputStream( append );
  } catch ( FileSystemException e ) {
    // Perhaps if it's a local file, we can retry using the standard
    // File object. This is because on Windows there is a bug in VFS.
    //
    if ( fileObject instanceof LocalFile ) {
      try {
        String filename = getFilename( fileObject );
        return new FileOutputStream( new File( filename ), append );
      } catch ( Exception e2 ) {
        throw e; // throw the original exception: hide the retry.
      }
    } else {
      throw e;
    }
  }
}
 
Example #9
Source File: HopVfs.java    From hop with Apache License 2.0 5 votes vote down vote up
/**
 * Get a FileInputStream for a local file. Local files can be read with NIO.
 *
 * @param fileObject
 * @return a FileInputStream
 * @throws IOException
 * @deprecated because of API change in Apache VFS. As a workaround use FileObject.getName().getPathDecoded(); Then
 * use a regular File() object to create a File Input stream.
 */
@Deprecated
public static FileInputStream getFileInputStream( FileObject fileObject ) throws IOException {

  if ( !( fileObject instanceof LocalFile ) ) {
    // We can only use NIO on local files at the moment, so that's what we limit ourselves to.
    //
    throw new IOException( BaseMessages.getString( PKG, "FixedInput.Log.OnlyLocalFilesAreSupported" ) );
  }

  return new FileInputStream( fileObject.getName().getPathDecoded() );
}
 
Example #10
Source File: PoiWorkbook.java    From hop with Apache License 2.0 5 votes vote down vote up
public PoiWorkbook( String filename, String encoding ) throws HopException {
  this.filename = filename;
  this.encoding = encoding;
  this.log = HopLogStore.getLogChannelFactory().create( this );
  try {
    FileObject fileObject = HopVfs.getFileObject( filename );
    if ( fileObject instanceof LocalFile ) {
      // This supposedly shaves off a little bit of memory usage by allowing POI to randomly access data in the file
      //
      String localFilename = HopVfs.getFilename( fileObject );
      File excelFile = new File( localFilename );
      try {
        npoifs = new NPOIFSFileSystem( excelFile );
        workbook = org.apache.poi.ss.usermodel.WorkbookFactory.create( npoifs );
      } catch ( Exception ofe ) {
        try {
          opcpkg = OPCPackage.open( excelFile );
          workbook = org.apache.poi.ss.usermodel.WorkbookFactory.create( opcpkg );
        } catch ( Exception ex ) {
          workbook = org.apache.poi.ss.usermodel.WorkbookFactory.create( excelFile );
        }
      }
    } else {
      internalIS = HopVfs.getInputStream( filename );
      workbook = org.apache.poi.ss.usermodel.WorkbookFactory.create( internalIS );
    }
  } catch ( Exception e ) {
    throw new HopException( e );
  }
}
 
Example #11
Source File: KettleVFS.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static OutputStream getOutputStream( FileObject fileObject, boolean append ) throws IOException {
  FileObject parent = fileObject.getParent();
  if ( parent != null ) {
    if ( !parent.exists() ) {
      throw new IOException( BaseMessages.getString(
        PKG, "KettleVFS.Exception.ParentDirectoryDoesNotExist", getFriendlyURI( parent ) ) );
    }
  }
  try {
    fileObject.createFile();
    FileContent content = fileObject.getContent();
    return content.getOutputStream( append );
  } catch ( FileSystemException e ) {
    // Perhaps if it's a local file, we can retry using the standard
    // File object. This is because on Windows there is a bug in VFS.
    //
    if ( fileObject instanceof LocalFile ) {
      try {
        String filename = getFilename( fileObject );
        return new FileOutputStream( new File( filename ), append );
      } catch ( Exception e2 ) {
        throw e; // throw the original exception: hide the retry.
      }
    } else {
      throw e;
    }
  }
}
 
Example #12
Source File: KettleVFS.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Get a FileInputStream for a local file. Local files can be read with NIO.
 *
 * @param fileObject
 * @return a FileInputStream
 * @throws IOException
 * @deprecated because of API change in Apache VFS. As a workaround use FileObject.getName().getPathDecoded(); Then
 * use a regular File() object to create a File Input stream.
 */
@Deprecated
public static FileInputStream getFileInputStream( FileObject fileObject ) throws IOException {

  if ( !( fileObject instanceof LocalFile ) ) {
    // We can only use NIO on local files at the moment, so that's what we limit ourselves to.
    //
    throw new IOException( BaseMessages.getString( PKG, "FixedInput.Log.OnlyLocalFilesAreSupported" ) );
  }

  return new FileInputStream( fileObject.getName().getPathDecoded() );
}
 
Example #13
Source File: ResourceService.java    From spoofax with Apache License 2.0 5 votes vote down vote up
@Override public File localFile(FileObject resource) {
    if(resource instanceof LocalFile) {
        return FileUtils.toFile(resource);
    }

    try {
        return resource.getFileSystem().replicateFile(resource, new AllFileSelector());
    } catch(FileSystemException e) {
        throw new MetaborgRuntimeException("Could not get local file for " + resource, e);
    }
}
 
Example #14
Source File: PentahoReportingOutput.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
protected static Object getKeyValue( FileObject fileObject ) throws MalformedURLException {
  return fileObject instanceof LocalFile ? new URL( fileObject.getName().getURI() ) : fileObject;
}
 
Example #15
Source File: ResourceService.java    From spoofax with Apache License 2.0 4 votes vote down vote up
@Override public File localPath(FileObject resource) {
    if(resource instanceof LocalFile) {
        return FileUtils.toFile(resource);
    }
    return null;
}