Java Code Examples for org.apache.commons.vfs2.FileObject

The following examples show how to use org.apache.commons.vfs2.FileObject. These examples are extracted from open source projects. 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
@Test
public void stageForCache_destination_no_overwrite() throws Exception {
  DistributedCacheUtilImpl ch = new DistributedCacheUtilImpl();

  Configuration conf = new Configuration();
  FileSystem fs = DistributedCacheTestUtil.getLocalFileSystem( conf );

  FileObject source = DistributedCacheTestUtil.createTestFolderWithContent();
  try {
    Path root = new Path( "bin/test/stageForCache_destination_exists" );
    Path dest = new Path( root, "dest" );

    fs.mkdirs( dest );
    assertTrue( fs.exists( dest ) );
    assertTrue( fs.getFileStatus( dest ).isDir() );
    try {
      ch.stageForCache( source, fs, dest, false );
    } catch ( KettleFileException ex ) {
      assertTrue( ex.getMessage(), ex.getMessage().contains( "Destination exists" ) );
    } finally {
      fs.delete( root, true );
    }
  } finally {
    source.delete( new AllFileSelector() );
  }
}
 
Example 2
Source Project: spoofax   Source File: StrategoRuntimeService.java    License: Apache License 2.0 6 votes vote down vote up
private void loadJars(HybridInterpreter runtime, Iterable<FileObject> jars) throws MetaborgException {
    try {
        final URL[] classpath = new URL[Iterables.size(jars)];
        int i = 0;
        for(FileObject jar : jars) {
            final File localJar = resourceService.localFile(jar);
            classpath[i] = localJar.toURI().toURL();
            ++i;
        }
        logger.trace("Loading jar files {}", (Object) classpath);
        final ClassLoader classLoader = new DynamicClassLoader(additionalClassLoaders);
        runtime.loadJars(classLoader, classpath);
    } catch(IncompatibleJarException | IOException | MetaborgRuntimeException e) {
        throw new MetaborgException("Failed to load JAR", e);
    }
}
 
Example 3
Source Project: spoofax   Source File: ParseFileStrategy.java    License: Apache License 2.0 6 votes vote down vote up
@Override public IStrategoTerm invoke(Context context, IStrategoTerm current) {
    if(!TermUtils.isString(current)) return null;

    try {
        final String path = TermUtils.toJavaString(current);
        final FileObject resource = resourceService.resolve(path);
        if(resource.getType() != FileType.FILE) {
            return null;
        }
        final IdentifiedResource identifiedResource = languageIdentifierService.identifyToResource(resource);
        if(identifiedResource == null) {
            return null;
        }
        final String text = sourceTextService.text(resource);
        final ISpoofaxInputUnit input =
            unitService.inputUnit(resource, text, identifiedResource.language, identifiedResource.dialect);
        final ISpoofaxParseUnit result = syntaxService.parse(input);
        return result.ast();
    } catch(ParseException | IOException e) {
        throw new StrategoException("Parsing failed unexpectedly", e);
    }
}
 
Example 4
Source Project: pentaho-kettle   Source File: KettleVFS.java    License: Apache License 2.0 6 votes vote down vote up
public static FileObject createTempFile( String prefix, String suffix, String directory, VariableSpace space )
  throws KettleFileException {
  try {
    FileObject fileObject;
    do {
      // Build temporary file name using UUID to ensure uniqueness. Old mechanism would fail using Sort Rows (for
      // example)
      // when there multiple nodes with multiple JVMs on each node. In this case, the temp file names would end up
      // being
      // duplicated which would cause the sort to fail.
      String filename =
        new StringBuilder( 50 ).append( directory ).append( '/' ).append( prefix ).append( '_' ).append(
          UUIDUtil.getUUIDAsString() ).append( suffix ).toString();
      fileObject = getFileObject( filename, space );
    } while ( fileObject.exists() );
    return fileObject;
  } catch ( IOException e ) {
    throw new KettleFileException( e );
  }
}
 
Example 5
Source Project: hop   Source File: XsdValidatorMeta.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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.
 *
 * @param space                   the variable space to use
 * @param definitions
 * @param resourceNamingInterface The repository to optionally load other resources from (to be converted to XML)
 * @param metadataProvider        the metadataProvider in which non-kettle metadata could reside.
 * @return the filename of the exported resource
 */
