Skip to content

Commit

Permalink
#2820 Fix package name retrieval from Jar files
Browse files Browse the repository at this point in the history
  • Loading branch information
marevol committed Jun 19, 2024
1 parent 3551d13 commit 4afbb34
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/main/java/org/codelibs/fess/helper/ProtocolHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -70,12 +73,33 @@ protected void loadProtocols(final String basePackage) {

while (resources.hasMoreElements()) {
final URL resource = resources.nextElement();
final File directory = new File(resource.getFile());
if (directory.exists() && directory.isDirectory()) {
final File[] files = directory.listFiles(File::isDirectory);
if (files != null) {
for (final File file : files) {
subPackages.add(file.getName());
logger.debug("loading {}", resource);
if ("file".equals(resource.getProtocol())) {
final File directory = new File(resource.getFile());
if (directory.exists() && directory.isDirectory()) {
final File[] files = directory.listFiles(File::isDirectory);
if (files != null) {
for (final File file : files) {
final String name = file.getName();
subPackages.add(name);
logger.debug("found {} in {}", name, resource);
}
}
}
} else if ("jar".equals(resource.getProtocol())) {
final JarURLConnection jarURLConnection = (JarURLConnection) resource.openConnection();
try (JarFile jarFile = jarURLConnection.getJarFile()) {
final Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
final JarEntry entry = entries.nextElement();
final String entryName = entry.getName();
if (entryName.endsWith("/") && entryName.startsWith(path) && entryName.length() > path.length() + 1) {
final String name = entryName.substring(path.length() + 1, entryName.length() - 1);
if (name.indexOf('/') == -1) {
subPackages.add(name);
logger.debug("found {} in {}", name, resource);
}
}
}
}
}
Expand Down

0 comments on commit 4afbb34

Please sign in to comment.