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

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

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.pentaho.hadoop.shim.HadoopExcludeJarsTest.java

@Test
public void filterJars_removeOnlyByArtifactIdVersionTemplate() throws Exception {
    HadoopConfigurationLocator locator = new HadoopConfigurationLocator();
    FileObject root = VFS.getManager().resolveFile(HADOOP_CONFIGURATIONS_PATH);

    List<URL> urls = locator.parseURLs(root, root.toString());
    count = urls.size();//from   w w w .ja  va  2 s  .  c  om
    List<URL> list = locator.filterJars(urls, "pentaho-hadoop-shims-api-61.2016.04.01-196");
    assertEquals(count - 1, list.size());
}

From source file:org.pentaho.hadoop.shim.HadoopExcludeJarsTest.java

@Test
public void filterJars_removeOnlyByWholeJarName() throws Exception {
    HadoopConfigurationLocator locator = new HadoopConfigurationLocator();
    FileObject root = VFS.getManager().resolveFile(HADOOP_CONFIGURATIONS_PATH);

    List<URL> urls = locator.parseURLs(root, root.toString());
    count = urls.size();/*  ww  w . ja v  a2 s . com*/
    List<URL> list = locator.filterJars(urls, "pentaho-hadoop-shims-api-61.2016.04.01-196.jar");
    assertEquals(count - 1, list.size());
}

From source file:org.pentaho.hadoop.shim.HadoopExcludeJarsTest.java

@Test
public void filterJars_removeThreeJarsWithDifferentTemplates() throws Exception {
    HadoopConfigurationLocator locator = new HadoopConfigurationLocator();
    FileObject root = VFS.getManager().resolveFile(HADOOP_CONFIGURATIONS_PATH);

    List<URL> urls = locator.parseURLs(root, root.toString());
    count = urls.size();//from  w w  w . jav a 2  s. c  o m
    List<URL> list = locator.filterJars(urls,
            "xercesImpl,trilead-ssh2-build215,pentaho-hadoop-shims-api-61.2016.04.01-196.jar");
    assertEquals(count - 3, list.size());
}

From source file:org.pentaho.hadoop.shim.HadoopRunningOnClusterTest.java

@Test
public void runningLocally_withLinuxClassPathProperty() throws Exception {
    HadoopConfigurationLocator locator = new HadoopConfigurationLocator();
    FileObject folder = VFS.getManager().resolveFile(CONFIG_PROPERTY_CLASSPATH);

    try {//from   ww w  .  j a  v  a 2s .c  o  m
        disablePmrFile();
        List<URL> classpathElements = null;
        if (!locator.isRunningOnCluster()) {
            classpathElements = locator.parseURLs(folder, folder.toString());
            count = classpathElements.size();
        }
        Assert.assertNotNull(classpathElements);
        Assert.assertEquals(6, count);
    } finally {
        activatePmrFile();
    }
}

From source file:org.pentaho.hadoop.shim.HadoopRunningOnClusterTest.java

@Test
public void runningOnCluster_ignoreLinuxClassPathProperty() throws Exception {
    HadoopConfigurationLocator locator = new HadoopConfigurationLocator();
    FileObject folder = VFS.getManager().resolveFile(CONFIG_PROPERTY_CLASSPATH);

    activatePmrFile();//  www  .j a va  2s.  c  o  m
    List<URL> classpathElements = null;
    if (!locator.isRunningOnCluster()) {
        classpathElements = locator.parseURLs(folder, folder.toString());
        count = classpathElements.size();
    }
    Assert.assertEquals(null, classpathElements);
    Assert.assertEquals(0, count);
}

From source file:org.renjin.primitives.files.FilesTest.java

@Before
public void setUpTests() throws FileSystemException {
    assumingBasePackagesLoad();//from   w w  w .j  a v a2 s.  c o m

    // For reproducible tests, we've included a hierarchy of files in src/test/resources
    // These should be on the classpath when tests are run
    URL resourceURL = FilesTest.class.getResource("FilesTest/a.txt");
    FileObject rootDir = topLevelContext.resolveFile(resourceURL.getPath()).getParent();

    topLevelContext.getGlobalEnvironment().setVariable(topLevelContext, "rootDir",
            StringVector.valueOf(rootDir.toString()));
}

From source file:org.wso2.carbon.connector.FileUnzipConnector.java