public String exportResources( IVariables space, Map<String, ResourceDefinition> definitions,
                               IResourceNaming resourceNamingInterface, IHopMetadataProvider metadataProvider )
  throws HopException {
  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!
    //

    // From : ${Internal.Transformation.Filename.Directory}/../foo/bar.xsd
    // To : /home/matt/test/files/foo/bar.xsd
    //
    if ( !Utils.isEmpty( xsdFilename ) ) {
      FileObject fileObject = HopVfs.getFileObject( space.environmentSubstitute( xsdFilename ) );
      xsdFilename = resourceNamingInterface.nameResource( fileObject, space, true );
      return xsdFilename;
    }

    return null;
  } catch ( Exception e ) {
    throw new HopException( e );
  }
}
 
Example 6
Source Project: pentaho-kettle   Source File: TextFileOutputMeta.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 the base path into an absolute path.
 *
 * @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
 */
@Override
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!
    //
    if ( !fileNameInField ) {

      if ( !Utils.isEmpty( fileName ) ) {
        FileObject fileObject = KettleVFS.getFileObject( space.environmentSubstitute( fileName ), space );
        fileName = resourceNamingInterface.nameResource( fileObject, space, true );
      }
    }

    return null;
  } catch ( Exception e ) {
    throw new KettleException( e );
  }
}
 
Example 7
Source Project: hop   Source File: SwingSvgImageUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Internal image loading from Hop's user.dir VFS.
 */
private static SwingUniversalImageSvg loadFromBasedVFS( String location ) {
  try {
    FileObject imageFileObject = HopVfs.getFileSystemManager().resolveFile( base, location );
    InputStream s = HopVfs.getInputStream( imageFileObject );
    if ( s == null ) {
      return null;
    }
    try {
      return loadImage( s, location );
    } finally {
      IOUtils.closeQuietly( s );
    }
  } catch ( FileSystemException ex ) {
    return null;
  }
}
 
Example 8
Source Project: hop   Source File: S3CommonFileObject.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void doRename( FileObject newFile ) throws Exception {
  // no folder renames on S3
  if ( getType().equals( FileType.FOLDER ) ) {
    throw new FileSystemException( "vfs.provider/rename-not-supported.error" );
  }

  s3ObjectMetadata = fileSystem.getS3Client().getObjectMetadata( bucketName, key );

  if ( s3ObjectMetadata == null ) {
    // object doesn't exist
    throw new FileSystemException( "vfs.provider/rename.error", this, newFile );
  }

  S3CommonFileObject dest = (S3CommonFileObject) newFile;

  // 1. copy the file
  CopyObjectRequest copyObjRequest = createCopyObjectRequest( bucketName, key, dest.bucketName, dest.key );
  fileSystem.getS3Client().copyObject( copyObjRequest );

  // 2. delete self
  delete();
}
 
Example 9
Source Project: commons-vfs   Source File: DefaultFileMonitorTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testFileModified() throws Exception {
    writeToFile(testFile);
    final FileObject fileObj = fsManager.resolveFile(testFile.toURI().toURL().toString());
    final DefaultFileMonitor monitor = new DefaultFileMonitor(new TestFileListener());
    // TestFileListener manipulates changeStatus
    monitor.setDelay(100);
    monitor.addFile(fileObj);
    monitor.start();
    try {
        // Need a long delay to insure the new timestamp doesn't truncate to be the same as
        // the current timestammp. Java only guarantees the timestamp will be to 1 second.
        Thread.sleep(1000);
        final long value = System.currentTimeMillis();
        final boolean rc = testFile.setLastModified(value);
        assertTrue("setLastModified succeeded", rc);
        Thread.sleep(300);
        assertTrue("No event occurred", changeStatus != 0);
        assertEquals("Incorrect event", 1, changeStatus);
    } finally {
        monitor.stop();
    }
}
 
Example 10
Source Project: commons-vfs   Source File: HdfsFileProviderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGetAttributes() throws Exception {
    final FileObject fo = manager.resolveFile(TEST_DIR1);
    Assert.assertNotNull(fo);
    Assert.assertFalse(fo.exists());

    // Create the test file
    final FileObject file = createTestFile(hdfs);
    Assert.assertTrue(fo.exists());
    final Map<String, Object> attributes = file.getContent().getAttributes();
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.BLOCK_SIZE.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.GROUP.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.LAST_ACCESS_TIME.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.LENGTH.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.MODIFICATION_TIME.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.OWNER.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.PERMISSIONS.toString()));
}
 
