Example usage for org.apache.commons.vfs2 FileObject getName

List of usage examples for org.apache.commons.vfs2 FileObject getName

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject getName.

Prototype

FileName getName();

Source Link

Document

Returns the name of this file.

Usage

From source file:org.apache.metron.common.utils.VFSClassloaderUtil.java

/**
 * Resolve a set of URIs into FileObject objects.
 * This is not recursive. The URIs can refer directly to a file or directory or an optional regex at the end.
 * (NOTE: This is NOT a glob)./* w  w w  .ja v a 2 s.  c om*/
 * @param vfs The file system manager to use to resolve URIs
 * @param uris comma separated URIs and URI + globs
 * @return
 * @throws FileSystemException
 */
static FileObject[] resolve(FileSystemManager vfs, String uris) throws FileSystemException {
    if (uris == null) {
        return new FileObject[0];
    }

    ArrayList<FileObject> classpath = new ArrayList<>();
    for (String path : uris.split(",")) {
        path = path.trim();
        if (path.equals("")) {
            continue;
        }
        FileObject fo = vfs.resolveFile(path);
        switch (fo.getType()) {
        case FILE:
        case FOLDER:
            classpath.add(fo);
            break;
        case IMAGINARY:
            // assume its a pattern
            String pattern = fo.getName().getBaseName();
            if (fo.getParent() != null && fo.getParent().getType() == FileType.FOLDER) {
                FileObject[] children = fo.getParent().getChildren();
                for (FileObject child : children) {
                    if (child.getType() == FileType.FILE && child.getName().getBaseName().matches(pattern)) {
                        classpath.add(child);
                    }
                }
            } else {
                LOG.warn("ignoring classpath entry " + fo);
            }
            break;
        default:
            LOG.warn("ignoring classpath entry " + fo);
            break;
        }
    }
    return classpath.toArray(new FileObject[classpath.size()]);
}

From source file:org.apache.metron.stellar.common.utils.VFSClassloaderUtil.java

/**
 * Resolve a set of URIs into FileObject objects.
 * This is not recursive. The URIs can refer directly to a file or directory or an optional regex at the end.
 * (NOTE: This is NOT a glob).//from w ww .  ja va 2s  .  c o  m
 * @param vfs The file system manager to use to resolve URIs
 * @param uris comma separated URIs and URI + globs
 * @return
 * @throws FileSystemException
 */
static FileObject[] resolve(FileSystemManager vfs, String uris) throws FileSystemException {
    if (uris == null) {
        return new FileObject[0];
    }

    ArrayList<FileObject> classpath = new ArrayList<>();
    for (String path : uris.split(",")) {
        path = path.trim();
        if (path.equals("")) {
            continue;
        }
        FileObject fo = vfs.resolveFile(path);
        switch (fo.getType()) {
        case FILE:
        case FOLDER:
            classpath.add(fo);
            break;
        case IMAGINARY:
            // assume its a pattern
            String pattern = fo.getName().getBaseName();
            if (fo.getParent() != null && fo.getParent().getType() == FileType.FOLDER) {
                FileObject[] children = fo.getParent().getChildren();
                for (FileObject child : children) {
                    if (child.getType() == FileType.FILE && child.getName().getBaseName().matches(pattern)) {
                        classpath.add(child);
                    }
                }
            } else {
                LOG.warn("ignoring classpath entry {}", fo);
            }
            break;
        default:
            LOG.warn("ignoring classpath entry {}", fo);
            break;
        }
    }
    return classpath.toArray(new FileObject[classpath.size()]);
}

From source file:org.apache.olingo.fit.utils.FSManager.java

