[...] Therefore, OSGi does not add that package to the
bundle's ClassLoader and you get a ClassNotFoundException when Drools
attempts to find the Class.
Even when adding the Drools runtime jars in the bundle using them I run
into problems.
When using Drools (version 5.0.1) from within an OSGi context the
NoClassDefFoundError seems unavoidable. The really strange thing is that
the error is triggered by drools itself and in an attempt to access a
class from within the very same jar file. Have a look at the foolowing
stacktrace:
java.lang.NoClassDefFoundError:
org/drools/base/extractors/BaseObjectClassFieldReader
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at
org.drools.base.ClassFieldAccessorCache$ByteArrayClassLoader.defineClass(ClassFieldAccessorCache.java:367)
at
org.drools.base.ClassFieldAccessorFactory.getClassFieldReader(ClassFieldAccessorFactory.java:135)
at
org.drools.base.ClassFieldAccessorCache$CacheEntry.getReadAccessor(ClassFieldAccessorCache.java:315)
at
org.drools.base.ClassFieldAccessorCache.getReadAcessor(ClassFieldAccessorCache.java:245)
at
org.drools.base.ClassFieldAccessorStore.wire(ClassFieldAccessorStore.java:379)
at
org.drools.base.ClassFieldAccessorStore.getReader(ClassFieldAccessorStore.java:97)
at
org.drools.base.ClassFieldAccessorStore.getReader(ClassFieldAccessorStore.java:74)
at
org.drools.rule.builder.PatternBuilder.getFieldReadAccessor(PatternBuilder.java:1031)
at
org.drools.rule.builder.PatternBuilder.build(PatternBuilder.java:326)
at
org.drools.rule.builder.PatternBuilder.buildConstraint(PatternBuilder.java:239)
at
org.drools.rule.builder.PatternBuilder.build(PatternBuilder.java:196)
at
org.drools.rule.builder.PatternBuilder.build(PatternBuilder.java:104)
at
org.drools.rule.builder.GroupElementBuilder.build(GroupElementBuilder.java:69)
at org.drools.rule.builder.RuleBuilder.build(RuleBuilder.java:68)
at
org.drools.compiler.PackageBuilder.addRule(PackageBuilder.java:1159)
at
org.drools.compiler.PackageBuilder.addPackage(PackageBuilder.java:649)
at
org.drools.compiler.PackageBuilder.addPackageFromDrl(PackageBuilder.java:278)
at
org.drools.compiler.PackageBuilder.addKnowledgeResource(PackageBuilder.java:510)
at
org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:31)
at
com.apc.config.configurator.rules.decisiontable.DecisionTableHelper.<init>(DecisionTableHelper.java:53)
The actual Drools code is a simple use of a DecisionTable:
public DecisionTableHelper(String path) {
DecisionTableConfiguration dtableconfiguration =
KnowledgeBuilderFactory.newDecisionTableConfiguration();
dtableconfiguration.setInputType(DecisionTableInputType.XLS);
KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();
Resource pathResource = ResourceFactory.newClassPathResource(path,
getClass());
kbuilder.add(pathResource, ResourceType.DTABLE,
dtableconfiguration);
if ( kbuilder.hasErrors() ) {
System.err.print( kbuilder.getErrors() );
return;
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
ksession = kbase.newStatelessKnowledgeSession();
}
As seen from the stack trace the problem occurs when adding the resource
to the KnowledgeBuilder.
Originally I attempted to created a Drools 5.0.1 OSGi bundle that I could
just add to the Eclipse targetplatform. However – as a result of the above
described issue – the setup is now reduced to adding the Drools runtime
jars directly to the bundle in which they are to be used. The problem
remains.
I have read Faron Duttons post regarding the ClassLoader behavior of OSGi.
I have added the BundleProxyClassLoader from the post and tried creating a
KnowledgeBuilderConfiguration based on this ClassLoader. It seems however
that the problem remains as the ByteArrayClassLoader (where the issue is
triggerd) does not use the ClassLoader specified in the
KnowledgeBuilderConfiguration.
Can anyone sketch a setup so that I can use Drools from within an Eclipse
plugin?
/Lasse