Search in sources :

Example 16 with VFS

use of org.apache.commons.vfs2.VFS in project pentaho-kettle by pentaho.

the class GoogleDrivePluginLifecycleListener method onEnvironmentInit.

public void onEnvironmentInit() throws LifecycleException {
    try {
        boolean proceed = true;
        if (!new File(GoogleDriveFileObject.resolveCredentialsPath() + "/" + resourceBundle.getString("client.secrets")).exists()) {
            proceed = false;
            log.warn("The Google Authorization secrets security token file (" + resourceBundle.getString("client.secrets") + ") is not present in the credentials folder. This file is necessary to activate the Google Drive VFS plugin.");
        }
        if (!new File(GoogleDriveFileObject.resolveCredentialsPath() + "/" + resourceBundle.getString("stored.credential")).exists()) {
            DefaultCapabilityManager capabilityManager = DefaultCapabilityManager.getInstance();
            if (capabilityManager.capabilityExist("pentaho-server")) {
                proceed = false;
                log.warn("The Google Authorization Code Flow security token file (" + resourceBundle.getString("stored.credential") + ") is not present in the credentials folder.  This file is necessary to activate the Google Drive VFS plugin.");
            }
        }
        /**
         * Registers the GoogleDrive VFS File Provider dynamically since it is bundled with our plugin and will not automatically
         * be registered through the normal class path search the default FileSystemManager performs.
         */
        if (proceed) {
            FileSystemManager fsm = KettleVFS.getInstance().getFileSystemManager();
            if (fsm instanceof DefaultFileSystemManager) {
                if (!Arrays.asList(fsm.getSchemes()).contains(GoogleDriveFileProvider.SCHEME)) {
                    ((DefaultFileSystemManager) fsm).addProvider(GoogleDriveFileProvider.SCHEME, new GoogleDriveFileProvider());
                }
            }
        }
    } catch (FileSystemException e) {
        throw new LifecycleException(e.getMessage(), false);
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) LifecycleException(org.pentaho.di.core.lifecycle.LifecycleException) DefaultCapabilityManager(org.pentaho.capabilities.impl.DefaultCapabilityManager) DefaultFileSystemManager(org.apache.commons.vfs2.impl.DefaultFileSystemManager) GoogleDriveFileProvider(org.pentaho.googledrive.vfs.GoogleDriveFileProvider) File(java.io.File) DefaultFileSystemManager(org.apache.commons.vfs2.impl.DefaultFileSystemManager) FileSystemManager(org.apache.commons.vfs2.FileSystemManager)

Example 17 with VFS

use of org.apache.commons.vfs2.VFS in project pentaho-kettle by pentaho.

the class LogWriter method createFileAppender.

/**
 * Create a file appender
 * @param filename The (VFS) filename (URL) to write to.
 * @param exact is this an exact filename of a filename to be stored in "java.io.tmp"
 * @param append
 * @return A new file appender
 * @throws KettleFileException In case there is a problem opening the file.
 */
public static final Log4jFileAppender createFileAppender(String filename, boolean exact, boolean append) throws KettleFileException {
    try {
        FileObject file;
        if (!exact) {
            file = KettleVFS.createTempFile(filename, ".log", System.getProperty("java.io.tmpdir"));
        } else {
            file = KettleVFS.getFileObject(filename);
        }
        Log4jFileAppender appender = new Log4jFileAppender(file, append);
        appender.setLayout(new Log4jKettleLayout(true));
        appender.setName(LogWriter.createFileAppenderName(filename, exact));
        return appender;
    } catch (IOException e) {
        throw new KettleFileException("Unable to add Kettle file appender to Log4J", e);
    }
}
Also used : KettleFileException(org.pentaho.di.core.exception.KettleFileException) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException)

Example 18 with VFS

use of org.apache.commons.vfs2.VFS in project pentaho-kettle by pentaho.

the class NameResourceIT method testNamingResourceLegacyAndNew.

/**
 * This tests ResourceNamingInterface.nameResouce(), comparing the directory maps generated by the legacy and new
 * method.
 *
 * @param fileName
 * @param pathOnly
 *          Resolve the path - leave out the file name
 * @throws Exception
 *
 *           Legacy: namingResource(String, String, String, FileNamingType) New: namingResource(FileObject, TransMeta)
 */
private void testNamingResourceLegacyAndNew(String fileName, String extension, String fileMask) throws Exception {
    // Create a new transformation.
    TransMeta transMeta = new TransMeta();
    FileObject fileObject = KettleVFS.getFileObject(fileName, transMeta);
    // This code is modeled after the legacy code in legacy step meta classes
    // that have an exportResources method that deal with file masks
    // e.g., ExcelInputMeta
    // 
    // There is a big exception: where the legacy code does a "getName()"
    // this code does a "getURL()". This is because of the JIRA case
    // that resulted in the refactoring of ResourceNamingInterface.
    // 
    // The UNC and VFS protocols where being dropped.
    // The code you see here would be the fix for that without adding
    // the new nameResource() to ResourceNamingInterface.
    // 
    String path = null;
    String prefix = null;
    if (Utils.isEmpty(fileMask)) {
        prefix = fileObject.getName().getBaseName();
        path = fileObject.getParent().getURL().toString();
    } else {
        prefix = "";
        path = fileObject.getURL().toString();
    }
    // Create a resource naming interface to use the legacy method call
    ResourceNamingInterface resourceNamingInterface_LEGACY = new SequenceResourceNaming();
    // Create two resource naming interfaces, one for legacy call, the other for new method call
    ResourceNamingInterface resourceNamingInterface_NEW = new SequenceResourceNaming();
    // The old and new interfaces to get the file name.
    String resolvedFileName_LEGACY = resourceNamingInterface_LEGACY.nameResource(prefix, path, extension, FileNamingType.DATA_FILE);
    String resolvedFileName_NEW = resourceNamingInterface_NEW.nameResource(fileObject, transMeta, Utils.isEmpty(fileMask));
    // get the variable name from both naming interfaces directory maps
    String pathFromMap_LEGACY = resourceNamingInterface_LEGACY.getDirectoryMap().get(path);
    String pathFromMap_NEW = resourceNamingInterface_NEW.getDirectoryMap().get(path);
    // The paths in both directories should be the same
    assertEquals(pathFromMap_LEGACY, pathFromMap_NEW);
    // The file names should be the same
    assertEquals(resolvedFileName_LEGACY, resolvedFileName_NEW);
}
Also used : TransMeta(org.pentaho.di.trans.TransMeta) FileObject(org.apache.commons.vfs2.FileObject)

