use of org.apache.commons.vfs2.VFS in project pentaho-kettle by pentaho.
the class ResourceUtil method serializeResourceExportInterface.
/**
* Serializes the referenced resource export interface (Job, Transformation, Mapping, Step, Job Entry, etc) to a ZIP
* file.
*
* @param zipFilename
* The ZIP file to put the content in
* @param resourceExportInterface
* the interface to serialize
* @param space
* the space to use for variable replacement
* @param repository
* the repository to load objects from (or null if not used)
* @param injectXML
* The XML to inject into the resulting ZIP archive (optional, can be null)
* @param injectFilename
* The name of the file for the XML to inject in the ZIP archive (optional, can be null)
* @return The full VFS filename reference to the serialized export interface XML file in the ZIP archive.
* @throws KettleException
* in case anything goes wrong during serialization
*/
public static final TopLevelResource serializeResourceExportInterface(String zipFilename, ResourceExportInterface resourceExportInterface, VariableSpace space, Repository repository, IMetaStore metaStore, String injectXML, String injectFilename) throws KettleException {
ZipOutputStream out = null;
try {
Map<String, ResourceDefinition> definitions = new HashMap<String, ResourceDefinition>();
//
if (injectXML != null) {
ResourceDefinition resourceDefinition = new ResourceDefinition(injectFilename, injectXML);
definitions.put(injectFilename, resourceDefinition);
}
ResourceNamingInterface namingInterface = new SequenceResourceNaming();
String topLevelResource = resourceExportInterface.exportResources(space, definitions, namingInterface, repository, metaStore);
if (topLevelResource != null && !definitions.isEmpty()) {
// Create the ZIP file...
//
FileObject fileObject = KettleVFS.getFileObject(zipFilename, space);
// Store the XML in the definitions in a ZIP file...
//
out = new ZipOutputStream(KettleVFS.getOutputStream(fileObject, false));
for (String filename : definitions.keySet()) {
ResourceDefinition resourceDefinition = definitions.get(filename);
ZipEntry zipEntry = new ZipEntry(resourceDefinition.getFilename());
String comment = BaseMessages.getString(PKG, "ResourceUtil.SerializeResourceExportInterface.ZipEntryComment.OriginatingFile", filename, Const.NVL(resourceDefinition.getOrigin(), "-"));
zipEntry.setComment(comment);
out.putNextEntry(zipEntry);
out.write(resourceDefinition.getContent().getBytes());
out.closeEntry();
}
String zipURL = fileObject.getName().toString();
return new TopLevelResource(topLevelResource, zipURL, "zip:" + zipURL + "!" + topLevelResource);
} else {
throw new KettleException(BaseMessages.getString(PKG, "ResourceUtil.Exception.NoResourcesFoundToExport"));
}
} catch (Exception e) {
throw new KettleException(BaseMessages.getString(PKG, "ResourceUtil.Exception.ErrorSerializingExportInterface", resourceExportInterface.toString()), e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
throw new KettleException(BaseMessages.getString(PKG, "ResourceUtil.Exception.ErrorClosingZipStream", zipFilename));
}
}
}
}
use of org.apache.commons.vfs2.VFS in project pentaho-kettle by pentaho.
the class KettleVFS method buildFsOptions.
private static FileSystemOptions buildFsOptions(VariableSpace varSpace, FileSystemOptions sourceOptions, String vfsFilename, String scheme) throws IOException {
if (varSpace == null || vfsFilename == null) {
// We cannot extract settings from a non-existant variable space
return null;
}
IKettleFileSystemConfigBuilder configBuilder = KettleFileSystemConfigBuilderFactory.getConfigBuilder(varSpace, scheme);
FileSystemOptions fsOptions = (sourceOptions == null) ? new FileSystemOptions() : sourceOptions;
String[] varList = varSpace.listVariables();
for (String var : varList) {
if (var.startsWith("vfs.")) {
String param = configBuilder.parseParameterName(var, scheme);
String varScheme = KettleGenericFileSystemConfigBuilder.extractScheme(var);
if (param != null) {
if (varScheme == null || varScheme.equals("sftp") || varScheme.equals(scheme)) {
configBuilder.setParameter(fsOptions, param, varSpace.getVariable(var), var, vfsFilename);
}
} else {
throw new IOException("FileSystemConfigBuilder could not parse parameter: " + var);
}
}
}
return fsOptions;
}
use of org.apache.commons.vfs2.VFS in project pentaho-kettle by pentaho.
the class KettleVFS method getFileObject.
public static FileObject getFileObject(String vfsFilename, VariableSpace space, FileSystemOptions fsOptions) throws KettleFileException {
try {
FileSystemManager fsManager = getInstance().getFileSystemManager();
// 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.
//
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;
}
}
if (fsOptions != null) {
return fsManager.resolveFile(filename, fsOptions);
} else {
return 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.VFS in project pentaho-kettle by pentaho.
the class KettleSftpFileSystemConfigBuilder method setParameter.
/**
* Publicly expose a generic way to set parameters
*/
@Override
public void setParameter(FileSystemOptions opts, String name, String value, String fullParameterName, String vfsUrl) throws IOException {
if (!fullParameterName.startsWith("vfs.sftp")) {
// This is not an SFTP parameter. Delegate to the generic handler
super.setParameter(opts, name, value, fullParameterName, vfsUrl);
} else {
// Check for the presence of a host in the full variable name
try {
// Parse server name from vfsFilename
FileNameParser sftpFilenameParser = SftpFileNameParser.getInstance();
URLFileName file = (URLFileName) sftpFilenameParser.parseUri(null, null, vfsUrl);
if (!parameterContainsHost(fullParameterName) || fullParameterName.endsWith(file.getHostName())) {
// Match special cases for parameter names
if (name.equalsIgnoreCase("AuthKeyPassphrase")) {
setParam(opts, UserInfo.class.getName(), new PentahoUserInfo(value));
} else if (name.equals("identity")) {
IdentityInfo[] identities = (IdentityInfo[]) this.getParam(opts, IDENTITY_KEY);
if (identities == null) {
identities = new IdentityInfo[] { new IdentityInfo(new File(value)) };
} else {
// Copy, in a Java 5 friendly manner, identities into a larger array
IdentityInfo[] temp = new IdentityInfo[identities.length + 1];
System.arraycopy(identities, 0, temp, 0, identities.length);
identities = temp;
identities[identities.length - 1] = new IdentityInfo(new File(value));
}
setParam(opts, IDENTITY_KEY, identities);
} else {
super.setParameter(opts, name, value, fullParameterName, vfsUrl);
}
} else {
// No host match found
log.logDebug("No host match found for: " + fullParameterName);
}
} catch (IOException e) {
log.logError("Failed to set VFS parameter: [" + fullParameterName + "] " + value, e);
}
}
}
use of org.apache.commons.vfs2.VFS in project pentaho-kettle by pentaho.
the class GroupBy method addToBuffer.
// Method is defined as package-protected in order to be accessible by unit tests
void addToBuffer(Object[] row) throws KettleFileException {
data.bufferList.add(row);
if (data.bufferList.size() > 5000 && data.rowsOnFile == 0) {
String pathToTmp = environmentSubstitute(getMeta().getDirectory());
try {
File ioFile = new File(pathToTmp);
if (!ioFile.exists()) {
// try to resolve as Apache VFS file
pathToTmp = retrieveVfsPath(pathToTmp);
}
data.tempFile = File.createTempFile(getMeta().getPrefix(), ".tmp", new File(pathToTmp));
data.fosToTempFile = new FileOutputStream(data.tempFile);
data.dosToTempFile = new DataOutputStream(data.fosToTempFile);
data.firstRead = true;
} catch (IOException e) {
throw new KettleFileException(BaseMessages.getString(PKG, "GroupBy.Exception.UnableToCreateTemporaryFile"), e);
}
// OK, save the oldest rows to disk!
Object[] oldest = data.bufferList.get(0);
data.inputRowMeta.writeData(data.dosToTempFile, oldest);
data.bufferList.remove(0);
data.rowsOnFile++;
}
}
Aggregations