use of org.talend.daikon.sandbox.SandboxedInstance in project components by Talend.
the class RuntimeControllerImpl method writeData.
@Override
public void writeData(InputStream rawPayload) throws IOException {
// 1) Read payload (with data as a stream of course)
DatasetWritePayload payload = DatasetWritePayload.readData(rawPayload, mapper);
String definitionName = payload.getConfiguration().getDefinitionName();
// 2) Create properties
Properties properties = propertiesHelpers.propertiesFromDto(payload.getConfiguration());
if (properties instanceof ComponentProperties) {
ComponentProperties componentProperties = (ComponentProperties) properties;
// 3) Retrieve component definition to be able to create the runtime
final ComponentDefinition definition = propertiesHelpers.getDefinition(ComponentDefinition.class, definitionName);
// 4) Get the execution engine
ExecutionEngine executionEngine;
if (definition.isSupportingExecutionEngines(DI)) {
executionEngine = DI;
// 5) Create the sandbox
try (SandboxedInstance instance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(executionEngine, componentProperties, INCOMING), definition.getClass().getClassLoader())) {
Sink datasetRuntimeInstance = (Sink) instance.getInstance();
datasetRuntimeInstance.initialize(null, componentProperties);
Iterator<IndexedRecord> data = payload.getData();
WriteOperation writeOperation = datasetRuntimeInstance.createWriteOperation();
// Supplier return null to signify end of data stream => see WriterDataSupplier.writeData
WriterDataSupplier<?, IndexedRecord> stringWriterDataSupplier = new WriterDataSupplier<Object, IndexedRecord>(writeOperation, () -> data.hasNext() ? data.next() : null, null);
stringWriterDataSupplier.writeData();
}
} else if (definition.isSupportingExecutionEngines(BEAM)) {
throw new UnsupportedOperationException("Beam runtime is not available for dataset write through HTTP API.");
} else {
throw new TalendRuntimeException(CommonErrorCodes.UNREGISTERED_DEFINITION);
}
} else if (properties instanceof DatasetProperties) {
throw new UnsupportedOperationException("HTTP API is only able to write using component implementations. Not " + properties.getClass());
}
}
use of org.talend.daikon.sandbox.SandboxedInstance in project components by Talend.
the class TMarketoConnectionProperties method validateTestConnection.
public ValidationResult validateTestConnection() {
try (SandboxedInstance sandboxedInstance = getSandboxedInstance(RUNTIME_SOURCEORSINK_CLASS, USE_CURRENT_JVM_PROPS)) {
MarketoSourceOrSinkRuntime sos = (MarketoSourceOrSinkRuntime) sandboxedInstance.getInstance();
sos.initialize(null, this);
ValidationResult vr = sos.validateConnection(this);
if (vr.getStatus() == ValidationResult.Result.OK) {
getForm(FORM_WIZARD).setAllowForward(true);
getForm(FORM_WIZARD).setAllowFinish(true);
} else {
getForm(FORM_WIZARD).setAllowForward(false);
}
return vr;
}
}
use of org.talend.daikon.sandbox.SandboxedInstance in project components by Talend.
the class TMarketoInputProperties method validateFetchCustomObjectSchema.
public ValidationResult validateFetchCustomObjectSchema() {
ValidationResultMutable vr = new ValidationResultMutable();
try (SandboxedInstance sandboxedInstance = getSandboxedInstance(RUNTIME_SOURCEORSINK_CLASS, USE_CURRENT_JVM_PROPS)) {
MarketoSourceOrSinkRuntime sos = (MarketoSourceOrSinkRuntime) sandboxedInstance.getInstance();
sos.initialize(null, this);
ValidationResult vConn = sos.validateConnection(this);
if (!Result.OK.equals(vConn.getStatus())) {
return vConn;
}
try {
Schema schema = ((MarketoSourceOrSinkSchemaProvider) sos).getSchemaForCustomObject(customObjectName.getValue());
if (schema == null) {
vr.setStatus(ValidationResult.Result.ERROR).setMessage(messages.getMessage("error.validation.customobjects.fetchcustomobjectschema", customObjectName.getValue(), "NULL"));
return vr;
}
schemaInput.schema.setValue(schema);
vr.setStatus(ValidationResult.Result.OK);
} catch (RuntimeException | IOException e) {
vr.setStatus(ValidationResult.Result.ERROR).setMessage(messages.getMessage("error.validation.customobjects.fetchcustomobjectschema", customObjectName.getValue(), e.getMessage()));
}
}
return vr;
}
use of org.talend.daikon.sandbox.SandboxedInstance in project components by Talend.
the class TMarketoInputProperties method validateFetchCompoundKey.
public ValidationResult validateFetchCompoundKey() {
ValidationResultMutable vr = new ValidationResultMutable();
try (SandboxedInstance sandboxedInstance = getSandboxedInstance(RUNTIME_SOURCEORSINK_CLASS, USE_CURRENT_JVM_PROPS)) {
MarketoSourceOrSinkRuntime sos = (MarketoSourceOrSinkRuntime) sandboxedInstance.getInstance();
sos.initialize(null, this);
ValidationResult vConn = sos.validateConnection(this);
if (!Result.OK.equals(vConn.getStatus())) {
return vConn;
}
try {
List<String> keys = ((MarketoSourceOrSinkSchemaProvider) sos).getCompoundKeyFields(customObjectName.getValue());
if (keys == null) {
vr.setStatus(ValidationResult.Result.ERROR).setMessage(messages.getMessage("error.validation.customobjects.fetchcompoundkey", customObjectName.getValue(), "NULL"));
return vr;
}
compoundKey.keyName.setValue(keys);
compoundKey.keyValue.setValue(Arrays.asList(new String[keys.size()]));
vr.setStatus(ValidationResult.Result.OK);
} catch (RuntimeException | IOException e) {
vr.setStatus(ValidationResult.Result.ERROR).setMessage(messages.getMessage("error.validation.customobjects.fetchcompoundkey", customObjectName.getValue(), e.getMessage()));
}
}
return vr;
}
use of org.talend.daikon.sandbox.SandboxedInstance in project components by Talend.
the class SandboxedFixedInputRuntimeTest method testDatasetGetSample.
@Test
public void testDatasetGetSample() throws Exception {
// The two records to use as values.
GenericRecord r1 = new GenericData.Record(SampleSchemas.recordSimple());
r1.put("id", 1);
r1.put("name", "one");
GenericRecord r2 = new GenericData.Record(SampleSchemas.recordSimple());
r2.put("id", 2);
r2.put("name", "two");
final FixedDatasetProperties props = createComponentProperties().getDatasetProperties();
props.format.setValue(FixedDatasetProperties.RecordFormat.AVRO);
props.schema.setValue(SampleSchemas.recordSimple().toString());
props.values.setValue(r1.toString() + r2.toString());
final List<IndexedRecord> consumed = new ArrayList<>();
RuntimeInfo ri = new FixedDatasetDefinition().getRuntimeInfo(props);
try (SandboxedInstance si = RuntimeUtil.createRuntimeClass(ri, getClass().getClassLoader())) {
DatasetRuntime<FixedDatasetProperties> runtime = (DatasetRuntime<FixedDatasetProperties>) si.getInstance();
runtime.initialize(null, props);
// The functionality of the runtime is tested in its own module.
runtime.getSample(100, new Consumer<IndexedRecord>() {
@Override
public void accept(IndexedRecord ir) {
consumed.add(ir);
}
});
}
assertThat(consumed, hasSize(2));
}
Aggregations