if ( ComponentBase.debug ) {
debug( Messages.getInstance().getString( "Kettle.DEBUG_START" ) ); //$NON-NLS-1$
}
TransMeta transMeta = null;
JobMeta jobMeta = null;
// Build lists of parameters, variables and command line arguments
Map<String, String> argumentMap = new HashMap<String, String>();
Map<String, String> variableMap = new HashMap<String, String>();
Map<String, String> parameterMap = new HashMap<String, String>();
for ( Node n : (List<Node>) getComponentDefinition().selectNodes( PARAMETER_MAP_CMD_ARG ) ) {
argumentMap
.put(
n.selectSingleNode( "name" ).getText(),
applyInputsToFormat( getInputStringValue( n.selectSingleNode( "mapping" ).getText() ) ) ); //$NON-NLS-1$ //$NON-NLS-2$
}
for ( Node n : (List<Node>) getComponentDefinition().selectNodes( PARAMETER_MAP_VARIABLE ) ) {
variableMap
.put(
n.selectSingleNode( "name" ).getText(),
applyInputsToFormat( getInputStringValue( n.selectSingleNode( "mapping" ).getText() ) ) ); //$NON-NLS-1$ //$NON-NLS-2$
}
for ( Node n : (List<Node>) getComponentDefinition().selectNodes( PARAMETER_MAP_PARAMETER ) ) {
parameterMap
.put(
n.selectSingleNode( "name" ).getText(),
applyInputsToFormat( getInputStringValue( n.selectSingleNode( "mapping" ).getText() ) ) ); //$NON-NLS-1$ //$NON-NLS-2$
}
String[] arguments = null;
// If no mappings are provided, assume all inputs are command line
// arguments (This supports the legacy method)
if ( argumentMap.size() <= 0 && variableMap.size() <= 0 && parameterMap.size() <= 0 ) {
// this use is now considered obsolete, as we prefer the
// action-sequence inputs since they
// now maintain order
boolean running = true;
int index = 1;
ArrayList<String> argumentList = new ArrayList<String>();
while ( running ) {
if ( isDefinedInput( "parameter" + index ) ) { //$NON-NLS-1$
String value = null;
String inputName = getInputStringValue( "parameter" + index ); //$NON-NLS-1$
// see if we have an input with this name
if ( isDefinedInput( inputName ) ) {
value = getInputStringValue( inputName );
}
argumentList.add( value );
} else {
running = false;
}
index++;
}
// this is the preferred way to provide inputs to the
// KetteComponent, the order of inputs is now preserved
Iterator<?> inputNamesIter = getInputNames().iterator();
while ( inputNamesIter.hasNext() ) {
String name = (String) inputNamesIter.next();
argumentList.add( getInputStringValue( name ) );
}
arguments = (String[]) argumentList.toArray( new String[argumentList.size()] );
} else {
// Extract arguments from argumentMap (Throw an error if the
// sequential ordering is broken)
arguments = new String[argumentMap.size()];
for ( int i = 0; i < argumentMap.size(); i++ ) {
arguments[i] = argumentMap.get( Integer.toString( i + 1 ) ); // Mapping
// is
// 1
// based
// to
// match
// Kettle
// UI
if ( arguments[i] == null ) {
error( Messages.getInstance().getErrorString( "Kettle.ERROR_0030_INVALID_ARGUMENT_MAPPING" ) ); //$NON-NLS-1$
}
}
}
// initialize environment variables
try {
KettleSystemListener.environmentInit( getSession() );
} catch ( KettleException ke ) {
error( ke.getMessage(), ke );
}
String solutionPath = "solution:";
Repository repository = connectToRepository();
boolean result = false;
try {
if ( isDefinedInput( KettleComponent.DIRECTORY ) ) {
String directoryName = getInputStringValue( KettleComponent.DIRECTORY );
if ( repository == null ) {
return false;
}
if ( isDefinedInput( KettleComponent.TRANSFORMATION ) ) {
String transformationName = getInputStringValue( KettleComponent.TRANSFORMATION );
transMeta = loadTransformFromRepository( directoryName, transformationName, repository );
if ( transMeta != null ) {
try {
for ( String key : parameterMap.keySet() ) {
transMeta.setParameterValue( key, parameterMap.get( key ) );
}
for ( String key : variableMap.keySet() ) {
transMeta.setVariable( key, variableMap.get( key ) );
}
} catch ( UnknownParamException e ) {
error( e.getMessage() );
}
transMeta.setArguments( arguments );
} else {
return false;
}
} else if ( isDefinedInput( KettleComponent.JOB ) ) {
String jobName = getInputStringValue( KettleComponent.JOB );
jobMeta = loadJobFromRepository( directoryName, jobName, repository );
if ( jobMeta != null ) {
try {
for ( String key : parameterMap.keySet() ) {
jobMeta.setParameterValue( key, parameterMap.get( key ) );
}
for ( String key : variableMap.keySet() ) {
jobMeta.setVariable( key, variableMap.get( key ) );
}
} catch ( UnknownParamException e ) {
error( e.getMessage() );
}
jobMeta.setArguments( arguments );
} else {
return false;
}
}
} else if ( isDefinedResource( KettleComponent.TRANSFORMFILE ) ) {
IActionSequenceResource transformResource = getResource( KettleComponent.TRANSFORMFILE );
String fileAddress = getActualFileName( transformResource );
try {
if ( fileAddress != null ) { // We have an actual loadable
// filesystem and file
transMeta = new TransMeta( fileAddress, repository, true );
transMeta.setFilename( fileAddress );
} else if ( repository != null && repository.isConnected() ) {
fileAddress = transformResource.getAddress();
// load transformation resource from kettle/settings.xml configured repository
transMeta = loadTransformFromRepository( FilenameUtils.getPathNoEndSeparator( fileAddress ), FilenameUtils.getBaseName( fileAddress ), repository );
} else {
String jobXmlStr = getResourceAsString( getResource( KettleComponent.TRANSFORMFILE ) );
jobXmlStr = jobXmlStr.replaceAll( "\\$\\{pentaho.solutionpath\\}", solutionPath ); //$NON-NLS-1$
jobXmlStr = jobXmlStr.replaceAll( "\\%\\%pentaho.solutionpath\\%\\%", solutionPath ); //$NON-NLS-1$
org.w3c.dom.Document doc = XmlW3CHelper.getDomFromString( jobXmlStr );
// create a tranformation from the document
transMeta = new TransMeta( doc.getFirstChild(), repository );
}
} catch ( Exception e ) {
error( Messages.getInstance().getErrorString(
"Kettle.ERROR_0015_BAD_RESOURCE", KettleComponent.TRANSFORMFILE, fileAddress ), e ); //$NON-NLS-1$
return false;
}
/*
* Unreachable code below... if (transMeta == null) {
* error(Messages.getInstance().getErrorString("Kettle.ERROR_0015_BAD_RESOURCE", KettleComponent.TRANSFORMFILE,
* fileAddress)); //$NON-NLS-1$ debug(getKettleLog(true)); return false; }
*/
// Don't forget to set the parameters here as well...
try {
for ( String key : parameterMap.keySet() ) {
transMeta.setParameterValue( key, parameterMap.get( key ) );
}
for ( String key : variableMap.keySet() ) {
transMeta.setVariable( key, variableMap.get( key ) );
}
} catch ( UnknownParamException e ) {
error( e.getMessage() );
}
transMeta.setArguments( arguments );
/*
* We do not need to concatenate the solutionPath info as the fileAddress has the complete location of the file
* from start to end. This is to resolve BISERVER-502.
*/
transMeta.setFilename( fileAddress );
} else if ( isDefinedResource( KettleComponent.JOBFILE ) ) {
String fileAddress = ""; //$NON-NLS-1$
try {
fileAddress = getResource( KettleComponent.JOBFILE ).getAddress();
if ( repository != null && repository.isConnected() ) {
solutionPath = StringUtils.EMPTY;
// load job resource from kettle/settings.xml configured repository
jobMeta = loadJobFromRepository( FilenameUtils.getPathNoEndSeparator( fileAddress ), FilenameUtils.getBaseName( fileAddress ), repository );
} else {
String jobXmlStr = getResourceAsString( getResource( KettleComponent.JOBFILE ) );
// String jobXmlStr =
// XmlW3CHelper.getContentFromSolutionResource(fileAddress);
jobXmlStr = jobXmlStr.replaceAll( "\\$\\{pentaho.solutionpath\\}", solutionPath ); //$NON-NLS-1$
jobXmlStr = jobXmlStr.replaceAll( "\\%\\%pentaho.solutionpath\\%\\%", solutionPath ); //$NON-NLS-1$
org.w3c.dom.Document doc = XmlW3CHelper.getDomFromString( jobXmlStr );
if ( doc == null ) {
error( Messages.getInstance().getErrorString(
"Kettle.ERROR_0015_BAD_RESOURCE", KettleComponent.JOBFILE, fileAddress ) ); //$NON-NLS-1$
debug( getKettleLog( true ) );
return false;
}
// create a job from the document
try {
repository = connectToRepository();
// if we get a valid repository its great, if not try it
// without
jobMeta = new JobMeta( solutionPath + fileAddress, repository );
} catch ( Exception e ) {
error( Messages.getInstance().getString( "Kettle.ERROR_0023_NO_META" ), e ); //$NON-NLS-1$
} finally {
if ( repository != null ) {
if ( ComponentBase.debug ) {
debug( Messages.getInstance().getString( "Kettle.DEBUG_DISCONNECTING" ) ); //$NON-NLS-1$
}
repository.disconnect();
}
}
}
} catch ( Exception e ) {
error( Messages.getInstance().getErrorString(
"Kettle.ERROR_0015_BAD_RESOURCE", KettleComponent.JOBFILE, fileAddress ), e ); //$NON-NLS-1$
return false;
}
if ( jobMeta == null ) {
error( Messages.getInstance().getErrorString(
"Kettle.ERROR_0015_BAD_RESOURCE", KettleComponent.JOBFILE, fileAddress ) ); //$NON-NLS-1$
debug( getKettleLog( true ) );
return false;
} else {
try {
for ( String key : parameterMap.keySet() ) {
jobMeta.setParameterValue( key, parameterMap.get( key ) );
}
for ( String key : variableMap.keySet() ) {
jobMeta.setVariable( key, variableMap.get( key ) );
}
} catch ( UnknownParamException e ) {
error( e.getMessage() );
}
jobMeta.setArguments( arguments );
jobMeta.setFilename( solutionPath + fileAddress );
}
}
// OK, we have the information, let's load and execute the
// transformation or job
if ( transMeta != null ) {
result = executeTransformation( transMeta );
}
if ( jobMeta != null ) {
result = executeJob( jobMeta, repository );
}
} finally {
if ( repository != null ) {
if ( ComponentBase.debug ) {
debug( Messages.getInstance().getString( "Kettle.DEBUG_DISCONNECTING" ) ); //$NON-NLS-1$
}
try {
repository.disconnect();
} catch ( Exception ignored ) {
//ignore
}
}
if ( transMeta != null ) {
try {
cleanLogChannel( transMeta );
transMeta.clear();
} catch ( Exception ignored ) {
//ignore
}
transMeta = null;
}