Example 19 with VFS

use of org.apache.commons.vfs2.VFS in project mondrian by pentaho.

the class Util method readVirtualFile.

/**
 * Gets content via Apache VFS. File must exist and have content
 *
 * @param url String
 * @return Apache VFS FileContent for further processing
 * @throws FileSystemException on error
 */
public static InputStream readVirtualFile(String url) throws FileSystemException {
    // Treat catalogUrl as an Apache VFS (Virtual File System) URL.
    // VFS handles all of the usual protocols (http:, file:)
    // and then some.
    FileSystemManager fsManager = VFS.getManager();
    if (fsManager == null) {
        throw newError("Cannot get virtual file system manager");
    }
    // Workaround VFS bug.
    if (url.startsWith("file://localhost")) {
        url = url.substring("file://localhost".length());
    }
    if (url.startsWith("file:")) {
        url = url.substring("file:".length());
    }
    // (Mondrian-585)
    if (url.startsWith("http")) {
        try {
            return new URL(url).openStream();
        } catch (IOException e) {
            throw newError("Could not read URL: " + url);
        }
    }
    File userDir = new File("").getAbsoluteFile();
    FileObject file = fsManager.resolveFile(userDir, url);
    FileContent fileContent = null;
    try {
        // Because of VFS caching, make sure we refresh to get the latest
        // file content. This refresh may possibly solve the following
        // workaround for defect MONDRIAN-508, but cannot be tested, so we
        // will leave the work around for now.
        file.refresh();
        // http://blah.com?param=B)
        if (file instanceof HttpFileObject && !file.getName().getURI().equals(url)) {
            fsManager.getFilesCache().removeFile(file.getFileSystem(), file.getName());
            file = fsManager.resolveFile(userDir, url);
        }
        if (!file.isReadable()) {
            throw newError("Virtual file is not readable: " + url);
        }
        fileContent = file.getContent();
    } finally {
        file.close();
    }
    if (fileContent == null) {
        throw newError("Cannot get virtual file content: " + url);
    }
    return fileContent.getInputStream();
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) HttpFileObject(org.apache.commons.vfs2.provider.http.HttpFileObject) FileObject(org.apache.commons.vfs2.FileObject) FileSystemManager(org.apache.commons.vfs2.FileSystemManager) URL(java.net.URL) HttpFileObject(org.apache.commons.vfs2.provider.http.HttpFileObject)

Example 20 with VFS

use of org.apache.commons.vfs2.VFS in project pentaho-metaverse by pentaho.

the class KettleAnalyzerUtil method normalizeFilePath.

/**
 * Utility method for normalizing file paths used in Metaverse Id generation. It will convert a valid path into a
 * consistent path regardless of URI notation or filesystem absolute path.
 *
 * @param filePath full path to normalize
 * @return the normalized path
 */
public static String normalizeFilePath(String filePath) throws MetaverseException {
    try {
        String path = filePath;
        FileObject fo = KettleVFS.getFileObject(filePath);
        try {
            path = fo.getURL().getPath();
        } catch (Throwable t) {
        // Something went wrong with VFS, just try the filePath
        }
        File f = new File(path);
        return f.getAbsolutePath();
    } catch (Exception e) {
        throw new MetaverseException(e);
    }
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) File(java.io.File) KettleException(org.pentaho.di.core.exception.KettleException) MetaverseException(org.pentaho.metaverse.api.MetaverseException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) MetaverseException(org.pentaho.metaverse.api.MetaverseException)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)50 IOException (java.io.IOException)27 KettleException (org.pentaho.di.core.exception.KettleException)23 FileSystemException (org.apache.commons.vfs2.FileSystemException)22 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)20 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)20 Result (org.pentaho.di.core.Result)19 File (java.io.File)18 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)11 DefaultFileSystemManager (org.apache.commons.vfs2.impl.DefaultFileSystemManager)11 ResultFile (org.pentaho.di.core.ResultFile)11 VFSClassLoader (org.apache.commons.vfs2.impl.VFSClassLoader)10 KettleFileException (org.pentaho.di.core.exception.KettleFileException)10 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)7 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)7 StandardFileSystemManager (org.apache.commons.vfs2.impl.StandardFileSystemManager)6 URL (java.net.URL)4 Matcher (java.util.regex.Matcher)4 Pattern (java.util.regex.Pattern)4