Example 11
Source Project: olingo-odata4   Source File: FSManager.java    License: Apache License 2.0 6 votes vote down vote up
private InputStream readFile(final String relativePath, final Accept accept, final String fs) {
  final String path = getAbsolutePath(relativePath, accept);
  LOG.info("Read {}{}", fs, path);

  try {
    final FileObject fileObject = fsManager.resolveFile(fs + path);

    if (fileObject.exists()) {
      // return new in-memory content
      return fileObject.getContent().getInputStream();
    } else {
      LOG.warn("In-memory path '{}' not found", path);
      throw new NotFoundException();
    }
  } catch (FileSystemException e) {
    throw new NotFoundException();
  }
}
 
Example 12
Source Project: commons-vfs   Source File: VerifyingFileSelector.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Determines if a file or folder should be selected.
 */
@Override
public boolean includeFile(final FileSelectInfo fileInfo) throws FileSystemException {
    final FileObject file = fileInfo.getFile();
    if (file == currentFolder) {
        // Pop current folder
        assertEquals(0, children.size());
        currentFolder = currentFolder.getParent();
        currentFolderInfo = currentFolderInfo.getParent();
        children = stack.remove(0);
    }

    final String baseName = file.getName().getBaseName();

    final FileInfo childInfo = getChild(baseName);
    assertSame(childInfo.type, file.getType());

    final boolean isChild = children.remove(baseName);
    assertTrue(isChild);

    files.add(file);
    return true;
}
 
Example 13
@Test
public void findFiles_vfs() throws Exception {
  DistributedCacheUtilImpl ch = new DistributedCacheUtilImpl();

  FileObject testFolder = DistributedCacheTestUtil.createTestFolderWithContent();

  try {
    // Simply test we can find the jar files in our test folder
    List<String> jars = ch.findFiles( testFolder, "jar" );
    assertEquals( 4, jars.size() );

    // Look for all files and folders
    List<String> all = ch.findFiles( testFolder, null );
    assertEquals( 15, all.size() );
  } finally {
    testFolder.delete( new AllFileSelector() );
  }
}
 
Example 14
Source Project: pentaho-kettle   Source File: SimpleResourceNaming.java    License: Apache License 2.0 5 votes vote down vote up
public String nameResource( FileObject fileObject, VariableSpace space, boolean includeFileName ) throws FileSystemException {
  if ( includeFileName ) {
    return handleDataFile( fileObject.getName().getBaseName(), fileObject.getParent().getURL().toString(), "" );
  } else {
    return handleDataFile( "", fileObject.getURL().toString(), "" );
  }
}
 
Example 15
Source Project: commons-vfs   Source File: CustomRamProviderTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests VFS-625.
 * @throws FileSystemException
 */
@Test
public void testMoveFile() throws FileSystemException {
    final FileObject fileSource = manager.resolveFile("ram://virtual/source");
    fileSource.createFile();
    final FileObject fileDest = manager.resolveFile("ram://virtual/dest");
    Assert.assertTrue(fileSource.canRenameTo(fileDest));
    fileSource.moveTo(fileDest);
}
 
Example 16
Source Project: otroslogviewer   Source File: Utils.java    License: Apache License 2.0 5 votes vote down vote up
public static void closeQuietly(FileObject fileObject) {

    if (fileObject != null) {
      String friendlyURI = fileObject.getName().getFriendlyURI();
      try {
        LOGGER.info(String.format("Closing file %s", friendlyURI));
        fileObject.close();
        LOGGER.info(String.format("File %s closed", friendlyURI));
      } catch (FileSystemException ignore) {
        LOGGER.error(String.format("File %s is not closed: %s", friendlyURI, ignore.getMessage()));
      }
    }
  }
 
Example 17
Source Project: spoofax   Source File: SourcePathProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override public Iterable<FileObject> sourcePaths(IProject project, String languageName) {
    final Collection<FileObject> sources = Lists.newArrayList();
    for(ISourceConfig source : project.config().sources()) {
        source.accept(ISourceVisitor.of(langSource -> {
            if(langSource.language.equals(languageName)) {
                sources.add(resolve(project.location(), langSource.directory));
            }
        }, allLangSource -> {
            sources.add(resolve(project.location(), allLangSource.directory));
        }));
    }
    return sources;
}
 
Example 18
Source Project: spoofax   Source File: ConstraintContext.java    License: Apache License 2.0 5 votes vote down vote up
private void deleteContextFile(FileObject file) {
    try {
        file.delete();
    } catch(FileSystemException e) {
        logger.warn("Deleting context {} failed: {}", file, e.getMessage());
    }
}
 
