Example usage for org.apache.commons.vfs FileSystemManager resolveFile

List of usage examples for org.apache.commons.vfs FileSystemManager resolveFile

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileSystemManager resolveFile.

Prototype

public FileObject resolveFile(String name) throws FileSystemException;

Source Link

Document

Locates a file by name.

Usage

From source file:egovframework.rte.fdl.filehandling.FilehandlingServiceTest.java

@Test
public void testCaching3() throws Exception {
    FileSystemManager manager = VFS.getManager();
    String testFolder = "d:/workspace/java/e-gov/eGovFramework/RTE/DEV/trunk/Foundation/egovframework.rte.fdl.filehandling/test";
    FileObject scratchFolder = manager.resolveFile(testFolder);

    // releaseable
    FileObject dir1 = scratchFolder.resolveFile("file1.txt");

    // avoid cache removal
    FileObject dir2 = scratchFolder.resolveFile("file2.txt");
    dir2.getContent();//from   w w  w . ja v a 2 s  .  c om

    // check if the cache still holds the right instance
    FileObject dir2_2 = scratchFolder.resolveFile("file2.txt");
    assertTrue(dir2 == dir2_2);

    // check if the cache still holds the right instance
    /* FileObject dir1_2 = scratchFolder.resolveFile("file1.txt");
     assertFalse(dir1 == dir1_2);*/
}

From source file:be.ibridge.kettle.job.JobMeta.java

/**
 * This method sets various internal kettle variables that can be used by the transformation.
 *///w  w  w .  j  a  va2  s. c  om
public void setInternalKettleVariables() {
    KettleVariables variables = KettleVariables.getInstance();

    if (filename != null) // we have a finename that's defined.
    {
        try {
            FileSystemManager fsManager = VFS.getManager();
            FileObject fileObject = fsManager.resolveFile(filename);
            FileName fileName = fileObject.getName();

            // The filename of the transformation
            variables.setVariable(Const.INTERNAL_VARIABLE_JOB_FILENAME_NAME, fileName.getBaseName());

            // The directory of the transformation
            FileName fileDir = fileName.getParent();
            variables.setVariable(Const.INTERNAL_VARIABLE_JOB_FILENAME_DIRECTORY, fileDir.getURI());
        } catch (IOException e) {
            variables.setVariable(Const.INTERNAL_VARIABLE_JOB_FILENAME_DIRECTORY, "");
            variables.setVariable(Const.INTERNAL_VARIABLE_JOB_FILENAME_NAME, "");
        }
    } else {
        variables.setVariable(Const.INTERNAL_VARIABLE_JOB_FILENAME_DIRECTORY, ""); //$NON-NLS-1$
        variables.setVariable(Const.INTERNAL_VARIABLE_JOB_FILENAME_NAME, ""); //$NON-NLS-1$
    }

    // The name of the job
    variables.setVariable(Const.INTERNAL_VARIABLE_JOB_NAME, Const.NVL(name, "")); //$NON-NLS-1$

    // The name of the directory in the repository
    variables.setVariable(Const.INTERNAL_VARIABLE_JOB_REPOSITORY_DIRECTORY,
            directory != null ? directory.getPath() : ""); //$NON-NLS-1$
}

From source file:be.ibridge.kettle.trans.TransMeta.java

/**
     * This method sets various internal kettle variables that can be used by the transformation.
     *//*ww w  .j av  a 2 s .c  o  m*/
    public void setInternalKettleVariables() {
        KettleVariables variables = KettleVariables.getInstance();

        if (filename != null) // we have a finename that's defined.
        {
            try {
                FileSystemManager fsManager = VFS.getManager();
                FileObject fileObject = fsManager.resolveFile(filename);
                FileName fileName = fileObject.getName();

                // The filename of the transformation
                variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_NAME, fileName.getBaseName());

                // The directory of the transformation
                FileName fileDir = fileName.getParent();
                variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_DIRECTORY, fileDir.getURI());
            } catch (IOException e) {
                variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_DIRECTORY, "");
                variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_NAME, "");
            }
        } else {
            variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_DIRECTORY, "");
            variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_NAME, "");
        }

        // The name of the transformation
        variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_NAME, Const.NVL(name, ""));

        // The name of the directory in the repository
        variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_REPOSITORY_DIRECTORY,
                directory != null ? directory.getPath() : "");
    }

From source file:org.ambud.marauder.source.ids.IDSFileSource.java