private FSManager() throws IOException {
    fsManager = VFS.getManager();//  w  ww .j  a  va2s .  c o  m

    final FileObject basePath = fsManager
            .resolveFile(RES_PREFIX + File.separatorChar + ODataServiceVersion.V40.name());
    final String absoluteBaseFolder = basePath.getURL().getPath();

    for (FileObject fo : find(basePath, null)) {
        if (fo.getType() == FileType.FILE && !fo.getName().getBaseName().contains("Metadata")
                && !fo.getName().getBaseName().contains("metadata")) {
            final String path = fo.getURL().getPath().replace(absoluteBaseFolder,
                    "//" + ODataServiceVersion.V40.name());
            putInMemory(fo.getContent().getInputStream(), path);
        }
    }
}

From source file:org.apache.olingo.fit.utils.XMLUtilities.java

public int countAllElements(final String entitySetName) throws Exception {
    final String basePath = entitySetName + File.separatorChar;
    int count = countFeedElements(fsManager.readFile(basePath + Constants.get(ConstantKey.FEED), Accept.XML),
            "entry");

    final String skipTokenDirPath = fsManager.getAbsolutePath(basePath + Constants.get(ConstantKey.SKIP_TOKEN),
            null);/*ww  w.  j av a 2  s  . c om*/

    try {
        final FileObject skipToken = fsManager.resolve(skipTokenDirPath);
        final FileObject[] files = fsManager.find(skipToken, Accept.XML.getExtension().substring(1));

        for (FileObject file : files) {
            count += countFeedElements(fsManager.readFile(basePath + Constants.get(ConstantKey.SKIP_TOKEN)
                    + File.separatorChar + file.getName().getBaseName(), null), "entry");
        }
    } catch (FileSystemException fse) {
        LOG.debug("Resource path '{}' not found", skipTokenDirPath);
    }

    return count;
}

From source file:org.apache.river.container.deployer.CommandLineAppRunner.java

private void tryInitialize() throws IOException, ParseException {
    log.log(Level.FINE, MessageNames.STARTER_SERVICE_DEPLOYER_STARTING, myName);

    /*/*from   ww  w  . ja  v a  2  s.  com*/
     Establish the deployment directory.
     */
    deploymentDirectoryFile = fileUtility.getProfileDirectory().resolveFile(deployDirectory);
    if (deploymentDirectoryFile == null || deploymentDirectoryFile.getType() != FileType.FOLDER) {
        log.log(Level.WARNING, MessageNames.NO_DEPLOYMENT_DIRECTORY,
                new Object[] { deployDirectory, fileUtility.getProfileDirectory() });
    }
    /*
     * Find the name of the client we need to deploy.  
     */
    /* First argument was the profile name.  Second argument is the name of 
     * the client app to run.  All the rest are parameters to the client
     * app.
     */
    if (clientAppName == null && commandLineArguments.length < 2) {
        System.out.println(messages.getString(MessageNames.CLIENT_APP_USAGE));
        System.exit(1);
    }
    String[] clientAppArgs;
    if (clientAppName == null) {
        clientAppName = commandLineArguments[1];
        clientAppArgs = new String[commandLineArguments.length - 2];
        System.arraycopy(commandLineArguments, 2, clientAppArgs, 0, clientAppArgs.length);
    } else {
        clientAppArgs = new String[commandLineArguments.length - 1];
        System.arraycopy(commandLineArguments, 1, clientAppArgs, 0, clientAppArgs.length);
    }
    // Locate the service archive that has the client's name.
    // First get all the jar files.
    List<FileObject> serviceArchives = Utils.findChildrenWithSuffix(deploymentDirectoryFile,
            org.apache.river.container.Strings.JAR);
    //Then find the one that starts with the client name
    FileObject serviceArchive = null;
    for (FileObject fo : serviceArchives) {
        if (fo.getName().getBaseName().startsWith(clientAppName + org.apache.river.container.Strings.DASH)) {
            serviceArchive = fo;
            break;
        }

    }

    if (serviceArchive == null) {
        System.err.println(
                MessageFormat.format(messages.getString(MessageNames.NO_SUCH_CLIENT_APP), clientAppName));
        System.exit(1);
    }
    // Deploy the service
    deployServiceArchive(serviceArchive, clientAppArgs);
    // Run the main method with the remaining command line parameters.
}