Example 19
Source Project: pentaho-kettle   Source File: TextFileOutputSplittingIT.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void splitWithNone() throws Exception {
  runTransformation( transMeta );

  FileObject[] children = getFolder().getChildren();
  // 2 files => [1,2,3], [4,5]
  assertEquals( 2, children.length );

  assertSplitFileIsCorrect( children[ 0 ], "data.txt_0", "1", "2", "3" );
  assertSplitFileIsCorrect( children[ 1 ], "data.txt_1", "4", "5" );
}
 
Example 20
Source Project: spoofax   Source File: SpoofaxLanguageSpecConfigService.java    License: Apache License 2.0 5 votes vote down vote up
@Override protected ConfigRequest<ISpoofaxLanguageSpecConfig>
    toConfig(HierarchicalConfiguration<ImmutableNode> config, FileObject configFile) {
    final SpoofaxProjectConfig projectConfig = new SpoofaxProjectConfig(config);
    final SpoofaxLanguageSpecConfig languageSpecConfig = new SpoofaxLanguageSpecConfig(config, projectConfig);
    final MessageBuilder mb = MessageBuilder.create().asError().asInternal().withSource(configFile);
    final Collection<IMessage> messages = languageSpecConfig.validate(mb);
    return new ConfigRequest<ISpoofaxLanguageSpecConfig>(languageSpecConfig, messages);
}
 
Example 21
Source Project: commons-vfs   Source File: SynchronizedFileObject.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void findFiles(final FileSelector selector, final boolean depthwise, final List<FileObject> selected)
        throws FileSystemException {
    synchronized (this) {
        super.findFiles(selector, depthwise, selected);
    }
}
 
Example 22
Source Project: spoofax   Source File: LanguageServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Try to add component with non-existent location. Assert that exception is thrown.
 */
@Test(expected = IllegalStateException.class) public void nonExistentLocation() throws Exception {
    final LanguageVersion version = version(0, 0, 1);
    final FileObject location = resourceService.resolve("ram:///doesnotexist");

    language(groupId, "org.metaborg.lang.entity", version, location, "Entity");
}
 
Example 23
Source Project: hop   Source File: WorkflowMeta.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Sets the internal filename hop variables.
 *
 * @param var the new internal filename hop variables
 */
@Override
protected void setInternalFilenameHopVariables( IVariables var ) {
  if ( filename != null ) {
    // we have a filename that's defined.
    try {
      FileObject fileObject = HopVfs.getFileObject( filename );
      FileName fileName = fileObject.getName();

      // The filename of the workflow
      variables.setVariable( Const.INTERNAL_VARIABLE_WORKFLOW_FILENAME_NAME, fileName.getBaseName() );

      // The directory of the workflow
      FileName fileDir = fileName.getParent();
      variables.setVariable( Const.INTERNAL_VARIABLE_WORKFLOW_FILENAME_DIRECTORY, fileDir.getURI() );
    } catch ( Exception e ) {
      variables.setVariable( Const.INTERNAL_VARIABLE_WORKFLOW_FILENAME_DIRECTORY, "" );
      variables.setVariable( Const.INTERNAL_VARIABLE_WORKFLOW_FILENAME_NAME, "" );
    }
  } else {
    variables.setVariable( Const.INTERNAL_VARIABLE_WORKFLOW_FILENAME_DIRECTORY, "" );
    variables.setVariable( Const.INTERNAL_VARIABLE_WORKFLOW_FILENAME_NAME, "" );
  }

  setInternalEntryCurrentDirectory();

}
 
Example 24
Source Project: commons-vfs   Source File: AbstractProviderTestCase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that the content of a file is the same as expected. Checks the length reported by getSize() is correct,
 * then reads the content as a byte stream and compares the result with the expected content. Assumes files are
 * encoded using UTF-8.
 */
protected void assertSameContent(final String expected, final FileObject file) throws Exception {
    // Check the file exists, and is a file
    assertTrue(file.exists());
    assertSame(FileType.FILE, file.getType());
    assertTrue(file.isFile());

    // Get file content as a binary stream
    final byte[] expectedBin = expected.getBytes("utf-8");

    // Check lengths
    final FileContent content = file.getContent();
    assertEquals("same content length", expectedBin.length, content.getSize());

    // Read content into byte array
    final InputStream instr = content.getInputStream();
    final ByteArrayOutputStream outstr;
    try {
        outstr = new ByteArrayOutputStream(expectedBin.length);
        final byte[] buffer = new byte[256];
        int nread = 0;
        while (nread >= 0) {
            outstr.write(buffer, 0, nread);
            nread = instr.read(buffer);
        }
    } finally {
        instr.close();
    }

    // Compare
    assertArrayEquals("same binary content", expectedBin, outstr.toByteArray());
}
 
