Search in sources :

Example 1 with ConnectionHolder

use of org.talend.components.salesforce.runtime.common.ConnectionHolder in project components by Talend.

the class SalesforceSourceOrSinkTestIT method prepareAndGetConnectionHolder.

private void prepareAndGetConnectionHolder(boolean isBulk) throws Exception {
    final SalesforceConnectionProperties connectionProperties = setupProps(null, false);
    connectionProperties.bulkConnection.setValue(isBulk);
    RuntimeContainer container = new DefaultComponentRuntimeContainerImpl() {

        @Override
        public String getCurrentComponentId() {
            return connectionProperties.getName();
        }
    };
    SalesforceSourceOrSink sourceOrSink = new SalesforceSourceOrSink();
    sourceOrSink.initialize(container, connectionProperties);
    // Creating and saving PartnerConnection + BulkConnection(holder) inside container.
    sourceOrSink.validate(container);
    PartnerConnection partnerConnection = (PartnerConnection) container.getComponentData(connectionProperties.getName(), SalesforceSourceOrSink.KEY_CONNECTION);
    TSalesforceInputProperties inputProperties = new TSalesforceInputProperties("input");
    inputProperties.connection.referencedComponent.componentInstanceId.setValue(connectionProperties.getName());
    sourceOrSink.initialize(container, inputProperties);
    ConnectionHolder actualHolder = sourceOrSink.connect(container);
    Assert.assertEquals(partnerConnection, actualHolder.connection);
    if (isBulk) {
        Assert.assertNotNull(actualHolder.bulkConnection);
    } else {
        // Check if bulk connection was not chosen but created.
        Assert.assertNull(actualHolder.bulkConnection);
    }
}
Also used : SalesforceConnectionProperties(org.talend.components.salesforce.SalesforceConnectionProperties) DefaultComponentRuntimeContainerImpl(org.talend.components.api.container.DefaultComponentRuntimeContainerImpl) PartnerConnection(com.sforce.soap.partner.PartnerConnection) TSalesforceInputProperties(org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties) RuntimeContainer(org.talend.components.api.container.RuntimeContainer) ConnectionHolder(org.talend.components.salesforce.runtime.common.ConnectionHolder)

Example 2 with ConnectionHolder

use of org.talend.components.salesforce.runtime.common.ConnectionHolder in project components by Talend.

the class SalesforceSessionReuseTestIT method invalidSession.

protected void invalidSession(SalesforceConnectionProperties props, String sessionId) throws Exception {
    SalesforceSourceOrSink sourceOrSink = new SalesforceSourceOrSink();
    sourceOrSink.initialize(null, props);
    ConnectionHolder connectionHolder = sourceOrSink.connect(null);
    assertNotNull(connectionHolder.connection);
    if (sessionId != null) {
        connectionHolder.connection.invalidateSessions(new String[] { sessionId });
    }
    connectionHolder.connection.logout();
    LOGGER.debug("session \"" + connectionHolder.connection.getConfig().getSessionId() + "\" invalided!");
}
Also used : ConnectionHolder(org.talend.components.salesforce.runtime.common.ConnectionHolder)

Example 3 with ConnectionHolder

use of org.talend.components.salesforce.runtime.common.ConnectionHolder in project components by Talend.

the class SalesforceSourceOrSink method connect.

