Search in sources :

Example 76 with ObjectOutput

use of java.io.ObjectOutput in project drools by kiegroup.

the class JavaDialectRuntimeData method writeExternal.

/**
 * Handles the write serialization of the PackageCompilationData. Patterns in Rules may reference generated data which cannot be serialized by
 * default methods. The PackageCompilationData holds a reference to the generated bytecode. The generated bytecode must be restored before any Rules.
 */
public void writeExternal(ObjectOutput stream) throws IOException {
    KeyStoreHelper helper = new KeyStoreHelper();
    stream.writeBoolean(helper.isSigned());
    if (helper.isSigned()) {
        stream.writeObject(helper.getPvtKeyAlias());
    }
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(bos);
    out.writeInt(this.store.size());
    for (Entry<String, byte[]> entry : this.store.entrySet()) {
        out.writeObject(entry.getKey());
        out.writeObject(entry.getValue());
    }
    out.flush();
    out.close();
    byte[] buff = bos.toByteArray();
    stream.writeObject(buff);
    if (helper.isSigned()) {
        sign(stream, helper, buff);
    }
    stream.writeInt(this.invokerLookups.size());
    for (Entry<String, Wireable> entry : this.invokerLookups.entrySet()) {
        stream.writeObject(entry.getKey());
        stream.writeObject(entry.getValue());
    }
    stream.writeInt(this.classLookups.size());
    for (Entry<String, byte[]> entry : this.classLookups.entrySet()) {
        stream.writeObject(entry.getKey());
        stream.writeObject(entry.getValue());
    }
}
Also used : ObjectOutput(java.io.ObjectOutput) KeyStoreHelper(org.drools.core.util.KeyStoreHelper) Wireable(org.drools.core.spi.Wireable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 77 with ObjectOutput

use of java.io.ObjectOutput in project drools by kiegroup.

the class PackageDescrTest method testSerialization.

@Test
public void testSerialization() {
    PackageDescrBuilder builder = DescrFactory.newPackage().name("foo");
    String className = Person.class.getName();
    builder.newImport().target(className).end();
    PackageDescr descr = builder.getDescr();
    ImportDescr importDescr = new ImportDescr(className);
    ImportDescr badImportDescr = new ImportDescr(null);
    assertTrue(descr.getImports().contains(importDescr));
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(baos);
        descr.writeExternal(out);
        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
        PackageDescr newDescr = new PackageDescr();
        newDescr.readExternal(in);
        assertFalse(newDescr.getImports().contains(badImportDescr));
        assertTrue(newDescr.getImports().contains(importDescr));
    } catch (IOException ioe) {
        fail(ioe.getMessage());
    } catch (ClassNotFoundException cnfe) {
        fail(cnfe.getMessage());
    }
}
Also used : PackageDescrBuilder(org.drools.compiler.lang.api.PackageDescrBuilder) ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectInput(java.io.ObjectInput) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Aggregations

ObjectOutput (java.io.ObjectOutput)77 ObjectOutputStream (java.io.ObjectOutputStream)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)46 IOException (java.io.IOException)33 ObjectInput (java.io.ObjectInput)25 Test (org.junit.Test)20 ObjectInputStream (java.io.ObjectInputStream)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 WorkingMemory (org.drools.core.WorkingMemory)13 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)12 Pattern (org.drools.core.rule.Pattern)12 Consequence (org.drools.core.spi.Consequence)12 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)12 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 Declaration (org.drools.core.rule.Declaration)8 IntrospectionException (java.beans.IntrospectionException)7 InvalidRuleException (org.drools.core.rule.InvalidRuleException)7 ConsequenceException (org.drools.core.spi.ConsequenceException)7 OutputStream (java.io.OutputStream)6 ClassObjectType (org.drools.core.base.ClassObjectType)6