use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class KieModuleRepoTest method testDeployTwoArtifactVersionsSameTime.
/**
* TESTS ----------------------------------------------------------------------------------------------------------------------
*/
// simultaneous requests to deploy two new deployments (different versions)
// for an empty/new GA artifactMap
@Test(timeout = 5000)
public void testDeployTwoArtifactVersionsSameTime() throws Exception {
final String groupId = "org";
final String artifactId = "one";
final String firstVersion = "1.0";
final String secondVersion = "1.0-NEW-FEATURE";
final CyclicBarrier storeOperationBarrier = new CyclicBarrier(2);
final CyclicBarrier threadsFinishedBarrier = new CyclicBarrier(3);
final Thread firstThread = new Thread(getStoreArtifactRunnable(kieModuleRepo, groupId, artifactId, firstVersion, storeOperationBarrier, threadsFinishedBarrier));
final Thread secondThread = new Thread(getStoreArtifactRunnable(kieModuleRepo, groupId, artifactId, secondVersion, storeOperationBarrier, threadsFinishedBarrier));
final ExecutorService executor = Executors.newFixedThreadPool(2);
firstThread.setName("normal");
executor.submit(firstThread);
secondThread.setName("newFeature");
executor.submit(secondThread);
try {
waitFor(threadsFinishedBarrier);
} finally {
executor.shutdownNow();
}
final String ga = groupId + ":" + artifactId;
final Map<ComparableVersion, KieModule> artifactMap = kieModuleRepo.kieModules.get(ga);
final ComparableVersion normalVersion = new ComparableVersion(firstVersion);
final KieModule normalKieModule = artifactMap.get(normalVersion);
final ComparableVersion newFeatureVersion = new ComparableVersion(secondVersion);
final KieModule newFeatureKieModule = artifactMap.get(newFeatureVersion);
assertNotNull("Race condition occurred: normal KieModule disappeared from KieModuleRepo!", normalKieModule);
assertNotNull("Race condition occurred: new feature KieModule disappeared from KieModuleRepo!", newFeatureKieModule);
}
use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class KieModuleRepoTest method testOldKieModulesLRUCache.
@Test
public void testOldKieModulesLRUCache() throws Exception {
// setup
setFinalField(maxSizeGaCacheField, null, 2);
setFinalField(maxSizeGaVersionsCacheField, null, 4);
final ReleaseIdImpl[] releaseIds = new ReleaseIdImpl[9];
for (int i = 0; i < releaseIds.length; ++i) {
final String artifactId = Character.toString((char) ('A' + i / 2));
releaseIds[i] = new ReleaseIdImpl("org", artifactId, "1." + i);
}
// store
for (int i = 0; i < releaseIds.length; ++i) {
final InternalKieModule kieModule = mock(InternalKieModule.class);
when(kieModule.getReleaseId()).thenReturn(releaseIds[i]);
when(kieModule.getCreationTimestamp()).thenReturn(10l);
kieModuleRepo.store(kieModule);
// in order to trigger storage to oldKieModules
kieModuleRepo.store(kieModule);
}
int maxSameGAModules = 0;
int maxGAs = 0;
for (final Map<ComparableVersion, KieModule> artifactMap : kieModuleRepo.kieModules.values()) {
maxGAs++;
if (artifactMap.size() > maxSameGAModules) {
maxSameGAModules = artifactMap.size();
}
}
assertTrue("The maximum of artifacts per GA should not grow past " + KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE + ": " + KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE + " < " + maxSameGAModules, KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE >= maxSameGAModules);
assertTrue("The number of GAs not grow past " + KieModuleRepo.MAX_SIZE_GA_CACHE + ": " + KieModuleRepo.MAX_SIZE_GA_CACHE + " > " + maxGAs, KieModuleRepo.MAX_SIZE_GA_CACHE >= maxGAs);
final int oldKieModulesSize = kieModuleRepo.oldKieModules.size();
final int maxOldKieModules = KieModuleRepo.MAX_SIZE_GA_CACHE * KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE;
assertTrue("KieModuleRepo old KieModules map is not limited in it's growth: " + oldKieModulesSize + " > " + maxOldKieModules, oldKieModulesSize <= maxOldKieModules);
}
use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class KieBuilderImpl method init.
private PomModel init() {
KieServices ks = KieServices.Factory.get();
results = new ResultsImpl();
// if pomXML is null it will generate a default, using default ReleaseId
// if pomXml is invalid, it assign pomModel to null
PomModel pomModel = getPomModel();
// if kModuleModelXML is null or invalid it will generate a default kModule, with a default kbase name
buildKieModuleModel();
if (pomModel != null) {
// creates ReleaseId from build pom
// If the pom was generated, it will be the same as teh default ReleaseId
releaseId = pomModel.getReleaseId();
// add all the pom dependencies to this builder ... not sure this is a good idea (?)
KieRepositoryImpl repository = (KieRepositoryImpl) ks.getRepository();
for (AFReleaseId dep : pomModel.getDependencies(DependencyFilter.COMPILE_FILTER)) {
KieModule depModule = repository.getKieModule(adapt(dep), pomModel);
if (depModule != null) {
addKieDependency(depModule);
}
}
} else {
// if the pomModel is null it means that the provided pom.xml is invalid so use the default releaseId
releaseId = KieServices.Factory.get().getRepository().getDefaultReleaseId();
}
return pomModel;
}
use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class KieBuilderImpl method buildAll.
public KieBuilder buildAll(BiFunction<InternalKieModule, ClassLoader, KieModuleKieProject> kprojectSupplier, Predicate<String> classFilter) {
PomModel pomModel = init();
// kModuleModel will be null if a provided pom.xml or kmodule.xml is invalid
if (!isBuilt() && kModuleModel != null) {
trgMfs = new MemoryFileSystem();
writePomAndKModule();
addKBasesFilesToTrg();
markSource();
MemoryKieModule memoryKieModule = new MemoryKieModule(adapt(releaseId), kModuleModel, trgMfs);
if (kieDependencies != null && !kieDependencies.isEmpty()) {
for (KieModule kieModule : kieDependencies) {
memoryKieModule.addKieDependency((InternalKieModule) kieModule);
}
}
if (pomModel != null) {
memoryKieModule.setPomModel(pomModel);
}
KieModuleKieProject kProject = kprojectSupplier.apply(memoryKieModule, classLoader);
for (ReleaseId unresolvedDep : memoryKieModule.getUnresolvedDependencies()) {
results.addMessage(Level.ERROR, "pom.xml", "Unresolved dependency " + unresolvedDep);
}
List<String> pmmlFileNames = getPmmlFileNames(srcMfs);
if (pmmlFileNames != null && !pmmlFileNames.isEmpty()) {
buildPMMLPojos(pmmlFileNames, kProject);
}
compileJavaClasses(kProject.getClassLoader(), classFilter);
buildKieProject(results, kProject, trgMfs);
kModule = kProject.getInternalKieModule();
}
return this;
}
use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class KieRepositoryImpl method addKieModule.
public KieModule addKieModule(Resource resource, Resource... dependencies) {
log.info("Adding KieModule from resource: " + resource);
KieModule kModule = getKieModule(resource);
if (dependencies != null && dependencies.length > 0) {
for (Resource depRes : dependencies) {
InternalKieModule depKModule = (InternalKieModule) getKieModule(depRes);
((InternalKieModule) kModule).addKieDependency(depKModule);
log.info("Adding KieModule dependency from resource: " + resource);
}
}
addKieModule(kModule);
return kModule;
}
Aggregations