protected ConnectionHolder connect(RuntimeContainer container) throws IOException {
    SalesforceRuntimeCommon.enableTLSv11AndTLSv12ForJava7();
    final ConnectionHolder ch = new ConnectionHolder();
    SalesforceConnectionProperties connProps = properties.getConnectionProperties();
    String refComponentId = connProps.getReferencedComponentId();
    // Using another component's connection
    if (refComponentId != null) {
        // In a runtime container
        if (container != null) {
            // PartnerConnection must be created. BulkConnection is optional.
            PartnerConnection connection = (PartnerConnection) container.getComponentData(refComponentId, KEY_CONNECTION);
            if (connection == null) {
                throw new IOException("Referenced component: " + refComponentId + " not connected");
            }
            ch.connection = connection;
            ch.bulkConnection = (BulkConnection) container.getComponentData(refComponentId, KEY_CONNECTION_BULK);
            return ch;
        }
        // Design time
        connProps = connProps.getReferencedConnectionProperties();
    }
    ConnectorConfig config = new ConnectorConfig();
    config.setUsername(StringUtils.strip(connProps.userPassword.userId.getStringValue(), "\""));
    String password = StringUtils.strip(connProps.userPassword.password.getStringValue(), "\"");
    String securityKey = StringUtils.strip(connProps.userPassword.securityKey.getStringValue(), "\"");
    if (StringUtils.isNotEmpty(securityKey)) {
        password = password + securityKey;
    }
    config.setPassword(password);
    setProxy(config);
    // Notes on how to test this
    // http://thysmichels.com/2014/02/15/salesforce-wsc-partner-connection-session-renew-when-session-timeout/
    config.setSessionRenewer(new SessionRenewer() {

        @Override
        public SessionRenewalHeader renewSession(ConnectorConfig connectorConfig) throws ConnectionException {
            LOG.debug("renewing session...");
            SessionRenewalHeader header = new SessionRenewalHeader();
            connectorConfig.setSessionId(null);
            PartnerConnection connection = doConnection(connectorConfig, true);
            // update the connection session header
            ch.connection.setSessionHeader(connection.getSessionHeader().getSessionId());
            header.name = new QName("urn:partner.soap.sforce.com", "SessionHeader");
            header.headerElement = connection.getSessionHeader();
            LOG.debug("session renewed!");
            return header;
        }
    });
    if (connProps.timeout.getValue() > 0) {
        config.setConnectionTimeout(connProps.timeout.getValue());
    }
    config.setCompression(connProps.needCompression.getValue());
    config.setUseChunkedPost(connProps.httpChunked.getValue());
    config.setValidateSchema(false);
    try {
        // Get session from session file or new connection
        if (isReuseSession()) {
            Properties properties = getSessionProperties();
            if (properties != null) {
                this.sessionId = properties.getProperty(SalesforceConstant.SESSION_ID);
                this.serviceEndPoint = properties.getProperty(SalesforceConstant.SERVICE_ENDPOINT);
            }
        }
        if (this.sessionId != null && this.serviceEndPoint != null) {
            ch.connection = doConnection(config, false);
        } else {
            ch.connection = doConnection(config, true);
        }
        if (ch.connection != null) {
            String clientId = connProps.clientId.getStringValue();
            if (clientId != null) {
                // Need the test.
                ch.connection.setCallOptions(clientId, null);
            }
        }
        if (connProps.bulkConnection.getValue()) {
            ch.bulkConnection = connectBulk(ch.connection.getConfig());
        }
        if (container != null) {
            container.setComponentData(container.getCurrentComponentId(), KEY_CONNECTION, ch.connection);
            if (ch.bulkConnection != null) {
                container.setComponentData(container.getCurrentComponentId(), KEY_CONNECTION_BULK, ch.bulkConnection);
            }
        }
        return ch;
    } catch (ConnectionException e) {
        throw new IOException(e);
    }
}
Also used : SessionRenewer(com.sforce.ws.SessionRenewer) SalesforceConnectionProperties(org.talend.components.salesforce.SalesforceConnectionProperties) PartnerConnection(com.sforce.soap.partner.PartnerConnection) ConnectorConfig(com.sforce.ws.ConnectorConfig) QName(javax.xml.namespace.QName) IOException(java.io.IOException) SalesforceProvideConnectionProperties(org.talend.components.salesforce.SalesforceProvideConnectionProperties) Properties(java.util.Properties) ComponentProperties(org.talend.components.api.properties.ComponentProperties) SalesforceConnectionProperties(org.talend.components.salesforce.SalesforceConnectionProperties) ConnectionException(com.sforce.ws.ConnectionException) ConnectionHolder(org.talend.components.salesforce.runtime.common.ConnectionHolder)

