Search in sources :

Example 46 with FileSystemManager

use of org.apache.commons.vfs2.FileSystemManager in project wso2-synapse by wso2.

the class VFSUtils method acquireLock.

/**
 * 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
 *
 * @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 static synchronized boolean acquireLock(FileSystemManager fsManager, FileObject fo, VFSParamDTO paramDTO, FileSystemOptions fso, boolean isListener) {
    String strLockValue = getLockValue();
    byte[] lockValue = strLockValue.getBytes();
    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 = getFullPath(fo);
        lockObject = fsManager.resolveFile(fullPath + LOCK_FILE_SUFFIX, 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 {
            if (isListener) {
                // Check the original file existence before the lock file to handle concurrent access scenario
                FileObject originalFileObject = fsManager.resolveFile(fullPath, fso);
                if (!originalFileObject.exists()) {
                    return false;
                }
            }
            if (!createLockFile(lockValue, lockObject, fullPath)) {
                return false;
            }
            // 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_FILE_SUFFIX, 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", 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;
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) FileObject(org.apache.commons.vfs2.FileObject)

Example 47 with FileSystemManager

use of org.apache.commons.vfs2.FileSystemManager in project pentaho-platform by pentaho.

the class ApacheVFSOutputHandler method getFileOutputContentItem.

@Override
public IContentItem getFileOutputContentItem() {
    String contentRef = getContentRef();
    try {
        // $NON-NLS-1$
        String contentName = getHandlerId().substring(4) + ":" + contentRef;
        FileSystemManager fsManager = getFileSystemManager();
        if (fsManager == null) {
            logError(Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0001_CANNOT_GET_VFSMGR"));
            return null;
        }
        FileObject file = fsManager.resolveFile(contentName);
        if (file == null) {
            logError(Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0002_CANNOT_GET_VF", contentName));
            return null;
        }
        if (!file.isWriteable()) {
            logError(Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0003_CANNOT_WRITE", contentName));
            return null;
        }
        FileContent fileContent = file.getContent();
        if (fileContent == null) {
            logError(Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0004_CANNOT_GET_CTX", contentName));
            return null;
        }
        OutputStream outputStream = fileContent.getOutputStream();
        SimpleContentItem content = new SimpleContentItem(outputStream);
        return content;
    } catch (Throwable t) {
        Logger.error(ApacheVFSOutputHandler.class.getName(), Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0005_CANNOT_GET_HANDLER", contentRef), // $NON-NLS-1$
        t);
    }
    return null;
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) SimpleContentItem(org.pentaho.platform.engine.core.output.SimpleContentItem) OutputStream(java.io.OutputStream) FileObject(org.apache.commons.vfs2.FileObject) FileSystemManager(org.apache.commons.vfs2.FileSystemManager)

Example 48 with FileSystemManager

use of org.apache.commons.vfs2.FileSystemManager in project pentaho-platform by pentaho.

the class MondrianCatalogHelper method docAtUrlToString.

protected String docAtUrlToString(final String urlStr, final IPentahoSession pentahoSession) {
    // String relPath = getSolutionRepositoryRelativePath(urlStr, pentahoSession);
    String res = null;
    InputStream in = null;
    try {
        LocalizingDynamicSchemaProcessor schemaProcessor = new LocalizingDynamicSchemaProcessor();
        PropertyList localeInfo = new PropertyList();
        // $NON-NLS-1$
        localeInfo.put("Locale", getLocale().toString());
        FileSystemManager fsManager = VFS.getManager();
        FileObject mondrianDS = fsManager.resolveFile(urlStr);
        in = mondrianDS.getContent().getInputStream();
        res = schemaProcessor.filter(null, localeInfo, in);
    } catch (FileNotFoundException fnfe) {
        throw new MondrianCatalogServiceException(Messages.getInstance().getErrorString("MondrianCatalogHelper.ERROR_0007_FILE_NOT_FOUND"), // $NON-NLS-1$
        fnfe);
    } catch (Exception e) {
        throw new MondrianCatalogServiceException(Messages.getInstance().getErrorString("MondrianCatalogHelper.ERROR_0006_IO_PROBLEM"), // $NON-NLS-1$
        e);
    } finally {
        IOUtils.closeQuietly(in);
    }
    return res;
}
Also used : LocalizingDynamicSchemaProcessor(mondrian.i18n.LocalizingDynamicSchemaProcessor) PropertyList(mondrian.olap.Util.PropertyList) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) FileObject(org.apache.commons.vfs2.FileObject) DefaultFileSystemManager(org.apache.commons.vfs2.impl.DefaultFileSystemManager) FileSystemManager(org.apache.commons.vfs2.FileSystemManager) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) XOMException(org.eigenbase.xom.XOMException) FileSystemException(org.apache.commons.vfs2.FileSystemException) FileNotFoundException(java.io.FileNotFoundException) ObjectFactoryException(org.pentaho.platform.api.engine.ObjectFactoryException) XmlParseException(org.pentaho.platform.api.util.XmlParseException) SAXException(org.xml.sax.SAXException) MondrianException(mondrian.olap.MondrianException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DBDatasourceServiceException(org.pentaho.platform.api.data.DBDatasourceServiceException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

FileSystemManager (org.apache.commons.vfs2.FileSystemManager)30 FileObject (org.apache.commons.vfs2.FileObject)29 FileSystemException (org.apache.commons.vfs2.FileSystemException)21 DefaultFileSystemManager (org.apache.commons.vfs2.impl.DefaultFileSystemManager)17 File (java.io.File)12 IOException (java.io.IOException)11 StandardFileSystemManager (org.apache.commons.vfs2.impl.StandardFileSystemManager)7 Test (org.junit.Test)6 FileContent (org.apache.commons.vfs2.FileContent)4 FileSystemOptions (org.apache.commons.vfs2.FileSystemOptions)4 OutputStream (java.io.OutputStream)3 RepositoryException (javax.jcr.RepositoryException)3 FileName (org.apache.commons.vfs2.FileName)3 SoftRefFilesCache (org.apache.commons.vfs2.cache.SoftRefFilesCache)3 FileOutputStream (java.io.FileOutputStream)2 FileWriter (java.io.FileWriter)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Map (java.util.Map)2