Search in sources :

Example 56 with VFS

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

the class VFSUtils method resolveUriHost.

/**
 * Function to resolve the hostname of uri to ip for following vfs protocols. if not the protocol listed, return
 * same uri provided for {uri}
 * Protocols resolved : SMB
 * @param uri URI need to resolve
 * @param strBuilder string builder to use to build the resulting uri
 * @return hostname resolved uri
 * @throws FileSystemException Unable to decode due to malformed URI
 * @throws UnknownHostException Error occurred while resolving hostname of URI
 */
public static String resolveUriHost(String uri, StringBuilder strBuilder) throws FileSystemException, UnknownHostException {
    if (uri != null && strBuilder != null) {
        // Extract the scheme
        String scheme = UriParser.extractScheme(uri, strBuilder);
        // need to resolve hosts of smb URIs due to limitation in jcifs library
        if (scheme != null && (scheme.equals("smb"))) {
            // Expecting "//"
            if (strBuilder.length() < 2 || strBuilder.charAt(0) != '/' || strBuilder.charAt(1) != '/') {
                throw new FileSystemException("vfs.provider/missing-double-slashes.error", uri);
            }
            strBuilder.delete(0, 2);
            // Extract userinfo
            String userInfo = extractUserInfo(strBuilder);
            // Extract hostname
            String hostName = extractHostName(strBuilder);
            // resolve host name
            InetAddress hostAddress = InetAddress.getByName(hostName);
            String resolvedHostAddress = hostAddress.getHostAddress();
            // build resolved uri
            StringBuilder uriStrBuilder = new StringBuilder();
            uriStrBuilder.append(scheme).append("://");
            if (userInfo != null) {
                // user information can be null since it's optional
                uriStrBuilder.append(userInfo).append("@");
            }
            uriStrBuilder.append(resolvedHostAddress).append(strBuilder);
            return uriStrBuilder.toString();
        }
    }
    return uri;
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) InetAddress(java.net.InetAddress)

Example 57 with VFS

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

the class VFSUtils method releaseLock.

/**
 * 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.
 */
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");
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) FileObject(org.apache.commons.vfs2.FileObject)

Example 58 with VFS

use of org.apache.commons.vfs2.VFS in project javautils by jiadongpo.

the class SFTPClientVFS method getFileObject.

public static FileObject getFileObject(String vfsFilename) throws Exception {
    try {
        FileSystemManager fsManager = getInstance().getFileSystemManager();
        boolean relativeFilename = true;
        String[] schemes = fsManager.getSchemes();
        for (int i = 0; i < schemes.length && relativeFilename; i++) {
            if (vfsFilename.startsWith(schemes[i] + ":")) {
                relativeFilename = false;
            // We have a VFS URL, load any options for the file system driver
            // fsOptions = buildFsOptions( space, fsOptions, vfsFilename, schemes[i] );
            }
        }
        String filename;
        if (vfsFilename.startsWith("\\\\")) {
            File file = new File(vfsFilename);
            filename = file.toURI().toString();
        } else {
            if (relativeFilename) {
                File file = new File(vfsFilename);
                filename = file.getAbsolutePath();
            } else {
                filename = vfsFilename;
            }
        }
        FileObject fileObject = null;
        return fsManager.resolveFile(filename);
    } catch (IOException e) {
        throw new Exception("Unable to get VFS File object for filename '" + cleanseFilename(vfsFilename) + "' : " + e.getMessage());
    }
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) StandardFileSystemManager(org.apache.commons.vfs2.impl.StandardFileSystemManager) FileSystemManager(org.apache.commons.vfs2.FileSystemManager) DefaultFileSystemManager(org.apache.commons.vfs2.impl.DefaultFileSystemManager) File(java.io.File) FileSystemException(org.apache.commons.vfs2.FileSystemException) IOException(java.io.IOException)

Example 59 with VFS

use of org.apache.commons.vfs2.VFS in project accumulo by apache.

the class AccumuloVFSClassLoader method printClassPath.

