-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #427 from elandau/bugfix/reduce_property_resolution
config: Fix property resolution when config not loaded
- Loading branch information
Showing
8 changed files
with
87 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
ribbon-core/src/main/java/com/netflix/client/config/PropertyUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.netflix.client.config; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class PropertyUtils { | ||
private static Logger LOG = LoggerFactory.getLogger(PropertyUtils.class); | ||
|
||
private PropertyUtils() {} | ||
|
||
/** | ||
* Returns the internal property to the desiredn type | ||
*/ | ||
private static Map<Class<?>, Optional<Method>> valueOfMethods = new ConcurrentHashMap<>(); | ||
|
||
public static <T> Optional<T> resolveWithValueOf(Class<T> type, String value) { | ||
return valueOfMethods.computeIfAbsent(type, ignore -> { | ||
try { | ||
return Optional.of(type.getDeclaredMethod("valueOf", String.class)); | ||
} catch (NoSuchMethodException e) { | ||
return Optional.empty(); | ||
} catch (Exception e) { | ||
LOG.warn("Unable to determine if type " + type + " has a valueOf() static method", e); | ||
return Optional.empty(); | ||
} | ||
}).map(method -> { | ||
try { | ||
return (T)method.invoke(null, value); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters