Search in sources :

Example 1 with VariableSpace

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;
}
Also used : VariableSpace(org.pentaho.di.core.variables.VariableSpace) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with VariableSpace

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);
        }
    }
}
Also used : HashMap(java.util.HashMap) VariableSpace(org.pentaho.di.core.variables.VariableSpace) Properties(java.util.Properties)

Example 3 with VariableSpace

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));
                        }
                    }
                }
            }
        }
    }
}
Also used : FTPFileFactory(com.enterprisedt.net.ftp.FTPFileFactory) VariableSpace(org.pentaho.di.core.variables.VariableSpace) FileObject(org.apache.commons.vfs2.FileObject) FTPFileParser(com.enterprisedt.net.ftp.FTPFileParser) FTPException(com.enterprisedt.net.ftp.FTPException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 4 with VariableSpace

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));
                        }
                    }
                }
            }
        }
    }
}
Also used : MVSFileParser(org.pentaho.di.job.entries.ftp.MVSFileParser) FTPFileFactory(com.enterprisedt.net.ftp.FTPFileFactory) VariableSpace(org.pentaho.di.core.variables.VariableSpace) FTPFileParser(com.enterprisedt.net.ftp.FTPFileParser) FTPException(com.enterprisedt.net.ftp.FTPException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) IOException(java.io.IOException)

Example 5 with VariableSpace

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;
}
Also used : RepositoryDirectoryInterface(org.pentaho.di.repository.RepositoryDirectoryInterface) KettleException(org.pentaho.di.core.exception.KettleException) VariableSpace(org.pentaho.di.core.variables.VariableSpace) CurrentDirectoryResolver(org.pentaho.di.core.util.CurrentDirectoryResolver) KettleException(org.pentaho.di.core.exception.KettleException) UnknownParamException(org.pentaho.di.core.parameters.UnknownParamException)

Aggregations

VariableSpace (org.pentaho.di.core.variables.VariableSpace)49 Test (org.junit.Test)21 Variables (org.pentaho.di.core.variables.Variables)14 KettleException (org.pentaho.di.core.exception.KettleException)12 Repository (org.pentaho.di.repository.Repository)10 TransMeta (org.pentaho.di.trans.TransMeta)10 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)9 RepositoryDirectoryInterface (org.pentaho.di.repository.RepositoryDirectoryInterface)8 StepMeta (org.pentaho.di.trans.step.StepMeta)8 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)7 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)6 IOException (java.io.IOException)5 FileObject (org.apache.commons.vfs2.FileObject)5 CurrentDirectoryResolver (org.pentaho.di.core.util.CurrentDirectoryResolver)5 HashMap (java.util.HashMap)4 Properties (java.util.Properties)4 Matchers.anyString (org.mockito.Matchers.anyString)4 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)4 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)4 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)4