kbasesToRemove.add( kbaseName );
} else {
// attaching the builder to the kbase
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder( (KnowledgeBase) kBaseEntry.getValue() );
PackageBuilder pkgbuilder = kbuilder instanceof PackageBuilder ? ((PackageBuilder) kbuilder) : ((KnowledgeBuilderImpl)kbuilder).getPackageBuilder();
CompositeKnowledgeBuilder ckbuilder = kbuilder.batch();
List<String> modifiedClasses = new ArrayList<String>();
int fileCount = 0;
// then update and add new resources
for ( ResourceChangeSet rcs : cs.getChanges().values() ) {
if ( rcs.getChangeType() != ChangeType.REMOVED ) {
String resourceName = rcs.getResourceName();
if ( !resourceName.endsWith( ".properties" ) && filterFileInKBase(newKM, kieBaseModel, resourceName) ) {
Resource resource = currentKM.getResource(resourceName);
List<ResourceChange> changes = rcs.getChanges();
if ( ! changes.isEmpty() ) {
// we need to deal with individual parts of the resource
fileCount += AbstractKieModule.updateResource( ckbuilder,
newKM,
resourceName,
rcs ) ? 1 : 0;
} else {
// the whole resource has to handled
if( rcs.getChangeType() == ChangeType.UPDATED ) {
pkgbuilder.removeObjectsGeneratedFromResource( resource );
}
fileCount += newKM.addResourceToCompiler(ckbuilder, resourceName) ? 1 : 0;
}
} else if ( resourceName.endsWith( ".class" ) ) {
modifiedClasses.add(resourceName);
fileCount++;
}
}
KieBase kBase = kBaseEntry.getValue();
for ( ResourceChangeSet.RuleLoadOrder loadOrder : rcs.getLoadOrder() ) {
Rule rule = (Rule) ((KnowledgePackageImp)kBase.getKiePackage( loadOrder.getPkgName() )).getRule( loadOrder.getRuleName() );
if ( rule != null ) {
// rule can be null, if it didn't exist before
rule.setLoadOrder( loadOrder.getLoadOrder() );
}
}
}
pkgbuilder.startPackageUpdate();
try {
// remove resources first
for ( ResourceChangeSet rcs : cs.getChanges().values() ) {
if ( rcs.getChangeType() == ChangeType.REMOVED ) {
String resourceName = rcs.getResourceName();
if ( !resourceName.endsWith( ".properties" ) && filterFileInKBase(newKM, kieBaseModel, resourceName) ) {
pkgbuilder.removeObjectsGeneratedFromResource( currentKM.getResource( resourceName ) );
}
}
}
if( fileCount > 0 ) {
Set<String> modifiedPackages = new HashSet<String>();
if (!modifiedClasses.isEmpty()) {
ClassLoader rootClassLoader = pkgbuilder.getRootClassLoader();
if ( rootClassLoader instanceof ProjectClassLoader ) {
ProjectClassLoader projectClassLoader = (ProjectClassLoader) rootClassLoader;
projectClassLoader.reinitTypes();
for (String resourceName : modifiedClasses) {
String className = convertResourceToClassName( resourceName );
byte[] bytes = newKM.getBytes(resourceName);
Class<?> clazz = projectClassLoader.defineClass(className, resourceName, bytes);
modifiedPackages.add(clazz.getPackage().getName());
}
pkgbuilder.setAllRuntimesDirty(modifiedPackages);
}
}
ckbuilder.build();
PackageBuilderErrors errors = pkgbuilder.getErrors();
if ( !errors.isEmpty() ) {
for ( KnowledgeBuilderError error : errors.getErrors() ) {
results.addMessage(error);