/**
 * Decompress the compressed file into the given directory.
 *
 * @param messageContext The message context that is generated for processing unzip operation.
 * @return true, if zip file successfully extracts and false, if not.
 * @throws FileSystemException On error parsing the file name, determining if the file exists and creating the
 * folder.//w ww .  j a  va  2 s  . c o m
 */
private boolean unzip(MessageContext messageContext) throws FileSystemException {
    String source = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.FILE_LOCATION);
    String destination = (String) ConnectorUtils.lookupTemplateParamater(messageContext,
            FileConstants.NEW_FILE_LOCATION);
    StandardFileSystemManager manager = FileConnectorUtils.getManager();
    FileSystemOptions opts = FileConnectorUtils.init(messageContext);
    FileObject remoteFile = manager.resolveFile(source, opts);
    FileObject remoteDesFile = manager.resolveFile(destination, opts);

    if (!remoteFile.exists()) {
        log.error("File does not exist.");
        return false;
    }
    if (!remoteDesFile.exists()) {
        //create a folder
        remoteDesFile.createFolder();
    }
    //open the zip file
    ZipInputStream zipIn = new ZipInputStream(remoteFile.getContent().getInputStream());
    try {
        ZipEntry entry = zipIn.getNextEntry();

        // iterates over entries in the zip file
        while (entry != null) {
            String filePath = destination + File.separator + entry.getName();
            // create remote object
            FileObject remoteFilePath = manager.resolveFile(filePath, opts);
            if (log.isDebugEnabled()) {
                log.debug("The created path is " + remoteFilePath.toString());
            }
            try {
                if (!entry.isDirectory()) {
                    // if the entry is a file, extracts it
                    extractFile(zipIn, remoteFilePath);
                } else {
                    // if the entry is a directory, make the directory
                    remoteFilePath.createFolder();
                }
            } finally {
                try {
                    zipIn.closeEntry();
                    entry = zipIn.getNextEntry();
                } catch (IOException e) {
                    log.error("Error while closing the ZipInputStream", e);
                }
            }
        }
    } catch (IOException e) {
        throw new SynapseException("Error while reading the next ZIP file entry", e);
    } finally {
        // close the zip file
        try {
            zipIn.close();
        } catch (IOException e) {
            log.error("Error while closing the ZipInputStream", e);
        }
        // close the StandardFileSystemManager
        manager.close();
        try {
            remoteFile.close();
            remoteDesFile.close();
        } catch (FileSystemException e) {
            log.error("Error while closing the FileObject", e);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("File extracted to" + destination);
    }
    return true;
}

From source file:org.wso2.carbon.connector.util.FileUnzipUtil.java

/**
 * @param source        Location of the zip file
 * @param destDirectory Location of the destination folder
 *//* w ww .j  a v  a 2  s  .c om*/
public boolean unzip(String source, String destDirectory, MessageContext messageContext) {
    OMFactory factory = OMAbstractFactory.getOMFactory();
    OMNamespace ns = factory.createOMNamespace(FileConstants.FILECON, FileConstants.NAMESPACE);
    OMElement result = factory.createOMElement(FileConstants.RESULT, ns);
    boolean resultStatus = false;
    FileSystemOptions opts = FileConnectorUtils.init(messageContext);
    try {
        manager = FileConnectorUtils.getManager();
        // Create remote object
        FileObject remoteFile = manager.resolveFile(source, opts);
        FileObject remoteDesFile = manager.resolveFile(destDirectory, opts);
        // File destDir = new File(destDirectory);
        if (remoteFile.exists()) {
            if (!remoteDesFile.exists()) {
                //create a folder
                remoteDesFile.createFolder();
            }
            //open the zip file
            ZipInputStream zipIn = new ZipInputStream(remoteFile.getContent().getInputStream());
            ZipEntry entry = zipIn.getNextEntry();
            try {
                // iterates over entries in the zip file
                while (entry != null) {
                    // boolean testResult;
                    String filePath = destDirectory + File.separator + entry.getName();
                    // Create remote object
                    FileObject remoteFilePath = manager.resolveFile(filePath, opts);
                    if (log.isDebugEnabled()) {
                        log.debug("The created path is " + remoteFilePath.toString());
                    }
                    try {
                        if (!entry.isDirectory()) {
                            // if the entry is a file, extracts it
                            extractFile(zipIn, filePath, opts);
                            OMElement messageElement = factory.createOMElement(FileConstants.FILE, ns);
                            messageElement.setText(entry.getName() + " | status:" + "true");
                            result.addChild(messageElement);
                        } else {
                            // if the entry is a directory, make the directory
                            remoteFilePath.createFolder();
                        }
                    } catch (IOException e) {
                        log.error("Unable to process the zip file. ", e);
                    } finally {
                        zipIn.closeEntry();
                        entry = zipIn.getNextEntry();
                    }
                }
                messageContext.getEnvelope().getBody().addChild(result);
                resultStatus = true;
            } finally {
                //we must always close the zip file
                zipIn.close();
            }
        } else {
            log.error("File does not exist.");
        }
    } catch (IOException e) {
        log.error("Unable to process the zip file." + e.getMessage(), e);
    } finally {
        manager.close();
    }
    return resultStatus;
}