@Override
public void configure(Context context) {
    super.configure(context);
    context = new Context(context.getSubProperties(getPrefix()));
    this.watchDirectory = new File(context.getString(PROP_DIRECTORY, getDefaultDirectory()));
    this.logBaseName = context.getString(PROP_BASE_NAME, getDefaultFilename());
    this.isSequential = context.getBoolean(PROP_IS_SEQUENTIAL, true);
    logger.info("Snort Source will spool/watch - " + this.watchDirectory.getAbsolutePath()
            + " for Snort log files whose names start with:" + this.logBaseName);
    FileSystemManager fsMgr = null;
    try {/*ww w .  j  ava  2  s  . c  om*/
        fsMgr = VFS.getManager();
    } catch (FileSystemException e) {
        Throwables.propagate(e);
    }
    try {
        this.watchObject = fsMgr.resolveFile(watchDirectory.getAbsolutePath());
    } catch (FileSystemException e) {
        Throwables.propagate(e);
    }
    this.monitor = new DefaultFileMonitor(new FileListener() {

        @Override
        public void fileChanged(FileChangeEvent arg0) throws Exception {
            // ignore these
        }

        @Override
        public void fileCreated(FileChangeEvent fileEvent) throws Exception {
            if (acceptFile(fileEvent.getFile().getName().getBaseName())) {
                logger.info("Acknowledged new file:" + fileEvent.getFile().getName().getPath());
                builderFileReader(fileEvent.getFile(), true);
            }
        }

        @Override
        public void fileDeleted(FileChangeEvent arg0) throws Exception {
            // acknowledge these
        }

    });
    int bufferSize = context.getInteger(PROP_BUFFER_SIZE, 500);
    this.outputQueue = new ArrayBlockingQueue<MarauderIDSEvent>(bufferSize);
}

From source file:org.ambud.marauder.source.readers.MarauderPollingTextFileReader.java

public MarauderPollingTextFileReader(File file, int pollingInterval)
        throws FileNotFoundException, FileSystemException {
    super(file);// ww  w .  jav a 2  s. c  o  m
    FileSystemManager fsMgr = VFS.getManager();
    FileObject fo = fsMgr.resolveFile(getFile().getAbsolutePath());
    this.monitor = new DefaultFileMonitor(new FileListener() {

        @Override
        public void fileDeleted(FileChangeEvent arg0) throws Exception {
            // can't do anything since the file is closed
            closeReader();
        }

        @Override
        public void fileCreated(FileChangeEvent arg0) throws Exception {
            // shouldn't happen if file already exist
        }

        @Override
        public void fileChanged(FileChangeEvent arg0) throws Exception {
            readFile();
        }
    });
    monitor.addFile(fo);
    monitor.setRecursive(false);
    monitor.setDelay(pollingInterval);
}

From source file:org.ambud.marauder.source.snort.SnortSourceBack.java

@Override
public void configure(Context context) {
    super.configure(context);
    context = new Context(context.getSubProperties(PROP_PREFIX));
    this.watchDirectory = new File(context.getString(PROP_DIRECTORY, PROP_DEFAULT_DIR));
    this.logBaseName = context.getString(PROP_BASE_NAME, PROP_DEFAULT_FILENAME);
    this.isSequential = context.getBoolean(PROP_IS_SEQUENTIAL, true);
    logger.info("Snort Source will spool/watch - " + this.watchDirectory.getAbsolutePath()
            + " for Snort log files whose names start with:" + this.logBaseName);
    FileSystemManager fsMgr = null;
    try {//from   w w  w. j  ava  2 s.co  m
        fsMgr = VFS.getManager();
    } catch (FileSystemException e) {
        Throwables.propagate(e);
    }
    try {
        this.watchObject = fsMgr.resolveFile(watchDirectory.getAbsolutePath());
    } catch (FileSystemException e) {
        Throwables.propagate(e);
    }
    this.monitor = new DefaultFileMonitor(new FileListener() {

        @Override
        public void fileChanged(FileChangeEvent arg0) throws Exception {
            // ignore these
        }

        @Override
        public void fileCreated(FileChangeEvent fileEvent) throws Exception {
            if (acceptFile(fileEvent.getFile().getName().getBaseName())) {
                logger.info("Acknowledged new file:" + fileEvent.getFile().getName().getPath());
                processFile(fileEvent.getFile(), true);
            }
        }

        @Override
        public void fileDeleted(FileChangeEvent arg0) throws Exception {
            // acknowledge these
        }

    });
    int bufferSize = context.getInteger("buffer.size", 500);
    this.outputQueue = new ArrayBlockingQueue<MarauderIDSEvent>(bufferSize);
}

From source file:org.apache.commons.vfs.example.ShowProperties.java

