use of org.pentaho.di.core.variables.VariableSpace in project pentaho-kettle by pentaho.
the class AbstractMetaTest method testFieldSubstitute.
@Test
public void testFieldSubstitute() throws Exception {
// This is just a delegate method, verify it's called
VariableSpace vars = mock(VariableSpace.class);
// This method is reused by the stub to set the mock as the variables object
meta.setInternalKettleVariables(vars);
RowMetaInterface rowMeta = mock(RowMetaInterface.class);
Object[] data = new Object[0];
meta.fieldSubstitute("?{param}", rowMeta, data);
verify(vars, times(1)).fieldSubstitute("?{param}", rowMeta, data);
}
use of org.pentaho.di.core.variables.VariableSpace in project pentaho-kettle by pentaho.
the class ScriptAddedFunctions method setVariable.
// Setting Variable
public static void setVariable(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) {
String sArg1 = "";
String sArg2 = "";
String sArg3 = "";
if (ArgList.length == 3) {
try {
Object scmo = actualObject.get("_step_");
Object scmO = scmo;
if (scmO instanceof ScriptInterface) {
ScriptInterface scm = (ScriptInterface) scmO;
sArg1 = (String) ArgList[0];
sArg2 = (String) ArgList[1];
sArg3 = (String) ArgList[2];
if ("s".equals(sArg3)) {
// System wide properties
System.setProperty(sArg1, sArg2);
// Set also all the way to the root as else we will take
// stale values
scm.setVariable(sArg1, sArg2);
VariableSpace parentSpace = scm.getParentVariableSpace();
while (parentSpace != null) {
parentSpace.setVariable(sArg1, sArg2);
parentSpace = parentSpace.getParentVariableSpace();
}
} else if ("r".equals(sArg3)) {
// Upto the root... this should be the default.
scm.setVariable(sArg1, sArg2);
VariableSpace parentSpace = scm.getParentVariableSpace();
while (parentSpace != null) {
parentSpace.setVariable(sArg1, sArg2);
parentSpace = parentSpace.getParentVariableSpace();
}
} else if ("p".equals(sArg3)) {
// Upto the parent
scm.setVariable(sArg1, sArg2);
VariableSpace parentSpace = scm.getParentVariableSpace();
if (parentSpace != null) {
parentSpace.setVariable(sArg1, sArg2);
}
} else if ("g".equals(sArg3)) {
// Upto the grand parent
scm.setVariable(sArg1, sArg2);
VariableSpace parentSpace = scm.getParentVariableSpace();
if (parentSpace != null) {
parentSpace.setVariable(sArg1, sArg2);
VariableSpace grandParentSpace = parentSpace.getParentVariableSpace();
if (grandParentSpace != null) {
grandParentSpace.setVariable(sArg1, sArg2);
}
}
} else {
throw new RuntimeException("The argument type of function call setVariable should either be \"s\", \"r\", \"p\", or \"g\".");
}
}
// Else: Ignore for now... if we're executing via the Test Button
} catch (Exception e) {
throw new RuntimeException(e.toString());
}
} else {
throw new RuntimeException("The function call setVariable requires 3 arguments.");
}
}
use of org.pentaho.di.core.variables.VariableSpace in project pentaho-kettle by pentaho.
the class FileExistsValidator 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 failIfDoesNotExist = getFailIfDoesNotExist(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 == null || (fileObject != null && !fileObject.exists() && failIfDoesNotExist)) {
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 JobEntryJob method getJobMeta.
public JobMeta getJobMeta(Repository rep, IMetaStore metaStore, VariableSpace space) throws KettleException {
JobMeta jobMeta = null;
try {
CurrentDirectoryResolver r = new CurrentDirectoryResolver();
VariableSpace tmpSpace = r.resolveCurrentDirectory(specificationMethod, space, rep, parentJob, getFilename());
switch(specificationMethod) {
case FILENAME:
String realFilename = tmpSpace.environmentSubstitute(getFilename());
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);
jobMeta = rep.loadJob(tmpFilename, dir, null, null);
} catch (KettleException ke) {
// try without extension
if (realFilename.endsWith(Const.STRING_JOB_DEFAULT_EXT)) {
try {
String tmpFilename = realFilename.substring(realFilename.lastIndexOf("/") + 1, realFilename.indexOf("." + Const.STRING_JOB_DEFAULT_EXT));
String dirStr = realFilename.substring(0, realFilename.lastIndexOf("/"));
RepositoryDirectoryInterface dir = rep.findDirectory(dirStr);
jobMeta = rep.loadJob(tmpFilename, dir, null, null);
} catch (KettleException ke2) {
// fall back to try loading from file system (mappingJobMeta is going to be null)
}
}
}
}
if (jobMeta == null) {
jobMeta = new JobMeta(tmpSpace, realFilename, rep, metaStore, null);
}
break;
case REPOSITORY_BY_NAME:
String realDirectory = tmpSpace.environmentSubstitute(getDirectory());
String realJobName = tmpSpace.environmentSubstitute(getJobName());
if (rep != null) {
realDirectory = r.normalizeSlashes(realDirectory);
RepositoryDirectoryInterface repositoryDirectory = rep.loadRepositoryDirectoryTree().findDirectory(realDirectory);
if (repositoryDirectory == null) {
throw new KettleException("Unable to find repository directory [" + Const.NVL(realDirectory, "") + "]");
}
// reads
jobMeta = rep.loadJob(realJobName, repositoryDirectory, null, null);
} else {
// rep is null, let's try loading by filename
try {
jobMeta = new JobMeta(tmpSpace, realDirectory + "/" + realJobName, rep, metaStore, null);
} catch (KettleException ke) {
try {
// add .kjb extension and try again
jobMeta = new JobMeta(tmpSpace, realDirectory + "/" + realJobName + "." + Const.STRING_JOB_DEFAULT_EXT, rep, metaStore, null);
} catch (KettleException ke2) {
ke2.printStackTrace();
throw new KettleException("Could not execute job specified in a repository since we're not connected to one");
}
}
}
break;
case REPOSITORY_BY_REFERENCE:
if (rep != null) {
// Load the last version...
//
jobMeta = rep.loadJob(jobObjectId, null);
break;
} else {
throw new KettleException("Could not execute job specified in a repository since we're not connected to one");
}
default:
throw new KettleException("The specified object location specification method '" + specificationMethod + "' is not yet supported in this job entry.");
}
if (jobMeta != null) {
jobMeta.setRepository(rep);
jobMeta.setMetaStore(metaStore);
}
return jobMeta;
} catch (Exception e) {
throw new KettleException("Unexpected error during job metadata load", e);
}
}
use of org.pentaho.di.core.variables.VariableSpace in project pentaho-kettle by pentaho.
the class JobEntryTrans method getTransMeta.
public TransMeta getTransMeta(Repository rep, IMetaStore metaStore, VariableSpace space) throws KettleException {
try {
TransMeta transMeta = null;
CurrentDirectoryResolver r = new CurrentDirectoryResolver();
VariableSpace tmpSpace = r.resolveCurrentDirectory(specificationMethod, space, rep, parentJob, getFilename());
switch(specificationMethod) {
case FILENAME:
String realFilename = tmpSpace.environmentSubstitute(getFilename());
if (rep != null) {
if (StringUtils.isBlank(realFilename)) {
throw new KettleException(BaseMessages.getString(PKG, "JobTrans.Exception.MissingTransFileName"));
}
realFilename = r.normalizeSlashes(realFilename);
// need to try to load from the repository
try {
String dirStr = realFilename.substring(0, realFilename.lastIndexOf("/"));
String tmpFilename = realFilename.substring(realFilename.lastIndexOf("/") + 1);
RepositoryDirectoryInterface dir = rep.findDirectory(dirStr);
transMeta = 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);
transMeta = 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 (transMeta == null) {
logBasic("Loading transformation from XML file [" + realFilename + "]");
transMeta = new TransMeta(realFilename, metaStore, null, true, null, null);
}
break;
case REPOSITORY_BY_NAME:
String transname = tmpSpace.environmentSubstitute(getTransname());
String realDirectory = tmpSpace.environmentSubstitute(getDirectory());
logBasic(BaseMessages.getString(PKG, "JobTrans.Log.LoadingTransRepDirec", transname, realDirectory));
if (rep != null) {
//
// It only makes sense to try to load from the repository when the
// repository is also filled in.
//
// It reads last the last revision from the repository.
//
realDirectory = r.normalizeSlashes(realDirectory);
RepositoryDirectoryInterface repositoryDirectory = rep.findDirectory(realDirectory);
transMeta = rep.loadTransformation(transname, repositoryDirectory, null, true, null);
} else {
// rep is null, let's try loading by filename
try {
transMeta = new TransMeta(realDirectory + "/" + transname, metaStore, null, true, this, null);
} catch (KettleException ke) {
try {
// add .ktr extension and try again
transMeta = new TransMeta(realDirectory + "/" + transname + "." + Const.STRING_TRANS_DEFAULT_EXT, metaStore, null, true, this, null);
} catch (KettleException ke2) {
throw new KettleException(BaseMessages.getString(PKG, "JobTrans.Exception.NoRepDefined"), ke2);
}
}
}
break;
case REPOSITORY_BY_REFERENCE:
if (transObjectId == null) {
throw new KettleException(BaseMessages.getString(PKG, "JobTrans.Exception.ReferencedTransformationIdIsNull"));
}
if (rep != null) {
// Load the last revision
//
transMeta = rep.loadTransformation(transObjectId, null);
}
break;
default:
throw new KettleException("The specified object location specification method '" + specificationMethod + "' is not yet supported in this job entry.");
}
if (transMeta != null) {
// set Internal.Entry.Current.Directory again because it was changed
transMeta.setInternalKettleVariables();
// When the child parameter does exist in the parent parameters, overwrite the child parameter by the
// parent parameter.
StepWithMappingMeta.replaceVariableValues(transMeta, space);
if (isPassingAllParameters()) {
// All other parent parameters need to get copied into the child parameters (when the 'Inherit all
// variables from the transformation?' option is checked)
StepWithMappingMeta.addMissingVariables(transMeta, space);
}
// Pass repository and metastore references
//
transMeta.setRepository(rep);
transMeta.setMetaStore(metaStore);
}
return transMeta;
} catch (final KettleException ke) {
// if we get a KettleException, simply re-throw it
throw ke;
} catch (Exception e) {
throw new KettleException(BaseMessages.getString(PKG, "JobTrans.Exception.MetaDataLoad"), e);
}
}
Aggregations