Example 25
Source Project: spoofax   Source File: CLIUtils.java    License: Apache License 2.0 5 votes vote down vote up
/** Get or create project at the given location */
public IProject getOrCreateProject(FileObject location) throws MetaborgException {
    final ISimpleProjectService projectService = spoofax.injector.getInstance(ISimpleProjectService.class);
    final IProject project = projectService.get(location);
    if(project == null) {
        return projectService.create(location);
    }
    return project;
}
 
Example 26
Source Project: commons-vfs   Source File: ZipProviderTestCase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the base folder for read tests.
 */
@Override
public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
    final File zipFile = AbstractVfsTestCase.getTestResource("test.zip");
    final String uri = "zip:file:" + zipFile.getAbsolutePath() + "!/";
    return manager.resolveFile(uri);
}
 
Example 27
Source Project: hop   Source File: CurrentDirectoryResolver.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * The logic of this method:
 * <p>
 * We return the child var space with directory extracted from filename
 * if we do not have a filename we will return the child var space without updates
 *
 * @param parentVariables - parent variable space which can be inherited
 * @param filename        - is file which we use at this moment
 * @return new var space if inherit was set false or child var space with updated system variables
 */
public IVariables resolveCurrentDirectory( IVariables parentVariables, String filename ) {
  Variables tmpSpace = new Variables();
  tmpSpace.setParentVariableSpace( parentVariables );
  tmpSpace.initializeVariablesFrom( parentVariables );

  if ( filename != null ) {
    try {
      FileObject fileObject = HopVfs.getFileObject( filename );

      if ( !fileObject.exists() ) {
        // don't set variables if the file doesn't exist
        return tmpSpace;
      }

      FileName fileName = fileObject.getName();

      // The filename of the pipeline
      tmpSpace.setVariable( Const.INTERNAL_VARIABLE_WORKFLOW_FILENAME_NAME, fileName.getBaseName() );

      // The directory of the pipeline
      FileName fileDir = fileName.getParent();
      tmpSpace.setVariable( Const.INTERNAL_VARIABLE_PIPELINE_FILENAME_DIRECTORY, fileDir.getURI() );
      tmpSpace.setVariable( Const.INTERNAL_VARIABLE_WORKFLOW_FILENAME_DIRECTORY, fileDir.getURI() );
      tmpSpace.setVariable( Const.INTERNAL_VARIABLE_ENTRY_CURRENT_DIRECTORY, fileDir.getURI() );
    } catch ( Exception e ) {
      throw new RuntimeException( "Unable to figure out the current directory", e );
    }
  }
  return tmpSpace;
}
 
Example 28
Source Project: pentaho-kettle   Source File: RepositoryExporter.java    License: Apache License 2.0 5 votes vote down vote up
private String calcRepositoryDirectory( KettleFileRepository fileRep, FileObject fileObject ) throws FileSystemException {
  String path = fileObject.getParent().getName().getPath();
  String baseDirectory = fileRep.getRepositoryMeta().getBaseDirectory();
  // Double check!
  //
  if ( path.startsWith( baseDirectory ) ) {
    return path.substring( baseDirectory.length() );
  } else {
    return path;
  }
}
 
Example 29
Source Project: spoofax   Source File: CommonPaths.java    License: Apache License 2.0 5 votes vote down vote up
protected FileObject resolve(FileObject dir, String relativePath) {
    try {
        return dir.resolveFile(relativePath);
    } catch(FileSystemException e) {
        throw new MetaborgRuntimeException(e);
    }
}
 
Example 30
Source Project: pentaho-kettle   Source File: VfsFileChooserControls.java    License: Apache License 2.0 5 votes vote down vote up
protected FileObject getInitialFile( String filePath ) throws KettleFileException {
  FileObject initialFile = null;
  if ( filePath != null && !filePath.isEmpty() ) {
    String fileName = space.environmentSubstitute( filePath );
    if ( fileName != null && !fileName.isEmpty() ) {
      initialFile = KettleVFS.getFileObject( fileName );
    }
  }
  if ( initialFile == null ) {
    initialFile = KettleVFS.getFileObject( Spoon.getInstance().getLastFileOpened() );
  }
  return initialFile;
}