use of org.drools.core.common.DroolsObjectInputStream in project drools by kiegroup.
the class PersisterHelper method loadStrategiesIndex.
private static void loadStrategiesIndex(MarshallerReaderContext context, ProtobufMessages.Header _header) throws IOException, ClassNotFoundException {
for (ProtobufMessages.Header.StrategyIndex _entry : _header.getStrategyList()) {
ObjectMarshallingStrategy strategyObject = context.resolverStrategyFactory.getStrategyObject(_entry.getName());
if (strategyObject == null) {
throw new IllegalStateException("No strategy of type " + _entry.getName() + " available.");
}
context.usedStrategies.put(_entry.getId(), strategyObject);
Context ctx = strategyObject.createContext();
context.strategyContexts.put(strategyObject, ctx);
if (_entry.hasData() && ctx != null) {
ClassLoader classLoader = null;
if (context.classLoader != null) {
classLoader = context.classLoader;
} else if (context.kBase != null) {
classLoader = context.kBase.getRootClassLoader();
}
if (classLoader instanceof ProjectClassLoader) {
readRuntimeDefinedClasses(_header, (ProjectClassLoader) classLoader);
}
ctx.read(new DroolsObjectInputStream(_entry.getData().newInput(), classLoader));
}
}
}
use of org.drools.core.common.DroolsObjectInputStream in project drools by kiegroup.
the class MarshallingIssuesTest method testJBRULES_1946_2.
@Test
public void testJBRULES_1946_2() {
KieBase kbase = loadKnowledgeBase("../Sample.drl");
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DroolsObjectOutputStream oos = new DroolsObjectOutputStream(baos);
oos.writeObject(kbase);
oos.flush();
oos.close();
baos.flush();
baos.close();
byte[] serializedKb = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(serializedKb);
DroolsObjectInputStream ois = new DroolsObjectInputStream(bais);
KieBase kb2 = (KieBase) ois.readObject();
} catch (OptionalDataException ode) {
ode.printStackTrace();
fail("EOF? " + ode.eof);
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception: " + e.getMessage());
}
}
use of org.drools.core.common.DroolsObjectInputStream in project drools by kiegroup.
the class SerializedPackageMergeTest method testBuildAndSerializePackagesWithSamePackageName.
@Test
public void testBuildAndSerializePackagesWithSamePackageName() throws IOException, ClassNotFoundException {
// RHBRMS-2773
String str1 = "package com.sample\n" + "import org.drools.compiler.Person\n" + "global java.util.List list\n" + "rule R1 when\n" + " $p : Person( name == \"John\" )\n" + "then\n" + " list.add($p);" + "end\n";
String str2 = "package com.sample\n" + "import org.drools.compiler.Person\n" + "global java.util.List list\n" + "rule R2 when\n" + " $p : Person( name == \"Paul\" )\n" + "then\n" + " list.add($p);" + "end\n";
// Create 2 knowledgePackages separately (but these rules have the same package name)
KnowledgeBuilder builder1 = KnowledgeBuilderFactory.newKnowledgeBuilder();
builder1.add(ResourceFactory.newByteArrayResource(str1.getBytes()), ResourceType.DRL);
Collection<KiePackage> knowledgePackages1 = builder1.getKnowledgePackages();
KnowledgeBuilder builder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
builder2.add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL);
Collection<KiePackage> knowledgePackages2 = builder2.getKnowledgePackages();
// Combine the knowledgePackages
InternalKnowledgeBase knowledgeBase1 = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase1.addPackages(knowledgePackages1);
knowledgeBase1.addPackages(knowledgePackages2);
Collection<KiePackage> knowledgePackagesCombined = knowledgeBase1.getKiePackages();
// serialize
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new DroolsObjectOutputStream(baos);
out.writeObject(knowledgePackagesCombined);
out.flush();
out.close();
// deserialize
ObjectInputStream in = new DroolsObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
Collection<KiePackage> deserializedPackages = (Collection<KiePackage>) in.readObject();
// Use the deserialized knowledgePackages
InternalKnowledgeBase knowledgeBase2 = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase2.addPackages(deserializedPackages);
KieSession ksession = knowledgeBase2.newKieSession();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert(new org.drools.compiler.Person("John"));
ksession.insert(new org.drools.compiler.Person("Paul"));
ksession.fireAllRules();
assertEquals(2, list.size());
ksession.dispose();
}
use of org.drools.core.common.DroolsObjectInputStream in project drools by kiegroup.
the class EventAccessorRestoreTest method loadSession.
public KieSession loadSession(FileInputStream input) throws IOException, ClassNotFoundException {
KieSession ksession = null;
DroolsObjectInputStream droolsIn = new DroolsObjectInputStream(input, this.getClass().getClassLoader());
try {
KieBase kbase = (KieBase) droolsIn.readObject();
Marshaller mas = createMarshaller(kbase);
ksession = mas.unmarshall(droolsIn);
} catch (EOFException e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
droolsIn.close();
}
return ksession;
}
use of org.drools.core.common.DroolsObjectInputStream in project drools by kiegroup.
the class JPAPlaceholderResolverStrategy method unmarshal.
public Object unmarshal(Context context, ObjectInputStream ois, byte[] object, ClassLoader classloader) throws IOException, ClassNotFoundException {
ClassLoader clToUse = classloader;
if (this.classLoader != null) {
clToUse = this.classLoader;
}
DroolsObjectInputStream is = new DroolsObjectInputStream(new ByteArrayInputStream(object), clToUse);
String canonicalName = is.readUTF();
Object id = is.readObject();
EntityManager em = getEntityManager();
return em.find(Class.forName(canonicalName, true, (clToUse == null ? this.getClass().getClassLoader() : clToUse)), id);
}
Aggregations