diff --git a/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/KieRepositoryImpl.java b/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/KieRepositoryImpl.java index e678855439c..b7095573a46 100644 --- a/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/KieRepositoryImpl.java +++ b/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/KieRepositoryImpl.java @@ -15,6 +15,19 @@ package org.drools.compiler.kie.builder.impl; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.NavigableMap; +import java.util.TreeMap; +import java.util.concurrent.atomic.AtomicReference; + import org.drools.compiler.compiler.io.memory.MemoryFileSystem; import org.drools.compiler.kproject.models.KieModuleModelImpl; import org.drools.io.InternalResource; @@ -33,20 +46,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.NavigableMap; -import java.util.Set; -import java.util.TreeMap; -import java.util.concurrent.atomic.AtomicReference; - import static org.drools.compiler.kie.builder.impl.KieBuilderImpl.setDefaultsforEmptyKieModule; import static org.kie.util.maven.support.ReleaseIdImpl.fromPropertiesStream; @@ -66,7 +65,7 @@ public class KieRepositoryImpl public static final KieRepository INSTANCE = new KieRepositoryImpl(); - private final KieModuleRepo kieModuleRepo = new KieModuleRepo(); + private final KieModuleRepo kieModuleRepo; public static void setInternalKieScanner(InternalKieScanner scanner) { synchronized (KieScannerHolder.class) { @@ -95,6 +94,10 @@ private static InternalKieScanner getInternalKieScanner() { } } + public KieRepositoryImpl() { + kieModuleRepo = new KieModuleRepo(); + } + public void setDefaultGAV(ReleaseId releaseId) { this.defaultGAV.set(releaseId); } @@ -122,26 +125,20 @@ KieModule getOldKieModule(ReleaseId releaseId) { } public KieModule getKieModule(ReleaseId releaseId, PomModel pomModel) { - if (kieModuleRepo.isKnownNotKieModule(releaseId)) { - return null; - } - KieModule kieModule = kieModuleRepo.load( KieScannerHolder.kieScanner, releaseId ); - if (kieModule != null) { - return kieModule; + if (kieModule == null) { + log.debug("KieModule Lookup. ReleaseId {} was not in cache, checking classpath", + releaseId.toExternalForm()); + kieModule = checkClasspathForKieModule(releaseId); } - log.debug("KieModule Lookup. ReleaseId {} was not in cache, checking classpath", - releaseId.toExternalForm()); - kieModule = checkClasspathForKieModule(releaseId); - if (kieModule == null) { log.debug("KieModule Lookup. ReleaseId {} was not in cache, checking maven repository", releaseId.toExternalForm()); kieModule = loadKieModuleFromMavenRepo(releaseId, pomModel); } - return kieModuleRepo.storeKieModule(releaseId, kieModule); + return kieModule; } private KieModule checkClasspathForKieModule(ReleaseId releaseId) { @@ -259,7 +256,7 @@ public Collection getListeners() { public KieModule addKieModule(Resource resource, Resource... dependencies) { log.info("Adding KieModule from resource: " + resource); KieModule kModule = getKieModule(resource); - if (dependencies != null) { + if (dependencies != null && dependencies.length > 0) { for (Resource depRes : dependencies) { InternalKieModule depKModule = (InternalKieModule) getKieModule(depRes); ((InternalKieModule) kModule).addKieDependency(depKModule); @@ -328,35 +325,30 @@ public static class KieModuleRepo { public static int MAX_SIZE_GA_VERSIONS_CACHE // made changeable for test purposes = Integer.parseInt(System.getProperty(CACHE_VERSIONS_MAX_PROPERTY, "10")); - private final Set notKieModules = Collections.newSetFromMap(new LinkedHashMap<>(){ - @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - return size() > 1_000; - } - }); - // FIELDS ----------------------------------------------------------------------------------------------------------------- // kieModules evicts based on access-time, not on insertion-time - public final Map> kieModules = new LinkedHashMap<>(16, 0.75f, true) { + public final Map> kieModules + = new LinkedHashMap>(16, 0.75f, true) { @Override protected boolean removeEldestEntry( Map.Entry> eldest) { return (size() > MAX_SIZE_GA_CACHE); } }; - public final LinkedHashMap oldKieModules = new LinkedHashMap<>() { + public final LinkedHashMap oldKieModules = new LinkedHashMap() { @Override protected boolean removeEldestEntry( Map.Entry eldest ) { return size() > (MAX_SIZE_GA_CACHE*MAX_SIZE_GA_VERSIONS_CACHE); - } + }; + }; // METHODS ---------------------------------------------------------------------------------------------------------------- public synchronized KieModule remove(ReleaseId releaseId) { KieModule removedKieModule = null; - String ga = toArtifactId(releaseId); + String ga = releaseId.getGroupId() + ":" + releaseId.getArtifactId(); ComparableVersion comparableVersion = new ComparableVersion(releaseId.getVersion()); NavigableMap artifactMap = kieModules.get(ga); @@ -371,24 +363,9 @@ public synchronized KieModule remove(ReleaseId releaseId) { return removedKieModule; } - private KieModule storeKieModule(ReleaseId releaseId, KieModule kieModule) { - if (kieModule != null) { - store(kieModule); - } else { - notKieModules.add(releaseId); - } - return kieModule; - } - - private boolean isKnownNotKieModule(ReleaseId releaseId) { - return notKieModules.contains(releaseId); - } - public synchronized void store(KieModule kieModule) { ReleaseId releaseId = kieModule.getReleaseId(); - notKieModules.remove(releaseId); - - String ga = toArtifactId(releaseId); + String ga = releaseId.getGroupId() + ":" + releaseId.getArtifactId(); ComparableVersion comparableVersion = new ComparableVersion(releaseId.getVersion()); NavigableMap artifactMap = kieModules.get(ga); @@ -416,11 +393,11 @@ public synchronized void store(KieModule kieModule) { * @return a {@link NavigableMap} that is "backed" by a {@link LinkedHashMap} to enforce a LRU cache */ private NavigableMap createNewArtifactMap() { - return new TreeMap<>() { + NavigableMap newArtifactMap = new TreeMap() { private final Map artifactMap = this; - final LinkedHashMap backingLRUMap = new LinkedHashMap<>(16, 0.75f, true) { + LinkedHashMap backingLRUMap = new LinkedHashMap(16, 0.75f, true) { @Override protected boolean removeEldestEntry( Map.Entry eldest ) { boolean remove = (size() > MAX_SIZE_GA_VERSIONS_CACHE); @@ -438,6 +415,7 @@ public KieModule put( ComparableVersion key, KieModule value ) { } }; + return newArtifactMap; } synchronized KieModule loadOldAndRemove(ReleaseId releaseId) { @@ -449,7 +427,9 @@ synchronized KieModule load(InternalKieScanner kieScanner, ReleaseId releaseId) } synchronized KieModule load(InternalKieScanner kieScanner, ReleaseId releaseId, VersionRange versionRange) { - NavigableMap artifactMap = kieModules.get( toArtifactId(releaseId) ); + String ga = releaseId.getGroupId() + ":" + releaseId.getArtifactId(); + + NavigableMap artifactMap = kieModules.get(ga); if ( artifactMap == null || artifactMap.isEmpty() ) { return null; } @@ -492,10 +472,6 @@ synchronized KieModule load(InternalKieScanner kieScanner, ReleaseId releaseId, } - private static String toArtifactId(ReleaseId releaseId) { - return releaseId.getGroupId() + ":" + releaseId.getArtifactId(); - } - private static class VersionRange { private String lowerBound;