private ClassLoader getRootClassLoader() { KieBase kbase = ((InternalKnowledgeBase) kruntime.getKieBase()); if (kbase != null) { return ((InternalKnowledgeBase) kbase).getRootClassLoader(); } CompositeClassLoader result = new CompositeClassLoader(); result.addClassLoader(Thread.currentThread().getContextClassLoader()); return result; }
public static Object unmarshallVariableValue(MarshallerReaderContext context, JBPMMessages.Variable _variable) throws IOException, ClassNotFoundException { if(_variable.getValue() == null || _variable.getValue().isEmpty()){ return null; } ObjectMarshallingStrategy strategy = context.usedStrategies.get( _variable.getStrategyIndex() ); Object value = strategy.unmarshal( context.strategyContexts.get( strategy ), context, _variable.getValue().toByteArray(), (context.kBase == null)?null:context.kBase.getRootClassLoader() ); return value; }
protected KieBase getKnowledgeBase(KieBaseConfiguration kBaseConfig) { KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kBaseConfig); try { kbase = SerializationHelper.serializeObject(kbase, ((InternalKnowledgeBase) kbase).getRootClassLoader()); } catch (Exception e) { throw new RuntimeException(e); } return kbase; }
@Test(timeout=10000) public void testSerializeKieBaseWithTraits() { // DRL-1123 String drl = "package org.drools.test; " + "import " + StudentImpl.class.getName() + "; " + "import " + IStudent.class.getName() + "; " + "global java.util.List list; " + "rule Test1 " + "when " + " StudentImpl( this isA IStudent.class ) " + "then list.add( 1 ); end " + "rule Test2 " + "when " + " IStudent( this isA StudentImpl.class ) " + "then list.add( 2 ); end " + ""; KieBase kbase = getKieBaseFromString( drl ); try { kbase = SerializationHelper.serializeObject( kbase, ((InternalKnowledgeBase) kbase).getRootClassLoader() ); } catch (Exception e) { throw new RuntimeException(e); } List list = new ArrayList( ); TraitFactory.setMode( mode, kbase ); KieSession knowledgeSession = kbase.newKieSession(); knowledgeSession.setGlobal( "list", list ); knowledgeSession.insert( new StudentImpl( ) ); assertEquals( 2, knowledgeSession.fireAllRules() ); assertEquals( Arrays.asList( 1, 2 ), list ); }
@Test public void testKieBaseSerialization() throws Exception { // DROOLS-944 String drl = "import " + Container.class.getCanonicalName() + ";" + "rule R1 when\n" + " Container($offer : objects[\"1-CZ26IQW\"] != null)\n" + "then\n" + "end\n" + "\n" + "rule R2 when\n" + " Container($offer : objects[\"1-CZ26IR8\"] != null)\n" + "then\n" + "end\n"; KieBase kbase1 = new KieHelper().addContent( drl, ResourceType.DRL ).build(); KieBase kbase2 = SerializationHelper.serializeObject( kbase1, ( (InternalKnowledgeBase) kbase1 ).getRootClassLoader() ); assertTrue( ReteComparator.areEqual( kbase1, kbase2 ) ); }
@Test public void testClassNotFoundAfterDeserialization() throws Exception { // JBRULES-3670 String drl = "package completely.other.deal;\n" + "\n" + "declare Person\n" + " firstName : String\n" + " lastName : String\n" + "end\n" + "\n" + "rule \"now use it B\"\n" + " when\n" + " Person( $christianName, $surname; )\n" + " then\n" + " insert( new Person( $christianName, null ) );\n" + "end"; KieBase kbase1 = new KieHelper().addContent( drl, ResourceType.DRL ).build(); KieBase kbase2 = SerializationHelper.serializeObject( kbase1, ( (InternalKnowledgeBase) kbase1 ).getRootClassLoader() ); assertTrue( ReteComparator.areEqual( kbase1, kbase2 ) ); }
@Before public void setUp() { this.kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(); pkg = new KnowledgePackageImpl("org.droos.test"); pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader())); JavaDialectRuntimeData data = new JavaDialectRuntimeData(); data.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader()); pkg.getDialectRuntimeRegistry().setDialectData("java", data); // we need to add one rule to the package because the previous deadlock was encountered // while removing rules from a package when said package is removed from the rulebase rule = new RuleImpl("Test"); rule.setDialect("java"); rule.setConsequence(new Consequence() { public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception { } public String getName() { return "default"; } }); pkg.addRule(rule); kBase.addPackage(pkg); }
private ClassLoader getRootClassLoader() { KieBase kbase = ((InternalKnowledgeBase) kruntime.getKieBase()); if (kbase != null) { return ((InternalKnowledgeBase) kbase).getRootClassLoader(); } CompositeClassLoader result = new CompositeClassLoader(); result.addClassLoader(Thread.currentThread().getContextClassLoader()); return result; }
public synchronized Library init(KieContext kcontext) { if (emf == null) { final ClassLoader tccl = Thread.currentThread().getContextClassLoader(); try { // https://issues.jboss.org/browse/DROOLS-1108 ClassLoader cl = ((InternalKnowledgeBase)kcontext.getKieRuntime().getKieBase()).getRootClassLoader(); Thread.currentThread().setContextClassLoader(cl); emf = Persistence.createEntityManagerFactory("library"); } finally { Thread.currentThread().setContextClassLoader(tccl); } new Init(emf).transact(); } return this; }
data.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader()); pkg.getDialectRuntimeRegistry().setDialectData("java", data); rule = new RuleImpl("Test");
public static Object unmarshallVariableValue(MarshallerReaderContext context, JBPMMessages.Variable _variable) throws IOException, ClassNotFoundException { if(_variable.getValue() == null || _variable.getValue().isEmpty()){ return null; } ObjectMarshallingStrategy strategy = context.usedStrategies.get( _variable.getStrategyIndex() ); Object value = strategy.unmarshal( context.strategyContexts.get( strategy ), context, _variable.getValue().toByteArray(), (context.kBase == null)?null:context.kBase.getRootClassLoader() ); return value; }
@Test public void testCCEAfterDeserialization() throws Exception { // DROOLS-1155 String drl = "function boolean checkLength(int length) { return true; }\n" + "rule R dialect \"mvel\" when\n" + " Boolean()" + " String( $length : length )\n" + " eval( checkLength($length) )\n" + " ( Integer( ) or eval( true ) )\n" + "then\n" + "end"; KieBase kbase1 = new KieHelper().addContent( drl, ResourceType.DRL ).build(); KieSession ksession1 = kbase1.newKieSession(); ksession1.insert( true ); ksession1.insert( "test" ); assertEquals(1, ksession1.fireAllRules()); KieBase kbase2 = SerializationHelper.serializeObject( kbase1, ( (InternalKnowledgeBase) kbase1 ).getRootClassLoader() ); KieSession ksession2 = kbase2.newKieSession(); ksession2.insert( true ); ksession2.insert( "test" ); assertEquals(1, ksession2.fireAllRules()); }
newPkg.getDialectRuntimeRegistry().onAdd( kBase.getRootClassLoader() ); newPkg.getDialectRuntimeRegistry().onBeforeExecute();
public ClassLoader getClassLoader(CommandExecutor exec) { ClassLoader cl = null; if (exec instanceof StatefulKnowledgeSessionImpl) { cl = ((StatefulKnowledgeSessionImpl)exec).getKnowledgeBase().getRootClassLoader(); } else if (exec instanceof StatelessKnowledgeSessionImpl) { cl = ((StatelessKnowledgeSessionImpl)exec).getKnowledgeBase().getRootClassLoader(); } else if (exec instanceof CommandBasedStatefulKnowledgeSession) { cl = ((InternalKnowledgeBase)((CommandBasedStatefulKnowledgeSession)exec).getKieBase()).getRootClassLoader(); } return cl; }
protected KnowledgeBase getKnowledgeBase(KieBaseConfiguration kBaseConfig) { kBaseConfig.setOption(phreak); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kBaseConfig); try { kbase = org.drools.compiler.integrationtests.SerializationHelper.serializeObject(kbase, ((InternalKnowledgeBase)kbase).getRootClassLoader()); } catch (Exception e) { throw new RuntimeException(e); } return kbase; }
public void run(final Scenario scenario) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException, InvalidClockTypeException { final Map<String, Object> populatedData = new HashMap<String, Object>(); final Map<String, Object> globalData = new HashMap<String, Object>(); // This looks safe! final KieBase kieBase = ksession.getKieBase(); final ClassLoader classloader2 = ((InternalKnowledgeBase) kieBase).getRootClassLoader(); final ClassTypeResolver resolver = new ClassTypeResolver(getImports(scenario), classloader2); this.workingMemoryWrapper = new TestScenarioKSessionWrapper(ksession, resolver, populatedData, globalData, scenarioUsesTimeWalk(scenario)); this.factPopulatorFactory = new FactPopulatorFactory(populatedData, globalData, resolver); this.factPopulator = new FactPopulator(ksession, populatedData); MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true; scenario.setLastRunResult(new Date()); populateGlobals(scenario.getGlobals()); applyFixtures(scenario.getFixtures(), createScenarioSettings(scenario)); }
String outXml = null; ClassLoader orig = null; ClassLoader cl = ((StatefulKnowledgeSessionImpl)ksession).getKnowledgeBase().getRootClassLoader(); try { orig = Thread.currentThread().getContextClassLoader();
@Test public void testInsertObjectWithDeclaredFactAndQuery() throws Exception { String str = ""; str += "package org.foo \n"; str += "declare Whee \n\ttype: String\n\tprice: Integer\n\toldPrice: Integer\nend\n"; str += "rule rule1 \n"; str += " when \n"; str += " $c : Whee(price < 30) \n"; str += " then \n"; str += " $c.setPrice( $c.getPrice() + 5 ); \n update($c);\n"; str += "end\n"; str += "query results\n"; str += " w: Whee(price > 0)"; str += "end\n"; KieSession ksession = getKieSession(ResourceFactory.newByteArrayResource(str.getBytes())); setExec(ksession); ClassLoader cl = ((StatefulKnowledgeSessionImpl)ksession).getKnowledgeBase().getRootClassLoader(); ClassLoader orig = null; try { orig = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(cl); String outXml = execContent("testInsertObjectWithDeclaredFactAndQuery.in.1"); ExecutionResults results = unmarshalOutXml(outXml, ExecutionResults.class); FactHandle fh = (FactHandle)results.getFactHandle("outStilton"); outXml = execContent("testInsertObjectWithDeclaredFactAndQuery.in.2"); assertXMLEqual(getContent("testInsertObjectWithDeclaredFactAndQuery.expected.1", fh.toExternalForm()), outXml); } finally { Thread.currentThread().setContextClassLoader(orig); } }
@Test public void testClassNotFoundAfterDeserialization() throws Exception { // JBRULES-3670 String drl = "package completely.other.deal;\n" + "\n" + "declare Person\n" + " firstName : String\n" + " lastName : String\n" + "end\n" + "\n" + "rule \"now use it B\"\n" + " when\n" + " Person( $christianName, $surname; )\n" + " then\n" + " insert( new Person( $christianName, null ) );\n" + "end"; KieBase kbase1 = new KieHelper().addContent( drl, ResourceType.DRL ).build(); KieBase kbase2 = SerializationHelper.serializeObject( kbase1, ( (InternalKnowledgeBase) kbase1 ).getRootClassLoader() ); ReteComparator.compare( kbase1, kbase2 ); }
ClassLoader cl = ((StatefulKnowledgeSessionImpl)ksession).getKnowledgeBase().getRootClassLoader(); XStream xstream = BatchExecutionHelper.newXStreamMarshaller(); xstream.setClassLoader(cl);