From 82ca9ab8e4e918632192427c15ad06bad0ece4a5 Mon Sep 17 00:00:00 2001 From: Rob Fellows Date: Mon, 6 Jun 2016 14:54:10 -0400 Subject: [PATCH] [BACKLOG-8241] - Move the writing out of the Metadata-Injected KTR ... (#2582) * [BACKLOG-8241] - Move the writing out of the Metadata-Injected KTR from before execution to after to allow for customizations to MDI to happen in a step's init() method. This allows for complex data structures to be handled in a manual fashion when traditional annotation-based MDI isn't sufficient. * [CLEANUP] - License header date --- .../di/trans/steps/metainject/MetaInject.java | 79 ++++++++++--------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/engine/src/org/pentaho/di/trans/steps/metainject/MetaInject.java b/engine/src/org/pentaho/di/trans/steps/metainject/MetaInject.java index 9c628cd71cbd..7c5feeca3708 100644 --- a/engine/src/org/pentaho/di/trans/steps/metainject/MetaInject.java +++ b/engine/src/org/pentaho/di/trans/steps/metainject/MetaInject.java @@ -2,7 +2,7 @@ * * Pentaho Data Integration * - * Copyright (C) 2002-2013 by Pentaho : http://www.pentaho.com + * Copyright (C) 2002-2016 by Pentaho : http://www.pentaho.com * ******************************************************************************* * @@ -22,18 +22,6 @@ package org.pentaho.di.trans.steps.metainject; -import java.io.IOException; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - import org.pentaho.di.core.Const; import org.pentaho.di.core.Result; import org.pentaho.di.core.RowMetaAndData; @@ -61,6 +49,18 @@ import org.pentaho.di.trans.step.StepMetaInjectionInterface; import org.pentaho.di.trans.step.StepMetaInterface; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + /** * Read a simple CSV file Just output Strings found in the file... * @@ -118,30 +118,6 @@ public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws } } - if ( log.isDetailed() ) { - logDetailed( "XML of transformation after injection: " + data.transMeta.getXML() ); - } - String targetFile = environmentSubstitute( meta.getTargetFile() ); - if ( !Const.isEmpty( targetFile ) ) { - OutputStream os = null; - try { - os = KettleVFS.getOutputStream( targetFile, false ); - os.write( XMLHandler.getXMLHeader().getBytes( Const.XML_ENCODING ) ); - os.write( data.transMeta.getXML().getBytes( Const.XML_ENCODING ) ); - } catch ( IOException e ) { - throw new KettleException( "Unable to write target file (ktr after injection) to file '" + targetFile + "'", - e ); - } finally { - if ( os != null ) { - try { - os.close(); - } catch ( Exception e ) { - throw new KettleException( e ); - } - } - } - } - if ( !meta.isNoExecution() ) { // Now we can execute this modified transformation metadata. // @@ -215,6 +191,15 @@ public void rowWrittenEvent( RowMetaInterface rowMeta, Object[] row ) throws Ket copyResult( injectTrans ); } + // let the transformation complete it's execution to allow for any customizations to MDI to happen in the init methods of steps + if ( log.isDetailed() ) { + logDetailed( "XML of transformation after injection: " + data.transMeta.getXML() ); + } + String targetFile = environmentSubstitute( meta.getTargetFile() ); + if ( !Const.isEmpty( targetFile ) ) { + writeInjectedKtr( targetFile ); + } + // All done! setOutputDone(); @@ -222,6 +207,26 @@ public void rowWrittenEvent( RowMetaInterface rowMeta, Object[] row ) throws Ket return false; } + private void writeInjectedKtr( String targetFile ) throws KettleException { + OutputStream os = null; + try { + os = KettleVFS.getOutputStream( targetFile, false ); + os.write( XMLHandler.getXMLHeader().getBytes( Const.XML_ENCODING ) ); + os.write( data.transMeta.getXML().getBytes( Const.XML_ENCODING ) ); + } catch ( IOException e ) { + throw new KettleException( "Unable to write target file (ktr after injection) to file '" + targetFile + "'", + e ); + } finally { + if ( os != null ) { + try { + os.close(); + } catch ( Exception e ) { + throw new KettleException( e ); + } + } + } + } + private void newInjection( String targetStep, StepMetaInterface targetStepMeta ) throws KettleException { if ( log.isDetailed() ) { logDetailed( "Handing step '" + targetStep + "' injection!" );