Search in sources :

Example 1 with BuildVersion

use of org.pentaho.di.version.BuildVersion in project pentaho-kettle by pentaho.

the class Import method main.

public static void main(String[] a) throws KettleException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    KettleEnvironment.init();
    Props.init(Props.TYPE_PROPERTIES_SPOON);
    List<String> args = pickupCmdArguments(a);
    StringBuilder optionRepname, optionUsername, optionPassword, optionDirname;
    StringBuilder optionLimitDir, optionFilename, optionRules, optionComment;
    StringBuilder optionReplace, optionContinueOnError, optionVersion, optionFileDir, optionNoRules;
    CommandLineOption[] options = new CommandLineOption[] { // 
    createOption("rep", "Import.CmdLine.RepName", optionRepname = new StringBuilder()), createOption("user", "Import.CmdLine.RepUsername", optionUsername = new StringBuilder()), createOption("pass", "Import.CmdLine.RepPassword", optionPassword = new StringBuilder()), createOption("dir", "Import.CmdLine.RepDir", optionDirname = new StringBuilder()), createOption("limitdir", "Import.CmdLine.LimitDir", optionLimitDir = new StringBuilder()), createOption("file", "Import.CmdLine.File", optionFilename = new StringBuilder()), createOption("filedir", "Import.CmdLine.FileDir", optionFileDir = new StringBuilder()), createOption("rules", "Import.CmdLine.RulesFile", optionRules = new StringBuilder()), createOption("norules", "Import.CmdLine.NoRules", optionNoRules = new StringBuilder(), true, false), createOption("comment", "Import.CmdLine.Comment", optionComment = new StringBuilder(), false, false), createOption("replace", "Import.CmdLine.Replace", optionReplace = new StringBuilder(), true, false), createOption("coe", "Import.CmdLine.ContinueOnError", optionContinueOnError = new StringBuilder(), true, false), createOption("version", "Import.CmdLine.Version", optionVersion = new StringBuilder(), true, false), new CommandLineOption("", BaseMessages.getString(PKG, "Import.CmdLine.ExtraFiles"), new StringBuilder(), false, true, true) };
    if (args.isEmpty()) {
        CommandLineOption.printUsage(options);
        exitJVM(9);
    }
    final LogChannelInterface log = new LogChannel(STRING_IMPORT);
    CommandLineOption.parseArguments(args, options, log);
    // The arguments that are still left in args are in fact filenames that need to be imported.
    // This list is otherwise empty.
    // To that we add the normal filename option
    // 
    List<String> filenames = new ArrayList<String>(args);
    if (!Utils.isEmpty(optionFilename)) {
        filenames.add(optionFilename.toString());
    }
    String kettleRepname = Const.getEnvironmentVariable("KETTLE_REPOSITORY", null);
    String kettleUsername = Const.getEnvironmentVariable("KETTLE_USER", null);
    String kettlePassword = Const.getEnvironmentVariable("KETTLE_PASSWORD", null);
    if (!Utils.isEmpty(kettleRepname)) {
        optionRepname = new StringBuilder(kettleRepname);
    }
    if (!Utils.isEmpty(kettleUsername)) {
        optionUsername = new StringBuilder(kettleUsername);
    }
    if (!Utils.isEmpty(kettlePassword)) {
        optionPassword = new StringBuilder(kettlePassword);
    }
    if (!Utils.isEmpty(optionVersion)) {
        BuildVersion buildVersion = BuildVersion.getInstance();
        log.logBasic(BaseMessages.getString(PKG, "Import.Log.KettleVersion", buildVersion.getVersion(), buildVersion.getRevision(), buildVersion.getBuildDate()));
        if (a.length == 1) {
            exitJVM(6);
        }
    }
    // 
    if (Utils.isEmpty(optionRepname)) {
        log.logError(BaseMessages.getString(PKG, "Import.Error.NoRepProvided"));
        exitJVM(1);
    }
    if (Utils.isEmpty(filenames)) {
        log.logError(BaseMessages.getString(PKG, "Import.Error.NoExportFileProvided"));
        exitJVM(1);
    }
    if (Utils.isEmpty(optionDirname)) {
        log.logError(BaseMessages.getString(PKG, "Import.Error.NoRepositoryDirectoryProvided"));
        exitJVM(1);
    }
    if (Utils.isEmpty(optionRules) && Utils.isEmpty(optionNoRules) && !"Y".equalsIgnoreCase(optionNoRules.toString())) {
        log.logError(BaseMessages.getString(PKG, "Import.Error.NoRulesFileProvided"));
        exitJVM(1);
    }
    // Load the rules file!
    // 
    ImportRules importRules = new ImportRules();
    String rulesFile = optionRules.toString();
    if (!Utils.isEmpty(rulesFile)) {
        try {
            Document document = XMLHandler.loadXMLFile(rulesFile);
            Node rulesNode = XMLHandler.getSubNode(document, ImportRules.XML_TAG);
            importRules.loadXML(rulesNode);
            log.logMinimal(BaseMessages.getString(PKG, "Import.Log.RulesLoaded", rulesFile, Integer.toString(importRules.getRules().size())));
            for (ImportRuleInterface rule : importRules.getRules()) {
                log.logBasic(" - " + rule.toString());
            }
        } catch (KettleException e) {
            log.logError(BaseMessages.getString(PKG, "Import.Log.ExceptionLoadingRules", rulesFile), e);
            exitJVM(7);
        }
    }
    // Get the list of limiting source directories
    // 
    List<String> limitDirs;
    if (!Utils.isEmpty(optionLimitDir)) {
        String[] directories = optionLimitDir.toString().split(",");
        limitDirs = Arrays.asList(directories);
    } else {
        limitDirs = Collections.emptyList();
    }
    // Find the repository metadata...
    // 
    RepositoriesMeta repsinfo = new RepositoriesMeta();
    repsinfo.getLog().setLogLevel(log.getLogLevel());
    try {
        repsinfo.readData();
    } catch (Exception e) {
        log.logError(BaseMessages.getString(PKG, "Import.Error.UnableToLoadRepositoryInformation"), e);
        exitJVM(1);
    }
    RepositoryMeta repositoryMeta = repsinfo.findRepository(optionRepname.toString());
    if (repositoryMeta == null) {
        log.logError(BaseMessages.getString(PKG, "Import.Error.RepositoryCouldNotBeFound", optionRepname.toString()));
        exitJVM(1);
    }
    if (Utils.isEmpty(optionRepname)) {
        log.logError(BaseMessages.getString(PKG, "Import.Error.NoRepProvided"));
        exitJVM(1);
    }
    // Load the repository object as a plugin...
    // 
    Repository repository = null;
    try {
        repository = PluginRegistry.getInstance().loadClass(RepositoryPluginType.class, repositoryMeta, Repository.class);
        repository.init(repositoryMeta);
        repository.getLog().setLogLevel(log.getLogLevel());
    } catch (Exception e) {
        log.logError(BaseMessages.getString(PKG, "Import.Error.UnableToLoadOrInitializeRepository"));
        exitJVM(1);
    }
    try {
        repository.connect(optionUsername != null ? optionUsername.toString() : null, optionPassword != null ? optionPassword.toString() : null);
    } catch (KettleException ke) {
        log.logError(ke.getMessage());
        exitJVM(1);
    } catch (Exception e) {
        log.logError(BaseMessages.getString(PKG, "Import.Error.UnableToConnectToRepository"));
        exitJVM(1);
    }
    final boolean replace = Utils.isEmpty(optionReplace) ? false : ValueMetaString.convertStringToBoolean(optionReplace.toString());
    final boolean continueOnError = Utils.isEmpty(optionContinueOnError) ? false : ValueMetaString.convertStringToBoolean(optionContinueOnError.toString());
    // Start the import!
    // 
    log.logMinimal(BaseMessages.getString(PKG, "Import.Log.Starting"));
    Date start, stop;
    SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
    start = new Date();
    int returnCode = 0;
    if (ROOT_DIRECTORY.equals(optionDirname.toString())) {
        log.logError(BaseMessages.getString(PKG, "Import.Error.TargetDirectoryIsRootDirectory"));
        exitJVM(1);
    }
    try {
        RepositoryDirectoryInterface tree = repository.loadRepositoryDirectoryTree();
        RepositoryDirectoryInterface targetDirectory = tree.findDirectory(optionDirname.toString());
        if (targetDirectory == null) {
            log.logError(BaseMessages.getString(PKG, "Import.Error.UnableToFindTargetDirectoryInRepository", optionDirname.toString()));
            exitJVM(1);
        }
        // Perform the actual import
        IRepositoryImporter importer = repository.getImporter();
        importer.setImportRules(importRules);
        if (!limitDirs.isEmpty()) {
            if (importer instanceof CanLimitDirs) {
                ((CanLimitDirs) importer).setLimitDirs(limitDirs);
            } else {
                throw new KettleException(BaseMessages.getString(PKG, "Import.CouldntLimitDirs", importer.getClass().getCanonicalName()));
            }
        }
        RepositoryImportFeedbackInterface feedbackInterface = new ImportFeedback(log, continueOnError, replace, reader);
        // Import files in a certain directory
        importer.importAll(feedbackInterface, optionFileDir.toString(), filenames.toArray(new String[filenames.size()]), targetDirectory, replace, continueOnError, optionComment.toString());
        // If the importer has exceptions, then our return code is 2
        List<Exception> exceptions = importer.getExceptions();
        if (exceptions != null && !exceptions.isEmpty()) {
            log.logError(BaseMessages.getString(PKG, "Import.Error.UnexpectedErrorDuringImport"), exceptions.get(0));
            returnCode = 2;
        }
    } catch (Exception e) {
        log.logError(BaseMessages.getString(PKG, "Import.Error.UnexpectedErrorDuringImport"), e);
        exitJVM(2);
    }
    log.logMinimal(BaseMessages.getString(PKG, "Import.Log.Finished"));
    stop = new Date();
    String begin = df.format(start);
    String end = df.format(stop);
    log.logMinimal(BaseMessages.getString(PKG, "Import.Log.StartStop", begin, end));
    long seconds = (stop.getTime() - start.getTime()) / 1000;
    if (seconds <= 60) {
        log.logMinimal(BaseMessages.getString(PKG, "Import.Log.ProcessEndAfter", String.valueOf(seconds)));
    } else if (seconds <= 60 * 60) {
        int min = (int) (seconds / 60);
        int rem = (int) (seconds % 60);
        log.logMinimal(BaseMessages.getString(PKG, "Import.Log.ProcessEndAfterLong", String.valueOf(min), String.valueOf(rem), String.valueOf(seconds)));
    } else if (seconds <= 60 * 60 * 24) {
        int rem;
        int hour = (int) (seconds / (60 * 60));
        rem = (int) (seconds % (60 * 60));
        int min = rem / 60;
        rem = rem % 60;
        log.logMinimal(BaseMessages.getString(PKG, "Import.Log.ProcessEndAfterLonger", String.valueOf(hour), String.valueOf(min), String.valueOf(rem), String.valueOf(seconds)));
    } else {
        int rem;
        int days = (int) (seconds / (60 * 60 * 24));
        rem = (int) (seconds % (60 * 60 * 24));
        int hour = rem / (60 * 60);
        rem = rem % (60 * 60);
        int min = rem / 60;
        rem = rem % 60;
        log.logMinimal(BaseMessages.getString(PKG, "Import.Log.ProcessEndAfterLongest", String.valueOf(days), String.valueOf(hour), String.valueOf(min), String.valueOf(rem), String.valueOf(seconds)));
    }
    exitJVM(returnCode);
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) RepositoryDirectoryInterface(org.pentaho.di.repository.RepositoryDirectoryInterface) RepositoryPluginType(org.pentaho.di.core.plugins.RepositoryPluginType) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) CanLimitDirs(org.pentaho.di.repository.CanLimitDirs) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Document(org.w3c.dom.Document) RepositoryImportFeedbackInterface(org.pentaho.di.repository.RepositoryImportFeedbackInterface) CommandLineOption(org.pentaho.di.pan.CommandLineOption) RepositoryMeta(org.pentaho.di.repository.RepositoryMeta) BuildVersion(org.pentaho.di.version.BuildVersion) RepositoriesMeta(org.pentaho.di.repository.RepositoriesMeta) InputStreamReader(java.io.InputStreamReader) ImportRuleInterface(org.pentaho.di.imp.rule.ImportRuleInterface) LogChannel(org.pentaho.di.core.logging.LogChannel) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException) Date(java.util.Date) IRepositoryImporter(org.pentaho.di.repository.IRepositoryImporter) Repository(org.pentaho.di.repository.Repository) BufferedReader(java.io.BufferedReader) LogChannelInterface(org.pentaho.di.core.logging.LogChannelInterface) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with BuildVersion

