Java Code Examples for org.pentaho.di.core.variables.VariableSpace#getVariable()

The following examples show how to use org.pentaho.di.core.variables.VariableSpace#getVariable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransUnitTest.java    From pentaho-pdi-dataset with Apache License 2.0 6 votes vote down vote up
public String calculateCompleteFilename( VariableSpace space ) {

    String baseFilePath = space.environmentSubstitute( basePath );
    if ( StringUtils.isEmpty( baseFilePath ) ) {
      // See if the base path environment variable is set
      //
      baseFilePath = space.getVariable( DataSetConst.VARIABLE_UNIT_TESTS_BASE_PATH );
    }
    if ( StringUtils.isEmpty( baseFilePath ) ) {
      baseFilePath = "";
    }
    if ( StringUtils.isNotEmpty( baseFilePath ) ) {
      if ( !baseFilePath.endsWith( File.separator ) ) {
        baseFilePath += File.separator;
      }
    }
    return baseFilePath + transFilename;
  }
 
Example 2
Source File: ValueDataUtil.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private static MathContext buildMathContext( VariableSpace space ) {
  String precisionString = space.getVariable( Const.KETTLE_BIGDECIMAL_DIVISION_PRECISION );
  String roundingModeString = space.getVariable( Const.KETTLE_BIGDECIMAL_DIVISION_ROUNDING_MODE );

  if ( precisionString != null ) {
    RoundingMode roundingMode;
    try {
      roundingMode = RoundingMode.valueOf( roundingModeString );
    } catch ( IllegalArgumentException | NullPointerException e ) {
      roundingMode = RoundingMode.HALF_EVEN;
    }
    int precision = Integer.parseInt( precisionString );
    if ( precision < 0 ) {
      return MathContext.UNLIMITED;
    } else {
      return new MathContext( precision, roundingMode );
    }
  }
  return MathContext.UNLIMITED;
}
 
Example 3
Source File: BeamGenericStepHandler.java    From kettle-beam with Apache License 2.0 5 votes vote down vote up
private List<VariableValue> getVariableValues( VariableSpace space ) {

    List<VariableValue> variableValues = new ArrayList<>();
    for ( String variable : space.listVariables() ) {
      String value = space.getVariable( variable );
      variableValues.add( new VariableValue( variable, value ) );
    }
    return variableValues;
  }
 
Example 4
Source File: JobExecutionConfiguration.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public void setVariables( VariableSpace space ) {
  this.variables = new HashMap<String, String>();

  for ( String name : space.listVariables() ) {
    String value = space.getVariable( name );
    this.variables.put( name, value );
  }
}
 
Example 5
Source File: TransExecutionConfiguration.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public void setVariables( VariableSpace space ) {
  this.variables = new HashMap<String, String>();

  for ( String name : space.listVariables() ) {
    String value = space.getVariable( name );
    this.variables.put( name, value );
  }
}
 
Example 6
Source File: StepWithMappingMeta.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static  void addMissingVariables( VariableSpace fromSpace, VariableSpace toSpace ) {
  if ( toSpace == null ) {
    return;
  }
  String[] variableNames = toSpace.listVariables();
  for ( String variable : variableNames ) {
    if ( fromSpace.getVariable( variable ) == null ) {
      fromSpace.setVariable( variable, toSpace.getVariable( variable ) );
    }
  }
}
 
Example 7
Source File: StepWithMappingMeta.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static void replaceVariableValues( VariableSpace childTransMeta, VariableSpace replaceBy, String type ) {
  if ( replaceBy == null ) {
    return;
  }
  String[] variableNames = replaceBy.listVariables();
  for ( String variableName : variableNames ) {
    if ( childTransMeta.getVariable( variableName ) != null && !isInternalVariable( variableName, type ) ) {
      childTransMeta.setVariable( variableName, replaceBy.getVariable( variableName ) );
    }
  }
}
 
Example 8
Source File: BaseLogTable.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
protected String getLogBuffer( VariableSpace space, String logChannelId, LogStatus status, String limit ) {

  LoggingBuffer loggingBuffer = KettleLogStore.getAppender();
  // if job is starting, then remove all previous events from buffer with that job logChannelId.
  // Prevents recursive job calls logging issue.
  if ( status.getStatus().equalsIgnoreCase( String.valueOf( LogStatus.START ) ) ) {
    loggingBuffer.removeChannelFromBuffer( logChannelId );
  }

  StringBuffer buffer = loggingBuffer.getBuffer( logChannelId, true );

  if ( Utils.isEmpty( limit ) ) {
    String defaultLimit = space.getVariable( Const.KETTLE_LOG_SIZE_LIMIT, null );
    if ( !Utils.isEmpty( defaultLimit ) ) {
      limit = defaultLimit;
    }
  }

  // See if we need to limit the amount of rows
  //
  int nrLines = Utils.isEmpty( limit ) ? -1 : Const.toInt( space.environmentSubstitute( limit ), -1 );

  if ( nrLines > 0 ) {
    int start = buffer.length() - 1;
    for ( int i = 0; i < nrLines && start > 0; i++ ) {
      start = buffer.lastIndexOf( Const.CR, start - 1 );
    }
    if ( start > 0 ) {
      buffer.delete( 0, start + Const.CR.length() );
    }
  }

  return buffer.append( Const.CR + status.getStatus().toUpperCase() + Const.CR ).toString();
}
 
Example 9
Source File: VariableButtonListenerFactory.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static final String getVariableName( Shell shell, VariableSpace space ) {
  String[] keys = space.listVariables();
  Arrays.sort( keys );

  int size = keys.length;
  String[] key = new String[size];
  String[] val = new String[size];
  String[] str = new String[size];

  for ( int i = 0; i < keys.length; i++ ) {
    key[i] = keys[i];
    val[i] = space.getVariable( key[i] );
    str[i] = key[i] + "  [" + val[i] + "]";
  }

  EnterSelectionDialog esd = new EnterSelectionDialog( shell, str,
    BaseMessages.getString( PKG, "System.Dialog.SelectEnvironmentVar.Title" ),
    BaseMessages.getString( PKG, "System.Dialog.SelectEnvironmentVar.Message" ) );
  esd.clearModal();
  if ( esd.open() != null ) {
    int nr = esd.getSelectionNr();
    String var = key[nr];

    return var;
  } else {
    return null;
  }
}
 
Example 10
Source File: KettleFileSystemConfigBuilderFactory.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * This factory returns a FileSystemConfigBuilder. Custom FileSystemConfigBuilders can be created by implementing the
 * {@link IKettleFileSystemConfigBuilder} or overriding the {@link KettleGenericFileSystemConfigBuilder}
 *
 * @see org.apache.commons.vfs.FileSystemConfigBuilder
 *
 * @param varSpace
 *          A Kettle variable space for resolving VFS config parameters
 * @param scheme
 *          The VFS scheme (FILE, HTTP, SFTP, etc...)
 * @return A FileSystemConfigBuilder that can translate Kettle variables into VFS config parameters
 * @throws IOException
 */
public static IKettleFileSystemConfigBuilder getConfigBuilder( VariableSpace varSpace, String scheme ) throws IOException {
  IKettleFileSystemConfigBuilder result = null;

  // Attempt to load the Config Builder from a variable: vfs.config.parser = class
  String parserClass = varSpace.getVariable( "vfs." + scheme + ".config.parser" );

  if ( parserClass != null ) {
    try {
      Class<?> configBuilderClass =
        KettleFileSystemConfigBuilderFactory.class.getClassLoader().loadClass( parserClass );
      Method mGetInstance = configBuilderClass.getMethod( "getInstance" );
      if ( ( mGetInstance != null )
        && ( IKettleFileSystemConfigBuilder.class.isAssignableFrom( mGetInstance.getReturnType() ) ) ) {
        result = (IKettleFileSystemConfigBuilder) mGetInstance.invoke( null );
      } else {
        result = (IKettleFileSystemConfigBuilder) configBuilderClass.newInstance();
      }
    } catch ( Exception e ) {
      // Failed to load custom parser. Throw exception.
      throw new IOException( BaseMessages.getString( PKG, "CustomVfsSettingsParser.Log.FailedToLoad" ) );
    }
  } else {
    // No custom parser requested, load default
    if ( scheme.equalsIgnoreCase( "sftp" ) ) {
      result = KettleSftpFileSystemConfigBuilder.getInstance();
    } else {
      result = KettleGenericFileSystemConfigBuilder.getInstance();
    }
  }

  return result;
}
 
Example 11
Source File: KettleVFS.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
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.equalsIgnoreCase( CONNECTION ) && varSpace.getVariable( var ) != null ) {
      FileSystemOptions fileSystemOptions = VFSHelper.getOpts( vfsFilename, varSpace.getVariable( var ) );
      if ( fileSystemOptions != null ) {
        return fileSystemOptions;
      }
    }
    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;
}
 
Example 12
Source File: JobEntryFTP.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 * 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 ) );
            }
          }
        }
      }
    }
  }
}
 
Example 13
Source File: JobEntryFTPPUT.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 * 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 ) );
            }
          }
        }
      }
    }
  }
}
 
