// Creates first class loader and use it to load fact classes
ClassLoader loader1 = new SubvertedClassLoader( new URL[]{getClass().getResource( "/testEnum.jar" )},
this.getClass().getClassLoader() );
// create a builder with the given classloader
KnowledgeBuilderConfiguration conf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration( null,
loader1 );
KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder( conf );
builder.add( ResourceFactory.newInputStreamResource( getClass().getResourceAsStream( "test_EnumSerialization.drl" ) ),
ResourceType.DRL );
if ( builder.hasErrors() ) {
fail( builder.getErrors().toString() );
}
Collection<KnowledgePackage> pkgs = builder.getKnowledgePackages();
KnowledgePackage pkg = pkgs.iterator().next();
// serialize out
byte[] out = DroolsStreamUtils.streamOut( ((KnowledgePackageImp) pkg).pkg );
// adding original packages to a kbase just to make sure they are fine
KieBaseConfiguration kbaseConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration( null,
loader1 );
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase( kbaseConf );
kbase.addKnowledgePackages( pkgs );
// now, create another classloader and make sure it has access to the classes
ClassLoader loader2 = new SubvertedClassLoader( new URL[]{getClass().getResource( "/testEnum.jar" )},
this.getClass().getClassLoader() );
// create another builder
KnowledgeBuilderConfiguration conf2 = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration( null,
loader2 );
KnowledgeBuilder builder2 = KnowledgeBuilderFactory.newKnowledgeBuilder( conf2 );
builder2.add( ResourceFactory.newByteArrayResource( out ),
ResourceType.PKG );
Collection<KnowledgePackage> pkgs2 = builder2.getKnowledgePackages();