org.apache.commons.vfs2.FileObject Java Examples
The following examples show how to use
org.apache.commons.vfs2.FileObject.
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: DistributedCacheUtilImplTest.java From pentaho-hadoop-shims with Apache License 2.0 | 6 votes |
@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 #2
Source File: FSManager.java From olingo-odata4 with Apache License 2.0 | 6 votes |
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 #3
Source File: ParseFileStrategy.java From spoofax with Apache License 2.0 | 6 votes |
@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 File: KettleVFS.java From pentaho-kettle with Apache License 2.0 | 6 votes |
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 File: XsdValidatorMeta.java From hop with Apache License 2.0 | 6 votes |
/** * 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 File: HdfsFileProviderTest.java From commons-vfs with Apache License 2.0 | 6 votes |
@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 #7
Source File: StrategoRuntimeService.java From spoofax with Apache License 2.0 | 6 votes |
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 #8
Source File: DefaultFileMonitorTest.java From commons-vfs with Apache License 2.0 | 6 votes |
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 #9
Source File: TextFileOutputMeta.java From pentaho-kettle with Apache License 2.0 | 6 votes |
/** * 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 #10
Source File: SwingSvgImageUtil.java From hop with Apache License 2.0 | 6 votes |
/** * 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 #11
Source File: DistributedCacheUtilImplTest.java From pentaho-hadoop-shims with Apache License 2.0 | 6 votes |
@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 #12
Source File: S3CommonFileObject.java From hop with Apache License 2.0 | 6 votes |
@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 #13
Source File: VerifyingFileSelector.java From commons-vfs with Apache License 2.0 | 6 votes |
/** * 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 #14
Source File: AbstractProviderTestCase.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * 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 #15
Source File: CustomRamProviderTest.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * 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 File: ChangeLastModificationTime.java From commons-vfs with Apache License 2.0 | 5 votes |
public static void main(final String[] args) throws Exception { if (args.length == 0) { System.err.println("Please pass the name of a file as parameter."); return; } final FileObject fo = VFS.getManager().resolveFile(args[0]); final long setTo = System.currentTimeMillis(); System.err.println("set to: " + setTo); fo.getContent().setLastModifiedTime(setTo); System.err.println("after set: " + fo.getContent().getLastModifiedTime()); }
Example #17
Source File: TestFTPRemoteFile.java From datacollector with Apache License 2.0 | 5 votes |
@Test public void testCreateAndCommitOutputStream() throws Exception { String name = "file.txt"; String filePath = "/some/path/"; FileObject fileObject = Mockito.mock(FileObject.class); FileName fileName = Mockito.mock(FileName.class); Mockito.when(fileObject.getName()).thenReturn(fileName); Mockito.when(fileName.getBaseName()).thenReturn(name); FileObject parentFileObject = Mockito.mock(FileObject.class); FileObject tempFileObject = Mockito.mock(FileObject.class); Mockito.when(fileObject.getParent()).thenReturn(parentFileObject); Mockito.when(parentFileObject.resolveFile(Mockito.any())).thenReturn(tempFileObject); FileContent tempFileContent = Mockito.mock(FileContent.class); Mockito.when(tempFileObject.getContent()).thenReturn(tempFileContent); FTPRemoteFile file = new FTPRemoteFile(filePath + name, 0L, fileObject); try { file.commitOutputStream(); Assert.fail("Expected IOException because called commitOutputStream before createOutputStream"); } catch (IOException ioe) { Assert.assertEquals("Cannot commit " + filePath + name + " - it must be written first", ioe.getMessage()); } file.createOutputStream(); Mockito.verify(parentFileObject).resolveFile("_tmp_" + name); Mockito.verify(tempFileContent).getOutputStream(); file.commitOutputStream(); Mockito.verify(tempFileObject).moveTo(fileObject); }
Example #18
Source File: RepositoryTableModel.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 5 votes |
public FileObject getElementForRow( final int row ) { if ( selectedPath == null ) { return null; } try { if ( selectedPath.getType() != FileType.FOLDER ) { return null; } final FileObject[] children = selectedPath.getChildren(); int count = 0; for ( int i = 0; i < children.length; i++ ) { final FileObject child = children[i]; if ( isShowHiddenFiles() == false && child.isHidden() ) { continue; } if ( child.getType() != FileType.FOLDER ) { if ( PublishUtil.acceptFilter( filters, child.getName().getBaseName() ) == false ) { continue; } } if ( count == row ) { return child; } count += 1; } return null; } catch ( FileSystemException fse ) { UncaughtExceptionsModel.getInstance().addException( fse ); return null; } }
Example #19
Source File: LanguageServiceTest.java From spoofax with Apache License 2.0 | 5 votes |
/** * 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 #20
Source File: Tbz2ProviderTestCase.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * Returns the base folder for read tests. */ @Override public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception { final File tarFile = AbstractVfsTestCase.getTestResource("test.tbz2"); final String uri = "tbz2:file:" + tarFile.getAbsolutePath() + "!/"; return manager.resolveFile(uri); }
Example #21
Source File: DefaultFileSystemManager.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * Converts a URL into a {@link FileObject}. * * @param url The URL to convert. * @return The {@link FileObject} that represents the URL. Never returns null. * @throws FileSystemException On error converting the URL. * @since 2.1 */ @Override public FileObject resolveFile(final URL url) throws FileSystemException { try { return this.resolveFile(url.toURI()); } catch (final URISyntaxException e) { throw new FileSystemException(e); } }
Example #22
Source File: WorkflowMeta.java From hop with Apache License 2.0 | 5 votes |
/** * 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 #23
Source File: RepositoryExporter.java From pentaho-kettle with Apache License 2.0 | 5 votes |
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 #24
Source File: CurrentDirectoryResolver.java From hop with Apache License 2.0 | 5 votes |
/** * 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 #25
Source File: VfsFileChooserControls.java From pentaho-kettle with Apache License 2.0 | 5 votes |
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; }
Example #26
Source File: CommonPaths.java From spoofax with Apache License 2.0 | 5 votes |
protected FileObject resolve(FileObject dir, String relativePath) { try { return dir.resolveFile(relativePath); } catch(FileSystemException e) { throw new MetaborgRuntimeException(e); } }
Example #27
Source File: CLIUtils.java From spoofax with Apache License 2.0 | 5 votes |
/** 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 #28
Source File: SmartProxyImpl.java From scheduling with GNU Affero General Public License v3.0 | 5 votes |
@Override protected void createFolder(String fUri) throws NotConnectedException, PermissionException { FileObject fo = null; try { fo = jobTracker.resolveFile(fUri); fo.createFolder(); } catch (FileSystemException e) { log.error("Error while creating folder: " + fo, e); } log.debug("Created remote folder: " + fUri); }
Example #29
Source File: VFSClassLoader.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * Returns an Enumeration of all the resources in the search path with the specified name. * <p> * Gets called from {@link ClassLoader#getResources(String)} after parent class loader was questioned. * * @param name The resources to find. * @return An Enumeration of the resources associated with the name. * @throws FileSystemException if an error occurs. */ @Override protected Enumeration<URL> findResources(final String name) throws IOException { final List<URL> result = new ArrayList<>(2); for (final FileObject baseFile : resources) { try (final FileObject file = baseFile.resolveFile(name, NameScope.DESCENDENT_OR_SELF)) { if (FileObjectUtils.exists(file)) { result.add(new Resource(name, baseFile, file).getURL()); } } } return Collections.enumeration(result); }
Example #30
Source File: ZipProviderTestCase.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * 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); }