Example 14
Source File: TransTestFactory.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public static List<RowMetaAndData> executeTestTransformation( TransMeta transMeta, String injectorStepname,
    String testStepname, String dummyStepname, List<RowMetaAndData> inputData,
    VariableSpace runTimeVariables, VariableSpace runTimeParameters ) throws KettleException {
  // Now execute the transformation...
  Trans trans = new Trans( transMeta );

  trans.initializeVariablesFrom( runTimeVariables );
  if ( runTimeParameters != null ) {
    for ( String param : trans.listParameters() ) {
      String value = runTimeParameters.getVariable( param );
      if ( value != null ) {
        trans.setParameterValue( param, value );
        transMeta.setParameterValue( param, value );
      }
    }
  }
  trans.prepareExecution( null );

  // Capture the rows that come out of the dummy step...
  //
  StepInterface si = trans.getStepInterface( dummyStepname, 0 );
  RowStepCollector dummyRc = new RowStepCollector();
  si.addRowListener( dummyRc );

  // Add a row producer...
  //
  RowProducer rp = trans.addRowProducer( injectorStepname, 0 );

  // Start the steps...
  //
  trans.startThreads();

  // Inject the actual test rows...
  //
  List<RowMetaAndData> inputList = inputData;
  Iterator<RowMetaAndData> it = inputList.iterator();
  while ( it.hasNext() ) {
    RowMetaAndData rm = it.next();
    rp.putRow( rm.getRowMeta(), rm.getData() );
  }
  rp.finished();

  // Wait until the transformation is finished...
  //
  trans.waitUntilFinished();

  // If there is an error in the result, throw an exception here...
  //
  if ( trans.getResult().getNrErrors() > 0 ) {
    throw new KettleException( "Test transformation finished with errors. Check the log." );
  }

  // Return the result from the dummy step...
  //
  return dummyRc.getRowsRead();
}
 
Example 15
Source File: VariableButtonListenerFactory.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public static final SelectionAdapter getSelectionAdapter( final Composite composite, final Text destination,
    final GetCaretPositionInterface getCaretPositionInterface, final InsertTextInterface insertTextInterface,
    final VariableSpace space ) {
  return new SelectionAdapter() {
    public void widgetSelected( SelectionEvent e ) {
      String[] keys = space.listVariables();
      Arrays.sort( keys );

      int size = keys.length;
      String[] key = new String[size];
      String[] val = new String[size];
      String[] str = new String[size];

      for ( int i = 0; i < keys.length; i++ ) {
        key[i] = keys[i];
        val[i] = space.getVariable( key[i] );
        str[i] = key[i] + "  [" + val[i] + "]";
      }

      // Before focus is lost, we get the position of where the selected variable needs to be inserted.
      int position = 0;
      if ( getCaretPositionInterface != null ) {
        position = getCaretPositionInterface.getCaretPosition();
      }

      EnterSelectionDialog esd =
          new EnterSelectionDialog( composite.getShell(), str, BaseMessages.getString( PKG,
              "System.Dialog.SelectEnvironmentVar.Title" ), BaseMessages.getString( PKG,
                  "System.Dialog.SelectEnvironmentVar.Message" ) );
      if ( esd.open() != null ) {
        int nr = esd.getSelectionNr();
        String var = "${" + key[nr] + "}";

        if ( insertTextInterface == null ) {
          destination.insert( var );
          // destination.setToolTipText(StringUtil.environmentSubstitute( destination.getText() ) );
          e.doit = false;
        } else {
          insertTextInterface.insertText( var, position );
        }
      }
    }
  };
}
 
Example 16
Source File: VariableButtonListenerFactory.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public static final SelectionAdapter getSelectionAdapter( final Composite composite, final Text destination,
  final GetCaretPositionInterface getCaretPositionInterface, final InsertTextInterface insertTextInterface,
  final VariableSpace space ) {
  return new SelectionAdapter() {
    public void widgetSelected( SelectionEvent e ) {
      String[] keys = space.listVariables();
      Arrays.sort( keys );

      int size = keys.length;
      String[] key = new String[size];
      String[] val = new String[size];
      String[] str = new String[size];

      for ( int i = 0; i < keys.length; i++ ) {
        key[i] = keys[i];
        val[i] = space.getVariable( key[i] );
        str[i] = key[i] + "  [" + val[i] + "]";
      }

      // Before focus is lost, we get the position of where the selected variable needs to be inserted.
      int position = 0;
      if ( getCaretPositionInterface != null ) {
        position = getCaretPositionInterface.getCaretPosition();
      }

      EnterSelectionDialog esd = new EnterSelectionDialog( composite.getShell(), str,
        BaseMessages.getString( PKG, "System.Dialog.SelectEnvironmentVar.Title" ),
        BaseMessages.getString( PKG, "System.Dialog.SelectEnvironmentVar.Message" ) );
      if ( esd.open() != null ) {
        int nr = esd.getSelectionNr();
        String var = "${" + key[nr] + "}";

        if ( insertTextInterface == null ) {
          destination.insert( var );
          // destination.setToolTipText(StringUtil.environmentSubstitute( destination.getText() ) );
          e.doit = false;
        } else {
          insertTextInterface.insertText( var, position );
        }
      }
    }
  };
}