/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.wgadesigner;
import java.io.File;
import java.io.IOException;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSelectInfo;
import org.apache.commons.vfs.FileSelector;
import org.apache.commons.vfs.VFS;
/**
* manages file system resources which need to be copied to the current workspace metadata
* for e.g. tomcat installation, wgadeployments etc.
*
*
*/
public class ResourceManager {
private File _workDir;
private FileObject _work;
private FileObject _source;
public void init(File source, File workDir) throws IOException {
if (source.isDirectory()) {
_source = VFS.getManager().resolveFile(source.toURI().toString());
} else {
_source = VFS.getManager().resolveFile("jar:" + source.toURI().toString());
}
_work = VFS.getManager().resolveFile(workDir.getAbsolutePath());
_workDir = workDir;
}
public File getResourceAsFile(String path) throws IOException {
FileObject unpackedResource = _work.resolveFile(path);
if (!unpackedResource.exists()) {
FileObject sourceFile = _source.resolveFile(path);
sourceFile.copyFrom(unpackedResource, new FileSelector() {
public boolean includeFile(FileSelectInfo arg0) throws Exception {
return true;
}
public boolean traverseDescendents(FileSelectInfo arg0) throws Exception {
return true;
}
});
}
return new File(_workDir, path);
}
public void copy(String sourcePath, File targetDir) throws IOException {
FileObject target = VFS.getManager().resolveFile(targetDir.getAbsolutePath());
FileObject source = _source.resolveFile(sourcePath);
if (source.exists()) {
target.copyFrom(source, new FileSelector() {
public boolean includeFile(FileSelectInfo arg0) throws Exception {
return true;
}
public boolean traverseDescendents(FileSelectInfo arg0) throws Exception {
return true;
}
});
}
}
}