Search in sources :

Example 1 with Version

use of org.knime.core.util.Version in project knime-core by knime.

the class DatabaseConnectionSettings method loadConnection.

private boolean loadConnection(final ConfigRO settings, final boolean write, final CredentialsProvider cp) throws InvalidSettingsException {
    if (settings == null) {
        throw new InvalidSettingsException("Connection settings not available!");
    }
    final Version knimeVersion = new Version(settings.getString("knimeVersion", LEGACY_VERSION.toString()));
    String driver = settings.getString("driver");
    String jdbcUrl = settings.getString("database");
    String user = "";
    String password = null;
    String credName = null;
    String timezone = settings.getString("timezone", "none");
    boolean validateConnection = settings.getBoolean("validateConnection", false);
    boolean retrieveMetadataInConfigure = settings.getBoolean("retrieveMetadataInConfigure", true);
    boolean allowSpacesInColumnNames = settings.getBoolean("allowSpacesInColumnNames", false);
    boolean rowIdsStartWithZero = settings.getBoolean("rowIdsStartWithZero", false);
    boolean kerberos = settings.getBoolean("kerberos", false);
    boolean useCredential = settings.containsKey("credential_name");
    if (useCredential) {
        credName = settings.getString("credential_name");
        if (cp != null) {
            try {
                ICredentials cred = cp.get(credName);
                user = cred.getLogin();
                password = cred.getPassword();
                if (password == null) {
                    LOGGER.warn("Credentials/Password has not been set, using empty password.");
                }
            } catch (IllegalArgumentException e) {
                if (!write) {
                    throw new InvalidSettingsException(e.getMessage());
                }
            }
        }
    } else {
        user = settings.getString("user");
        if (USER_INDICATOR.equals(user) && settings.containsKey(CFG_USER_NAME)) {
            // this is the new user name setting starting with KNIME 3.4.1 and no flow variables where used to set
            // the old 'user' setting
            user = settings.getString(CFG_USER_NAME);
        }
        final String pw = settings.getString("password", null);
        if (pw != null) {
            // these settings where either created prior KNIME 3.4 or flow variables are used to set the password
            try {
                password = KnimeEncryption.decrypt(pw);
            } catch (Exception e) {
                LOGGER.error("Password could not be decrypted, reason: " + e.getMessage());
            }
        } else {
            password = settings.getPassword("passwordEncrypted", ";Op5~pK{31AIN^eH~Ab`:YaiKM8CM`8_Dw:1Kl4_WHrvuAXO", "");
        }
    }
    final String dbIdentifier = settings.getString("databaseIdentifier", null);
    // write settings or skip it
    if (write) {
        m_driver = driver;
        DRIVER_ORDER.add(m_driver);
        boolean changed = false;
        if (useCredential) {
            changed = (m_credName != null) && (credName != null) && credName.equals(m_credName);
            m_credName = credName;
        } else {
            if ((m_user != null) && (m_jdbcUrl != null) && (m_pass != null)) {
                if (!m_user.equals(user) || !m_jdbcUrl.equals(jdbcUrl) || !m_pass.equals(password)) {
                    changed = true;
                }
            }
            m_credName = null;
        }
        m_user = user;
        m_pass = (password == null ? "" : password);
        m_jdbcUrl = jdbcUrl;
        m_timezone = timezone;
        m_validateConnection = validateConnection;
        m_retrieveMetadataInConfigure = retrieveMetadataInConfigure;
        m_allowSpacesInColumnNames = allowSpacesInColumnNames;
        m_rowIdsStartWithZero = rowIdsStartWithZero;
        m_dbIdentifier = dbIdentifier;
        m_kerberos = kerberos;
        m_knimeVersion = knimeVersion;
        DATABASE_URLS.add(m_jdbcUrl);
        return changed;
    }
    return false;
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) Version(org.knime.core.util.Version) ICredentials(org.knime.core.node.workflow.ICredentials) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) SQLException(java.sql.SQLException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 2 with Version

use of org.knime.core.util.Version in project knime-core by knime.

the class BugAP7982_FutureKNIMEVersion_AllCompatible method loadWorkflow.

/**
 * Load workflow, expect no errors.
 */
@Test
public void loadWorkflow() throws Exception {
    File wkfDir = getDefaultWorkflowDirectory();
    WorkflowLoadResult loadWorkflow = loadWorkflow(wkfDir, new ExecutionMonitor(), new WorkflowLoadHelper(wkfDir) {

        @Override
        public UnknownKNIMEVersionLoadPolicy getUnknownKNIMEVersionLoadPolicy(final LoadVersion workflowKNIMEVersion, final Version createdByKNIMEVersion, final boolean isNightlyBuild) {
            throw new AssertionFailedError("Not to be called - workflow is expected to be compatible");
        }
    });
    setManager(loadWorkflow.getWorkflowManager());
    assertThat("Expected to loaded without errors", loadWorkflow.getType(), is(LoadResultEntryType.Ok));
    assertThat("Workflow version incorrect", getManager().getLoadVersion(), is(LoadVersion.V280));
}
Also used : LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion) Version(org.knime.core.util.Version) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion) AssertionFailedError(junit.framework.AssertionFailedError) File(java.io.File) WorkflowLoadResult(org.knime.core.node.workflow.WorkflowPersistor.WorkflowLoadResult) Test(org.junit.Test)

