Skip to content

Commit 0a2b1f4

Browse files
committed
handle invalid entries and Jar FD close
1 parent 75eb5be commit 0a2b1f4

File tree

1 file changed

+56
-65
lines changed

1 file changed

+56
-65
lines changed

jpos/src/main/java/org/jpos/q2/install/ModuleUtils.java

Lines changed: 56 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.security.NoSuchAlgorithmException;
2929
import java.util.*;
3030
import java.util.jar.JarEntry;
31+
import java.util.jar.JarFile;
3132
import java.util.stream.Collectors;
3233

3334
/**
@@ -38,34 +39,35 @@ public class ModuleUtils
3839
private static final String MODULES_UUID_DIR = "META-INF/modules/uuids/";
3940
private static final String MODULES_RKEYS_DIR = "META-INF/modules/rkeys/";
4041

41-
public static List<String> getModuleEntries(String prefix) throws IOException
42-
{
43-
List<String> result=new ArrayList<String>();
42+
public static List<String> getModuleEntries(String prefix) throws IOException {
43+
List<String> result = new ArrayList<>();
4444

4545
Enumeration<URL> urls = ModuleUtils.class.getClassLoader().getResources(prefix);
46-
while (urls.hasMoreElements())
47-
{
46+
while (urls.hasMoreElements()) {
4847
URL url = urls.nextElement();
49-
if(url==null) return Collections.emptyList();
50-
51-
try
52-
{
53-
final List<String> lst = url.getProtocol().equals("jar") ?
54-
resolveModuleEntriesFromJar(url,prefix) :
55-
resolveModuleEntriesFromFiles(url,prefix);
56-
result.addAll(lst);
57-
}
58-
catch (URISyntaxException e)
59-
{
60-
throw new IOException("Bad URL",e);
48+
if (url == null) continue;
49+
50+
try {
51+
List<String> entries;
52+
String protocol = url.getProtocol();
53+
if ("jar".equals(protocol)) {
54+
entries = resolveModuleEntriesFromJar(url, prefix);
55+
} else if ("file".equals(protocol)) {
56+
entries = resolveModuleEntriesFromFiles(url, prefix);
57+
} else {
58+
// Unsupported protocol, skip with optional logging
59+
continue;
60+
}
61+
result.addAll(entries);
62+
} catch (URISyntaxException e) {
63+
throw new IOException("Bad URL: " + url, e);
6164
}
6265
}
6366
return result;
6467
}
6568

66-
public static List<String> getModulesUUIDs () throws IOException {
67-
return ModuleUtils.getModuleEntries(MODULES_UUID_DIR)
68-
.stream()
69+
public static List<String> getModulesUUIDs() throws IOException {
70+
return getModuleEntries(MODULES_UUID_DIR).stream()
6971
.sorted()
7072
.map(p -> p.substring(MODULES_UUID_DIR.length()))
7173
.collect(Collectors.toList());
@@ -81,60 +83,49 @@ public static List<String> getRKeys () throws IOException {
8183

8284
public static String getSystemHash() throws IOException, NoSuchAlgorithmException {
8385
MessageDigest digest = MessageDigest.getInstance("SHA-256");
84-
boolean updated = false;
85-
for (String s : getModulesUUIDs()) {
86-
digest.update(s.getBytes());
87-
updated = true;
88-
}
89-
return updated ? Base64.getEncoder().encodeToString(digest.digest()) : "";
90-
}
91-
92-
private static List<String> resolveModuleEntriesFromFiles(URL url,String _prefix) throws URISyntaxException
93-
{
94-
final String prefix=_prefix.endsWith("/")?_prefix:_prefix+"/";
86+
List<String> uuids = getModulesUUIDs();
87+
if (uuids.isEmpty()) return "";
9588

96-
List<String> resourceList =new ArrayList<String>();
97-
98-
final URI uri = url.toURI();
99-
File f=new File(uri);
100-
addFiles(f,prefix, resourceList);
89+
uuids.forEach(uuid -> digest.update(uuid.getBytes()));
90+
return Base64.getEncoder().encodeToString(digest.digest());
91+
}
10192

93+
private static List<String> resolveModuleEntriesFromFiles(URL url, String prefix)
94+
throws URISyntaxException {
95+
String normalizedPrefix = prefix.endsWith("/") ? prefix : prefix + "/";
96+
List<String> resourceList = new ArrayList<>();
97+
File dir = new File(url.toURI());
98+
addFiles(dir, normalizedPrefix, resourceList);
10299
return resourceList;
103100
}
104101

105-
private static void addFiles(File f,String prefix,List<String> resourceList)
106-
{
107-
File files[]=f.listFiles();
108-
if(files==null) return;
109-
110-
for (File file : files)
111-
{
112-
if(file.isDirectory())
113-
{
114-
addFiles(file,prefix+file.getName()+"/", resourceList);
115-
}
116-
else
117-
{
118-
resourceList.add(prefix+file.getName());
102+
private static void addFiles(File dir, String prefix, List<String> resourceList) {
103+
File[] files = dir.listFiles();
104+
if (files == null) return;
105+
106+
for (File file : files) {
107+
if (file.isDirectory()) {
108+
addFiles(file, prefix + file.getName() + "/", resourceList);
109+
} else {
110+
resourceList.add(prefix + file.getName());
119111
}
120112
}
121113
}
122114

123-
private static List<String> resolveModuleEntriesFromJar(URL url,String _prefix) throws IOException
124-
{
125-
final String prefix=_prefix.endsWith("/")?_prefix:_prefix+"/";
126-
127-
List<String> resourceList =new ArrayList<String>();
128-
129-
JarURLConnection conn=(JarURLConnection)url.openConnection();
130-
Enumeration entries=conn.getJarFile().entries();
131-
while (entries.hasMoreElements())
132-
{
133-
JarEntry entry = (JarEntry) entries.nextElement();
134-
String name=entry.getName();
135-
if(name.startsWith(prefix) && !entry.isDirectory())
136-
{
137-
resourceList.add(name);
115+
private static List<String> resolveModuleEntriesFromJar(URL url, String prefix)
116+
throws IOException {
117+
String normalizedPrefix = prefix.endsWith("/") ? prefix : prefix + "/";
118+
List<String> resourceList = new ArrayList<>();
119+
120+
JarURLConnection conn = (JarURLConnection) url.openConnection();
121+
try (JarFile jarFile = conn.getJarFile()) {
122+
Enumeration<JarEntry> entries = jarFile.entries();
123+
while (entries.hasMoreElements()) {
124+
JarEntry entry = entries.nextElement();
125+
String name = entry.getName();
126+
if (name.startsWith(normalizedPrefix) && !entry.isDirectory()) {
127+
resourceList.add(name);
128+
}
138129
}
139130
}
140131
return resourceList;

0 commit comments

Comments
 (0)