From source file:org.wso2.carbon.inbound.endpoint.protocol.file.FilePollingConsumer.java

/**
 * // w w  w  .  j  ava2 s. c  o m
 * Handle directory with chile elements
 * 
 * @param children
 * @return
 * @throws FileSystemException
 */
private FileObject directoryHandler(FileObject[] children) throws FileSystemException {
    // Process Directory
    lastCycle = 0;
    int failCount = 0;
    int successCount = 0;
    int processCount = 0;

    if (log.isDebugEnabled()) {
        log.debug("File name pattern : "
                + vfsProperties.getProperty(VFSConstants.TRANSPORT_FILE_FILE_NAME_PATTERN));
    }

    // Sort the files
    String strSortParam = vfsProperties.getProperty(VFSConstants.FILE_SORT_PARAM);
    if (strSortParam != null && !"NONE".equals(strSortParam)) {
        log.debug("Start Sorting the files.");
        String strSortOrder = vfsProperties.getProperty(VFSConstants.FILE_SORT_ORDER);
        boolean bSortOrderAsscending = true;
        if (strSortOrder != null && strSortOrder.toLowerCase().equals("false")) {
            bSortOrderAsscending = false;
        }
        if (log.isDebugEnabled()) {
            log.debug("Sorting the files by : " + strSortOrder + ". (" + bSortOrderAsscending + ")");
        }
        if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_NAME) && bSortOrderAsscending) {
            Arrays.sort(children, new FileNameAscComparator());
        } else if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_NAME) && !bSortOrderAsscending) {
            Arrays.sort(children, new FileNameDesComparator());
        } else if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_SIZE) && bSortOrderAsscending) {
            Arrays.sort(children, new FileSizeAscComparator());
        } else if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_SIZE) && !bSortOrderAsscending) {
            Arrays.sort(children, new FileSizeDesComparator());
        } else if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_LASTMODIFIEDTIMESTAMP)
                && bSortOrderAsscending) {
            Arrays.sort(children, new FileLastmodifiedtimestampAscComparator());
        } else if (strSortParam.equals(VFSConstants.FILE_SORT_VALUE_LASTMODIFIEDTIMESTAMP)
                && !bSortOrderAsscending) {
            Arrays.sort(children, new FileLastmodifiedtimestampDesComparator());
        }
        log.debug("End Sorting the files.");
    }

    for (FileObject child : children) {
        // skipping *.lock / *.fail file
        if (child.getName().getBaseName().endsWith(".lock")
                || child.getName().getBaseName().endsWith(".fail")) {
            continue;
        }
        boolean isFailedRecord = VFSUtils.isFailRecord(fsManager, child);

        // child's file name matches the file name pattern or process all
        // files now we try to get the lock and process
        if ((strFilePattern == null || child.getName().getBaseName().matches(strFilePattern))
                && !isFailedRecord) {

            if (log.isDebugEnabled()) {
                log.debug("Matching file : " + child.getName().getBaseName());
            }

            if ((!fileLock || (fileLock && acquireLock(fsManager, child)))) {
                // process the file
                boolean runPostProcess = true;
                try {
                    if (log.isDebugEnabled()) {
                        log.debug("Processing file :" + VFSUtils.maskURLPassword(child.toString()));
                    }
                    processCount++;
                    if (processFile(child) == null) {
                        runPostProcess = false;
                    } else {
                        successCount++;
                    }
                    // tell moveOrDeleteAfterProcessing() file was success
                    lastCycle = 1;
                } catch (Exception e) {
                    if (e.getCause() instanceof FileNotFoundException) {
                        log.warn("Error processing File URI : "
                                + VFSUtils.maskURLPassword(child.getName().toString())
                                + ". This can be due to file moved from another process.");
                        runPostProcess = false;
                    } else {
                        log.error("Error processing File URI : "
                                + VFSUtils.maskURLPassword(child.getName().toString()), e);
                        failCount++;
                        // tell moveOrDeleteAfterProcessing() file failed
                        lastCycle = 2;
                    }

                }
                // skipping un-locking file if failed to do delete/move
                // after process
                boolean skipUnlock = false;
                if (runPostProcess) {
                    try {
                        moveOrDeleteAfterProcessing(child);
                    } catch (SynapseException synapseException) {
                        log.error(
                                "File object '" + VFSUtils.maskURLPassword(child.getURL().toString())
                                        + "'cloud not be moved, will remain in \"locked\" state",
                                synapseException);
                        skipUnlock = true;
                        failCount++;
                        lastCycle = 3;
                        VFSUtils.markFailRecord(fsManager, child);
                    }
                }
                // if there is a failure or not we'll try to release the
                // lock
                if (fileLock && !skipUnlock) {
                    // TODO: passing null to avoid build break. Fix properly
                    VFSUtils.releaseLock(fsManager, child, fso);
                }
                if (injectHandler == null) {
                    return child;
                }
            }
        } else if (log.isDebugEnabled() && strFilePattern != null
                && !child.getName().getBaseName().matches(strFilePattern) && !isFailedRecord) {
            // child's file name does not match the file name pattern
            log.debug("Non-Matching file : " + child.getName().getBaseName());
        } else if (isFailedRecord) {
            // it is a failed record
            try {
                lastCycle = 1;
                moveOrDeleteAfterProcessing(child);
            } catch (SynapseException synapseException) {
                log.error("File object '" + VFSUtils.maskURLPassword(child.getURL().toString())
                        + "'cloud not be moved, will remain in \"fail\" state", synapseException);
            }
            if (fileLock) {
                // TODO: passing null to avoid build break. Fix properly
                VFSUtils.releaseLock(fsManager, child, fso);
                VFSUtils.releaseLock(fsManager, fileObject, fso);
            }
            if (log.isDebugEnabled()) {
                log.debug("File '" + VFSUtils.maskURLPassword(fileObject.getURL().toString())
                        + "' has been marked as a failed record, it will not " + "process");
            }
        }

        //close the file system after processing
        try {
            child.close();
        } catch (Exception e) {
        }

        // Manage throttling of file processing
        if (iFileProcessingInterval != null && iFileProcessingInterval > 0) {
            try {
                if (log.isDebugEnabled()) {
                    log.debug("Put the VFS processor to sleep for : " + iFileProcessingInterval);
                }
                Thread.sleep(iFileProcessingInterval);
            } catch (InterruptedException ie) {
                log.error("Unable to set the interval between file processors." + ie);
            }
        } else if (iFileProcessingCount != null && iFileProcessingCount <= processCount) {
            break;
        }
    }
    if (failCount == 0 && successCount > 0) {
        lastCycle = 1;
    } else if (successCount == 0 && failCount > 0) {
        lastCycle = 4;
    } else {
        lastCycle = 5;
    }
    return null;
}

