Search in sources :

Example 1 with DroolsObjectInput

use of org.drools.core.common.DroolsObjectInput in project drools by kiegroup.

the class KnowledgeBaseImpl method readExternal.

// ------------------------------------------------------------
// Instance methods
// ------------------------------------------------------------
/**
 * Handles the read serialization of the Package. Patterns in Rules may reference generated data which cannot be serialized by default methods.
 * The Package uses PackageCompilationData to hold a reference to the generated bytecode; which must be restored before any Rules.
 * A custom ObjectInputStream, able to resolve classes against the bytecode in the PackageCompilationData, is used to restore the Rules.
 */
public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
    // PackageCompilationData must be restored before Rules as it has the ClassLoader needed to resolve the generated code references in Rules
    DroolsObjectInput droolsStream;
    boolean isDrools = in instanceof DroolsObjectInputStream;
    boolean wasDrools = in.readBoolean();
    if (wasDrools && !isDrools) {
        throw new IllegalArgumentException("The knowledge base was serialized using a DroolsObjectOutputStream. A DroolsObjectInputStream is required for deserialization.");
    }
    if (isDrools) {
        droolsStream = (DroolsObjectInput) in;
    } else {
        ByteArrayInputStream bytes = new ByteArrayInputStream((byte[]) in.readObject());
        droolsStream = new DroolsObjectInputStream(bytes);
    }
    boolean classLoaderCacheEnabled = droolsStream.readBoolean();
    Map<String, byte[]> store = (Map<String, byte[]>) droolsStream.readObject();
    this.rootClassLoader = createProjectClassLoader(droolsStream.getParentClassLoader(), store);
    droolsStream.setClassLoader(this.rootClassLoader);
    droolsStream.setKnowledgeBase(this);
    this.classFieldAccessorCache = new ClassFieldAccessorCache(this.rootClassLoader);
    this.config = (RuleBaseConfiguration) droolsStream.readObject();
    this.config.setClassLoader(droolsStream.getParentClassLoader());
    this.sessionConfiguration = new SessionConfigurationImpl(null, config.getClassLoader(), config.getChainedProperties());
    kieComponentFactory = getConfiguration().getComponentFactory();
    this.pkgs = (Map<String, InternalKnowledgePackage>) droolsStream.readObject();
    for (InternalKnowledgePackage pkg : this.pkgs.values()) {
        pkg.getDialectRuntimeRegistry().onAdd(this.rootClassLoader);
    }
    // PackageCompilationData must be restored before Rules as it has the ClassLoader needed to resolve the generated code references in Rules
    this.id = (String) droolsStream.readObject();
    this.workingMemoryCounter.set(droolsStream.readInt());
    this.processes = (Map<String, Process>) droolsStream.readObject();
    Class cls = null;
    try {
        cls = droolsStream.getParentClassLoader().loadClass(droolsStream.readUTF());
        this.factHandleFactory = (FactHandleFactory) cls.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        DroolsObjectInputStream.newInvalidClassException(cls, e);
    }
    for (InternalKnowledgePackage pkg : this.pkgs.values()) {
        pkg.getDialectRuntimeRegistry().onBeforeExecute();
        pkg.getClassFieldAccessorStore().setClassFieldAccessorCache(this.classFieldAccessorCache);
        pkg.getClassFieldAccessorStore().wire();
    }
    this.populateTypeDeclarationMaps();
    // read globals
    Map<String, String> globs = (Map<String, String>) droolsStream.readObject();
    populateGlobalsMap(globs);
    this.eventSupport = (KieBaseEventSupport) droolsStream.readObject();
    this.eventSupport.setKnowledgeBase(this);
    this.reteooBuilder = (ReteooBuilder) droolsStream.readObject();
    this.reteooBuilder.setRuleBase(this);
    this.rete = (Rete) droolsStream.readObject();
    this.resolvedReleaseId = (ReleaseId) droolsStream.readObject();
    ((DroolsObjectInputStream) droolsStream).bindAllExtractors(this);
    if (!isDrools) {
        droolsStream.close();
    }
    this.getConfiguration().getComponentFactory().getTraitFactory().setRuleBase(this);
    rewireReteAfterDeserialization();
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) Process(org.kie.api.definition.process.Process) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) DroolsObjectInput(org.drools.core.common.DroolsObjectInput) ByteArrayInputStream(java.io.ByteArrayInputStream) SessionConfigurationImpl(org.drools.core.SessionConfigurationImpl) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 SessionConfigurationImpl (org.drools.core.SessionConfigurationImpl)1 ClassFieldAccessorCache (org.drools.core.base.ClassFieldAccessorCache)1 DroolsObjectInput (org.drools.core.common.DroolsObjectInput)1 DroolsObjectInputStream (org.drools.core.common.DroolsObjectInputStream)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 Process (org.kie.api.definition.process.Process)1