Java Code Examples for org.apache.commons.vfs2.VFS#getManager()

The following examples show how to use org.apache.commons.vfs2.VFS#getManager() . 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: VFSFileHandlerReloadingDetector.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the file that is monitored by this strategy. Note that the return
 * value can be <b>null </b> under some circumstances.
 *
 * @return the monitored file
 */
protected FileObject getFileObject()
{
    if (!getFileHandler().isLocationDefined())
    {
        return null;
    }

    try
    {
        final FileSystemManager fsManager = VFS.getManager();
        final String uri = resolveFileURI();
        if (uri == null)
        {
            throw new ConfigurationRuntimeException("Unable to determine file to monitor");
        }
        return fsManager.resolveFile(uri);
    }
    catch (final FileSystemException fse)
    {
        final String msg = "Unable to monitor " + getFileHandler().getURL().toString();
        log.error(msg);
        throw new ConfigurationRuntimeException(msg, fse);
    }
}
 
Example 2
Source File: Http5FilesCacheTestCase.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Tests https://issues.apache.org/jira/browse/VFS-426
 */
@Test
public void testQueryStringUrls() throws FileSystemException {
    final String noQueryStringUrl = "http5://commons.apache.org/vfs";
    final String queryStringUrl = "http5://commons.apache.org/vfs?query=string";
    final String queryStringUrl2 = "http5://commons.apache.org/vfs?query=string&more=stuff";

    final FileSystemManager fileSystemManager = VFS.getManager();

    final FileObject noQueryFile = fileSystemManager.resolveFile(noQueryStringUrl);
    Assert.assertEquals(noQueryStringUrl, noQueryFile.getURL().toExternalForm());

    final FileObject queryFile = fileSystemManager.resolveFile(queryStringUrl);
    Assert.assertEquals(queryStringUrl, queryFile.getURL().toExternalForm()); // failed for VFS-426

    final FileObject queryFile2 = fileSystemManager.resolveFile(queryStringUrl2);
    Assert.assertEquals(queryStringUrl2, queryFile2.getURL().toExternalForm()); // failed for VFS-426
}
 
Example 3
Source File: DefaultFileContentTest.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Test
public void testMarkingWorks() throws Exception {
    final File temp = File.createTempFile("temp-file-name", ".tmp");
    final FileSystemManager fileSystemManager = VFS.getManager();

    try (FileObject file = fileSystemManager.resolveFile(temp.getAbsolutePath())) {
        try (OutputStream outputStream = file.getContent().getOutputStream()) {
            outputStream.write(expected.getBytes());
            outputStream.flush();
        }
        try (InputStream stream = file.getContent().getInputStream()) {
            if (stream.markSupported()) {
                for (int i = 0; i < 10; i++) {
                    stream.mark(0);
                    final byte[] data = new byte[100];
                    stream.read(data, 0, 7);
                    stream.read();
                    Assert.assertEquals(expected, new String(data).trim());
                    stream.reset();
                }
            }
        }
    }
}
 
Example 4
Source File: BaseParsingTest.java    From hop with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize transform info. Method is final against redefine in descendants.
 */
@Before
public final void beforeCommon() throws Exception {
  HopEnvironment.init();
  PluginRegistry.addPluginType( CompressionPluginType.getInstance() );
  PluginRegistry.init( false );

  transformMeta = new TransformMeta();
  transformMeta.setName( "test" );

  pipeline = new LocalPipelineEngine();
  pipeline.setLogChannel( log );
  pipeline.setRunning( true );
  pipelineMeta = new PipelineMeta() {
    @Override
    public TransformMeta findTransform( String name ) {
      return transformMeta;
    }
  };

  fs = VFS.getManager();
  inPrefix = '/' + this.getClass().getPackage().getName().replace( '.', '/' ) + "/files/";
}
 
Example 5
Source File: ZipFileObjectTestCase.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that we can get a stream from one file in a zip file, then close another file from the same zip, then
 * process the initial input stream.
 *
 * @throws IOException
 */
