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.wso2.carbon.transport.remotefilesystem.server.RemoteFileSystemConsumer.java

/**
 * Handle directory with child elements.
 *
 * @param children The array containing child elements of a folder
 *//*from  ww  w  . j  a va2 s.  c o  m*/
private void directoryHandler(FileObject[] children) throws RemoteFileSystemConnectorException {
    // Sort the files according to given properties
    String strSortParam = fileProperties.get(Constants.FILE_SORT_PARAM);

    // TODO: rethink the way the string constants are handled
    if (strSortParam != null && !Constants.ACTION_NONE.equals(strSortParam)) {
        if (log.isDebugEnabled()) {
            log.debug("Starting to sort the files in folder: "
                    + FileTransportUtils.maskURLPassword(listeningDirURI));
        }
        String strSortOrder = fileProperties.get(Constants.FILE_SORT_ORDER);
        boolean bSortOrderAscending = true;
        if (strSortOrder != null) {
            bSortOrderAscending = Boolean.parseBoolean(strSortOrder);
        }
        if (log.isDebugEnabled()) {
            log.debug("Sorting the files by : " + strSortOrder + ". (" + bSortOrderAscending + ")");
        }
        switch (strSortParam) {
        case Constants.FILE_SORT_VALUE_NAME:
            if (bSortOrderAscending) {
                Arrays.sort(children, new FileNameAscComparator());
            } else {
                Arrays.sort(children, new FileNameDesComparator());
            }
            break;
        case Constants.FILE_SORT_VALUE_SIZE:
            if (bSortOrderAscending) {
                Arrays.sort(children, new FileSizeAscComparator());
            } else {
                Arrays.sort(children, new FileSizeDesComparator());
            }
            break;
        case Constants.FILE_SORT_VALUE_LASTMODIFIEDTIMESTAMP:
            if (bSortOrderAscending) {
                Arrays.sort(children, new FileLastModifiedTimestampAscComparator());
            } else {
                Arrays.sort(children, new FileLastModifiedTimestampDesComparator());
            }
            break;
        default:
            log.warn("[" + serviceName + "] Invalid value given for " + Constants.FILE_SORT_PARAM
                    + " parameter. " + " Expected one of the values: " + Constants.FILE_SORT_VALUE_NAME + ", "
                    + Constants.FILE_SORT_VALUE_SIZE + " or " + Constants.FILE_SORT_VALUE_LASTMODIFIEDTIMESTAMP
                    + ". Found: " + strSortParam);
            break;
        }
        if (log.isDebugEnabled()) {
            log.debug("End sorting the files.");
        }
    }
    for (FileObject child : children) {
        if (fileProcessCount != 0 && processCount > fileProcessCount) {
            return;
        }
        if (child.getName().getBaseName().endsWith(".lock")
                || child.getName().getBaseName().endsWith(".fail")) {
            continue;
        }
        if (!(fileNamePattern == null || child.getName().getBaseName().matches(fileNamePattern))) {
            if (log.isDebugEnabled()) {
                log.debug("File " + FileTransportUtils.maskURLPassword(listeningDir.getName().getBaseName())
                        + " is not processed because it did not match the specified pattern.");
            }
        } else {
            FileType childType = getFileType(child);
            if (childType == FileType.FOLDER) {
                FileObject[] c = null;
                try {
                    c = child.getChildren();
                } catch (FileSystemException ignored) {
                    if (log.isDebugEnabled()) {
                        log.debug("The file does not exist, or is not a folder, or an error "
                                + "has occurred when trying to list the children. File URI : "
                                + FileTransportUtils.maskURLPassword(listeningDirURI), ignored);
                    }
                }

                // if this is a file that would translate to a single message
                if (c == null || c.length == 0) {
                    if (log.isDebugEnabled()) {
                        log.debug("Folder at " + FileTransportUtils.maskURLPassword(child.getName().getURI())
                                + " is empty.");
                    }
                } else {
                    directoryHandler(c);
                }
                postProcess(child, true);
            } else {
                fileHandler(child);
            }
        }
    }
}

From source file:org.wso2.carbon.transport.remotefilesystem.server.RemoteFileSystemConsumer.java

/**
 * Process a single file.//ww w  . j  a v a2  s  .co  m
 *
 * @param file A single file to be processed
 */