From source file:org.wso2.carbon.inbound.endpoint.protocol.file.FilePollingConsumer.java

/**
 * Actual processing of the file/folder/*w w  w.  j  ava2 s .  c  o  m*/
 * 
 * @param file
 * @return
 * @throws synapseException
 */
private FileObject processFile(FileObject file) throws SynapseException {
    try {
        FileContent content = file.getContent();
        String fileName = file.getName().getBaseName();
        String filePath = file.getName().getPath();
        String fileURI = file.getName().getURI();

        if (injectHandler != null) {
            Map<String, Object> transportHeaders = new HashMap<String, Object>();
            transportHeaders.put(VFSConstants.FILE_PATH, filePath);
            transportHeaders.put(VFSConstants.FILE_NAME, fileName);
            transportHeaders.put(VFSConstants.FILE_URI, fileURI);

            try {
                transportHeaders.put(VFSConstants.FILE_LENGTH, content.getSize());
                transportHeaders.put(VFSConstants.LAST_MODIFIED, content.getLastModifiedTime());
            } catch (FileSystemException e) {
                log.warn("Unable to set file length or last modified date header.", e);
            }

            injectHandler.setTransportHeaders(transportHeaders);
            // injectHandler
            if (!injectHandler.invoke(file, name)) {
                return null;
            }
        }

    } catch (FileSystemException e) {
        log.error("Error reading file content or attributes : " + VFSUtils.maskURLPassword(file.toString()), e);
    }
    return file;
}