From source file:org.apache.river.container.deployer.FolderBasedAppRunner.java

private Map<String, DeploymentRecord> scanDeploymentArchives() throws FileSystemException {
    /*/* w w w.j av  a  2  s .c  o  m*/
     Go through the deployment directory looking for services to deploy.
     */
    Map<String, DeploymentRecord> deployDirListing = new HashMap<String, DeploymentRecord>();
    deploymentDirectoryFile.refresh();
    List<FileObject> serviceArchives = Utils.findChildrenWithSuffix(deploymentDirectoryFile,
            org.apache.river.container.Strings.JAR);
    if (serviceArchives != null) {
        log.log(Level.FINER, MessageNames.FOUND_SERVICE_ARCHIVES,
                new Object[] { serviceArchives.size(), deployDirectory });
        for (FileObject serviceArchive : serviceArchives) {
            DeploymentRecord rec = new DeploymentRecord();
            rec.fileObject = serviceArchive;
            rec.name = serviceArchive.getName().getBaseName();
            rec.updateTime = serviceArchive.getContent().getLastModifiedTime();
            deployDirListing.put(rec.name, rec);
        }
    }
    return deployDirListing;
}

From source file:org.apache.synapse.commons.vfs.VFSUtils.java

/**
 * Acquires a file item lock before processing the item, guaranteing that
 * the file is not processed while it is being uploaded and/or the item is
 * not processed by two listeners//from   w w w.ja  va 2  s .  com
 * 
 * @param fsManager
 *            used to resolve the processing file
 * @param fo
 *            representing the processing file item
 * @param fso
 *            represents file system options used when resolving file from file system manager.
 * @return boolean true if the lock has been acquired or false if not
 */
public synchronized static boolean acquireLock(FileSystemManager fsManager, FileObject fo, VFSParamDTO paramDTO,
        FileSystemOptions fso) {

    // generate a random lock value to ensure that there are no two parties
    // processing the same file
    Random random = new Random();
    // Lock format random:hostname:hostip:time
    String strLockValue = String.valueOf(random.nextLong());
    try {
        strLockValue += STR_SPLITER + InetAddress.getLocalHost().getHostName();
        strLockValue += STR_SPLITER + InetAddress.getLocalHost().getHostAddress();
    } catch (UnknownHostException ue) {
        if (log.isDebugEnabled()) {
            log.debug("Unable to get the Hostname or IP.");
        }
    }
    strLockValue += STR_SPLITER + (new Date()).getTime();
    byte[] lockValue = strLockValue.getBytes();

    try {
        // check whether there is an existing lock for this item, if so it is assumed
        // to be processed by an another listener (downloading) or a sender (uploading)
        // lock file is derived by attaching the ".lock" second extension to the file name
        String fullPath = fo.getName().getURI();
        int pos = fullPath.indexOf("?");
        if (pos != -1) {
            fullPath = fullPath.substring(0, pos);
        }
        FileObject lockObject = fsManager.resolveFile(fullPath + ".lock", fso);
        if (lockObject.exists()) {
            log.debug("There seems to be an external lock, aborting the processing of the file "
                    + maskURLPassword(fo.getName().getURI())
                    + ". This could possibly be due to some other party already "
                    + "processing this file or the file is still being uploaded");
            if (paramDTO != null && paramDTO.isAutoLockRelease()) {
                releaseLock(lockValue, strLockValue, lockObject, paramDTO.isAutoLockReleaseSameNode(),
                        paramDTO.getAutoLockReleaseInterval());
            }
        } else {
            // write a lock file before starting of the processing, to ensure that the
            // item is not processed by any other parties
            lockObject.createFile();
            OutputStream stream = lockObject.getContent().getOutputStream();
            try {
                stream.write(lockValue);
                stream.flush();
                stream.close();
            } catch (IOException e) {
                lockObject.delete();
                log.error(
                        "Couldn't create the lock file before processing the file " + maskURLPassword(fullPath),
                        e);
                return false;
            } finally {
                lockObject.close();
            }

            // check whether the lock is in place and is it me who holds the lock. This is
            // required because it is possible to write the lock file simultaneously by
            // two processing parties. It checks whether the lock file content is the same
            // as the written random lock value.
            // NOTE: this may not be optimal but is sub optimal
            FileObject verifyingLockObject = fsManager.resolveFile(fullPath + ".lock", fso);
            if (verifyingLockObject.exists() && verifyLock(lockValue, verifyingLockObject)) {
                return true;
            }
        }
    } catch (FileSystemException fse) {
        log.error("Cannot get the lock for the file : " + maskURLPassword(fo.getName().getURI())
                + " before processing");
    }
    return false;
}