private void fileHandler(FileObject file) {
    String uri = file.getName().getURI();
    synchronized (this) {
        if (postProcessAction.equals(Constants.ACTION_NONE) && processed.contains(uri)) {
            if (log.isDebugEnabled()) {
                log.debug("The file: " + FileTransportUtils.maskURLPassword(uri) + " is already processed");
            }
            return;
        }
    }
    if (!postProcessAction.equals(Constants.ACTION_NONE) && isFailRecord(file)) {
        // it is a failed record
        try {
            postProcess(file, true);
        } catch (RemoteFileSystemConnectorException e) {
            log.error("File object '" + FileTransportUtils.maskURLPassword(uri) + "'could not complete action "
                    + postProcessAction + ", will remain in \"fail\" state", e);
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Processing file: " + FileTransportUtils.maskURLPassword(file.getName().getBaseName()));
        }
        if (!processPending.contains(uri)) {
            // Temporary block adding same file to the queue. File lock will acquire in the spawn thread.
            processPending.add(uri);
            RemoteFileSystemProcessor fsp = new RemoteFileSystemProcessor(remoteFileSystemListener, serviceName,
                    file, this, postProcessAction, fsManager, fso);
            threadPool.execute(fsp);
            processCount++;
        }
    }
}

From source file:org.wso2.carbon.transport.remotefilesystem.server.RemoteFileSystemConsumer.java

/**
 * Do the post processing actions./*from  w  w  w. j  av a2 s .c o m*/
 *
 * @param file The file object which needs to be post processed
 * @param processSucceed Whether processing of file passed or not.
 */
synchronized void postProcess(FileObject file, boolean processSucceed)
        throws RemoteFileSystemConnectorException {
    String moveToDirectoryURI = null;
    FileType fileType = getFileType(file);
    if (processSucceed) {
        if (postProcessAction.equals(Constants.ACTION_MOVE)) {
            moveToDirectoryURI = fileProperties.get(Constants.MOVE_AFTER_PROCESS);
        }
    } else {
        if (postFailureAction.equals(Constants.ACTION_MOVE)) {
            moveToDirectoryURI = fileProperties.get(Constants.MOVE_AFTER_FAILURE);
        }
    }
    if (moveToDirectoryURI != null) {
        try {
            if (getFileType(fsManager.resolveFile(moveToDirectoryURI, fso)) == FileType.FILE) {
                moveToDirectoryURI = null;
                if (processSucceed) {
                    postProcessAction = Constants.ACTION_NONE;
                } else {
                    postFailureAction = Constants.ACTION_NONE;
                }
                log.warn("[" + serviceName + "] Cannot move file because provided location is not a folder."
                        + " File is kept at source.");
            }
        } catch (FileSystemException e) {
            remoteFileSystemListener.onError(new RemoteFileSystemConnectorException(
                    "Error occurred when resolving move destination file: "
                            + FileTransportUtils.maskURLPassword(listeningDirURI),
                    e));
        }
    }
    if (postProcessAction.equals(Constants.ACTION_NONE) && fileType == FileType.FOLDER) {
        return;
    }
    try {
        if (!(moveToDirectoryURI == null || fileType == FileType.FOLDER)) {
            FileObject moveToDirectory;
            String relativeName = file.getName().getURI().split(listeningDir.getName().getURI())[1];
            int index = relativeName.lastIndexOf(File.separator);
            moveToDirectoryURI += relativeName.substring(0, index);
            moveToDirectory = fsManager.resolveFile(moveToDirectoryURI, fso);
            String prefix;
            if (fileProperties.get(Constants.MOVE_TIMESTAMP_FORMAT) != null) {
                prefix = new SimpleDateFormat(fileProperties.get(Constants.MOVE_TIMESTAMP_FORMAT))
                        .format(new Date());
            } else {
                prefix = "";
            }
            //Forcefully create the folder(s) if does not exists
            String strForceCreateFolder = fileProperties.get(Constants.FORCE_CREATE_FOLDER);
            if (strForceCreateFolder != null && strForceCreateFolder.equalsIgnoreCase("true")
                    && !moveToDirectory.exists()) {
                moveToDirectory.createFolder();
            }
            FileObject destination = moveToDirectory.resolveFile(prefix + file.getName().getBaseName());
            if (log.isDebugEnabled()) {
                log.debug("Moving file: " + FileTransportUtils.maskURLPassword(file.getName().getBaseName()));
            }
            try {
                file.moveTo(destination);
                if (isFailRecord(file)) {
                    releaseFail(file);
                }
            } catch (FileSystemException e) {
                if (!isFailRecord(file)) {
                    markFailRecord(file);
                }
                remoteFileSystemListener.onError(new RemoteFileSystemConnectorException("[" + serviceName
                        + "] Error moving file: " + FileTransportUtils.maskURLPassword(file.toString()) + " to "
                        + FileTransportUtils.maskURLPassword(moveToDirectoryURI), e));
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Deleting file: " + FileTransportUtils.maskURLPassword(file.getName().getBaseName()));
            }
            try {
                if (!file.delete()) {
                    if (log.isDebugEnabled()) {
                        log.debug("Could not delete file: "
                                + FileTransportUtils.maskURLPassword(file.getName().getBaseName()));
                    }
                } else {
                    if (isFailRecord(file)) {
                        releaseFail(file);
                    }
                }
            } catch (FileSystemException e) {
                remoteFileSystemListener.onError(
                        new RemoteFileSystemConnectorException("[" + serviceName + "] Could not delete file: "
                                + FileTransportUtils.maskURLPassword(file.getName().getBaseName()), e));
            }
        }
    } catch (FileSystemException e) {
        if (!isFailRecord(file)) {
            markFailRecord(file);
            remoteFileSystemListener.onError(new RemoteFileSystemConnectorException(
                    "[" + serviceName + "] Error resolving directory to move file : "
                            + FileTransportUtils.maskURLPassword(moveToDirectoryURI),
                    e));
        }
    }
}

