Skip to content

Commit

Permalink
Added multiple URLs support in FinderFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
mackdk committed Jul 7, 2023
1 parent d05f218 commit 07fa328
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 30 deletions.
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());
}
}
11 changes: 3 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 Expand Up @@ -98,4 +92,5 @@ public List<String> findExcluding(String[] excludes, String endStr) {
}
return results;
}

}
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
51 changes: 39 additions & 12 deletions java/code/src/com/redhat/rhn/common/finder/FinderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@

package com.redhat.rhn.common.finder;

import org.apache.commons.lang3.StringUtils;

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.
Expand All @@ -33,27 +40,47 @@ 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;
List<URL> possibleUrls = getAllUrlsForPackage(packageName);
if (possibleUrls.isEmpty()) {
throw new IllegalArgumentException("Not a well formed jar file");
}
name = name.replace('.', '/');

URL packageUrl = FinderFactory.class.getResource(name);

// 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");
if (possibleUrls.size() == 1) {
return getFinderForPackage(packageName, possibleUrls.get(0));
}

return possibleUrls.stream()
.map(packageUrl -> getFinderForPackage(packageName, packageUrl))
.collect(Collectors.collectingAndThen(Collectors.toList(), CompositeFinder::new));
}

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('.', '/');
}

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()) {
return Collections.emptyList();
}
return Collections.list(systemResources);
}
catch (IOException ex) {
return Collections.emptyList();
}
}

}
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

0 comments on commit 07fa328

Please sign in to comment.