public static void main(String[] args) {
    if (args.length == 0) {
        System.err.println("Please pass the name of a file as parameter.");
        System.err.println("e.g. java org.apache.commons.vfs.example.ShowProperties LICENSE.txt");
        return;/*from w w  w  . ja v a2 s.  co m*/
    }
    for (int i = 0; i < args.length; i++) {
        try {
            FileSystemManager mgr = VFS.getManager();
            System.out.println();
            System.out.println("Parsing: " + args[i]);
            FileObject file = mgr.resolveFile(args[i]);
            System.out.println("URL: " + file.getURL());
            System.out.println("getName(): " + file.getName());
            System.out.println("BaseName: " + file.getName().getBaseName());
            System.out.println("Extension: " + file.getName().getExtension());
            System.out.println("Path: " + file.getName().getPath());
            System.out.println("Scheme: " + file.getName().getScheme());
            System.out.println("URI: " + file.getName().getURI());
            System.out.println("Root URI: " + file.getName().getRootURI());
            System.out.println("Parent: " + file.getName().getParent());
            System.out.println("Type: " + file.getType());
            System.out.println("Exists: " + file.exists());
            System.out.println("Readable: " + file.isReadable());
            System.out.println("Writeable: " + file.isWriteable());
            System.out.println("Root path: " + file.getFileSystem().getRoot().getName().getPath());
            if (file.exists()) {
                if (file.getType().equals(FileType.FILE)) {
                    System.out.println("Size: " + file.getContent().getSize() + " bytes");
                } else if (file.getType().equals(FileType.FOLDER) && file.isReadable()) {
                    FileObject[] children = file.getChildren();
                    System.out.println("Directory with " + children.length + " files");
                    for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
                        System.out.println("#" + iterChildren + ": " + children[iterChildren].getName());
                        if (iterChildren > 5) {
                            break;
                        }
                    }
                }
                System.out.println("Last modified: "
                        + DateFormat.getInstance().format(new Date(file.getContent().getLastModifiedTime())));
            } else {
                System.out.println("The file does not exist");
            }
            file.close();
        } catch (FileSystemException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:org.bibalex.gallery.storage.BAGStorage.java

public static URL cacheFileLocally(String cacheLocalPath, String fileUrlStr) throws BAGException {
    try {/*from ww w. j a  va  2  s  .c  o  m*/
        // String extension = "";
        //
        // int lastDotIx = fileUrlStr.lastIndexOf('.');
        // if (lastDotIx > fileUrlStr.lastIndexOf('/')) {
        // extension = fileUrlStr.substring(lastDotIx);
        // }
        // String cacheFileName = "cached" + fileUrlStr.hashCode() + extension;

        int lastSlashIx = fileUrlStr.lastIndexOf('/');

        String cacheFileName = fileUrlStr.substring(lastSlashIx + 1);

        String cacheFilePath = URLPathStrUtils.appendParts(cacheLocalPath, cacheFileName);

        FileSystemManager fsMgr = VFS.getManager();

        final FileObject cacheFileFO = fsMgr.resolveFile(cacheFilePath);
        final FileObject cacheDirFO = fsMgr.resolveFile(cacheLocalPath);

        if (!cacheFileFO.exists()) {
            synchronized (BAGStorage.class) {

                if (!cacheDirFO.exists()) {
                    cacheDirFO.createFolder();
                }

                cacheFileFO.createFile();

                OutputStream cacheFileOut = cacheFileFO.getContent().getOutputStream();
                try {
                    readRemoteFile(fileUrlStr, cacheFileOut);
                } finally {
                    cacheFileOut.close();
                }

            }
        }

        return cacheFileFO.getURL();

    } catch (FileSystemException e) {
        throw new BAGException(e);
    } catch (IOException e) {
        throw new BAGException(e);
    }
}

From source file:org.bibalex.gallery.storage.BAGStorage.java

public static List<String> listChildren(String dirUrlStr, FileType childrenType, int timeout)
        throws BAGException {

    try {/*from w w w  .  j a v  a  2s .  c om*/
        List<String> result;

        if (new URI(dirUrlStr).getScheme().startsWith("http")) {
            BasicHttpParams httpParams = new BasicHttpParams();

            HttpConnectionParams.setConnectionTimeout(httpParams, timeout);

            DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
            try {
                result = listChildrenApacheHttpd(dirUrlStr, childrenType, httpClient);

            } finally {
                // When HttpClient instance is no longer needed,
                // shut down the connection manager to ensure
                // immediate deallocation of all system resources
                httpClient.getConnectionManager().shutdown();
            }
        } else {

            result = new ArrayList<String>();

            FileSystemManager fsMgr = VFS.getManager();

            FileObject dir = fsMgr.resolveFile(dirUrlStr);

            final FileObject[] children = dir.getChildren();

            for (final FileObject child : children) {
                if ((childrenType == FileType.FILE_OR_FOLDER) || (child.getType() == childrenType)) {
                    result.add(child.getName().getBaseName());
                }
            }
        }
        // TODO define a comparator that orders the files properly as in
        // http://forums.sun.com/thread.jspa?threadID=5289401
        Collections.sort(result);

        return result;

    } catch (FileSystemException e) {
        throw new BAGException(e);
    } catch (URISyntaxException e) {
        throw new BAGException(e);
    }
}

From source file:org.bibalex.gallery.storage.BAGStorage.java

public static OutputStream openFileForOutput(String fileUrlStr, boolean bAppend) throws BAGException {
    try {/*from   w ww  .  j a  v  a2s .co m*/
        FileSystemManager mgr = VFS.getManager();

        FileObject fileFO = mgr.resolveFile(fileUrlStr);

        return fileFO.getContent().getOutputStream(bAppend);

    } catch (FileSystemException e) {
        throw new BAGException(e);
    }
}