Example 3 with Version

use of org.knime.core.util.Version in project knime-core by knime.

the class BugAP7982_FutureKNIMEVersion_FutureVersion method loadWorkflow.

private WorkflowLoadResult loadWorkflow(final boolean tryToLoadInsteadOfFail) throws Exception {
    File wkfDir = getDefaultWorkflowDirectory();
    WorkflowLoadResult loadWorkflow = loadWorkflow(wkfDir, new ExecutionMonitor(), new WorkflowLoadHelper(wkfDir) {

        @Override
        public UnknownKNIMEVersionLoadPolicy getUnknownKNIMEVersionLoadPolicy(final LoadVersion workflowKNIMEVersion, final Version createdByKNIMEVersion, final boolean isNightlyBuild) {
            assertThat("Unexpected KNIME version in file", workflowKNIMEVersion, is(LoadVersion.FUTURE));
            assertThat("Nightly flag wrong", isNightlyBuild, is(m_isExpectNightly));
            if (tryToLoadInsteadOfFail) {
                return UnknownKNIMEVersionLoadPolicy.Try;
            } else {
                return UnknownKNIMEVersionLoadPolicy.Abort;
            }
        }
    });
    return loadWorkflow;
}
Also used : LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion) Version(org.knime.core.util.Version) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion) File(java.io.File) WorkflowLoadResult(org.knime.core.node.workflow.WorkflowPersistor.WorkflowLoadResult)

Example 4 with Version

use of org.knime.core.util.Version in project knime-core by knime.

the class WorkflowLoadHelper method createTemplateLoadPersistor.

/**
 * Create persistor for a workflow or template.
 * @noreference Clients should only be required to load projects using
 * {@link WorkflowManager#loadProject(File, ExecutionMonitor, WorkflowLoadHelper)}
 * @param directory The directory to load from
 * @param templateSourceURI URI of the link to the template (will load a template and when the template is
 * instantiated and put into the workflow a link is created)
 * @return The persistor
 * @throws IOException If an IO error occured
 * @throws UnsupportedWorkflowVersionException If the workflow is of an unsupported version
 */
