Search in sources :

Example 6 with TopLevelResource

use of org.pentaho.di.resource.TopLevelResource in project pentaho-kettle by pentaho.

the class KitchenCommandExecutor method execute.

public Result execute(Params params, String[] arguments) throws Throwable {
    getLog().logMinimal(BaseMessages.getString(getPkgClazz(), "Kitchen.Log.Starting"));
    logDebug("Kitchen.Log.AllocateNewJob");
    Job job = null;
    // In case we use a repository...
    Repository repository = null;
    try {
        if (getMetaStore() == null) {
            setMetaStore(createDefaultMetastore());
        }
        // Read kettle job specified on command-line?
        if (!Utils.isEmpty(params.getRepoName()) || !Utils.isEmpty(params.getLocalFile())) {
            logDebug("Kitchen.Log.ParsingCommandLine");
            if (!Utils.isEmpty(params.getRepoName()) && !isEnabled(params.getBlockRepoConns())) {
                /**
                 * if set, _trust_user_ needs to be considered. See pur-plugin's:
                 *
                 * @link https://github.com/pentaho/pentaho-kettle/blob/8.0.0.0-R/plugins/pur/core/src/main/java/org/pentaho/di/repository/pur/PurRepositoryConnector.java#L97-L101
                 * @link https://github.com/pentaho/pentaho-kettle/blob/8.0.0.0-R/plugins/pur/core/src/main/java/org/pentaho/di/repository/pur/WebServiceManager.java#L130-L133
                 */
                if (isEnabled(params.getTrustRepoUser())) {
                    System.setProperty("pentaho.repository.client.attemptTrust", YES);
                }
                // In case we use a repository...
                // some commands are to load a Trans from the repo; others are merely to print some repo-related information
                RepositoryMeta repositoryMeta = loadRepositoryConnection(params.getRepoName(), "Kitchen.Log.LoadingRep", "Kitchen.Error.NoRepDefinied", "Kitchen.Log.FindingRep");
                if (repositoryMeta == null) {
                    System.out.println(BaseMessages.getString(getPkgClazz(), "Kitchen.Error.CanNotConnectRep"));
                    return exitWithStatus(CommandExecutorCodes.Kitchen.COULD_NOT_LOAD_JOB.getCode());
                }
                logDebug("Kitchen.Log.CheckUserPass");
                repository = establishRepositoryConnection(repositoryMeta, params.getRepoUsername(), params.getRepoPassword(), RepositoryOperation.EXECUTE_JOB);
                // If so, nothing else is needed ( other than executing the actual requested operation )
                if (isEnabled(params.getListRepoFiles()) || isEnabled(params.getListRepoDirs())) {
                    executeRepositoryBasedCommand(repository, params.getInputDir(), params.getListRepoFiles(), params.getListRepoDirs());
                    return exitWithStatus(CommandExecutorCodes.Kitchen.SUCCESS.getCode());
                }
                job = loadJobFromRepository(repository, params.getInputDir(), params.getInputFile());
            }
            // Try to load if from file
            if (job == null) {
                // Try to load the job from file, even if it failed to load from the repository
                job = loadJobFromFilesystem(params.getLocalInitialDir(), params.getLocalFile(), params.getBase64Zip());
            }
        } else if (isEnabled(params.getListRepos())) {
            // list the repositories placed at repositories.xml
            printRepositories(loadRepositoryInfo("Kitchen.Log.ListRep", "Kitchen.Error.NoRepDefinied"));
        }
    } catch (KettleException e) {
        job = null;
        if (repository != null) {
            repository.disconnect();
        }
        System.out.println(BaseMessages.getString(getPkgClazz(), "Kitchen.Error.StopProcess", e.getMessage()));
    }
    if (job == null) {
        if (!isEnabled(params.getListRepoFiles()) && !isEnabled(params.getListRepoDirs()) && !isEnabled(params.getListRepos())) {
            System.out.println(BaseMessages.getString(getPkgClazz(), "Kitchen.Error.canNotLoadJob"));
        }
        return exitWithStatus(CommandExecutorCodes.Kitchen.COULD_NOT_LOAD_JOB.getCode(), job);
    }
    if (!Utils.isEmpty(params.getExportRepo())) {
        try {
            // Export the resources linked to the currently loaded file...
            TopLevelResource topLevelResource = ResourceUtil.serializeResourceExportInterface(params.getExportRepo(), job.getJobMeta(), job, repository, getMetaStore());
            String launchFile = topLevelResource.getResourceName();
            String message = ResourceUtil.getExplanation(params.getExportRepo(), launchFile, job.getJobMeta());
            System.out.println();
            System.out.println(message);
            // Setting the list parameters option will make kitchen exit below in the parameters section
            (params).setListFileParams(YES);
        } catch (Exception e) {
            System.out.println(Const.getStackTracker(e));
            return exitWithStatus(CommandExecutorCodes.Kitchen.UNEXPECTED_ERROR.getCode());
        }
    }
    Date start = Calendar.getInstance().getTime();
    try {
        // Set the command line arguments on the job ...
        job.setArguments(arguments);
        job.initializeVariablesFrom(null);
        job.setLogLevel(getLog().getLogLevel());
        job.getJobMeta().setInternalKettleVariables(job);
        job.setRepository(repository);
        job.getJobMeta().setRepository(repository);
        job.getJobMeta().setMetaStore(getMetaStore());
        // Map the command line named parameters to the actual named parameters. Skip for
        // the moment any extra command line parameter not known in the job.
        String[] jobParams = job.getJobMeta().listParameters();
        for (String param : jobParams) {
            try {
                String value = params.getNamedParams().getParameterValue(param);
                if (value != null) {
                    job.getJobMeta().setParameterValue(param, value);
                }
            } catch (UnknownParamException e) {
            /* no-op */
            }
        }
        job.copyParametersFrom(job.getJobMeta());
        // Put the parameters over the already defined variable space. Parameters get priority.
        job.activateParameters();
        // Set custom options in the job extension map as Strings
        for (String optionName : params.getCustomNamedParams().listParameters()) {
            try {
                String optionValue = params.getCustomNamedParams().getParameterValue(optionName);
                if (optionName != null && optionValue != null) {
                    job.getExtensionDataMap().put(optionName, optionValue);
                }
            } catch (UnknownParamException e) {
            /* no-op */
            }
        }
        // List the parameters defined in this job, then simply exit...
        if (isEnabled(params.getListFileParams())) {
            printJobParameters(job);
            // same as the other list options
            return exitWithStatus(CommandExecutorCodes.Kitchen.COULD_NOT_LOAD_JOB.getCode());
        }
        // Execute the selected job.
        job.start();
        job.waitUntilFinished();
        // get the execution result
        setResult(job.getResult());
    } finally {
        if (repository != null) {
            repository.disconnect();
        }
        if (isEnabled(params.getTrustRepoUser())) {
            // we set it, now we sanitize it
            System.clearProperty("pentaho.repository.client.attemptTrust");
        }
    }
    getLog().logMinimal(BaseMessages.getString(getPkgClazz(), "Kitchen.Log.Finished"));
    int returnCode = getReturnCode();
    Date stop = Calendar.getInstance().getTime();
    calculateAndPrintElapsedTime(start, stop, "Kitchen.Log.StartStop", "Kitchen.Log.ProcessEndAfter", "Kitchen.Log.ProcessEndAfterLong", "Kitchen.Log.ProcessEndAfterLonger", "Kitchen.Log.ProcessEndAfterLongest");
    getResult().setElapsedTimeMillis(stop.getTime() - start.getTime());
    return exitWithStatus(returnCode);
}
Also used : RepositoryMeta(org.pentaho.di.repository.RepositoryMeta) TopLevelResource(org.pentaho.di.resource.TopLevelResource) KettleException(org.pentaho.di.core.exception.KettleException) Repository(org.pentaho.di.repository.Repository) UnknownParamException(org.pentaho.di.core.parameters.UnknownParamException) Job(org.pentaho.di.job.Job) KettleException(org.pentaho.di.core.exception.KettleException) UnknownParamException(org.pentaho.di.core.parameters.UnknownParamException) ExecutionException(java.util.concurrent.ExecutionException) Date(java.util.Date) KettleExtensionPoint(org.pentaho.di.core.extension.KettleExtensionPoint)

