Search in sources :

Example 1 with InternalKieScanner

use of org.drools.compiler.kie.builder.impl.InternalKieScanner in project drools by kiegroup.

the class KieScannersRegistry method getAllKieScanners.

public static Collection<InternalKieScanner> getAllKieScanners() {
    List<InternalKieScanner> allScanners = new ArrayList<InternalKieScanner>();
    Iterator<WeakReference<InternalKieScanner>> i = scanners.iterator();
    while (i.hasNext()) {
        InternalKieScanner scanner = i.next().get();
        if (scanner == null) {
            i.remove();
        } else {
            allScanners.add(scanner);
        }
    }
    return allScanners;
}
Also used : InternalKieScanner(org.drools.compiler.kie.builder.impl.InternalKieScanner) WeakReference(java.lang.ref.WeakReference) ArrayList(java.util.ArrayList)

Example 2 with InternalKieScanner

use of org.drools.compiler.kie.builder.impl.InternalKieScanner in project drools by kiegroup.

the class KieRepositoryScannerTest method testKScannerWithRange_withListener.

@Test
public void testKScannerWithRange_withListener() throws Exception {
    KieServices ks = KieServices.Factory.get();
    KieMavenRepository repository = getKieMavenRepository();
    String artifactId = "scanner-range-test-withlistener";
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", artifactId, "1.0.1");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", artifactId, "1.0.2");
    // Because KieScanner also check the local repo, ensure the artifact are not deployed on the local maven repo
    repository.removeLocalArtifact(releaseId1);
    repository.removeLocalArtifact(releaseId2);
    ReleaseId releaseRange = ks.newReleaseId("org.kie", artifactId, "[1.0.0,)");
    InternalKieModule kJar1 = createKieJar(ks, releaseId1, "rule1", "rule2");
    KieContainer kieContainer = ks.newKieContainer(releaseRange);
    List<KieScannerEvent> events = new ArrayList<>();
    InternalKieScanner scanner = (KieRepositoryScannerImpl) ks.newKieScanner(kieContainer);
    KieScannerEventListener listener = new KieScannerEventListener() {

        public void onKieScannerStatusChangeEvent(KieScannerStatusChangeEvent statusChange) {
            LOG.info("KieScanner event: {}", statusChange);
            events.add(statusChange);
        }

        public void onKieScannerUpdateResultsEvent(KieScannerUpdateResultsEvent updtaeResults) {
            LOG.info("KieScanner event: {}", updtaeResults);
            events.add(updtaeResults);
        }
    };
    scanner.addListener(listener);
    scanner.scanNow();
    // Because 1.0.2 does not exist, will perform a scan but will NOT update.
    assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.SCANNING)));
    assertThat(events, CoreMatchers.not(CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.UPDATING))));
    events.clear();
    repository.installArtifact(releaseId1, kJar1, createKPom(fileManager, releaseId1));
    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.installArtifact(releaseId2, kJar2, createKPom(fileManager, releaseId2));
    // since I am not calling start() on the scanner it means it won't have automatic scheduled scanning
    assertEquals(releaseId1, scanner.getCurrentReleaseId());
    assertEquals(InternalKieScanner.Status.STOPPED, scanner.getStatus());
    // scan the maven repo to get the new kjar version and deploy it on the kcontainer
    scanner.scanNow();
    assertEquals(releaseId2, scanner.getCurrentReleaseId());
    assertEquals(InternalKieScanner.Status.STOPPED, scanner.getStatus());
    // create a ksesion and check it works as expected
    KieSession ksession2 = kieContainer.newKieSession("KSession1");
    checkKSession(ksession2, "rule2", "rule3");
    assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.SCANNING)));
    assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.UPDATING)));
    assertTrue(events.get(2) instanceof KieScannerUpdateResultsEventImpl);
    assertFalse(((KieScannerUpdateResultsEventImpl) events.get(2)).getResults().hasMessages(Message.Level.ERROR));
    events.clear();
    ks.getRepository().removeKieModule(releaseId1);
    ks.getRepository().removeKieModule(releaseId2);
    scanner.removeListener(listener);
    assertEquals(0, scanner.getListeners().size());
    repository.removeLocalArtifact(releaseId1);
    repository.removeLocalArtifact(releaseId2);
}
Also used : KieScannerEventListener(org.kie.api.event.kiescanner.KieScannerEventListener) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieScannerUpdateResultsEventImpl(org.kie.scanner.event.KieScannerUpdateResultsEventImpl) InternalKieScanner(org.drools.compiler.kie.builder.impl.InternalKieScanner) KieScannerStatusChangeEventImpl(org.kie.scanner.event.KieScannerStatusChangeEventImpl) KieMavenRepository.getKieMavenRepository(org.kie.scanner.KieMavenRepository.getKieMavenRepository) KieScannerEvent(org.kie.api.event.kiescanner.KieScannerEvent) KieScannerStatusChangeEvent(org.kie.api.event.kiescanner.KieScannerStatusChangeEvent) KieSession(org.kie.api.runtime.KieSession) KieScannerUpdateResultsEvent(org.kie.api.event.kiescanner.KieScannerUpdateResultsEvent) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 3 with InternalKieScanner