@Test
public void testReadingOneAfterClosingAnotherFile() throws IOException {
    final File newZipFile = createTempFile();
    final FileSystemManager manager = VFS.getManager();
    final FileObject zipFileObject1;
    final InputStream inputStream1;
    try (final FileObject zipFileObject = manager.resolveFile("zip:file:" + newZipFile.getAbsolutePath())) {
        // leave resources open
        zipFileObject1 = zipFileObject.resolveFile(NESTED_FILE_1);
        inputStream1 = zipFileObject1.getContent().getInputStream();
    }
    // The zip file is "closed", but we read from the stream now.
    readAndAssert(zipFileObject1, inputStream1, "1");
    // clean up
    zipFileObject1.close();
    assertDelete(newZipFile);
}
 
Example 6
Source File: BaseParsingTest.java    From hop with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize transform info. Method is final against redefine in descendants.
 */
@Before
public final void beforeCommon() throws Exception {
  HopEnvironment.init();
  PluginRegistry.addPluginType( CompressionPluginType.getInstance() );
  PluginRegistry.init( false );

  transformMeta = new TransformMeta();
  transformMeta.setName( "test" );

  pipeline = new LocalPipelineEngine();
  pipeline.setLogChannel( log );
  pipeline.setRunning( true );
  pipelineMeta = new PipelineMeta() {
    @Override
    public TransformMeta findTransform( String name ) {
      return transformMeta;
    }
  };

  fs = VFS.getManager();
  inPrefix = '/' + this.getClass().getPackage().getName().replace( '.', '/' ) + "/files/";
}
 
Example 7
Source File: ParseXmlInZipTestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseXmlInZip() throws IOException, SAXException {
    final File newZipFile = createTempFile();
    final String xmlFilePath = "zip:file:" + newZipFile.getAbsolutePath() + "!/read-xml-tests/file1.xml";
    final FileSystemManager manager = VFS.getManager();
    try (final FileObject zipFileObject = manager.resolveFile(xmlFilePath)) {
        try (final InputStream inputStream = zipFileObject.getContent().getInputStream()) {
            final Document document = newDocumentBuilder(zipFileObject, zipFileObject, null).parse(inputStream);
            Assert.assertNotNull(document);
        }
    }
}
 
Example 8
Source File: DefaultFileContentTest.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Test
public void testMarkingWhenReadingEOS() throws Exception {
    final File temp = File.createTempFile("temp-file-name", ".tmp");
    final FileSystemManager fileSystemManager = VFS.getManager();

    try (FileObject file = fileSystemManager.resolveFile(temp.getAbsolutePath())) {
        try (OutputStream outputStream = file.getContent().getOutputStream()) {
            outputStream.write(expected.getBytes());
            outputStream.flush();
        }
        try (InputStream stream = file.getContent().getInputStream()) {
            int readCount = 0;
            if (stream.markSupported()) {
                for (int i = 0; i < 10; i++) {
                    stream.mark(0);
                    final byte[] data = new byte[100];
                    readCount = stream.read(data, 0, 7);
                    stream.read();
                    Assert.assertEquals(7, readCount);
                    Assert.assertEquals(expected, new String(data).trim());
                    readCount = stream.read(data, 8, 10);
                    Assert.assertEquals(-1, readCount);
                    stream.reset();
                }
            }
        }
    }
}
 
Example 9
Source File: DefaultFileContentTest.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
private void testInputStreamBufferSize(final int bufferSize) throws Exception {
    final File temp = File.createTempFile("temp-file-name", ".tmp");
    final FileSystemManager fileSystemManager = VFS.getManager();

    try (FileObject file = fileSystemManager.resolveFile(temp.getAbsolutePath())) {
        file.getContent().getInputStream(bufferSize);
    }
}
 
Example 10
Source File: VFSFileSystem.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Override
public OutputStream getOutputStream(final URL url) throws ConfigurationException
{
    try
    {
        final FileSystemOptions opts = getOptions(url.getProtocol());
        final FileSystemManager fsManager = VFS.getManager();
        final FileObject file = opts == null ? fsManager.resolveFile(url.toString())
                : fsManager.resolveFile(url.toString(), opts);
        // throw an exception if the target URL is a directory
        if (file == null || file.getType() == FileType.FOLDER)
        {
            throw new ConfigurationException("Cannot save a configuration to a directory");
        }
        final FileContent content = file.getContent();

        if (content == null)
        {
            throw new ConfigurationException("Cannot access content of " + url);
        }
        return content.getOutputStream();
    }
    catch (final FileSystemException fse)
    {
        throw new ConfigurationException("Unable to access " + url, fse);
    }
}
 
