Skip to content

Commit

Permalink
[DROOLS-356] make KieScanner to work with a KieContainer using a vers…
Browse files Browse the repository at this point in the history
…ion range
  • Loading branch information
mariofusco committed Nov 28, 2013
1 parent 3fe0533 commit ba79df0
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 16 deletions.
@@ -1,5 +1,6 @@
package org.drools.compiler.kie.builder.impl;

import org.kie.api.builder.ReleaseId;
import org.kie.api.builder.model.KieBaseModel;
import org.kie.api.builder.model.KieSessionModel;
import org.kie.api.runtime.KieContainer;
Expand Down Expand Up @@ -40,4 +41,6 @@ public interface InternalKieContainer extends KieContainer {
* Returns the KieSessionModel for the KieSession with the given name
*/
KieSessionModel getKieSessionModel(String kSessionName);

ReleaseId getContainerReleaseId();
}
Expand Up @@ -72,17 +72,27 @@ public class KieContainerImpl

private final KieRepository kr;

public KieContainerImpl(KieProject kProject,
KieRepository kr) {
private ReleaseId containerReleaseId;

public KieContainerImpl(KieProject kProject, KieRepository kr) {
this.kr = kr;
this.kProject = kProject;
kProject.init();
}

public KieContainerImpl(KieProject kProject, KieRepository kr, ReleaseId containerReleaseId) {
this(kProject, kr);
this.containerReleaseId = containerReleaseId;
}

public ReleaseId getReleaseId() {
return kProject.getGAV();
}

public ReleaseId getContainerReleaseId() {
return containerReleaseId != null ? containerReleaseId : getReleaseId();
}

public void updateToVersion(ReleaseId newReleaseId) {
if( kProject instanceof ClasspathKieProject ) {
throw new UnsupportedOperationException( "It is not possible to update a classpath container to a new version." );
Expand Down
Expand Up @@ -258,13 +258,12 @@ KieModule load(ReleaseId releaseId, VersionRange versionRange) {
return kieModule;
}

if (versionRange.upperBound == null) {
return artifactMap.lastEntry().getValue();
}

Map.Entry<ComparableVersion, KieModule> entry = versionRange.upperInclusive ?
artifactMap.ceilingEntry(new ComparableVersion(versionRange.upperBound)) :
artifactMap.lowerEntry(new ComparableVersion(versionRange.upperBound));
Map.Entry<ComparableVersion, KieModule> entry =
versionRange.upperBound == null ?
artifactMap.lastEntry() :
versionRange.upperInclusive ?
artifactMap.floorEntry(new ComparableVersion(versionRange.upperBound)) :
artifactMap.lowerEntry(new ComparableVersion(versionRange.upperBound));

if ( entry == null ) {
return null;
Expand All @@ -274,8 +273,8 @@ KieModule load(ReleaseId releaseId, VersionRange versionRange) {
return entry.getValue();
}

int versionComparison = entry.getKey().compareTo(new ComparableVersion(versionRange.lowerBound));
return versionComparison > 0 || (versionComparison == 0 && versionRange.lowerInclusive) ? entry.getValue() : null;
int comparison = entry.getKey().compareTo(new ComparableVersion(versionRange.lowerBound));
return comparison > 0 || (comparison == 0 && versionRange.lowerInclusive) ? entry.getValue() : null;
}
}

Expand Down
Expand Up @@ -86,7 +86,7 @@ public KieContainer newKieContainer(ReleaseId releaseId) {
throw new RuntimeException("Cannot find KieModule: " + releaseId);
}
KieProject kProject = new KieModuleKieProject( kieModule );
return new KieContainerImpl( kProject, getRepository() );
return new KieContainerImpl( kProject, getRepository(), releaseId );
}


Expand Down
Expand Up @@ -67,7 +67,8 @@ public String getType() {
}

public boolean isFixedVersion() {
return !isSnapshot() && !version.equals("LATEST") && !version.equals("RELEASE");
return !isSnapshot() && !version.equals("LATEST") && !version.equals("RELEASE") &&
version.indexOf('(') < 0 && version.indexOf(')') < 0 && version.indexOf('[') < 0 && version.indexOf(']') < 0;
}

public boolean isSnapshot() {
Expand Down
@@ -1,5 +1,6 @@
package org.kie.scanner;

import org.drools.compiler.kie.builder.impl.InternalKieContainer;
import org.drools.compiler.kproject.ReleaseIdImpl;
import org.drools.compiler.kproject.models.KieModuleModelImpl;
import org.kie.api.builder.ReleaseId;
Expand Down Expand Up @@ -48,7 +49,7 @@ public class KieRepositoryScannerImpl implements InternalKieScanner {

public void setKieContainer(KieContainer kieContainer) {
this.kieContainer = kieContainer;
ReleaseId releaseId = kieContainer.getReleaseId();
ReleaseId releaseId = ((InternalKieContainer)kieContainer).getContainerReleaseId();
if (releaseId == null) {
throw new RuntimeException("The KieContainer's ReleaseId cannot be null. Are you using a KieClasspathContainer?");
}
Expand Down Expand Up @@ -200,8 +201,6 @@ public void scanNow() {
}
for (Artifact artifact : updatedArtifacts) {
DependencyDescriptor depDescr = new DependencyDescriptor(artifact);
usedDependencies.remove(depDescr);
usedDependencies.add(depDescr);
updateKieModule(artifact, depDescr.getGav());
}
log.info("The following artifacts have been updated: " + updatedArtifacts);
Expand Down
34 changes: 34 additions & 0 deletions kie-ci/src/test/java/org/kie/scanner/KieRepositoryScannerTest.java
Expand Up @@ -80,6 +80,40 @@ public void testKScanner() throws Exception {
checkKSession(ksession2, "rule2", "rule3");
}

@Test @Ignore
public void testKScannerWithRange() throws Exception {
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "scanner-test", "1.0.1");
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "scanner-test", "1.0.2");
ReleaseId releaseRange = ks.newReleaseId("org.kie", "scanner-test", "[1.0.0,)");

InternalKieModule kJar1 = createKieJar(ks, releaseId1, "rule1", "rule2");
KieContainer kieContainer = ks.newKieContainer(releaseRange);

MavenRepository repository = getMavenRepository();
repository.deployArtifact(releaseId1, kJar1, kPom);

// create a ksesion and check it works as expected
KieSession ksession = kieContainer.newKieSession("KSession1");
checkKSession(ksession, "rule1", "rule2");

// create a new kjar
InternalKieModule kJar2 = createKieJar(ks, releaseId2, "rule2", "rule3");

// deploy it on maven
repository.deployArtifact(releaseId2, kJar2, kPom);

// since I am not calling start() on the scanner it means it won't have automatic scheduled scanning
KieScanner scanner = ks.newKieScanner(kieContainer);

// scan the maven repo to get the new kjar version and deploy it on the kcontainer
scanner.scanNow();

// create a ksesion and check it works as expected
KieSession ksession2 = kieContainer.newKieSession("KSession1");
checkKSession(ksession2, "rule2", "rule3");
}

@Test @Ignore
public void testKScannerWithKJarContainingClasses() throws Exception {
testKScannerWithType(false);
Expand Down

0 comments on commit ba79df0

Please sign in to comment.