protected String getObjectTypeName(ObjectValue value, ValueFields valueFields) { String objectTypeName = value.getObjectTypeName(); if (objectTypeName == null && !value.isDeserialized() && value.getValueSerialized() != null) { throw new ProcessEngineException("Cannot write serialized value for variable '" + valueFields.getName() + "': no 'objectTypeName' provided for non-null value."); } // update type name if the object is deserialized if (value.isDeserialized() && value.getValue() != null) { objectTypeName = getTypeNameForDeserialized(value.getValue()); } return objectTypeName; }
@Test public void testJacksonBug146AsVariable() { InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("org/camunda/bpm/integrationtest/functional/spin/jackson146.json"); String jackson146 = SpinIoUtil.inputStreamAsString(resourceAsStream); ProcessInstance pi = runtimeService.startProcessInstanceByKey("testProcess", Variables.createVariables() .putValue("jackson146", serializedObjectValue(jackson146).serializationDataFormat("application/json").objectTypeName(HashMap.class.getName()))); // file has 4000 characters in length a // 20 characters per repeated JSON object ObjectValue objectValue = runtimeService.getVariableTyped(pi.getId(), "jackson146", true); HashMap<String, List<Object>> map = (HashMap<String, List<Object>>) objectValue.getValue(); assertEquals(200, map.get("abcdef").size()); }
protected String getObjectTypeName(ObjectValue value, ValueFields valueFields) { String objectTypeName = value.getObjectTypeName(); if (objectTypeName == null && !value.isDeserialized() && value.getValueSerialized() != null) { throw new ProcessEngineException("Cannot write serialized value for variable '" + valueFields.getName() + "': no 'objectTypeName' provided for non-null value."); } // update type name if the object is deserialized if (value.isDeserialized() && value.getValue() != null) { objectTypeName = getTypeNameForDeserialized(value.getValue()); } return objectTypeName; }
public static void assertObjectValueDeserialized(ObjectValue typedValue, Object value) { Class<? extends Object> expectedObjectType = value.getClass(); assertTrue(typedValue.isDeserialized()); assertEquals(ValueType.OBJECT, typedValue.getType()); assertEquals(value, typedValue.getValue()); assertEquals(value, typedValue.getValue(expectedObjectType)); assertEquals(expectedObjectType, typedValue.getObjectType()); assertEquals(expectedObjectType.getName(), typedValue.getObjectTypeName()); }
public static MockObjectValue fromObjectValue(ObjectValue objectValue) { MockObjectValue result = new MockObjectValue(objectValue.getValue()); result.isDeserialized = objectValue.isDeserialized(); result.objectTypeName = objectValue.getObjectTypeName(); result.serializationDataFormat = objectValue.getSerializationDataFormat(); result.serializedValue = objectValue.getValueSerialized(); result.type = objectValue.getType(); return result; }
@Test public void spinCanBeUsedForVariableSerialization() { ProcessInstance pi = runtimeService.startProcessInstanceByKey("testProcess", Variables.createVariables() .putValue("serializedObject", serializedObjectValue("{\"foo\": \"bar\"}").serializationDataFormat("application/json").objectTypeName(HashMap.class.getName()))); ObjectValue objectValue = runtimeService.getVariableTyped(pi.getId(), "serializedObject", true); HashMap<String, String> expected = new HashMap<String, String>(); expected.put("foo", "bar"); Assert.assertEquals(expected, objectValue.getValue()); }
public static void assertObjectValueDeserializedNull(ObjectValue typedValue) { assertNotNull(typedValue); assertTrue(typedValue.isDeserialized()); assertNotNull(typedValue.getSerializationDataFormat()); assertNull(typedValue.getValue()); assertNull(typedValue.getValueSerialized()); assertNull(typedValue.getObjectType()); assertNull(typedValue.getObjectTypeName()); }
public void writeValue(ObjectValue objectValue, ValueFields valueFields) { EntityManagerSession entityManagerSession = Context .getCommandContext() .getSession(EntityManagerSession.class); if (entityManagerSession == null) { throw new ProcessEngineException("Cannot set JPA variable: " + EntityManagerSession.class + " not configured"); } else { // Before we set the value we must flush all pending changes from the entitymanager // If we don't do this, in some cases the primary key will not yet be set in the object // which will cause exceptions down the road. entityManagerSession.flush(); } Object value = objectValue.getValue(); if(value != null) { String className = mappings.getJPAClassString(value); String idString = mappings.getJPAIdString(value); valueFields.setTextValue(className); valueFields.setTextValue2(idString); } else { valueFields.setTextValue(null); valueFields.setTextValue2(null); } }
public void testDisableCustomObjectDeserialization() { // given Task newTask = taskService.newTask(); taskService.saveTask(newTask); Map<String, Object> variables = new HashMap<String, Object>(); variables.put("customSerializable", new CustomSerializable()); variables.put("failingSerializable", new FailingSerializable()); taskService.setVariables(newTask.getId(), variables); // when List<HistoricVariableInstance> variableInstances = historyService.createHistoricVariableInstanceQuery() .disableCustomObjectDeserialization() .list(); // then assertEquals(2, variableInstances.size()); for (HistoricVariableInstance variableInstance : variableInstances) { assertNull(variableInstance.getErrorMessage()); ObjectValue typedValue = (ObjectValue) variableInstance.getTypedValue(); assertNotNull(typedValue); assertFalse(typedValue.isDeserialized()); // cannot access the deserialized value try { typedValue.getValue(); } catch(IllegalStateException e) { assertTextPresent("Object is not deserialized", e.getMessage()); } assertNotNull(typedValue.getValueSerialized()); } taskService.deleteTask(newTask.getId(), true); }
public void writeValue(ObjectValue objectValue, ValueFields valueFields) { EntityManagerSession entityManagerSession = Context .getCommandContext() .getSession(EntityManagerSession.class); if (entityManagerSession == null) { throw new ProcessEngineException("Cannot set JPA variable: " + EntityManagerSession.class + " not configured"); } else { // Before we set the value we must flush all pending changes from the entitymanager // If we don't do this, in some cases the primary key will not yet be set in the object // which will cause exceptions down the road. entityManagerSession.flush(); } Object value = objectValue.getValue(); if(value != null) { String className = mappings.getJPAClassString(value); String idString = mappings.getJPAIdString(value); valueFields.setTextValue(className); valueFields.setTextValue2(idString); } else { valueFields.setTextValue(null); valueFields.setTextValue2(null); } }
typedValue.getValue();
private void testVariablesWithoutDeserialization(String processDefinitionKey) throws Exception { //given serializable variable JavaSerializable javaSerializable = new JavaSerializable("foo"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); new ObjectOutputStream(baos).writeObject(javaSerializable); String serializedObject = StringUtil.fromBytes(Base64.encodeBase64(baos.toByteArray()), engineRule.getProcessEngine()); //when execute process with serialized variable and wait state ProcessInstanceWithVariables procInstance = engineRule.getRuntimeService() .createProcessInstanceByKey(processDefinitionKey) .setVariable("serializedVar", serializedObjectValue(serializedObject) .serializationDataFormat(Variables.SerializationDataFormats.JAVA) .objectTypeName(JavaSerializable.class.getName()) .create()) .executeWithVariablesInReturn(false, false); //then returned instance contains serialized variable VariableMap map = procInstance.getVariables(); assertNotNull(map); ObjectValue serializedVar = (ObjectValue) map.getValueTyped("serializedVar"); assertFalse(serializedVar.isDeserialized()); assertObjectValueSerializedJava(serializedVar, javaSerializable); //access on value should fail because variable is not deserialized try { serializedVar.getValue(); Assert.fail("Deserialization should fail!"); } catch (IllegalStateException ise) { assertTrue(ise.getMessage().equals("Object is not deserialized.")); } }
typedValue.getValue();
@SuppressWarnings("unchecked") protected void verifySerializedValue(Map<String, Object> serializedValue) { ObjectValue exampleValue = MockProvider.EXAMPLE_HISTORIC_DECISION_SERIALIZED_VALUE; assertThat(serializedValue, hasEntry("type", (Object) VariableValueDto.toRestApiTypeName(exampleValue.getType().getName()))); assertThat(serializedValue, hasEntry("value", exampleValue.getValue())); Map<String, String> valueInfo = (Map<String, String>) serializedValue.get("valueInfo"); assertThat(valueInfo, hasEntry("serializationDataFormat", exampleValue.getSerializationDataFormat())); assertThat(valueInfo, hasEntry("objectTypeName", exampleValue.getObjectTypeName())); }
typedValue.getValue();
/** * Tests that * 1) a serialized value can be set OUT OF process application context * even if the data format is not available (using the fallback serializer) * 2) and that this value can be deserialized IN process application context * by using the PA-local serializer */ @Test public void customFormatCanBeUsedForVariableSerialization() { final ProcessInstance pi = runtimeService.startProcessInstanceByKey("testProcess", Variables.createVariables() .putValue("serializedObject", serializedObjectValue("foo") .serializationDataFormat(FooDataFormat.NAME) .objectTypeName(Foo.class.getName()))); ObjectValue objectValue = null; try { ProcessApplicationContext.setCurrentProcessApplication(ReferenceStoringProcessApplication.INSTANCE); objectValue = runtimeService.getVariableTyped(pi.getId(), "serializedObject", true); } finally { ProcessApplicationContext.clear(); } Object value = objectValue.getValue(); Assert.assertNotNull(value); Assert.assertTrue(value instanceof Foo); }
@Test @Deployment(resources = ONE_TASK_PROCESS) public void testSetJavaObjectNullSerializedObjectTypeName() throws Exception { ProcessInstance instance = runtimeService.startProcessInstanceByKey("oneTaskProcess"); String typeName = "some.type.Name"; // set null value as "serialized" object runtimeService.setVariable(instance.getId(), "nullObject", serializedObjectValue() .serializationDataFormat(JAVA_DATA_FORMAT) .objectTypeName(typeName) // This time an objectTypeName is provided .create()); // get null value via untyped api assertNull(runtimeService.getVariable(instance.getId(), "nullObject")); // get null via typed api ObjectValue deserializedTypedValue = runtimeService.getVariableTyped(instance.getId(), "nullObject"); assertNotNull(deserializedTypedValue); assertTrue(deserializedTypedValue.isDeserialized()); assertEquals(JAVA_DATA_FORMAT, deserializedTypedValue.getSerializationDataFormat()); assertNull(deserializedTypedValue.getValue()); assertNull(deserializedTypedValue.getValueSerialized()); assertNull(deserializedTypedValue.getObjectType()); assertEquals(typeName, deserializedTypedValue.getObjectTypeName()); ObjectValue serializedTypedValue = runtimeService.getVariableTyped(instance.getId(), "nullObject", false); assertNotNull(serializedTypedValue); assertFalse(serializedTypedValue.isDeserialized()); assertEquals(JAVA_DATA_FORMAT, serializedTypedValue.getSerializationDataFormat()); assertNull(serializedTypedValue.getValueSerialized()); assertEquals(typeName, serializedTypedValue.getObjectTypeName()); }
@Test public void testFallbackSerializerDoesNotOverrideRegularSerializer() { // given // that the process engine is configured with a serializer for a certain format // and a fallback serializer factory for the same format ProcessEngineConfigurationImpl engineConfiguration = new StandaloneInMemProcessEngineConfiguration() .setJdbcUrl("jdbc:h2:mem:camunda-forceclose") .setProcessEngineName("engine-forceclose"); engineConfiguration.setCustomPreVariableSerializers(Arrays.<TypedValueSerializer>asList(new ExampleConstantSerializer())); engineConfiguration.setFallbackSerializerFactory(new ExampleSerializerFactory()); processEngine = engineConfiguration.buildProcessEngine(); deployOneTaskProcess(processEngine); // when setting a variable that no regular serializer can handle ObjectValue objectValue = Variables.objectValue("foo").serializationDataFormat(ExampleSerializer.FORMAT).create(); ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey("oneTaskProcess", Variables.createVariables().putValueTyped("var", objectValue)); ObjectValue fetchedValue = processEngine.getRuntimeService().getVariableTyped(pi.getId(), "var", true); // then the fallback serializer is used Assert.assertNotNull(fetchedValue); Assert.assertEquals(ExampleSerializer.FORMAT, fetchedValue.getSerializationDataFormat()); Assert.assertEquals(ExampleConstantSerializer.DESERIALIZED_VALUE, fetchedValue.getValue()); }
@Test public void testFallbackSerializer() { // given // that the process engine is configured with a fallback serializer factory ProcessEngineConfigurationImpl engineConfiguration = new StandaloneInMemProcessEngineConfiguration() .setJdbcUrl("jdbc:h2:mem:camunda-forceclose") .setProcessEngineName("engine-forceclose"); engineConfiguration.setFallbackSerializerFactory(new ExampleSerializerFactory()); processEngine = engineConfiguration.buildProcessEngine(); deployOneTaskProcess(processEngine); // when setting a variable that no regular serializer can handle ObjectValue objectValue = Variables.objectValue("foo").serializationDataFormat(ExampleSerializer.FORMAT).create(); ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey("oneTaskProcess", Variables.createVariables().putValueTyped("var", objectValue)); ObjectValue fetchedValue = processEngine.getRuntimeService().getVariableTyped(pi.getId(), "var", true); // then the fallback serializer is used Assert.assertNotNull(fetchedValue); Assert.assertEquals(ExampleSerializer.FORMAT, fetchedValue.getSerializationDataFormat()); Assert.assertEquals("foo", fetchedValue.getValue()); }
public void execute(DelegateExecution execution) throws Exception { // validate integer variable Integer expectedIntValue = 1234; assertEquals(expectedIntValue, execution.getVariable("anIntegerVariable")); assertEquals(expectedIntValue, execution.getVariableTyped("anIntegerVariable").getValue()); assertEquals(ValueType.INTEGER, execution.getVariableTyped("anIntegerVariable").getType()); assertNull(execution.getVariableLocal("anIntegerVariable")); assertNull(execution.getVariableLocalTyped("anIntegerVariable")); // set an additional local variable execution.setVariableLocal("aStringVariable", "aStringValue"); String expectedStringValue = "aStringValue"; assertEquals(expectedStringValue, execution.getVariable("aStringVariable")); assertEquals(expectedStringValue, execution.getVariableTyped("aStringVariable").getValue()); assertEquals(ValueType.STRING, execution.getVariableTyped("aStringVariable").getType()); assertEquals(expectedStringValue, execution.getVariableLocal("aStringVariable")); assertEquals(expectedStringValue, execution.getVariableLocalTyped("aStringVariable").getValue()); assertEquals(ValueType.STRING, execution.getVariableLocalTyped("aStringVariable").getType()); SimpleSerializableBean objectValue = (SimpleSerializableBean) execution.getVariable("anObjectValue"); assertNotNull(objectValue); assertEquals(10, objectValue.getIntProperty()); ObjectValue variableTyped = execution.getVariableTyped("anObjectValue"); assertEquals(10, variableTyped.getValue(SimpleSerializableBean.class).getIntProperty()); assertEquals(Variables.SerializationDataFormats.JAVA.getName(), variableTyped.getSerializationDataFormat()); objectValue = (SimpleSerializableBean) execution.getVariable("anUntypedObjectValue"); assertNotNull(objectValue); assertEquals(30, objectValue.getIntProperty()); variableTyped = execution.getVariableTyped("anUntypedObjectValue"); assertEquals(30, variableTyped.getValue(SimpleSerializableBean.class).getIntProperty()); assertEquals(Context.getProcessEngineConfiguration().getDefaultSerializationFormat(), variableTyped.getSerializationDataFormat()); }