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;
}
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");
}
}
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());
}
}
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);
}
}
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();
}
Aggregations