use of org.drools.compiler.kie.builder.impl.InternalKieScanner in project drools by kiegroup.

the class KieRepositoryScannerTest method testKScannerWithRange_withListener_errors.

@Test
public void testKScannerWithRange_withListener_errors() throws Exception {
    KieServices ks = KieServices.Factory.get();
    KieMavenRepository repository = getKieMavenRepository();
    String artifactId = "scanner-range-test-withlistener-errors";
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", artifactId, "1.0.1");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", artifactId, "1.0.2");
    // Because KieScanner also check the local repo, ensure the artifact are not deployed on the local maven repo
    repository.removeLocalArtifact(releaseId1);
    repository.removeLocalArtifact(releaseId2);
    ReleaseId releaseRange = ks.newReleaseId("org.kie", artifactId, "[1.0.0,)");
    InternalKieModule kJar1 = createKieJar(ks, releaseId1, "rule1", "rule2");
    KieContainer kieContainer = ks.newKieContainer(releaseRange);
    List<KieScannerEvent> events = new ArrayList<>();
    InternalKieScanner scanner = (KieRepositoryScannerImpl) ks.newKieScanner(kieContainer);
    KieScannerEventListener listener = new KieScannerEventListener() {

        public void onKieScannerStatusChangeEvent(KieScannerStatusChangeEvent statusChange) {
            LOG.info("KieScanner event: {}", statusChange);
            events.add(statusChange);
        }

        public void onKieScannerUpdateResultsEvent(KieScannerUpdateResultsEvent updtaeResults) {
            LOG.info("KieScanner event: {}", updtaeResults);
            events.add(updtaeResults);
        }
    };
    scanner.addListener(listener);
    scanner.scanNow();
    // Because 1.0.2 does not exist, will perform a scan but will NOT update.
    assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.SCANNING)));
    assertThat(events, CoreMatchers.not(CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.UPDATING))));
    events.clear();
    repository.installArtifact(releaseId1, kJar1, createKPom(fileManager, releaseId1));
    KieSession ksession = kieContainer.newKieSession("KSession1");
    checkKSession(ksession, "rule1", "rule2");
    // Create a wrong KJAR for releaseId2
    KieFileSystem kfs = ks.newKieFileSystem();
    String pomContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n" + "  <modelVersion>4.0.0</modelVersion>\n" + "\n" + "  <groupId>" + releaseId2.getGroupId() + "</groupId>\n" + "  <artifactId>" + releaseId2.getArtifactId() + "</artifactId>\n" + "  <version>" + releaseId2.getVersion() + "</version></project>\n";
    kfs.writePomXML(pomContent);
    kfs.write("rulebroken" + ".drl", "rule X when asd asd then end");
    kfs.write("META-INF/kmodule.xml", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <!-- This is a minimal kmodule.xml. Ref Drools documentation, see http://docs.jboss.org/drools/release/6.1.0.Final/drools-docs/html_single/index.html#d0e1112 -->\n" + "</kmodule>\n");
    File tempJar = File.createTempFile("org.kie" + artifactId + "1.0.2", "tmp");
    LOG.info("Writing to temp file: {}", tempJar);
    // please notice that writeAsJar would return a new file with .jar postfixed
    tempJar = ((KieFileSystemImpl) kfs).asMemoryFileSystem().writeAsJar(tempJar.getParentFile(), tempJar.getName());
    File tempPom = File.createTempFile("org.kie" + artifactId + "1.0.2", "tmp.xml");
    LOG.info("Writing to temp file: {}", tempPom);
    FileUtils.writeStringToFile(tempPom, pomContent, "UTF-8");
    repository.installArtifact(releaseId2, tempJar, tempPom);
    assertEquals(releaseId1, scanner.getCurrentReleaseId());
    assertEquals(InternalKieScanner.Status.STOPPED, scanner.getStatus());
    // scan the maven repo to get the new kjar version and deploy it on the kcontainer
    scanner.scanNow();
    // Keeping at previous release
    assertEquals(releaseId1, scanner.getCurrentReleaseId());
    assertEquals(InternalKieScanner.Status.STOPPED, scanner.getStatus());
    // there should be no update performed.
    assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.SCANNING)));
    assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.UPDATING)));
    assertTrue(events.get(2) instanceof KieScannerUpdateResultsEventImpl);
    assertTrue(((KieScannerUpdateResultsEventImpl) events.get(2)).getResults().hasMessages(Message.Level.ERROR));
    events.clear();
    ks.getRepository().removeKieModule(releaseId1);
    ks.getRepository().removeKieModule(releaseId2);
    scanner.removeListener(listener);
    assertEquals(0, scanner.getListeners().size());
    repository.removeLocalArtifact(releaseId1);
    repository.removeLocalArtifact(releaseId2);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) KieScannerEventListener(org.kie.api.event.kiescanner.KieScannerEventListener) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieScannerUpdateResultsEventImpl(org.kie.scanner.event.KieScannerUpdateResultsEventImpl) InternalKieScanner(org.drools.compiler.kie.builder.impl.InternalKieScanner) KieScannerStatusChangeEventImpl(org.kie.scanner.event.KieScannerStatusChangeEventImpl) KieMavenRepository.getKieMavenRepository(org.kie.scanner.KieMavenRepository.getKieMavenRepository) KieScannerEvent(org.kie.api.event.kiescanner.KieScannerEvent) KieScannerStatusChangeEvent(org.kie.api.event.kiescanner.KieScannerStatusChangeEvent) KieSession(org.kie.api.runtime.KieSession) File(java.io.File) KieScannerUpdateResultsEvent(org.kie.api.event.kiescanner.KieScannerUpdateResultsEvent) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 4 with InternalKieScanner