Example 4 with ConnectionHolder

use of org.talend.components.salesforce.runtime.common.ConnectionHolder in project components by Talend.

the class SalesforceWriter method open.

@Override
public void open(String uId) throws IOException {
    this.uId = uId;
    ConnectionHolder ch = sink.connect(container);
    connection = ch.connection;
    if (ch.bulkConnection != null) {
        LOGGER.info(MESSAGES.getMessage("info.bulkConnectionUsage"));
    }
    if (null == mainSchema) {
        mainSchema = sprops.module.main.schema.getValue();
        moduleSchema = sink.getSchema(connection, sprops.module.moduleName.getStringValue());
        if (AvroUtils.isIncludeAllFields(mainSchema)) {
            mainSchema = moduleSchema;
        }
    // else schema is fully specified
    }
    upsertKeyColumn = sprops.upsertKeyColumn.getStringValue();
    if (!StringUtils.isEmpty(sprops.logFileName.getValue())) {
        logWriter = new BufferedWriter(new FileWriter(sprops.logFileName.getValue()));
    }
}
Also used : FileWriter(java.io.FileWriter) ConnectionHolder(org.talend.components.salesforce.runtime.common.ConnectionHolder) BufferedWriter(java.io.BufferedWriter)

Example 5 with ConnectionHolder

use of org.talend.components.salesforce.runtime.common.ConnectionHolder in project components by Talend.

the class SalesforceBulkQueryReader method start.

@Override
public boolean start() throws IOException {
    try {
        if (bulkRuntime == null) {
            ConnectionHolder connectionHolder = ((SalesforceDataprepSource) getCurrentSource()).getConnectionHolder();
            bulkRuntime = new SalesforceBulkRuntime(connectionHolder.bulkConnection);
        }
        executeSalesforceBulkQuery();
        bulkResultSet = bulkRuntime.getQueryResultSet(bulkRuntime.nextResultId());
        currentRecord = bulkResultSet.next();
        boolean start = currentRecord != null;
        if (start) {
            dataCount++;
        }
        return start;
    } catch (ConnectionException | AsyncApiException e) {
        // Wrap the exception in an IOException.
        throw new IOException(e);
    }
}
Also used : SalesforceBulkRuntime(org.talend.components.salesforce.runtime.SalesforceBulkRuntime) IOException(java.io.IOException) ConnectionException(com.sforce.ws.ConnectionException) AsyncApiException(com.sforce.async.AsyncApiException) ConnectionHolder(org.talend.components.salesforce.runtime.common.ConnectionHolder)

Aggregations

ConnectionHolder (org.talend.components.salesforce.runtime.common.ConnectionHolder)9 PartnerConnection (com.sforce.soap.partner.PartnerConnection)5 ConnectionException (com.sforce.ws.ConnectionException)4 IOException (java.io.IOException)4 ConnectorConfig (com.sforce.ws.ConnectorConfig)2 SessionRenewer (com.sforce.ws.SessionRenewer)2 QName (javax.xml.namespace.QName)2 RuntimeContainer (org.talend.components.api.container.RuntimeContainer)2 SalesforceConnectionProperties (org.talend.components.salesforce.SalesforceConnectionProperties)2 AsyncApiException (com.sforce.async.AsyncApiException)1 DescribeSObjectResult (com.sforce.soap.partner.DescribeSObjectResult)1 Field (com.sforce.soap.partner.Field)1 BufferedWriter (java.io.BufferedWriter)1 FileWriter (java.io.FileWriter)1 Calendar (java.util.Calendar)1 Properties (java.util.Properties)1 Schema (org.apache.avro.Schema)1 GenericData (org.apache.avro.generic.GenericData)1 Test (org.junit.Test)1 DefaultComponentRuntimeContainerImpl (org.talend.components.api.container.DefaultComponentRuntimeContainerImpl)1