public final TemplateNodeContainerPersistor createTemplateLoadPersistor(final File directory, final URI templateSourceURI) throws IOException, UnsupportedWorkflowVersionException {
    if (directory == null) {
        throw new NullPointerException("Arguments must not be null.");
    }
    String fileName = getDotKNIMEFileName();
    if (!directory.isDirectory() || !directory.canRead()) {
        throw new IOException("Can't read metanode/template directory " + directory);
    }
    // template.knime or workflow.knime
    ReferencedFile dotKNIMERef = new ReferencedFile(new ReferencedFile(directory), fileName);
    File dotKNIME = dotKNIMERef.getFile();
    if (!dotKNIME.isFile()) {
        throw new IOException("No \"" + fileName + "\" file in directory \"" + directory.getAbsolutePath() + "\"");
    }
    NodeSettingsRO settings = NodeSettings.loadFromXML(new BufferedInputStream(new FileInputStream(dotKNIME)));
    // CeBIT 2006 version did not contain a version string.
    String versionString;
    if (settings.containsKey(WorkflowLoadHelper.CFG_VERSION)) {
        try {
            versionString = settings.getString(WorkflowLoadHelper.CFG_VERSION);
        } catch (InvalidSettingsException e) {
            throw new IOException("Can't read version number from \"" + dotKNIME.getAbsolutePath() + "\"", e);
        }
    } else {
        versionString = "0.9.0";
    }
    // might also be FUTURE
    LoadVersion version = FileWorkflowPersistor.parseVersion(versionString);
    boolean isSetDirtyAfterLoad = false;
    StringBuilder versionDetails = new StringBuilder(versionString);
    String createdBy = settings.getString(WorkflowLoadHelper.CFG_CREATED_BY, null);
    Version createdByVersion = null;
    if (createdBy != null) {
        versionDetails.append(" (created by KNIME ").append(createdBy).append(")");
        try {
            createdByVersion = new Version(createdBy);
        } catch (IllegalArgumentException e) {
            // ideally this goes into the 'LoadResult' but it's not instantiated yet
            LOGGER.warn(String.format("Unable to parse version string \"%s\" (file \"%s\"): %s", createdBy, dotKNIME.getAbsolutePath(), e.getMessage()), e);
        }
    }
    // added 3.5.0
    boolean isNightly = settings.getBoolean(CFG_NIGHTLY, false);
    boolean isRunningNightly = KNIMEConstants.isNightlyBuild();
    boolean isFutureWorkflow = createdByVersion != null && !new Version(KNIMEConstants.VERSION).isSameOrNewer(createdByVersion);
    if (version == LoadVersion.FUTURE || isFutureWorkflow || (!isRunningNightly && isNightly)) {
        switch(getUnknownKNIMEVersionLoadPolicy(version, createdByVersion, isNightly)) {
            case Abort:
                StringBuilder e = new StringBuilder("Unable to load ");
                e.append(isTemplateFlow() ? "template, " : "workflow, ");
                if (version == LoadVersion.FUTURE || isFutureWorkflow) {
                    e.append("it was created with a future version of KNIME (").append(createdBy).append("). ");
                    e.append("You are running ").append(KNIMEConstants.VERSION).append(".");
                } else {
                    e.append("it was created with a nightly build of KNIME (version ").append(createdBy).append("). ");
                    e.append("You are running ").append(KNIMEConstants.VERSION).append(".");
                }
                throw new UnsupportedWorkflowVersionException(e.toString());
            default:
                isSetDirtyAfterLoad = true;
        }
    } else if (version.isOlderThan(LoadVersion.V200)) {
        LOGGER.warn("The current KNIME version (" + KNIMEConstants.VERSION + ") is different from the one that " + "created the workflow (" + version + ") you are trying to load. In some rare cases, it  " + "might not be possible to load all data or some nodes can't be configured. " + "Please re-configure and/or re-execute these nodes.");
    }
    final MetaNodeTemplateInformation templateInfo;
    if (isTemplateFlow() && templateSourceURI != null) {
        try {
            templateInfo = MetaNodeTemplateInformation.load(settings, version);
            CheckUtils.checkSetting(Role.Template.equals(templateInfo.getRole()), "Role is not '%s' but '%s'", Role.Template, templateInfo.getRole());
        } catch (InvalidSettingsException e) {
            throw new IOException(String.format("Attempting to load template from \"%s\" but can't locate template information: %s", dotKNIME.getAbsolutePath(), e.getMessage()), e);
        }
    } else if (isTemplateFlow()) {
        // LOGGER.coding("Supposed to instantiate a template but the link URI is not set");
        // metanode template from node repository
        templateInfo = null;
    } else {
        templateInfo = null;
    }
    final TemplateNodeContainerPersistor persistor;
    // TODO only create new hash map if workflow is a project?
    HashMap<Integer, ContainerTable> tableRep = new GlobalTableRepository();
    WorkflowFileStoreHandlerRepository fileStoreHandlerRepository = new WorkflowFileStoreHandlerRepository();
    // ordinary workflow is loaded
    if (templateInfo == null) {
        persistor = new FileWorkflowPersistor(tableRep, fileStoreHandlerRepository, dotKNIMERef, this, version, !isTemplateFlow());
    } else {
        // some template is loaded
        switch(templateInfo.getNodeContainerTemplateType()) {
            case MetaNode:
                final ReferencedFile workflowDotKNIME;
                if (version.isOlderThan(LoadVersion.V2100)) {
                    // before 2.10 everything was stored in template.knime
                    workflowDotKNIME = dotKNIMERef;
                } else {
                    workflowDotKNIME = new ReferencedFile(dotKNIMERef.getParent(), WorkflowPersistor.WORKFLOW_FILE);
                }
                persistor = new FileWorkflowPersistor(tableRep, fileStoreHandlerRepository, workflowDotKNIME, this, version, !isTemplateFlow());
                break;
            case SubNode:
                final ReferencedFile settingsDotXML = new ReferencedFile(dotKNIMERef.getParent(), SingleNodeContainerPersistor.SETTINGS_FILE_NAME);
                persistor = new FileSubNodeContainerPersistor(settingsDotXML, this, version, tableRep, fileStoreHandlerRepository, true);
                break;
            default:
                throw new IllegalStateException("Unsupported template type");
        }
    }
    if (templateInfo != null) {
        persistor.setOverwriteTemplateInformation(templateInfo.createLink(templateSourceURI));
        persistor.setNameOverwrite(directory.getName());
    }
    if (isSetDirtyAfterLoad) {
        persistor.setDirtyAfterLoad();
    }
    return persistor;
}
Also used : WorkflowFileStoreHandlerRepository(org.knime.core.data.filestore.internal.WorkflowFileStoreHandlerRepository) ReferencedFile(org.knime.core.internal.ReferencedFile) ContainerTable(org.knime.core.data.container.ContainerTable) BufferedInputStream(java.io.BufferedInputStream) LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion) Version(org.knime.core.util.Version) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) LoadVersion(org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File)

Aggregations

Version (org.knime.core.util.Version)4 File (java.io.File)3 LoadVersion (org.knime.core.node.workflow.FileWorkflowPersistor.LoadVersion)3 IOException (java.io.IOException)2 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2 WorkflowLoadResult (org.knime.core.node.workflow.WorkflowPersistor.WorkflowLoadResult)2 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 InvalidKeyException (java.security.InvalidKeyException)1 SQLException (java.sql.SQLException)1 BadPaddingException (javax.crypto.BadPaddingException)1 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)1 AssertionFailedError (junit.framework.AssertionFailedError)1 Test (org.junit.Test)1 ContainerTable (org.knime.core.data.container.ContainerTable)1 WorkflowFileStoreHandlerRepository (org.knime.core.data.filestore.internal.WorkflowFileStoreHandlerRepository)1 ReferencedFile (org.knime.core.internal.ReferencedFile)1 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)1 ICredentials (org.knime.core.node.workflow.ICredentials)1