Example 11
Source File: YamlFederatedMetaStoreStorage.java    From waggle-dance with Apache License 2.0 5 votes vote down vote up
YamlMarshaller() {
  try {
    fsManager = VFS.getManager();
  } catch (FileSystemException e) {
    throw new RuntimeException("Unable to initialize Virtual File System", e);
  }
  yaml = YamlFactory.newYaml();
}
 
Example 12
Source File: SimpleVirtualFilesystem.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
@Activate
void activate(Map<String, Object> configuration) throws VirtualFilesystemException {
    SimpleVirtualFilesystemConfig conf = Configurable.createConfigurable(SimpleVirtualFilesystemConfig.class,
            configuration);
    try {
        this.fsManager = VFS.getManager();
        File rootDirectory = new File(conf.defaultRootDirectory());
        if (!rootDirectory.exists()) {
            rootDirectory.mkdirs();
        }
        ((DefaultFileSystemManager) this.fsManager).setBaseFile(rootDirectory);
    } catch (FileSystemException e) {
        throw new VirtualFilesystemException("Issue initializing virtual file system.", e);
    }
    // Set of queues.
    tempFiles = new ArrayBlockingQueue<>(conf.maxNumberOfTempFiles());

    // Schedule our temp file cleanup service.
    this.scheduledExecutorService = Executors.newScheduledThreadPool(1);
    this.scheduledExecutorService.scheduleAtFixedRate(new CleanTempFilesRunnable(LOGGER, tempFiles),
            conf.secondsBetweenTempCleanup(), conf.secondsBetweenTempCleanup(), TimeUnit.SECONDS);
    LOGGER.debug("Configured scheduled cleanup of temp files to run every {} seconds",
            conf.secondsBetweenTempCleanup());

    // Set default temp url template
    this.baseTempUrlTemplate = conf.defaultTemporaryDirectory() != null ? conf.defaultTemporaryDirectory() :
            ("file://" + System.getProperty("java.io.tmpdir"));
    LOGGER.debug("Going to use {} for our base temp directory template", this.baseTempUrlTemplate);

    // Initialize HashFactory
    this.hashFactory = XXHashFactory.fastestInstance();
}
 
Example 13
Source File: VFSZipperZIPTest.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws IOException {
    fileSystemManager = VFS.getManager();
    temporaryPath = temporaryFolder.newFolder().toPath();

    archivePath = getArchivePath();
    outputPath = getOutputPath();
}
 
Example 14
Source File: ConversionTestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void testFileNameWithSpaces() throws URISyntaxException, IOException {
    final File file = new File("target", "a name.txt");
    final String fileURL = file.toURI().toURL().toExternalForm();
    assertEquals(file.getAbsoluteFile(), new File(file.toURI().getPath()));
    assertEquals(file.getAbsoluteFile(), new File(new URL(fileURL).toURI().getPath()));

    final FileSystemManager manager = VFS.getManager();
    final FileObject fo = manager.resolveFile(fileURL);
    assertEquals(file.getAbsoluteFile(), new File(new URL(fo.getURL().toExternalForm()).toURI().getPath()));
}
 
Example 15
Source File: LoadFileInputTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws FileSystemException {
  fs = VFS.getManager();
  filesPath = '/' + this.getClass().getPackage().getName().replace( '.', '/' ) + "/files/";

  transName = "LoadFileInput";
  transMeta = new TransMeta();
  transMeta.setName( transName );
  trans = new Trans( transMeta );

  stepMetaInterface = spy( new LoadFileInputMeta() );
  stepInputFiles = new FileInputList();
  Mockito.doReturn( stepInputFiles ).when( stepMetaInterface ).getFiles( any( VariableSpace.class ) );
  String stepId = PluginRegistry.getInstance().getPluginId( StepPluginType.class, stepMetaInterface );
  stepMeta = new StepMeta( stepId, "Load File Input", stepMetaInterface );
  transMeta.addStep( stepMeta );

  stepDataInterface = new LoadFileInputData();

  stepCopyNr = 0;

  stepLoadFileInput = new LoadFileInput( stepMeta, stepDataInterface, stepCopyNr, transMeta, trans );

  assertSame( stepMetaInterface, stepMeta.getStepMetaInterface() );

  runtimeSMI = stepMetaInterface;
  runtimeSDI = runtimeSMI.getStepData();

  inputField = new LoadFileInputField();
  ((LoadFileInputMeta) runtimeSMI).setInputFields( new LoadFileInputField[] { inputField } );
  stepLoadFileInput.init( runtimeSMI, runtimeSDI );
}
 
Example 16
Source File: ZipFileObjectTestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that when we read a file inside a file Zip and leave it open, we can still delete the Zip after we clean up
 * the Zip file.
 *
 * @throws IOException
 */
@Test
@Ignore("Shows that leaving a stream open and not closing any resource leaves the container file locked")
public void testLeaveNestedFileOpen() throws IOException {
    final File newZipFile = createTempFile();
    final FileSystemManager manager = VFS.getManager();
    try (final FileObject zipFileObject = manager.resolveFile("zip:file:" + newZipFile.getAbsolutePath())) {
        @SuppressWarnings({ "resource" })
        final FileObject zipFileObject1 = zipFileObject.resolveFile(NESTED_FILE_1);
        getInputStreamAndAssert(zipFileObject1, "1");
    }
    assertDelete(newZipFile);
}
 
Example 17
Source File: SizeFileFilterExample.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {

        // Example, to print all files and directories in the current directory
        // whose size is greater than 1 MB
        final FileSystemManager fsManager = VFS.getManager();
        final FileObject dir = fsManager.toFileObject(new File("."));
        final SizeFileFilter filter = new SizeFileFilter(1024 * 1024);
        final FileObject[] files = dir.findFiles(new FileFilterSelector(filter));
        for (FileObject file : files) {
            System.out.println(file);
        }

    }
 
Example 18
Source File: GetContentInfoFunctionalTest.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Tests VFS-427 NPE on HttpFileObject.getContent().getContentInfo().
 *
 * @throws FileSystemException thrown when the getContentInfo API fails.
 */
@Test
public void testGetContentInfo() throws FileSystemException {
    final FileSystemManager fsManager = VFS.getManager();
    try (final FileObject fo = fsManager.resolveFile("http://www.apache.org/licenses/LICENSE-2.0.txt");
            final FileContent content = fo.getContent();) {
        Assert.assertNotNull(content);
        // Used to NPE before fix:
        content.getContentInfo();
    }
}
 
Example 19
Source File: SchemaResolver.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static String resolveSchema( final ResourceManager resourceManager,
                                    final ResourceKey contextKey,
                                    String catalogUrl )
  throws FileSystemException {
  final FileSystemManager fsManager = VFS.getManager();
  if ( fsManager == null ) {
    throw Util.newError( "Cannot get virtual file system manager" );
  }

  // Workaround VFS bug.
  if ( catalogUrl.startsWith( "file://localhost" ) ) {
    catalogUrl = catalogUrl.substring( "file://localhost".length() );
  }
  if ( catalogUrl.startsWith( "file:" ) ) {
    catalogUrl = catalogUrl.substring( "file:".length() );
  }

  try {
    final File catalogFile = new File( catalogUrl ).getCanonicalFile();
    final FileObject file = fsManager.toFileObject( catalogFile );
    if ( file.isReadable() ) {
      return catalogFile.getPath();
    }
  } catch ( FileSystemException fse ) {
    logger.info( "Failed to resolve schema file '" + catalogUrl + "' as local file. Treating file as non-readable.",
      fse );
  } catch ( IOException e ) {
    logger
      .info( "Failed to resolve schema file '" + catalogUrl + "' as local file. Treating file as non-readable.", e );
  }

  if ( contextKey == null ) {
    return catalogUrl;
  }

  final File contextAsFile = getContextAsFile( contextKey );
  if ( contextAsFile == null ) {
    return catalogUrl;
  }

  final File resolvedFile = new File( contextAsFile.getParentFile(), catalogUrl );
  if ( resolvedFile.isFile() && resolvedFile.canRead() ) {
    return resolvedFile.getAbsolutePath();
  }

  return catalogUrl;
}
 
Example 20
Source File: XMLConfigProvider.java    From java-trader with Apache License 2.0 4 votes vote down vote up
public XMLConfigProvider(File file) throws Exception
{
    FileSystemManager fsManager = VFS.getManager();
    this.file = fsManager.resolveFile(file.toURI());
}