Search in sources :

Example 1 with VariableContainer

use of org.jbpm.marshalling.impl.JBPMMessages.VariableContainer in project jbpm by kiegroup.

the class ContentMarshallerHelper method unmarshall.

public static Object unmarshall(byte[] content, Environment env, ClassLoader classloader) {
    ProtobufMarshallerReaderContext context = null;
    try {
        ByteArrayInputStream stream = new ByteArrayInputStream(content);
        MarshallingConfigurationImpl marshallingConfigurationImpl = null;
        if (env != null) {
            marshallingConfigurationImpl = new MarshallingConfigurationImpl((ObjectMarshallingStrategy[]) env.get(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES), false, false);
        } else {
            marshallingConfigurationImpl = new MarshallingConfigurationImpl(new ObjectMarshallingStrategy[] { new SerializablePlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) }, false, false);
        }
        ObjectMarshallingStrategyStore objectMarshallingStrategyStore = marshallingConfigurationImpl.getObjectMarshallingStrategyStore();
        context = new ProtobufMarshallerReaderContext(stream, null, null, objectMarshallingStrategyStore, null, env);
        if (classloader != null) {
            context.setClassLoader(classloader);
        } else {
            context.setClassLoader(ContentMarshallerHelper.class.getClassLoader());
        }
        ExtensionRegistry registry = PersisterHelper.buildRegistry(context, null);
        Header _header = PersisterHelper.readFromStreamWithHeaderPreloaded(context, registry);
        try {
            VariableContainer parseFrom = JBPMMessages.VariableContainer.parseFrom(_header.getPayload(), registry);
            Map<String, Object> value = ProtobufProcessMarshaller.unmarshallVariableContainerValue(context, parseFrom);
            // in case there was single variable stored return only that variable and not map
            if (value.containsKey(SINGLE_VAR_KEY) && value.size() == 1) {
                return value.get(SINGLE_VAR_KEY);
            }
            return value;
        } catch (Exception e) {
            // backward compatible fallback mechanism to ensure existing data can be read properly
            return fallbackParse(context, _header, registry);
        }
    } catch (Exception ex) {
        logger.warn("Exception while unmarshaling content", ex);
    }
    return null;
}
Also used : SerializablePlaceholderResolverStrategy(org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy) ObjectMarshallingStrategyStore(org.kie.api.marshalling.ObjectMarshallingStrategyStore) MarshallingConfigurationImpl(org.drools.core.marshalling.impl.MarshallingConfigurationImpl) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) IOException(java.io.IOException) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) ProtobufMarshallerReaderContext(org.drools.serialization.protobuf.ProtobufMarshallerReaderContext) Header(org.drools.serialization.protobuf.ProtobufMessages.Header) VariableContainer(org.jbpm.marshalling.impl.JBPMMessages.VariableContainer) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 2 with VariableContainer

use of org.jbpm.marshalling.impl.JBPMMessages.VariableContainer 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.getObjectMarshallingStrategyStore().getStrategyObject(variables.get(key));
            Integer index = context.getStrategyIndex(strategy);
            builder.setStrategyIndex(index).setValue(ByteString.copyFrom(strategy.marshal(context.getStrategyContext().get(strategy), (ObjectOutputStream) 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)

Aggregations

VariableContainer (org.jbpm.marshalling.impl.JBPMMessages.VariableContainer)2 ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)2 ByteString (com.google.protobuf.ByteString)1 ExtensionRegistry (com.google.protobuf.ExtensionRegistry)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 MarshallingConfigurationImpl (org.drools.core.marshalling.impl.MarshallingConfigurationImpl)1 SerializablePlaceholderResolverStrategy (org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy)1 ProtobufMarshallerReaderContext (org.drools.serialization.protobuf.ProtobufMarshallerReaderContext)1 Header (org.drools.serialization.protobuf.ProtobufMessages.Header)1 Variable (org.jbpm.marshalling.impl.JBPMMessages.Variable)1 ObjectMarshallingStrategyStore (org.kie.api.marshalling.ObjectMarshallingStrategyStore)1