use of org.pentaho.di.version.BuildVersion in project pentaho-kettle by pentaho.

the class Log4jKettleLayout method format.

public String format(LoggingEvent event) {
    // OK, perhaps the logging information has multiple lines of data.
    // We need to split this up into different lines and all format these
    // lines...
    // 
    StringBuffer line = new StringBuffer();
    String dateTimeString = "";
    if (timeAdded) {
        dateTimeString = LOCAL_SIMPLE_DATE_PARSER.get().format(new Date(event.timeStamp)) + " - ";
    }
    Object object = event.getMessage();
    if (object instanceof LogMessage) {
        LogMessage message = (LogMessage) object;
        String[] parts = message.getMessage().split(Const.CR);
        for (int i = 0; i < parts.length; i++) {
            // Start every line of the output with a dateTimeString
            line.append(dateTimeString);
            // Include the subject too on every line...
            if (message.getSubject() != null) {
                line.append(message.getSubject());
                if (message.getCopy() != null) {
                    line.append(".").append(message.getCopy());
                }
                line.append(" - ");
            }
            if (message.isError()) {
                BuildVersion buildVersion = BuildVersion.getInstance();
                line.append(ERROR_STRING);
                line.append(" (version ");
                line.append(buildVersion.getVersion());
                if (!Utils.isEmpty(buildVersion.getRevision())) {
                    line.append(", build ");
                    line.append(buildVersion.getRevision());
                }
                if (!Utils.isEmpty(buildVersion.getBuildDate())) {
                    line.append(" from ");
                    line.append(buildVersion.getBuildDate());
                }
                if (!Utils.isEmpty(buildVersion.getBuildUser())) {
                    line.append(" by ");
                    line.append(buildVersion.getBuildUser());
                }
                line.append(") : ");
            }
            line.append(parts[i]);
            if (i < parts.length - 1) {
                // put the CR's back in there!
                line.append(Const.CR);
            }
        }
    } else {
        line.append(dateTimeString);
        line.append((object != null ? object.toString() : "<null>"));
    }
    return line.toString();
}
Also used : BuildVersion(org.pentaho.di.version.BuildVersion) Date(java.util.Date)