use of org.drools.compiler.kie.builder.impl.InternalKieScanner in project drools by kiegroup.

the class KieRepositoryScannerTest method testKScannerWithRange.

@Test
public void testKScannerWithRange() throws Exception {
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "scanner-range-test", "1.0.1");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "scanner-range-test", "1.0.2");
    ReleaseId releaseRange = ks.newReleaseId("org.kie", "scanner-range-test", "[1.0.0,)");
    InternalKieModule kJar1 = createKieJar(ks, releaseId1, "rule1", "rule2");
    KieContainer kieContainer = ks.newKieContainer(releaseRange);
    KieMavenRepository repository = getKieMavenRepository();
    repository.installArtifact(releaseId1, kJar1, createKPom(fileManager, releaseId1));
    // 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.installArtifact(releaseId2, kJar2, createKPom(fileManager, releaseId2));
    // since I am not calling start() on the scanner it means it won't have automatic scheduled scanning
    InternalKieScanner scanner = (InternalKieScanner) ks.newKieScanner(kieContainer);
    assertEquals(releaseId1, scanner.getCurrentReleaseId());
    assertEquals(InternalKieScanner.Status.STOPPED, scanner.getStatus());
    // scan the maven repo to get the new kjar version and deploy it on the kcontainer
    scanner.scanNow();
    assertEquals(releaseId2, scanner.getCurrentReleaseId());
    assertEquals(InternalKieScanner.Status.STOPPED, scanner.getStatus());
    // create a ksesion and check it works as expected
    KieSession ksession2 = kieContainer.newKieSession("KSession1");
    checkKSession(ksession2, "rule2", "rule3");
    ks.getRepository().removeKieModule(releaseId1);
    ks.getRepository().removeKieModule(releaseId2);
}
Also used : InternalKieScanner(org.drools.compiler.kie.builder.impl.InternalKieScanner) KieMavenRepository.getKieMavenRepository(org.kie.scanner.KieMavenRepository.getKieMavenRepository) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

InternalKieScanner (org.drools.compiler.kie.builder.impl.InternalKieScanner)4 ArrayList (java.util.ArrayList)3 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)3 Test (org.junit.Test)3 KieServices (org.kie.api.KieServices)3 ReleaseId (org.kie.api.builder.ReleaseId)3 KieContainer (org.kie.api.runtime.KieContainer)3 KieSession (org.kie.api.runtime.KieSession)3 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)3 KieScannerEvent (org.kie.api.event.kiescanner.KieScannerEvent)2 KieScannerEventListener (org.kie.api.event.kiescanner.KieScannerEventListener)2 KieScannerStatusChangeEvent (org.kie.api.event.kiescanner.KieScannerStatusChangeEvent)2 KieScannerUpdateResultsEvent (org.kie.api.event.kiescanner.KieScannerUpdateResultsEvent)2 KieScannerStatusChangeEventImpl (org.kie.scanner.event.KieScannerStatusChangeEventImpl)2 KieScannerUpdateResultsEventImpl (org.kie.scanner.event.KieScannerUpdateResultsEventImpl)2 File (java.io.File)1 WeakReference (java.lang.ref.WeakReference)1 KieFileSystemImpl (org.drools.compiler.kie.builder.impl.KieFileSystemImpl)1 KieFileSystem (org.kie.api.builder.KieFileSystem)1