Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync main branch with Apache main branch #62

Merged
merged 13 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public boolean equals(final Object obj) {
return false;
}
final GlobalExtractor other = (GlobalExtractor) obj;
if (!(this.identifier.equals(other.identifier))) {
return false;
}
return this.objectType.equals( other.objectType );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;

import org.drools.compiler.compiler.io.memory.MemoryFileSystem;
import org.drools.compiler.kproject.models.KieModuleModelImpl;
Expand Down Expand Up @@ -70,6 +71,8 @@ public class KieRepositoryImpl

private final KieModuleRepo kieModuleRepo;

private final ReentrantLock lock = new ReentrantLock();

public static void setInternalKieScanner(InternalKieScanner scanner) {
synchronized (KieScannerHolder.class) {
KieScannerHolder.kieScanner = scanner;
Expand Down Expand Up @@ -98,7 +101,7 @@ private static InternalKieScanner getInternalKieScanner() {
}

public KieRepositoryImpl() {
kieModuleRepo = new KieModuleRepo();
kieModuleRepo = new KieModuleRepo(lock);
}

public void setDefaultGAV(ReleaseId releaseId) {
Expand Down Expand Up @@ -192,7 +195,12 @@ private KieModule checkClasspathForKieModule(ReleaseId releaseId) {
}

private KieModule loadKieModuleFromMavenRepo(ReleaseId releaseId, PomModel pomModel) {
return KieScannerHolder.kieScanner.loadArtifact( releaseId, pomModel );
try {
lock.lock();
return KieScannerHolder.kieScanner.loadArtifact(releaseId, pomModel);
} finally {
lock.unlock();
}
}

private static class DummyKieScanner
Expand Down Expand Up @@ -329,6 +337,7 @@ public static class KieModuleRepo {
= Integer.parseInt(System.getProperty(CACHE_VERSIONS_MAX_PROPERTY, "10"));

// FIELDS -----------------------------------------------------------------------------------------------------------------
private final ReentrantLock lock;

// kieModules evicts based on access-time, not on insertion-time
public final Map<String, NavigableMap<ComparableVersion, KieModule>> kieModules
Expand All @@ -348,44 +357,59 @@ protected boolean removeEldestEntry( Map.Entry<ReleaseId, KieModule> eldest ) {
};

// METHODS ----------------------------------------------------------------------------------------------------------------
public KieModuleRepo(ReentrantLock lock) {
this.lock = lock;
}

public synchronized KieModule remove(ReleaseId releaseId) {
KieModule removedKieModule = null;
String ga = releaseId.getGroupId() + ":" + releaseId.getArtifactId();
ComparableVersion comparableVersion = new ComparableVersion(releaseId.getVersion());
public KieModule remove(ReleaseId releaseId) {
try {
lock.lock();

KieModule removedKieModule = null;
String ga = releaseId.getGroupId() + ":" + releaseId.getArtifactId();
ComparableVersion comparableVersion = new ComparableVersion(releaseId.getVersion());

NavigableMap<ComparableVersion, KieModule> artifactMap = kieModules.get(ga);
if (artifactMap != null) {
removedKieModule = artifactMap.remove(comparableVersion);
if (artifactMap.isEmpty()) {
kieModules.remove(ga);
NavigableMap<ComparableVersion, KieModule> artifactMap = kieModules.get(ga);
if (artifactMap != null) {
removedKieModule = artifactMap.remove(comparableVersion);
if (artifactMap.isEmpty()) {
kieModules.remove(ga);
}
oldKieModules.remove(releaseId);
}
oldKieModules.remove(releaseId);
}

return removedKieModule;
return removedKieModule;
} finally {
lock.unlock();
}
}

public synchronized void store(KieModule kieModule) {
ReleaseId releaseId = kieModule.getReleaseId();
String ga = releaseId.getGroupId() + ":" + releaseId.getArtifactId();
ComparableVersion comparableVersion = new ComparableVersion(releaseId.getVersion());
public void store(KieModule kieModule) {
try {
lock.lock();

ReleaseId releaseId = kieModule.getReleaseId();
String ga = releaseId.getGroupId() + ":" + releaseId.getArtifactId();
ComparableVersion comparableVersion = new ComparableVersion(releaseId.getVersion());

NavigableMap<ComparableVersion, KieModule> artifactMap = kieModules.get(ga);
if( artifactMap == null ) {
artifactMap = createNewArtifactMap();
kieModules.put(ga, artifactMap);
}
NavigableMap<ComparableVersion, KieModule> artifactMap = kieModules.get(ga);
if( artifactMap == null ) {
artifactMap = createNewArtifactMap();
kieModules.put(ga, artifactMap);
}

KieModule oldReleaseIdKieModule = oldKieModules.get(releaseId);
// variable used in order to test race condition
if (oldReleaseIdKieModule == null) {
KieModule oldKieModule = artifactMap.get(comparableVersion);
if (oldKieModule != null) {
oldKieModules.put( releaseId, oldKieModule );
KieModule oldReleaseIdKieModule = oldKieModules.get(releaseId);
// variable used in order to test race condition
if (oldReleaseIdKieModule == null) {
KieModule oldKieModule = artifactMap.get(comparableVersion);
if (oldKieModule != null) {
oldKieModules.put( releaseId, oldKieModule );
}
}
artifactMap.put( comparableVersion, kieModule );
} finally {
lock.unlock();
}
artifactMap.put( comparableVersion, kieModule );
}

/**
Expand Down Expand Up @@ -421,56 +445,72 @@ public KieModule put( ComparableVersion key, KieModule value ) {
return newArtifactMap;
}

synchronized KieModule loadOldAndRemove(ReleaseId releaseId) {
return oldKieModules.remove(releaseId);
KieModule loadOldAndRemove(ReleaseId releaseId) {
try {
lock.lock();
return oldKieModules.remove(releaseId);
} finally {
lock.unlock();
}
}

synchronized KieModule load(InternalKieScanner kieScanner, ReleaseId releaseId) {
return load(kieScanner, releaseId, new VersionRange(releaseId.getVersion()));
KieModule load(InternalKieScanner kieScanner, ReleaseId releaseId) {
try {
lock.lock();
return load(kieScanner, releaseId, new VersionRange(releaseId.getVersion()));
} finally {
lock.unlock();
}
}

synchronized KieModule load(InternalKieScanner kieScanner, ReleaseId releaseId, VersionRange versionRange) {
String ga = releaseId.getGroupId() + ":" + releaseId.getArtifactId();
KieModule load(InternalKieScanner kieScanner, ReleaseId releaseId, VersionRange versionRange) {
try {
lock.lock();

NavigableMap<ComparableVersion, KieModule> artifactMap = kieModules.get(ga);
if ( artifactMap == null || artifactMap.isEmpty() ) {
return null;
}
KieModule kieModule = artifactMap.get(new ComparableVersion(releaseId.getVersion()));

if (versionRange.fixed) {
if ( kieModule != null && releaseId.isSnapshot() ) {
String oldSnapshotVersion = ((ReleaseIdImpl)kieModule.getReleaseId()).getSnapshotVersion();
if ( oldSnapshotVersion != null ) {
String currentSnapshotVersion = kieScanner.getArtifactVersion(releaseId);
if (currentSnapshotVersion != null &&
new ComparableVersion(currentSnapshotVersion).compareTo(new ComparableVersion(oldSnapshotVersion)) > 0) {
// if the snapshot currently available on the maven repo is newer than the cached one
// return null to enforce the building of this newer version
return null;
String ga = releaseId.getGroupId() + ":" + releaseId.getArtifactId();

NavigableMap<ComparableVersion, KieModule> artifactMap = kieModules.get(ga);
if ( artifactMap == null || artifactMap.isEmpty() ) {
return null;
}
KieModule kieModule = artifactMap.get(new ComparableVersion(releaseId.getVersion()));

if (versionRange.fixed) {
if ( kieModule != null && releaseId.isSnapshot() ) {
String oldSnapshotVersion = ((ReleaseIdImpl)kieModule.getReleaseId()).getSnapshotVersion();
if ( oldSnapshotVersion != null ) {
String currentSnapshotVersion = kieScanner.getArtifactVersion(releaseId);
if (currentSnapshotVersion != null &&
new ComparableVersion(currentSnapshotVersion).compareTo(new ComparableVersion(oldSnapshotVersion)) > 0) {
// if the snapshot currently available on the maven repo is newer than the cached one
// return null to enforce the building of this newer version
return null;
}
}
}
return kieModule;
}
return kieModule;
}

Map.Entry<ComparableVersion, KieModule> entry =
versionRange.upperBound == null ?
artifactMap.lastEntry() :
versionRange.upperInclusive ?
artifactMap.floorEntry(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;
}
if ( entry == null ) {
return null;
}

if ( versionRange.lowerBound == null ) {
return entry.getValue();
}
if ( versionRange.lowerBound == null ) {
return entry.getValue();
}

int comparison = entry.getKey().compareTo(new ComparableVersion(versionRange.lowerBound));
return comparison > 0 || (comparison == 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;
} finally {
lock.unlock();
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.drools.core.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
Expand All @@ -35,6 +36,7 @@
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.junit.AfterClass;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -52,6 +54,16 @@ public class KeyStoreHelperTest {
private static final String KEY_PASSWORD = "keypwd";
private static final String KEY_PHRASE = "secretkey";

@AfterClass
public static void cleanup() {
try {
new File(KEYSTORE_JCEKS_FILENAME).delete();
} catch (Exception e) {
// ignore
}

}

@Test
public void testSignDataWithPrivateKey() throws UnsupportedEncodingException,
UnrecoverableKeyException,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.drools.example.api.defaultkiesessionfromfile;

import java.io.File;
import java.io.PrintStream;

import org.kie.api.KieServices;
import org.kie.api.builder.KieModule;
import org.kie.api.builder.KieRepository;
Expand All @@ -26,9 +29,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.PrintStream;


public class DefaultKieSessionFromFileExample {

Expand Down Expand Up @@ -69,8 +69,7 @@ public static File getFile(String exampleName) {
File folder = new File("drools-examples-api").getAbsoluteFile();
File exampleFolder = null;
while (folder != null) {
exampleFolder = new File(folder,
exampleName);
exampleFolder = new File(folder, exampleName);
if (exampleFolder.exists()) {
break;
}
Expand All @@ -80,14 +79,13 @@ public static File getFile(String exampleName) {

if (exampleFolder != null) {

File targetFolder = new File(exampleFolder,
"target");
File targetFolder = new File(exampleFolder, "target");
if (!targetFolder.exists()) {
throw new RuntimeException("The target folder does not exist, please build project " + exampleName + " first");
}

for (String str : targetFolder.list()) {
if (str.startsWith(exampleName) && !str.endsWith("-sources.jar") && !str.endsWith("-tests.jar") && !str.endsWith("-javadoc.jar")) {
if (str.startsWith(exampleName) && str.endsWith(".jar") && !str.endsWith("-sources.jar") && !str.endsWith("-tests.jar") && !str.endsWith("-javadoc.jar")) {
return new File(targetFolder, str);
}
}
Expand Down
Loading
Loading