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

Added multiple URLs support in FinderFactory #7250

Merged
merged 3 commits into from
Jul 24, 2023
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions java/code/src/com/redhat/rhn/common/finder/CompositeFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2023 SUSE LLC
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* Red Hat trademarks are not licensed under GPLv2. No permission is
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/

package com.redhat.rhn.common.finder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
* A Finder implementation that uses multiple finders to search
*/
class CompositeFinder implements Finder {

private final List<Finder> finderList;

CompositeFinder(Collection<? extends Finder> finders) {
this.finderList = new ArrayList<>(finders);
}

@Override
public List<String> findExcluding(String[] excluding, String endStr) {
return this.finderList.stream()
.flatMap(finder -> finder.findExcluding(excluding, endStr).stream())
.collect(Collectors.toList());
}
}
10 changes: 2 additions & 8 deletions java/code/src/com/redhat/rhn/common/finder/FileFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

Expand All @@ -41,21 +42,14 @@ class FileFinder implements Finder {
}
}

/** {@inheritDoc} */
@Override
public List<String> find(String endStr) {
return findExcluding(null, endStr);
}

/** {@inheritDoc} */
@Override
public List<String> findExcluding(String[] excludes, String endStr) {
List<String> results = new LinkedList<>();

if (!startDir.exists()) {
// Shouldn't ever happen, because the FinderFactory should only
// return a FileFinder.
return null;
return Collections.emptyList();
}
String[] fileList = startDir.list();

Expand Down
4 changes: 3 additions & 1 deletion java/code/src/com/redhat/rhn/common/finder/Finder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public interface Finder {
* @return a list of all classes within the package that end with the
* specified string
*/
List<String> find(String endStr);
default List<String> find(String endStr) {
return findExcluding(null, endStr);
}

/**
* Find all files within a package that end with the specified string
Expand Down
59 changes: 48 additions & 11 deletions java/code/src/com/redhat/rhn/common/finder/FinderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,26 @@

package com.redhat.rhn.common.finder;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;

/**
* A factory that returns the correct type of finder.
*
*/
public class FinderFactory {

private static final Logger LOGGER = LogManager.getLogger(FinderFactory.class);

private FinderFactory() {
}

Expand All @@ -33,27 +44,53 @@ private FinderFactory() {
* @return Finder to use for given package name.
*/
public static Finder getFinder(String packageName) {
// Start by translating into an absolute path.
String name = packageName;
if (!packageName.startsWith("/")) {
name = "/" + name;
// Extract all the possible URLs for a package
List<URL> possibleUrls = getAllUrlsForPackage(packageName);
if (possibleUrls.isEmpty()) {
throw new IllegalArgumentException("Not a well-formed jar file");
}

if (possibleUrls.size() == 1) {
return getFinderForPackage(packageName, possibleUrls.get(0));
}
name = name.replace('.', '/');

URL packageUrl = FinderFactory.class.getResource(name);
return possibleUrls.stream()
.map(packageUrl -> getFinderForPackage(packageName, packageUrl))
.collect(Collectors.collectingAndThen(Collectors.toList(), CompositeFinder::new));
}

private static List<URL> getAllUrlsForPackage(String packageName) {
try {
ClassLoader classLoader = FinderFactory.class.getClassLoader();
Enumeration<URL> systemResources = classLoader.getResources(packageName.replace('.', '/'));
if (systemResources == null || !systemResources.hasMoreElements()) {
LOGGER.warn("No valid URLs found for package {}", packageName);
return Collections.emptyList();
}

// This only happens if the .jar file isn't well-formed, so we
// shouldn't have this problem, ever.
if (packageUrl == null) {
throw new IllegalArgumentException("Not a well formed jar file");
return Collections.list(systemResources);
}
catch (SecurityException ex) {
throw new IllegalStateException("Unable to access the class loader for searching " + packageName, ex);
}
catch (IOException ex) {
LOGGER.warn("Unable to find URLs for package {}", packageName, ex);
return Collections.emptyList();
}
}

private static Finder getFinderForPackage(String packageName, URL packageUrl) {
File directory = new File(packageUrl.getFile());

if (!directory.isFile() && !directory.isDirectory()) {
// This is a jar file that we are dealing with.
return new JarFinder(packageUrl);
}
return new FileFinder(directory, name);

return new FileFinder(directory, getAbsolutePath(packageName));
}

private static String getAbsolutePath(String packageName) {
return StringUtils.prependIfMissing(packageName, "/").replace('.', '/');
}
}
7 changes: 0 additions & 7 deletions java/code/src/com/redhat/rhn/common/finder/JarFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ class JarFinder implements Finder {
url = packageUrl;
}

/** {@inheritDoc} */
@Override
public List<String> find(String endStr) {
return findExcluding(null, endStr);
}

/** {@inheritDoc} */
@Override
public List<String> findExcluding(String[] excludes, String endStr) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void testFindFilesSubDir() {
assertNotNull(f);

List<String> result = f.find(".class");
assertEquals(6, result.size());
assertEquals(7, result.size());
}

@Test
Expand All @@ -60,7 +60,7 @@ public void testFindExcluding() {
assertNotNull(f);
String[] sarr = {"Test"};
List<String> result = f.findExcluding(sarr, "class");
assertEquals(4, result.size());
assertEquals(5, result.size());
}
}

Expand Down
Loading