public static void printClassPath(Printer out, boolean debug) {
    try {
        ClassLoader cl = getClassLoader();
        ArrayList<ClassLoader> classloaders = new ArrayList<>();
        while (cl != null) {
            classloaders.add(cl);
            cl = cl.getParent();
        }
        Collections.reverse(classloaders);
        int level = 0;
        for (ClassLoader classLoader : classloaders) {
            level++;
            if (debug && level > 1) {
                out.print("\n");
            }
            if (!debug && level < 2) {
                continue;
            }
            String classLoaderDescription;
            switch(level) {
                case 1:
                    classLoaderDescription = level + ": Java System Classloader (loads Java system resources)";
                    break;
                case 2:
                    classLoaderDescription = level + ": Java Classloader (loads everything defined by java classpath)";
                    break;
                case 3:
                    classLoaderDescription = level + ": Accumulo Classloader (loads everything defined by general.classpaths)";
                    break;
                case 4:
                    classLoaderDescription = level + ": Accumulo Dynamic Classloader (loads everything defined by general.dynamic.classpaths)";
                    break;
                default:
                    classLoaderDescription = level + ": Mystery Classloader (someone probably added a classloader and didn't update the switch statement in " + AccumuloVFSClassLoader.class.getName() + ")";
                    break;
            }
            boolean sawFirst = false;
            if (classLoader instanceof URLClassLoader) {
                if (debug)
                    out.print("Level " + classLoaderDescription + " URL classpath items are:\n");
                for (URL u : ((URLClassLoader) classLoader).getURLs()) {
                    printJar(out, u.getFile(), debug, sawFirst);
                    sawFirst = true;
                }
            } else if (classLoader instanceof VFSClassLoader) {
                if (debug)
                    out.print("Level " + classLoaderDescription + " VFS classpaths items are:\n");
                VFSClassLoader vcl = (VFSClassLoader) classLoader;
                for (FileObject f : vcl.getFileObjects()) {
                    printJar(out, f.getURL().getFile(), debug, sawFirst);
                    sawFirst = true;
                }
            } else {
                if (debug)
                    out.print("Unknown classloader configuration " + classLoader.getClass() + "\n");
            }
        }
        out.print("\n");
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}
Also used : ArrayList(java.util.ArrayList) VFSClassLoader(org.apache.commons.vfs2.impl.VFSClassLoader) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) VFSClassLoader(org.apache.commons.vfs2.impl.VFSClassLoader) AccumuloClassLoader(org.apache.accumulo.start.classloader.AccumuloClassLoader) URLClassLoader(java.net.URLClassLoader) FileObject(org.apache.commons.vfs2.FileObject) HdfsFileObject(org.apache.commons.vfs2.provider.hdfs.HdfsFileObject)

Example 60 with VFS

use of org.apache.commons.vfs2.VFS in project accumulo by apache.

the class AccumuloReloadingVFSClassLoaderTest method testReloading.

@Test
public void testReloading() throws Exception {
    FileObject testDir = vfs.resolveFile(folder1.getRoot().toURI().toString());
    FileObject[] dirContents = testDir.getChildren();
    AccumuloReloadingVFSClassLoader arvcl = new AccumuloReloadingVFSClassLoader(folderPath, vfs, new ReloadingClassLoader() {

        @Override
        public ClassLoader getClassLoader() {
            return ClassLoader.getSystemClassLoader();
        }
    }, 1000, true);
    FileObject[] files = ((VFSClassLoader) arvcl.getClassLoader()).getFileObjects();
    Assert.assertArrayEquals(createFileSystems(dirContents), files);
    Class<?> clazz1 = arvcl.getClassLoader().loadClass("test.HelloWorld");
    Object o1 = clazz1.newInstance();
    Assert.assertEquals("Hello World!", o1.toString());
    // Check that the class is the same before the update
    Class<?> clazz1_5 = arvcl.getClassLoader().loadClass("test.HelloWorld");
    Assert.assertEquals(clazz1, clazz1_5);
    assertTrue(new File(folder1.getRoot(), "HelloWorld.jar").delete());
    // VFS-487 significantly wait to avoid failure
    Thread.sleep(7000);
    // Update the class
    FileUtils.copyURLToFile(this.getClass().getResource("/HelloWorld.jar"), folder1.newFile("HelloWorld2.jar"));
    // Wait for the monitor to notice
    // VFS-487 significantly wait to avoid failure
    Thread.sleep(7000);
    Class<?> clazz2 = arvcl.getClassLoader().loadClass("test.HelloWorld");
    Object o2 = clazz2.newInstance();
    Assert.assertEquals("Hello World!", o2.toString());
    // This is false because they are loaded by a different classloader
    Assert.assertFalse(clazz1.equals(clazz2));
    Assert.assertFalse(o1.equals(o2));
    arvcl.close();
}
Also used : VFSClassLoader(org.apache.commons.vfs2.impl.VFSClassLoader) VFSClassLoader(org.apache.commons.vfs2.impl.VFSClassLoader) FileObject(org.apache.commons.vfs2.FileObject) FileObject(org.apache.commons.vfs2.FileObject) File(java.io.File) Test(org.junit.Test)

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