Aggregations

KettleException (org.pentaho.di.core.exception.KettleException)6 TopLevelResource (org.pentaho.di.resource.TopLevelResource)6 FileObject (org.apache.commons.vfs2.FileObject)4 KettleValueException (org.pentaho.di.core.exception.KettleValueException)4 UnknownParamException (org.pentaho.di.core.parameters.UnknownParamException)4 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)4 KettleFileException (org.pentaho.di.core.exception.KettleFileException)3 KettleExtensionPoint (org.pentaho.di.core.extension.KettleExtensionPoint)3 MalformedURLException (java.net.MalformedURLException)2 Date (java.util.Date)2 ExecutionException (java.util.concurrent.ExecutionException)2 FileSystemException (org.apache.commons.vfs2.FileSystemException)2 SWTException (org.eclipse.swt.SWTException)2 FileDialog (org.eclipse.swt.widgets.FileDialog)2 MessageBox (org.eclipse.swt.widgets.MessageBox)2 SlaveServer (org.pentaho.di.cluster.SlaveServer)2 KettleAuthException (org.pentaho.di.core.exception.KettleAuthException)2 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)2 KettleMissingPluginsException (org.pentaho.di.core.exception.KettleMissingPluginsException)2 KettleRowException (org.pentaho.di.core.exception.KettleRowException)2