use of org.apache.commons.vfs2.FileSystemManager in project scheduling by ow2-proactive.
the class TestGlobalSpace method testGlobalSpace.
@Test
public void testGlobalSpace() throws Throwable {
File in = tmpFolder.newFolder("input_space");
String inPath = in.getAbsolutePath();
File out = tmpFolder.newFolder("output_space");
String outPath = out.getAbsolutePath();
writeFiles(inFiles, inPath);
TaskFlowJob job = new TaskFlowJob();
job.setName(this.getClass().getSimpleName());
job.setInputSpace(in.toURI().toURL().toString());
job.setOutputSpace(out.toURI().toURL().toString());
JavaTask A = new JavaTask();
A.setExecutableClassName("org.ow2.proactive.scheduler.examples.EmptyTask");
A.setName("A");
for (String[] file : inFiles) {
A.addInputFiles(file[0], InputAccessMode.TransferFromInputSpace);
A.addOutputFiles(file[0] + ".glob.A", OutputAccessMode.TransferToGlobalSpace);
}
A.setPreScript(new SimpleScript(scriptA, "groovy"));
A.setForkEnvironment(new ForkEnvironment());
job.addTask(A);
JavaTask B = new JavaTask();
B.setExecutableClassName("org.ow2.proactive.scheduler.examples.EmptyTask");
B.setName("B");
B.addDependence(A);
for (String[] file : inFiles) {
B.addInputFiles(file[0] + ".glob.A", InputAccessMode.TransferFromGlobalSpace);
B.addOutputFiles(file[0] + ".out", OutputAccessMode.TransferToOutputSpace);
}
B.setPreScript(new SimpleScript(scriptB, "groovy"));
B.setForkEnvironment(new ForkEnvironment());
job.addTask(B);
Scheduler scheduler = schedulerHelper.getSchedulerInterface();
JobId id = scheduler.submit(job);
schedulerHelper.waitForEventJobFinished(id);
assertFalse(schedulerHelper.getJobResult(id).hadException());
/**
* check: inFiles > IN > LOCAL A > GLOBAL > LOCAL B > OUT
*/
for (String[] inFile : inFiles) {
File f = new File(outPath + File.separator + inFile[0] + ".out");
assertTrue("File does not exist: " + f.getAbsolutePath(), f.exists());
Assert.assertEquals("Original and copied files differ", inFile[1], FileUtils.readFileToString(f));
f.delete();
File inf = new File(inPath + File.separator + inFile[0]);
inf.delete();
}
/**
* check that the file produced is accessible in the global user space via the scheduler API
*/
String globalURI = scheduler.getGlobalSpaceURIs().get(0);
assertTrue(globalURI.startsWith("file:"));
String globalPath = new File(new URI(globalURI)).getAbsolutePath();
FileSystemManager fsManager = VFSFactory.createDefaultFileSystemManager();
for (String[] file : inFiles) {
FileObject outFile = fsManager.resolveFile(globalURI + "/" + file[0] + ".glob.A");
log("Checking existence of " + outFile.getURL());
assertTrue(outFile.getURL() + " exists", outFile.exists());
File outFile2 = new File(globalPath, file[0] + ".glob.A");
log("Checking existence of " + outFile2);
assertTrue(outFile2 + " exists", outFile2.exists());
}
}
use of org.apache.commons.vfs2.FileSystemManager 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();
}
use of org.apache.commons.vfs2.FileSystemManager in project pentaho-kettle by pentaho.
the class KettleVFS method getFileObject.
// IMPORTANT:
// We have one problem with VFS: if the file is in a subdirectory of the current one: somedir/somefile
// In that case, VFS doesn't parse the file correctly.
// We need to put file: in front of it to make it work.
// However, how are we going to verify this?
//
// We are going to see if the filename starts with one of the known protocols like file: zip: ram: smb: jar: etc.
// If not, we are going to assume it's a file, when no scheme found ( flag as null ), and it only changes if
// a scheme is provided.
//
public static FileObject getFileObject(String vfsFilename, VariableSpace space, FileSystemOptions fsOptions) throws KettleFileException {
// Protect the code below from invalid input.
if (vfsFilename == null) {
throw new IllegalArgumentException("Unexpected null VFS filename.");
}
try {
FileSystemManager fsManager = getInstance().getFileSystemManager();
String[] schemes = fsManager.getSchemes();
String scheme = getScheme(schemes, vfsFilename);
// Waiting condition - PPP-4374:
// We have to check for hasScheme even if scheme is null because that scheme could not
// be available by getScheme at the time we validate our scheme flag ( Kitchen loading problem )
// So we check if - even it has not a scheme - our vfsFilename has a possible scheme format (PROVIDER_PATTERN_SCHEME)
// If it does, then give it some time and tries to load. It stops when timeout is up or a scheme is found.
int timeOut = TIMEOUT_LIMIT;
if (hasSchemePattern(vfsFilename, PROVIDER_PATTERN_SCHEME)) {
while (scheme == null && timeOut > 0) {
// ask again to refresh schemes list
schemes = fsManager.getSchemes();
try {
Thread.sleep(TIME_TO_SLEEP_STEP);
timeOut -= TIME_TO_SLEEP_STEP;
scheme = getScheme(schemes, vfsFilename);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
}
fsOptions = getFileSystemOptions(scheme, vfsFilename, space, fsOptions);
String filename = normalizePath(vfsFilename, scheme);
return fsOptions != null ? fsManager.resolveFile(filename, fsOptions) : fsManager.resolveFile(filename);
} catch (IOException e) {
throw new KettleFileException("Unable to get VFS File object for filename '" + cleanseFilename(vfsFilename) + "' : " + e.getMessage(), e);
}
}
use of org.apache.commons.vfs2.FileSystemManager 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);
}
}
use of org.apache.commons.vfs2.FileSystemManager in project spoofax by metaborg.
the class DefaultFileSystemManagerProvider method get.
@Override
public FileSystemManager get() {
try {
final DefaultFileSystemManager manager = new DefaultFileSystemManager();
manager.setFilesCache(new DefaultFilesCache());
manager.setCacheStrategy(CacheStrategy.ON_RESOLVE);
final String baseTmpDir = System.getProperty("java.io.tmpdir");
final File tempDir = new File(baseTmpDir, "vfs_cache" + new Random().nextLong()).getAbsoluteFile();
final DefaultFileReplicator replicator = new DefaultFileReplicator(tempDir);
manager.setTemporaryFileStore(replicator);
manager.setReplicator(replicator);
addDefaultProvider(manager);
addProviders(manager);
setBaseFile(manager);
manager.init();
return manager;
} catch (FileSystemException e) {
throw new RuntimeException("Cannot initialize resource service: " + e.getMessage(), e);
}
}
Aggregations