Search in sources :

Example 6 with ConnectionHolder

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

the class SalesforceReader method getConnection.

protected PartnerConnection getConnection() throws IOException {
    if (connection == null) {
        ConnectionHolder ch = ((SalesforceSource) getCurrentSource()).connect(container);
        connection = ch.connection;
        if (ch.bulkConnection != null) {
            LOGGER.info(MESSAGES.getMessage("info.bulkConnectionUsage"));
        }
    }
    return connection;
}
Also used : ConnectionHolder(org.talend.components.salesforce.runtime.common.ConnectionHolder)

Example 7 with ConnectionHolder

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

the class SalesforceServerTimeStampReader method start.

@Override
public boolean start() throws IOException {
    ConnectionHolder ch = ((SalesforceSource) getCurrentSource()).connect(container);
    PartnerConnection connection = ch.connection;
    if (ch.bulkConnection != null) {
        LOGGER.info(MESSAGES.getMessage("info.bulkConnectionUsage"));
    }
    try {
        Calendar serverTimestamp = connection.getServerTimestamp().getTimestamp();
        if (serverTimestamp != null) {
            long timestamp = serverTimestamp.getTimeInMillis();
            result = new GenericData.Record(properties.schema.schema.getValue());
            result.put(0, timestamp);
        }
        if (result != null) {
            dataCount++;
            return true;
        } else {
            return false;
        }
    } catch (ConnectionException e) {
        throw new IOException(e);
    }
}
Also used : PartnerConnection(com.sforce.soap.partner.PartnerConnection) Calendar(java.util.Calendar) IOException(java.io.IOException) GenericData(org.apache.avro.generic.GenericData) ConnectionException(com.sforce.ws.ConnectionException) ConnectionHolder(org.talend.components.salesforce.runtime.common.ConnectionHolder)

Example 8 with ConnectionHolder

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

the class SalesforceDataprepSource method connect.

ConnectionHolder connect(RuntimeContainer container) throws IOException {
    SalesforceRuntimeCommon.enableTLSv11AndTLSv12ForJava7();
    final ConnectionHolder ch = new ConnectionHolder();
    ConnectorConfig config = new ConnectorConfig();
    config.setUsername(datastore.userId.getValue());
    String password = datastore.password.getValue();
    String securityKey = datastore.securityKey.getValue();
    if (!StringUtils.isEmpty(securityKey)) {
        password = password + securityKey;
    }
    config.setPassword(password);
    // 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);
            // 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;
        }
    });
    config.setConnectionTimeout(timeout);
    // This should only be false when doing debugging.
    config.setCompression(true);
    config.setUseChunkedPost(true);
    config.setValidateSchema(false);
    try {
        ch.connection = doConnection(config);
        ch.bulkConnection = connectBulk(ch.connection.getConfig());
        return ch;
    } catch (ConnectionException e) {
        throw new IOException(e);
    }
}
Also used : SessionRenewer(com.sforce.ws.SessionRenewer) ConnectorConfig(com.sforce.ws.ConnectorConfig) PartnerConnection(com.sforce.soap.partner.PartnerConnection) QName(javax.xml.namespace.QName) IOException(java.io.IOException) ConnectionException(com.sforce.ws.ConnectionException) ConnectionHolder(org.talend.components.salesforce.runtime.common.ConnectionHolder)

Example 9 with ConnectionHolder

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

the class SalesforceSourceOrSinkTest method testGuessSchema.

/**
 * Checks {@link SalesforceSourceOrSink#guessSchema(String)} returns the
 * {@link org.apache.avro.Schema} with date and string type
 */
@Test
public void testGuessSchema() throws Exception {
    String field0Name = "Id";
    String field1Name = "LastModifiedDate";
    String field2Name = "LastActivityDate";
    String drivingEntity = "Account";
    String soql = new StringBuilder().append("SELECT").append(" ").append(field0Name).append(",").append(SPACE).append(field1Name).append(",").append(SPACE).append(field2Name).append(SPACE).append("FROM").append(SPACE).append(drivingEntity).toString();
    final PartnerConnection partnerConnectionMock = Mockito.mock(PartnerConnection.class);
    class SalesforceSourceOrSinkChild extends SalesforceSourceOrSink {

        @Override
        protected ConnectionHolder connect(RuntimeContainer container) {
            ConnectionHolder connectionHolder = new ConnectionHolder();
            connectionHolder.connection = partnerConnectionMock;
            return connectionHolder;
        }
    }
    Field field0 = new Field();
    field0.setName(field0Name);
    field0.setType(FieldType.string);
    Field field1 = new Field();
    field1.setName(field1Name);
    field1.setType(FieldType.date);
    Field field2 = new Field();
    field2.setName(field2Name);
    field2.setType(FieldType.date);
    Field[] fields = new Field[3];
    fields[0] = field0;
    fields[1] = field1;
    fields[2] = field2;
    DescribeSObjectResult describeSObjectResult = new DescribeSObjectResult();
    describeSObjectResult.setFields(fields);
    Mockito.when(partnerConnectionMock.describeSObject(drivingEntity)).thenReturn(describeSObjectResult);
    SalesforceSourceOrSinkChild salesforceSourceOrSinkChild = new SalesforceSourceOrSinkChild();
    salesforceSourceOrSinkChild.initialize(runtimeContainerMock, properties);
    Schema resultSchema = salesforceSourceOrSinkChild.guessSchema(soql);
    LOGGER.debug("result schema: " + resultSchema.toString());
    Assert.assertEquals("GuessedSchema", resultSchema.getName());
    Assert.assertEquals(field0Name, resultSchema.getFields().get(0).name());
    Assert.assertEquals(field1Name, resultSchema.getFields().get(1).name());
    Assert.assertEquals("java.util.Date", AvroUtils.unwrapIfNullable(resultSchema.getFields().get(1).schema()).getProp("java-class"));
    Assert.assertEquals("yyyy-MM-dd", resultSchema.getFields().get(1).getProp(SchemaConstants.TALEND_COLUMN_PATTERN));
    Assert.assertEquals(field2Name, resultSchema.getFields().get(2).name());
    Assert.assertEquals("java.util.Date", AvroUtils.unwrapIfNullable(resultSchema.getFields().get(2).schema()).getProp("java-class"));
    Assert.assertEquals("yyyy-MM-dd", resultSchema.getFields().get(2).getProp(SchemaConstants.TALEND_COLUMN_PATTERN));
}
Also used : Field(com.sforce.soap.partner.Field) PartnerConnection(com.sforce.soap.partner.PartnerConnection) DescribeSObjectResult(com.sforce.soap.partner.DescribeSObjectResult) Schema(org.apache.avro.Schema) RuntimeContainer(org.talend.components.api.container.RuntimeContainer) ConnectionHolder(org.talend.components.salesforce.runtime.common.ConnectionHolder) Test(org.junit.Test)

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