From source file:org.wso2.carbon.transport.remotefilesystem.server.RemoteFileSystemConsumer.java

/**
 * Determine whether file object is a file or a folder.
 *
 * @param fileObject    File to get the type of
 * @return              FileType of given file
 *///from  www  . j  av a  2  s  .c  om
private FileType getFileType(FileObject fileObject) throws RemoteFileSystemConnectorException {
    try {
        return fileObject.getType();
    } catch (FileSystemException e) {
        remoteFileSystemListener.onError(new RemoteFileSystemConnectorException("[" + serviceName
                + "] Error occurred when determining whether file: "
                + FileTransportUtils.maskURLPassword(fileObject.getName().getURI()) + " is a file or a folder",
                e));
    }

    return FileType.IMAGINARY;
}

From source file:org.wso2.carbon.transport.remotefilesystem.server.util.FileTransportUtils.java

/**
 * Acquire the file level locking.//from   w w w .  j  a v a  2  s  .c  om
 *
 * @param fsManager     The file system manager instance
 * @param fileObject    The file object to get the lock from
 * @param fsOpts        The file system options to be used with the file system manager
 * @return              Boolean value whether lock was successful
 */
public static synchronized boolean acquireLock(FileSystemManager fsManager, FileObject fileObject,
        FileSystemOptions fsOpts) {
    String strContext = fileObject.getName().getURI();

    // When processing a directory list is fetched initially. Therefore
    // there is still a chance of file processed by another process.
    // Need to check the source file before processing.
    try {
        String parentURI = fileObject.getParent().getName().getURI();
        if (parentURI.contains("?")) {
            String suffix = parentURI.substring(parentURI.indexOf("?"));
            strContext += suffix;
        }
        FileObject sourceFile = fsManager.resolveFile(strContext, fsOpts);
        if (!sourceFile.exists()) {
            return false;
        }
    } catch (FileSystemException e) {
        return false;
    }
    FileObject lockObject = null;
    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 = fileObject.getName().getURI();
        int pos = fullPath.indexOf("?");
        if (pos != -1) {
            fullPath = fullPath.substring(0, pos);
        }
        lockObject = fsManager.resolveFile(fullPath + ".lock", fsOpts);
        if (lockObject.exists()) {
            if (log.isDebugEnabled()) {
                log.debug("There seems to be an external lock, aborting the processing of the file "
                        + maskURLPassword(fileObject.getName().getURI())
                        + ". This could possibly be due to some other party already "
                        + "processing this file or the file is still being uploaded");
            }
        } else if (processing.contains(fullPath)) {
            if (log.isDebugEnabled()) {
                log.debug(maskURLPassword(fileObject.getName().getURI()) + "is already being processed.");
            }
        } else {
            //Check the original file existence before the lock file to handle concurrent access scenario
            FileObject originalFileObject = fsManager.resolveFile(fullPath, fsOpts);
            if (!originalFileObject.exists()) {
                return false;
            }
            processing.add(fullPath);
            return true;
        }
    } catch (FileSystemException fse) {
        log.error("Cannot get the lock for the file : " + maskURLPassword(fileObject.getName().getURI())
                + " before processing", fse);
        if (lockObject != null) {
            try {
                fsManager.closeFileSystem(lockObject.getParent().getFileSystem());
            } catch (FileSystemException e) {
                log.warn("Unable to close the lockObject parent file system");
            }
        }
    }
    return false;
}

From source file:org.ysb33r.groovy.vfsplugin.cpio.CpioFileProvider.java

/**
 * Creates a layered file system.  This method is called if the file system
 * is not cached./*from  w  w w  .j  a va 2s. c om*/
 *
 * @param scheme The URI scheme.
 * @param file   The file to create the file system on top of.
 * @return The file system.
 */