Example 3 with BuildVersion

use of org.pentaho.di.version.BuildVersion in project pentaho-kettle by pentaho.

the class KettleLogLayout method format.

public String format(KettleLoggingEvent event) {
    // OK, perhaps the logging information has multiple lines of data.
    // We need to split this up into different lines and all format these
    // lines...
    // 
    StringBuilder line = new StringBuilder();
    String dateTimeString = "";
    if (timeAdded) {
        dateTimeString = LOCAL_SIMPLE_DATE_PARSER.get().format(new Date(event.timeStamp)) + " - ";
    }
    Object object = event.getMessage();
    if (object instanceof LogMessage) {
        LogMessage message = (LogMessage) object;
        String[] parts = message.getMessage() == null ? new String[] {} : message.getMessage().split(Const.CR);
        for (int i = 0; i < parts.length; i++) {
            // Start every line of the output with a dateTimeString
            line.append(dateTimeString);
            // Include the subject too on every line...
            if (message.getSubject() != null) {
                line.append(message.getSubject());
                if (message.getCopy() != null) {
                    line.append(".").append(message.getCopy());
                }
                line.append(" - ");
            }
            if (i == 0 && message.isError()) {
                BuildVersion buildVersion = BuildVersion.getInstance();
                line.append(ERROR_STRING);
                line.append(" (version ");
                line.append(buildVersion.getVersion());
                if (!Utils.isEmpty(buildVersion.getRevision())) {
                    line.append(", build ");
                    line.append(buildVersion.getRevision());
                }
                if (!Utils.isEmpty(buildVersion.getBuildDate())) {
                    line.append(" from ");
                    line.append(buildVersion.getBuildDate());
                }
                if (!Utils.isEmpty(buildVersion.getBuildUser())) {
                    line.append(" by ");
                    line.append(buildVersion.getBuildUser());
                }
                line.append(") : ");
            }
            line.append(parts[i]);
            if (i < parts.length - 1) {
                // put the CR's back in there!
                line.append(Const.CR);
            }
        }
    } else {
        line.append(dateTimeString);
        line.append((object != null ? object.toString() : "<null>"));
    }
    return line.toString();
}
Also used : BuildVersion(org.pentaho.di.version.BuildVersion) Date(java.util.Date)