From source file:org.apache.synapse.commons.vfs.VFSUtils.java

/**
 * Release a file item lock acquired either by the VFS listener or a sender
 *
 * @param fsManager which is used to resolve the processed file
 * @param fo representing the processed file
 * @param fso represents file system options used when resolving file from file system manager.
 *///w w  w  . j  a  v  a  2 s .c  o m
public static void releaseLock(FileSystemManager fsManager, FileObject fo, FileSystemOptions fso) {
    String fullPath = fo.getName().getURI();

    try {
        int pos = fullPath.indexOf("?");
        if (pos > -1) {
            fullPath = fullPath.substring(0, pos);
        }
        FileObject lockObject = fsManager.resolveFile(fullPath + ".lock", fso);
        if (lockObject.exists()) {
            lockObject.delete();
        }
    } catch (FileSystemException e) {
        log.error("Couldn't release the lock for the file : " + maskURLPassword(fo.getName().getURI())
                + " after processing");
    }
}

From source file:org.apache.synapse.commons.vfs.VFSUtils.java

public synchronized static void markFailRecord(FileSystemManager fsManager, FileObject fo) {

    // generate a random fail value to ensure that there are no two parties
    // processing the same file
    Random random = new Random();
    byte[] failValue = (Long.toString((new Date()).getTime())).getBytes();

    try {//from  w  w w . j a v  a 2s  .  c om
        String fullPath = fo.getName().getURI();
        int pos = fullPath.indexOf("?");
        if (pos != -1) {
            fullPath = fullPath.substring(0, pos);
        }
        FileObject failObject = fsManager.resolveFile(fullPath + ".fail");
        if (!failObject.exists()) {
            failObject.createFile();
        }

        // write a lock file before starting of the processing, to ensure that the
        // item is not processed by any other parties

        OutputStream stream = failObject.getContent().getOutputStream();
        try {
            stream.write(failValue);
            stream.flush();
            stream.close();
        } catch (IOException e) {
            failObject.delete();
            log.error("Couldn't create the fail file before processing the file " + maskURLPassword(fullPath),
                    e);
        } finally {
            failObject.close();
        }
    } catch (FileSystemException fse) {
        log.error("Cannot get the lock for the file : " + maskURLPassword(fo.getName().getURI())
                + " before processing");
    }
}

From source file:org.apache.synapse.commons.vfs.VFSUtils.java

public static boolean isFailRecord(FileSystemManager fsManager, FileObject fo) {
    try {//  w  w  w.  j  a  v a  2 s.  c o m
        String fullPath = fo.getName().getURI();
        int pos = fullPath.indexOf("?");
        if (pos > -1) {
            fullPath = fullPath.substring(0, pos);
        }
        FileObject failObject = fsManager.resolveFile(fullPath + ".fail");
        if (failObject.exists()) {
            return true;
        }
    } catch (FileSystemException e) {
        log.error("Couldn't release the fail for the file : " + maskURLPassword(fo.getName().getURI()));
    }
    return false;
}