use of org.pentaho.di.core.variables.VariableSpace in project pentaho-kettle by pentaho.
the class FileDoesNotExistValidator method validate.
public boolean validate(CheckResultSourceInterface source, String propertyName, List<CheckResultInterface> remarks, ValidatorContext context) {
String filename = ValidatorUtils.getValueAsString(source, propertyName);
VariableSpace variableSpace = getVariableSpace(source, propertyName, remarks, context);
boolean failIfExists = getFailIfExists(source, propertyName, remarks, context);
if (null == variableSpace) {
return false;
}
String realFileName = variableSpace.environmentSubstitute(filename);
FileObject fileObject = null;
try {
fileObject = KettleVFS.getFileObject(realFileName, variableSpace);
if (fileObject.exists() && failIfExists) {
JobEntryValidatorUtils.addFailureRemark(source, propertyName, VALIDATOR_NAME, remarks, JobEntryValidatorUtils.getLevelOnFail(context, VALIDATOR_NAME));
return false;
}
try {
// Just being paranoid
fileObject.close();
} catch (IOException ignored) {
// Ignore close errors
}
} catch (Exception e) {
JobEntryValidatorUtils.addExceptionRemark(source, propertyName, VALIDATOR_NAME, remarks, e);
return false;
}
return true;
}
use of org.pentaho.di.core.variables.VariableSpace in project pentaho-kettle by pentaho.
the class TransExecutionConfiguration method getAllVariables.
public void getAllVariables(TransMeta transMeta) {
Properties sp = new Properties();
VariableSpace space = Variables.getADefaultVariableSpace();
String[] keys = space.listVariables();
for (int i = 0; i < keys.length; i++) {
sp.put(keys[i], space.getVariable(keys[i]));
}
String[] vars = transMeta.listVariables();
if (vars != null && vars.length > 0) {
HashMap<String, String> newVariables = new HashMap<String, String>();
for (int i = 0; i < vars.length; i++) {
String varname = vars[i];
newVariables.put(varname, Const.NVL(variables.get(varname), sp.getProperty(varname, "")));
}
// variables.clear();
variables.putAll(newVariables);
}
//
for (String variableName : Const.INTERNAL_JOB_VARIABLES) {
String value = transMeta.getVariable(variableName);
if (!Utils.isEmpty(value)) {
variables.put(variableName, value);
}
}
}
use of org.pentaho.di.core.variables.VariableSpace in project pentaho-kettle by pentaho.
the class JobEntryFTP method hookInOtherParsers.
/**
* Hook in known parsers, and then those that have been specified in the variable ftp.file.parser.class.names
*
* @param ftpClient
* @throws FTPException
* @throws IOException
*/
protected void hookInOtherParsers(FTPClient ftpClient) throws FTPException, IOException {
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Hooking.Parsers"));
}
String system = ftpClient.system();
MVSFileParser parser = new MVSFileParser(log);
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Created.MVS.Parser"));
}
FTPFileFactory factory = new FTPFileFactory(system);
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Created.Factory"));
}
factory.addParser(parser);
ftpClient.setFTPFileFactory(factory);
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Get.Variable.Space"));
}
VariableSpace vs = this.getVariables();
if (vs != null) {
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Getting.Other.Parsers"));
}
String otherParserNames = vs.getVariable("ftp.file.parser.class.names");
if (otherParserNames != null) {
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Creating.Parsers"));
}
String[] parserClasses = otherParserNames.split("|");
String cName = null;
Class<?> clazz = null;
Object parserInstance = null;
for (int i = 0; i < parserClasses.length; i++) {
cName = parserClasses[i].trim();
if (cName.length() > 0) {
try {
clazz = Class.forName(cName);
parserInstance = clazz.newInstance();
if (parserInstance instanceof FTPFileParser) {
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Created.Other.Parser", cName));
}
factory.addParser((FTPFileParser) parserInstance);
}
} catch (Exception ignored) {
if (log.isDebug()) {
ignored.printStackTrace();
logError(BaseMessages.getString(PKG, "JobEntryFTP.ERROR.Creating.Parser", cName));
}
}
}
}
}
}
}
use of org.pentaho.di.core.variables.VariableSpace in project pentaho-kettle by pentaho.
the class JobEntryFTPPUT method hookInOtherParsers.
/**
* Hook in known parsers, and then those that have been specified in the variable ftp.file.parser.class.names
*
* @param ftpClient
* @throws FTPException
* @throws IOException
*/
protected void hookInOtherParsers(FTPClient ftpClient) throws FTPException, IOException {
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Hooking.Parsers"));
}
String system = ftpClient.system();
MVSFileParser parser = new MVSFileParser(log);
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Created.MVS.Parser"));
}
FTPFileFactory factory = new FTPFileFactory(system);
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Created.Factory"));
}
factory.addParser(parser);
ftpClient.setFTPFileFactory(factory);
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Get.Variable.Space"));
}
VariableSpace vs = this.getVariables();
if (vs != null) {
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Getting.Other.Parsers"));
}
String otherParserNames = vs.getVariable("ftp.file.parser.class.names");
if (otherParserNames != null) {
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Creating.Parsers"));
}
String[] parserClasses = otherParserNames.split("|");
String cName = null;
Class<?> clazz = null;
Object parserInstance = null;
for (int i = 0; i < parserClasses.length; i++) {
cName = parserClasses[i].trim();
if (cName.length() > 0) {
try {
clazz = Class.forName(cName);
parserInstance = clazz.newInstance();
if (parserInstance instanceof FTPFileParser) {
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobEntryFTP.DEBUG.Created.Other.Parser", cName));
}
factory.addParser((FTPFileParser) parserInstance);
}
} catch (Exception ignored) {
if (log.isDebug()) {
ignored.printStackTrace();
logError(BaseMessages.getString(PKG, "JobEntryFTP.ERROR.Creating.Parser", cName));
}
}
}
}
}
}
}
use of org.pentaho.di.core.variables.VariableSpace in project pentaho-kettle by pentaho.
the class StepWithMappingMeta method loadMappingMeta.
public static synchronized TransMeta loadMappingMeta(StepWithMappingMeta executorMeta, Repository rep, IMetaStore metaStore, VariableSpace space, boolean share) throws KettleException {
TransMeta mappingTransMeta = null;
CurrentDirectoryResolver r = new CurrentDirectoryResolver();
// send parentVariables = null we don't need it here for resolving resolveCurrentDirectory.
// Otherwise we destroy child variables and the option "Inherit all variables from the transformation" is enabled always.
VariableSpace tmpSpace = r.resolveCurrentDirectory(executorMeta.getSpecificationMethod(), null, rep, executorMeta.getParentStepMeta(), executorMeta.getFileName());
switch(executorMeta.getSpecificationMethod()) {
case FILENAME:
String realFilename = tmpSpace.environmentSubstitute(executorMeta.getFileName());
try {
// Don't set internal variables: they belong to the parent thread!
if (rep != null) {
// need to try to load from the repository
realFilename = r.normalizeSlashes(realFilename);
try {
String dirStr = realFilename.substring(0, realFilename.lastIndexOf("/"));
String tmpFilename = realFilename.substring(realFilename.lastIndexOf("/") + 1);
RepositoryDirectoryInterface dir = rep.findDirectory(dirStr);
mappingTransMeta = rep.loadTransformation(tmpFilename, dir, null, true, null);
} catch (KettleException ke) {
// try without extension
if (realFilename.endsWith(Const.STRING_TRANS_DEFAULT_EXT)) {
try {
String tmpFilename = realFilename.substring(realFilename.lastIndexOf("/") + 1, realFilename.indexOf("." + Const.STRING_TRANS_DEFAULT_EXT));
String dirStr = realFilename.substring(0, realFilename.lastIndexOf("/"));
RepositoryDirectoryInterface dir = rep.findDirectory(dirStr);
mappingTransMeta = rep.loadTransformation(tmpFilename, dir, null, true, null);
} catch (KettleException ke2) {
// fall back to try loading from file system (transMeta is going to be null)
}
}
}
}
if (mappingTransMeta == null) {
mappingTransMeta = new TransMeta(realFilename, metaStore, rep, true, tmpSpace, null);
LogChannel.GENERAL.logDetailed("Loading transformation from repository", "Transformation was loaded from XML file [" + realFilename + "]");
}
} catch (Exception e) {
throw new KettleException(BaseMessages.getString(PKG, "StepWithMappingMeta.Exception.UnableToLoadTrans"), e);
}
break;
case REPOSITORY_BY_NAME:
String realTransname = tmpSpace.environmentSubstitute(executorMeta.getTransName());
String realDirectory = tmpSpace.environmentSubstitute(executorMeta.getDirectoryPath());
if (rep != null) {
if (!Utils.isEmpty(realTransname) && !Utils.isEmpty(realDirectory)) {
realDirectory = r.normalizeSlashes(realDirectory);
RepositoryDirectoryInterface repdir = rep.findDirectory(realDirectory);
if (repdir != null) {
try {
// reads the last revision in the repository...
mappingTransMeta = rep.loadTransformation(realTransname, repdir, null, true, null);
// TODO: FIXME: pass in metaStore to repository?
LogChannel.GENERAL.logDetailed("Loading transformation from repository", "Executor transformation [" + realTransname + "] was loaded from the repository");
} catch (Exception e) {
throw new KettleException("Unable to load transformation [" + realTransname + "]", e);
}
}
}
} else {
// rep is null, let's try loading by filename
try {
mappingTransMeta = new TransMeta(realDirectory + "/" + realTransname, metaStore, null, true, tmpSpace, null);
} catch (KettleException ke) {
try {
// add .ktr extension and try again
mappingTransMeta = new TransMeta(realDirectory + "/" + realTransname + "." + Const.STRING_TRANS_DEFAULT_EXT, metaStore, null, true, tmpSpace, null);
} catch (KettleException ke2) {
throw new KettleException(BaseMessages.getString(PKG, "StepWithMappingMeta.Exception.UnableToLoadTrans", realTransname) + realDirectory);
}
}
}
break;
case REPOSITORY_BY_REFERENCE:
// Read the last revision by reference...
mappingTransMeta = rep.loadTransformation(executorMeta.getTransObjectId(), null);
break;
default:
break;
}
if (mappingTransMeta == null) {
// skip warning
return null;
}
// When the child parameter does exist in the parent parameters, overwrite the child parameter by the
// parent parameter.
replaceVariableValues(mappingTransMeta, space);
if (share) {
// All other parent parameters need to get copied into the child parameters (when the 'Inherit all
// variables from the transformation?' option is checked)
addMissingVariables(mappingTransMeta, space);
}
mappingTransMeta.setRepository(rep);
mappingTransMeta.setMetaStore(metaStore);
mappingTransMeta.setFilename(mappingTransMeta.getFilename());
return mappingTransMeta;
}
Aggregations