Example 4 with BuildVersion

use of org.pentaho.di.version.BuildVersion in project pentaho-kettle by pentaho.

the class AbstractBaseCommandExecutor method printVersion.

protected void printVersion(String kettleVersionMsgTkn) {
    BuildVersion buildVersion = BuildVersion.getInstance();
    getLog().logBasic(BaseMessages.getString(getPkgClazz(), kettleVersionMsgTkn, buildVersion.getVersion(), buildVersion.getRevision(), buildVersion.getBuildDate()));
}
Also used : BuildVersion(org.pentaho.di.version.BuildVersion)

Aggregations

BuildVersion (org.pentaho.di.version.BuildVersion)4 Date (java.util.Date)3 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 KettleException (org.pentaho.di.core.exception.KettleException)1 LogChannel (org.pentaho.di.core.logging.LogChannel)1 LogChannelInterface (org.pentaho.di.core.logging.LogChannelInterface)1 RepositoryPluginType (org.pentaho.di.core.plugins.RepositoryPluginType)1 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)1 ImportRuleInterface (org.pentaho.di.imp.rule.ImportRuleInterface)1 CommandLineOption (org.pentaho.di.pan.CommandLineOption)1 CanLimitDirs (org.pentaho.di.repository.CanLimitDirs)1 IRepositoryImporter (org.pentaho.di.repository.IRepositoryImporter)1 RepositoriesMeta (org.pentaho.di.repository.RepositoriesMeta)1 Repository (org.pentaho.di.repository.Repository)1 RepositoryDirectoryInterface (org.pentaho.di.repository.RepositoryDirectoryInterface)1 RepositoryImportFeedbackInterface (org.pentaho.di.repository.RepositoryImportFeedbackInterface)1