use of org.kie.api.marshalling.ObjectMarshallingStrategy in project jbpm by kiegroup.
the class ProtobufProcessMarshaller method marshallVariable.
public static Variable marshallVariable(MarshallerWriteContext context, String name, Object value) throws IOException {
JBPMMessages.Variable.Builder builder = JBPMMessages.Variable.newBuilder().setName(name);
if (value != null) {
ObjectMarshallingStrategy strategy = context.objectMarshallingStrategyStore.getStrategyObject(value);
Integer index = context.getStrategyIndex(strategy);
builder.setStrategyIndex(index).setValue(ByteString.copyFrom(strategy.marshal(context.strategyContext.get(strategy), context, value)));
}
return builder.build();
}
use of org.kie.api.marshalling.ObjectMarshallingStrategy in project jbpm by kiegroup.
the class ProtobufProcessMarshaller method marshallVariablesContainer.
public static VariableContainer marshallVariablesContainer(MarshallerWriteContext context, Map<String, Object> variables) throws IOException {
JBPMMessages.VariableContainer.Builder vcbuilder = JBPMMessages.VariableContainer.newBuilder();
for (String key : variables.keySet()) {
JBPMMessages.Variable.Builder builder = JBPMMessages.Variable.newBuilder().setName(key);
if (variables.get(key) != null) {
ObjectMarshallingStrategy strategy = context.objectMarshallingStrategyStore.getStrategyObject(variables.get(key));
Integer index = context.getStrategyIndex(strategy);
builder.setStrategyIndex(index).setValue(ByteString.copyFrom(strategy.marshal(context.strategyContext.get(strategy), context, variables.get(key))));
}
vcbuilder.addVariable(builder.build());
}
return vcbuilder.build();
}
use of org.kie.api.marshalling.ObjectMarshallingStrategy in project jbpm by kiegroup.
the class ProtobufProcessMarshaller method unmarshallVariableValue.
public static Object unmarshallVariableValue(MarshallerReaderContext context, JBPMMessages.Variable _variable) throws IOException, ClassNotFoundException {
if (_variable.getValue() == null || _variable.getValue().isEmpty()) {
return null;
}
ObjectMarshallingStrategy strategy = context.usedStrategies.get(_variable.getStrategyIndex());
Object value = strategy.unmarshal(context.strategyContexts.get(strategy), context, _variable.getValue().toByteArray(), (context.kBase == null) ? null : context.kBase.getRootClassLoader());
return value;
}
use of org.kie.api.marshalling.ObjectMarshallingStrategy in project jbpm by kiegroup.
the class ProcessInstanceResolverStrategyTest method testProcessInstanceResolverStrategy.
@Test
public void testProcessInstanceResolverStrategy() throws Exception {
// Setup
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource(PROCESS_NAME, this.getClass()), ResourceType.DRF);
KieBase kbase = kbuilder.newKieBase();
KieSession ksession = kbase.newKieSession();
ProcessInstance processInstance = ksession.createProcessInstance("process name", new HashMap<String, Object>());
ksession.insert(processInstance);
// strategy setup
ProcessInstanceResolverStrategy strategy = new ProcessInstanceResolverStrategy();
ObjectMarshallingStrategy[] strategies = { strategy, MarshallerFactory.newSerializeMarshallingStrategy() };
// Test strategy.write
org.kie.api.marshalling.MarshallingConfiguration marshallingConfig = new MarshallingConfigurationImpl(strategies, true, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MarshallerWriteContext writerContext = new MarshallerWriteContext(baos, ((InternalKnowledgeBase) kbase), (InternalWorkingMemory) ((StatefulKnowledgeSessionImpl) ksession), RuleBaseNodes.getNodeMap(((InternalKnowledgeBase) kbase)), marshallingConfig.getObjectMarshallingStrategyStore(), marshallingConfig.isMarshallProcessInstances(), marshallingConfig.isMarshallWorkItems(), ksession.getEnvironment());
strategy.write(writerContext, processInstance);
baos.close();
writerContext.close();
byte[] bytes = baos.toByteArray();
int numCorrectBytes = calculateNumBytesForLong(processInstance.getId());
assertTrue("Expected " + numCorrectBytes + " bytes, not " + bytes.length, bytes.length == numCorrectBytes);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
long serializedProcessInstanceId = ois.readLong();
assertTrue("Expected " + processInstance.getId() + ", not " + serializedProcessInstanceId, processInstance.getId() == serializedProcessInstanceId);
// Test other strategy stuff
ProcessInstanceManager pim = ProcessInstanceResolverStrategy.retrieveProcessInstanceManager(writerContext);
assertNotNull(pim);
assertNotNull(ProcessInstanceResolverStrategy.retrieveKnowledgeRuntime(writerContext));
assertTrue(processInstance == pim.getProcessInstance(serializedProcessInstanceId));
// Test strategy.read
bais = new ByteArrayInputStream(bytes);
MarshallerReaderContext readerContext = new MarshallerReaderContext(bais, ((KnowledgeBaseImpl) kbase), RuleBaseNodes.getNodeMap(((KnowledgeBaseImpl) kbase)), marshallingConfig.getObjectMarshallingStrategyStore(), ProtobufMarshaller.TIMER_READERS, marshallingConfig.isMarshallProcessInstances(), marshallingConfig.isMarshallWorkItems(), EnvironmentFactory.newEnvironment());
readerContext.wm = ((StatefulKnowledgeSessionImpl) ksession).getInternalWorkingMemory();
Object procInstObject = strategy.read(readerContext);
assertTrue(procInstObject != null && procInstObject instanceof ProcessInstance);
assertTrue(processInstance == procInstObject);
}
use of org.kie.api.marshalling.ObjectMarshallingStrategy in project jbpm by kiegroup.
the class TaskTransactionInterceptor method execute.
@Override
public synchronized RequestContext execute(Executable executable, RequestContext ctx) {
boolean transactionOwner = false;
try {
transactionOwner = txm.begin();
tpm.beginCommandScopedEntityManager();
TransactionManagerHelper.registerTransactionSyncInContainer(this.txm, new TaskSynchronizationImpl(this));
if (txm != null) {
ObjectMarshallingStrategy[] strategies = (ObjectMarshallingStrategy[]) environment.get(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES);
if (strategies != null) {
for (ObjectMarshallingStrategy strategy : strategies) {
if (strategy instanceof TransactionAware) {
((TransactionAware) strategy).onStart(txm);
}
}
}
}
RequestContext context = createContext();
executeNext(executable, context);
ctx.setResult(context.getResult());
postInit(ctx.getResult());
txm.commit(transactionOwner);
return ctx;
} catch (TaskException e) {
// if transaction is owned by other component like process engine
if (transactionOwner) {
rollbackTransaction(e, transactionOwner);
e.setRecoverable(false);
throw e;
} else {
throw e;
}
} catch (RuntimeException re) {
rollbackTransaction(re, transactionOwner);
throw re;
} catch (Exception t1) {
rollbackTransaction(t1, transactionOwner);
throw new RuntimeException("Wrapped exception see cause", t1);
}
}
Aggregations