Search in sources :

Example 1 with MarshallerReaderContext

use of org.drools.core.marshalling.impl.MarshallerReaderContext in project drools by kiegroup.

the class WorkItemInfo method getWorkItem.

public WorkItem getWorkItem(Environment env, InternalKnowledgeBase kBase) {
    this.env = env;
    if (workItem == null) {
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(workItemByteArray);
            MarshallerReaderContext context = new MarshallerReaderContext(bais, kBase, null, null, null, env);
            try {
                workItem = ProtobufInputMarshaller.readWorkItem(context);
            } catch (Exception e) {
                // for backward compatibility to be able to restore 5.x data
                try {
                    context.close();
                    bais = new ByteArrayInputStream(workItemByteArray);
                    context = new MarshallerReaderContext(bais, kBase, null, null, null, env);
                    workItem = InputMarshaller.readWorkItem(context);
                } catch (IOException e1) {
                    logger.error("Unable to read work item with InputMarshaller", e1);
                    // throw the original exception produced by failed protobuf op
                    throw new RuntimeException("Unable to read work item ", e);
                }
            }
            context.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("IOException while loading work item: " + e.getMessage());
        }
    }
    return workItem;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MarshallerReaderContext(org.drools.core.marshalling.impl.MarshallerReaderContext) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with MarshallerReaderContext

use of org.drools.core.marshalling.impl.MarshallerReaderContext in project drools by kiegroup.

the class FactHandleMarshallingTest method backwardsCompatibleEventFactHandleTest.

@Test
public void backwardsCompatibleEventFactHandleTest() throws IOException, ClassNotFoundException {
    InternalKnowledgeBase kBase = createKnowledgeBase();
    StatefulKnowledgeSessionImpl wm = createWorkingMemory(kBase);
    InternalFactHandle factHandle = createEventFactHandle(wm, kBase);
    // marshall/serialize workItem
    byte[] byteArray;
    {
        ObjectMarshallingStrategy[] strats = new ObjectMarshallingStrategy[] { MarshallerFactory.newSerializeMarshallingStrategy(), new MarshallerProviderImpl().newIdentityMarshallingStrategy() };
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        MarshallerWriteContext outContext = new MarshallerWriteContext(baos, null, null, null, new ObjectMarshallingStrategyStoreImpl(strats), true, true, null);
        OldOutputMarshallerMethods.writeFactHandle_v1(outContext, (ObjectOutputStream) outContext, outContext.objectMarshallingStrategyStore, 2, factHandle);
        outContext.close();
        byteArray = baos.toByteArray();
    }
    // unmarshall/deserialize workItem
    InternalFactHandle newFactHandle;
    {
        // Only put serialization strategy in
        ObjectMarshallingStrategy[] newStrats = new ObjectMarshallingStrategy[] { MarshallerFactory.newSerializeMarshallingStrategy() };
        ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
        MarshallerReaderContext inContext = new MarshallerReaderContext(bais, null, null, new ObjectMarshallingStrategyStoreImpl(newStrats), Collections.EMPTY_MAP, true, true, null);
        inContext.wm = wm;
        newFactHandle = InputMarshaller.readFactHandle(inContext);
        inContext.close();
    }
    assertTrue("Serialized FactHandle not the same as the original.", compareInstances(factHandle, newFactHandle));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) MarshallerProviderImpl(org.drools.core.marshalling.impl.MarshallerProviderImpl) MarshallerWriteContext(org.drools.core.marshalling.impl.MarshallerWriteContext) MarshallerReaderContext(org.drools.core.marshalling.impl.MarshallerReaderContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InternalFactHandle(org.drools.core.common.InternalFactHandle) ObjectOutputStream(java.io.ObjectOutputStream) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) ObjectMarshallingStrategyStoreImpl(org.drools.core.marshalling.impl.ObjectMarshallingStrategyStoreImpl) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 MarshallerReaderContext (org.drools.core.marshalling.impl.MarshallerReaderContext)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectOutputStream (java.io.ObjectOutputStream)1 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)1 MarshallerProviderImpl (org.drools.core.marshalling.impl.MarshallerProviderImpl)1 MarshallerWriteContext (org.drools.core.marshalling.impl.MarshallerWriteContext)1 ObjectMarshallingStrategyStoreImpl (org.drools.core.marshalling.impl.ObjectMarshallingStrategyStoreImpl)1 Test (org.junit.Test)1