@Override
protected FileSystem doCreateFileSystem(final String scheme, final FileObject file,
        final FileSystemOptions fileSystemOptions) throws FileSystemException {
    final AbstractFileName rootName = new LayeredFileName(scheme, file.getName(), FileName.ROOT_PATH,
            FileType.FOLDER);
    return new CpioFileSystem(rootName, file, fileSystemOptions);
}

From source file:pl.otros.logview.api.io.Utils.java

public static boolean checkIfIsGzipped(FileObject fileObject) throws IOException {
    boolean gziped = false;
    if (fileObject.getContent().getSize() == 0) {
        LOGGER.debug("File object " + fileObject.getName() + " is empty, can't detect gzip compression");
        return false;
    }/* www.  j a v  a  2s. c  o  m*/
    InputStream inputStream = fileObject.getContent().getInputStream();
    byte[] loadProbe = loadProbe(inputStream, GZIP_CHECK_BUFFER_SIZE);
    // IOUtils.closeQuietly(inputStream);
    if (loadProbe.length < GZIP_MIN_SIZE) {
        LOGGER.info("Loaded probe is too small to check if it is gziped");
        return false;
    }
    try {
        ByteArrayInputStream bin = new ByteArrayInputStream(loadProbe);
        int available = bin.available();
        byte[] b = new byte[available < GZIP_CHECK_BUFFER_SIZE ? available : GZIP_CHECK_BUFFER_SIZE];
        int read = bin.read(b);
        gziped = checkIfIsGzipped(b, read);
    } catch (IOException e) {
        // Not gziped
        LOGGER.debug(fileObject.getName() + " is not gzip");
    }

    return gziped;
}

From source file:pl.otros.logview.api.io.Utils.java

public static LoadingInfo openFileObject(FileObject fileObject, boolean tailing) throws Exception {
    LoadingInfo loadingInfo = new LoadingInfo();
    loadingInfo.setFileObject(fileObject);
    loadingInfo.setFriendlyUrl(fileObject.getName().getFriendlyURI());

    final FileContent content = fileObject.getContent();
    InputStream httpInputStream = content.getInputStream();
    byte[] buff = Utils.loadProbe(httpInputStream, 10000);

    loadingInfo.setGziped(checkIfIsGzipped(buff, buff.length));

    ByteArrayInputStream bin = new ByteArrayInputStream(buff);

    SequenceInputStream sequenceInputStream = new SequenceInputStream(bin, httpInputStream);

    ObservableInputStreamImpl observableInputStreamImpl = new ObservableInputStreamImpl(sequenceInputStream);

    if (loadingInfo.isGziped()) {
        loadingInfo.setContentInputStream(new GZIPInputStream(observableInputStreamImpl));
        loadingInfo.setInputStreamBufferedStart(ungzip(buff));
    } else {/*from w w w .j a v  a  2 s. c om*/
        loadingInfo.setContentInputStream(observableInputStreamImpl);
        loadingInfo.setInputStreamBufferedStart(buff);
    }
    loadingInfo.setObserableInputStreamImpl(observableInputStreamImpl);

    loadingInfo.setTailing(tailing);
    if (fileObject.getType().hasContent()) {
        loadingInfo.setLastFileSize(content.getSize());
    }
    return loadingInfo;

}

From source file:pl.otros.logview.api.io.Utils.java

public static void closeQuietly(FileObject fileObject) {

    if (fileObject != null) {
        String friendlyURI = fileObject.getName().getFriendlyURI();
        try {/*from ww  w. j  a  va 2  s  . c  om*/
            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()));
        }
    }
}

From source file:pl.otros.logview.api.io.Utils.java

/**
 * Get short name for URL//from  w w w. j av  a2  s  .c  o  m
 *
 * @param  fileObject File object
 * @return scheme://hostWithoutDomain/fileBaseName
 */
public static String getFileObjectShortName(FileObject fileObject) {
    StringBuilder sb = new StringBuilder();
    try {
        URI uri = new URI(fileObject.getName().getURI());
        String scheme = fileObject.getName().getScheme();
        sb.append(scheme);
        sb.append("://");
        if (!"file".equals(scheme)) {
            String host = uri.getHost();
            // if host name is not IP, return only host name
            if (!Pattern.matches("(\\d+\\.){3}\\d+", host)) {
                host = host.split("\\.")[0];
            }
            sb.append(host).append('/');
        }
        sb.append(fileObject.getName().getBaseName());
    } catch (URISyntaxException e) {
        LOGGER.warn("Problem with preparing short name of FileObject: " + e.getMessage());
        sb.setLength(0);
        sb.append(fileObject.getName().getScheme()).append("://").append(fileObject.getName().getBaseName());
    }
    return sb.toString();
}