Skip to content

Commit ed9d197

Browse files
committed
ClickUtils.getResourceAsStream final cleanup
1 parent 4122c39 commit ed9d197

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

click/src/main/java/org/apache/click/util/ClickUtils.java

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,37 +2427,38 @@ public static String getRequestURI(HttpServletRequest request) {
24272427
* @return the input stream of the resource if found or {@code null} otherwise
24282428
*/
24292429
@Nullable
2430-
public static InputStream getResourceAsStream (@NonNull String name, @NonNull Class<?> aClass) {
2430+
public static InputStream getResourceAsStream (String name, Class<?> aClass) {
2431+
if (StringUtils.isEmpty(name)){ return null; }// no resource
2432+
if (aClass == null){ aClass = ClickUtils.class; }
2433+
name = name.trim().replace('\\','/');
24312434
if (name.startsWith("/")){
2432-
log.warn("getResourceAsStream: name must be relative, but: {} @ {}", name, aClass);
2433-
name = name.substring(1);// ~ /WEB-INF/foo ~ WEB-INF/foo
2435+
log.warn("getResourceAsStream: `name` must be relative, but: {} @ {}", name, aClass);
2436+
name = name.substring(1);// ~ /WEB-INF/foo ~cut/~> WEB-INF/foo
2437+
}
2438+
val threadClassLoader = Thread.currentThread().getContextClassLoader();
2439+
if (threadClassLoader != null){
2440+
val is = threadClassLoader.getResourceAsStream(name);
2441+
if (is != null){ return is; }
24342442
}
2435-
val classLoader = Thread.currentThread().getContextClassLoader();
2436-
if (classLoader != null){
2437-
InputStream is = classLoader.getResourceAsStream(name);
2438-
if (is != null){ return is; }
2439-
}
24402443

2441-
val cl2 = aClass.getClassLoader();
2442-
if (classLoader != cl2){
2443-
InputStream is = cl2.getResourceAsStream(name);
2444+
val classClassLoader = aClass.getClassLoader();
2445+
if (threadClassLoader != classClassLoader){
2446+
val is = classClassLoader.getResourceAsStream(name);
24442447
if (is != null){ return is; }
24452448
}
24462449

2447-
// relative?
2450+
// relative?
24482451
InputStream is = aClass.getResourceAsStream(name);
2449-
if (is != null){ return is; }
2450-
2451-
is = ClickUtils.class.getResourceAsStream(name);// fallback to lib class loader
24522452
if (is != null){ return is; }
24532453

24542454
if (name.startsWith("WEB-INF/")){// hack old Click to work with Boot ?WEB-INF/classes
24552455
return getResourceAsStream(name.substring(8), aClass);// no more WEB-INF ⇒ resource in /resources?
2456-
} else if (!name.startsWith("META-INF/resources/")){// try in pseudo "/WEB-INF/" ~ replace "/WEB-INF/" with real servlet-container location of resources
2457-
return getResourceAsStream("META-INF/resources/"+ name, aClass);
2456+
} else if (!name.startsWith(WEB_INF_CLASSPATH)){// try in pseudo "/WEB-INF/" ~ replace "/WEB-INF/" with real servlet-container location of resources
2457+
return getResourceAsStream(WEB_INF_CLASSPATH+ name, aClass);
24582458
}
2459-
return null;// not found anywhere
2460-
}
2459+
return ClassLoader.getSystemResourceAsStream(name);// not found anywhere
2460+
}
2461+
public static final String WEB_INF_CLASSPATH = "META-INF/resources/";
24612462

24622463
/**
24632464
Get best ClassLoader.
@@ -2587,7 +2588,7 @@ public static void saveState (@NonNull Stateful control, String controlName, @No
25872588
+ " name has not been set. State cannot be saved until the name is set");
25882589
}
25892590
String resourcePath = context.getResourcePath();
2590-
Map pageMap = getOrCreatePageState(resourcePath, context);
2591+
val pageMap = getOrCreatePageState(resourcePath, context);
25912592
Object state = control.getState();
25922593
if (state == null) {
25932594
// Set null state to see if it differs from previous state
@@ -2617,7 +2618,6 @@ public static String toPropertyName (String prefix, String propertyName) {
26172618
return prefix + toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
26182619
}
26192620

2620-
26212621
/**
26222622
* Return a field label string from the given field name. For example:
26232623
* <pre class="codeHtml">
@@ -2658,7 +2658,6 @@ public static String toLabel (String name) {
26582658
}
26592659
}
26602660
}
2661-
26622661
return buffer.toString();
26632662
}
26642663

@@ -3102,9 +3101,9 @@ private static boolean bindField(Field field, Context context) {
31023101
* @return the map where page state is stored in
31033102
*/
31043103
private static Map getOrCreatePageState(String pagePath, Context context) {
3105-
Map pageMap = getPageState(pagePath, context);
3104+
var pageMap = getPageState(pagePath, context);
31063105
if (pageMap == null) {
3107-
pageMap = new HashMap();
3106+
pageMap = new HashMap<>();
31083107
}
31093108
return pageMap;
31103109
}
@@ -3205,8 +3204,8 @@ public static int len (@Nullable CharSequence str){
32053204
: str.length();
32063205
}
32073206

3208-
3209-
public static @Nullable String sysEnv (CharSequence key){
3207+
@Nullable
3208+
public static String sysEnv (CharSequence key){
32103209
val name = trim(key); String v;
32113210
// system properties -Dkey_name=key_value Gradle? -P
32123211
try {

0 commit comments

Comments
 (0)