sonatype-lift[bot] commented on a change in pull request #560: URL: https://github.com/apache/solr/pull/560#discussion_r795322626
########## File path: solr/core/src/java/org/apache/solr/pkg/PackageLoader.java ########## @@ -70,9 +82,112 @@ public PackageLoader(CoreContainer coreContainer) { this.coreContainer = coreContainer; + + List<String> enabledPackages = StrUtils.splitSmart(enabledLocalPkgsList, ','); packageAPI = new PackageAPI(coreContainer, this); - refreshPackageConf(); + if (localPkgsDir != null && !enabledPackages.isEmpty()) { + loadLocalPackages(localPkgsDir, enabledPackages); + } + if (repoPackagesEnabled) { + refreshPackageConf(); + } + } + + private void loadLocalPackages(String localPkgsDir, List<String> enabledPackages) { + final Path packagesPath = localPkgsDir.charAt(0) == File.separatorChar ? + Paths.get(localPkgsDir) : + Paths.get(coreContainer.getSolrHome()).resolve(localPkgsDir); + log.info("Packages to be loaded from directory: {}", packagesPath); + + if (!Files.exists(packagesPath)) { + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "No such directory: " + packagesPath); + } + Path packagesJsonPath = packagesPath.resolve(LOCAL_PACKAGES_JSON); + if(Files.exists(packagesJsonPath)) { + try { + + try (InputStream in = Files.newInputStream(packagesJsonPath)) { + localPackages = PackageAPI.mapper.readValue(in, PackageAPI.Packages.class); + } + } catch (IOException e) { + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error reading local_packages.json", e); + } + } else { + //the no local_packages.json + //we will look for each subdirectory and consider them as a package + localPackages = readDirAsPackages(packagesPath); + } + + if(localPackages != null) { + localPackages.packages.forEach((s, versions) -> versions.forEach(v -> { + v.local = Boolean.TRUE; + })); + for (Map.Entry<String, List<PackageAPI.PkgVersion>> e : localPackages.packages.entrySet()) { + if(!enabledPackages.contains(e.getKey())) continue; + Package p = new Package(e.getKey(), e.getValue()); + p.updateVersions(e.getValue(), packagesPath); + packageClassLoaders.put(e.getKey(), p); + } + } + } + + /** + * If a directory with no local_packages.json is provided, + * each sub directory that contains one or more jar files + * will be treated as a package and each jar file in that + * directory is added to the package classpath + */ + private PackageAPI.Packages readDirAsPackages(Path packagesPath) { + PackageAPI.Packages localDirAsPackages = new PackageAPI.Packages(); + try { + Files.list(packagesPath).forEach(subDir -> { + if (Files.isDirectory(subDir)) { + PackageAPI.PkgVersion version = new PackageAPI.PkgVersion(); + version.pkg = subDir.getFileName().toString(); + version.version = "0"; + version.files = new ArrayList<>(); + try { + Files.list(subDir).forEach(file -> { Review comment: *StreamResourceLeak:* Streams that encapsulate a closeable resource should be closed using try-with-resources [(details)](https://errorprone.info/bugpattern/StreamResourceLeak) (at-me [in a reply](https://help.sonatype.com/lift/talking-to-lift) with `help` or `ignore`) ########## File path: solr/core/src/java/org/apache/solr/pkg/PackageLoader.java ########## @@ -70,9 +82,112 @@ public PackageLoader(CoreContainer coreContainer) { this.coreContainer = coreContainer; + + List<String> enabledPackages = StrUtils.splitSmart(enabledLocalPkgsList, ','); packageAPI = new PackageAPI(coreContainer, this); - refreshPackageConf(); + if (localPkgsDir != null && !enabledPackages.isEmpty()) { + loadLocalPackages(localPkgsDir, enabledPackages); + } + if (repoPackagesEnabled) { + refreshPackageConf(); + } + } + + private void loadLocalPackages(String localPkgsDir, List<String> enabledPackages) { + final Path packagesPath = localPkgsDir.charAt(0) == File.separatorChar ? + Paths.get(localPkgsDir) : + Paths.get(coreContainer.getSolrHome()).resolve(localPkgsDir); + log.info("Packages to be loaded from directory: {}", packagesPath); + + if (!Files.exists(packagesPath)) { + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "No such directory: " + packagesPath); + } + Path packagesJsonPath = packagesPath.resolve(LOCAL_PACKAGES_JSON); + if(Files.exists(packagesJsonPath)) { + try { + + try (InputStream in = Files.newInputStream(packagesJsonPath)) { + localPackages = PackageAPI.mapper.readValue(in, PackageAPI.Packages.class); + } + } catch (IOException e) { + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error reading local_packages.json", e); + } + } else { + //the no local_packages.json + //we will look for each subdirectory and consider them as a package + localPackages = readDirAsPackages(packagesPath); + } + + if(localPackages != null) { + localPackages.packages.forEach((s, versions) -> versions.forEach(v -> { + v.local = Boolean.TRUE; + })); + for (Map.Entry<String, List<PackageAPI.PkgVersion>> e : localPackages.packages.entrySet()) { + if(!enabledPackages.contains(e.getKey())) continue; + Package p = new Package(e.getKey(), e.getValue()); + p.updateVersions(e.getValue(), packagesPath); + packageClassLoaders.put(e.getKey(), p); + } + } + } + + /** + * If a directory with no local_packages.json is provided, + * each sub directory that contains one or more jar files + * will be treated as a package and each jar file in that + * directory is added to the package classpath + */ + private PackageAPI.Packages readDirAsPackages(Path packagesPath) { + PackageAPI.Packages localDirAsPackages = new PackageAPI.Packages(); + try { + Files.list(packagesPath).forEach(subDir -> { Review comment: *StreamResourceLeak:* Streams that encapsulate a closeable resource should be closed using try-with-resources [(details)](https://errorprone.info/bugpattern/StreamResourceLeak) (at-me [in a reply](https://help.sonatype.com/lift/talking-to-lift) with `help` or `ignore`) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For additional commands, e-mail: issues-h...@solr.apache.org