Search in sources :

Example 16 with ObjectMarshallingStrategy

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();
}
Also used : Variable(org.jbpm.marshalling.impl.JBPMMessages.Variable) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy)

Example 17 with ObjectMarshallingStrategy

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();
}
Also used : Variable(org.jbpm.marshalling.impl.JBPMMessages.Variable) VariableContainer(org.jbpm.marshalling.impl.JBPMMessages.VariableContainer) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) ByteString(com.google.protobuf.ByteString)

Example 18 with ObjectMarshallingStrategy

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;
}
Also used : ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy)

Example 19 with ObjectMarshallingStrategy

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);
}
Also used : ProcessInstanceResolverStrategy(org.jbpm.marshalling.impl.ProcessInstanceResolverStrategy) ProcessInstanceManager(org.jbpm.process.instance.ProcessInstanceManager) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) MarshallerReaderContext(org.drools.core.marshalling.impl.MarshallerReaderContext) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) MarshallingConfigurationImpl(org.drools.core.marshalling.impl.MarshallingConfigurationImpl) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClassPathResource(org.drools.core.io.impl.ClassPathResource) ByteArrayInputStream(java.io.ByteArrayInputStream) MarshallerWriteContext(org.drools.core.marshalling.impl.MarshallerWriteContext) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 20 with ObjectMarshallingStrategy

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);
    }
}
Also used : TaskException(org.kie.internal.task.exception.TaskException) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) TransactionAware(org.drools.persistence.api.TransactionAware) RequestContext(org.kie.api.runtime.RequestContext) TaskException(org.kie.internal.task.exception.TaskException)

Aggregations

ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)35 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)7 IOException (java.io.IOException)6 SerializablePlaceholderResolverStrategy (org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 File (java.io.File)4 Map (java.util.Map)4 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)4 EventFactHandle (org.drools.core.common.EventFactHandle)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)4 Marshaller (org.kie.api.marshalling.Marshaller)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ObjectInputStream (java.io.ObjectInputStream)3 QueryElementFactHandle (org.drools.core.common.QueryElementFactHandle)3 MarshallerReaderContext (org.drools.core.marshalling.impl.MarshallerReaderContext)3 MarshallingConfigurationImpl (org.drools.core.marshalling.impl.MarshallingConfigurationImpl)3 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)3 KieMarshallers (org.kie.api.marshalling.KieMarshallers)3