Skip to content

Instantly share code, notes, and snippets.

HostResolutionRequestInterceptor.INSTANCE.install(
new DNSClientHostResolver("208.67.222.222"), //Open DNS server
new DNSClientHostResolver("208.67.222.220"), //Open DNS server
new DNSClientHostResolver("8.8.8.8"), //Google DNS server
new DNSClientHostResolver("8.8.4.4"), //Google DNS server
DefaultHostResolver.INSTANCE
);
InetAddress inetAddress = InetAddress.getByName("github.com");
HostResolutionRequestInterceptor.INSTANCE.install(
new HostResolver() {
@Override
public Collection<InetAddress> getAllAddressesForHostName(Map<String, Object> argumentMap) {
String hostName = (String)super.getMethodArguments(argumentMap)[0]
//Do the stuff...
}
@Override
Map<String, String> hostAliases = new LinkedHashMap<>();
hostAliases.put("my.hostname.one", "123.123.123.123");
//Installing the host resolvers
HostResolutionRequestInterceptor.INSTANCE.install(
new MappedHostResolver(hostAliases),
//This is the system default resolving wrapper
DefaultHostResolver.INSTANCE
);
@JJBRT
JJBRT / config.yml
Last active November 2, 2022 12:38
hostAliases:
- ip: 123.123.123.123
hostnames:
- my.hostname.one
- my.hostname.two
- ip: 12.21.34.43
hostnames:
- my.hostname.three
- my.hostname.four
dns:
public static void installHostResolutionRequestInterceptor() throws UnknownHostException {
Map<String, Object> configuration = loadConfiguration("config.yml");
List<HostResolver> resolvers = new ArrayList<>();
resolvers.add(
new MappedHostResolver(() -> (List<Map<String, Object>>)configuration.get("hostAliases"))
);
resolvers.addAll(
DNSClientHostResolver.newInstances(() ->
(List<Map<String, Object>>)((Map<String, Object>)configuration.get("dns")).get("servers")
)
//Installing the host resolvers
HostResolverService.INSTANCE.install(
new HostResolverService.Resolver() {
@Override
public Collection<InetAddress> getAllAddressesForHostName(Map<String, Object> arguments) {
...
}
@Override
org.burningwave.core.assembler.StaticComponentContainer.Modules.exportAllToAll();
//Now that we have called exportAllToAll () we can use any class of any module
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
ClassLoader classLoader = new URLClassLoader(new URL[] {}, null);
method.invoke(
classLoader,
org.burningwave.core.assembler.StaticComponentContainer.Resources.getClassPath(AllModulesToAllModulesExporter.class).getURL()
);
Class<?> fieldsHandlerClass = classLoader.loadClass(FieldsHandler.class.getName());
//Filtering and obtaining a MethodHandle reference
MethodHandle defineClassMethodHandle = org.burningwave.core.assembler.StaticComponentContainer.Methods.findFirstDirectHandle(
MethodCriteria.byScanUpTo((cls) ->
//We only analyze the ClassLoader class and not all of its hierarchy (default behavior)
cls.getName().equals(ClassLoader.class.getName())
).name(
"defineClass"::equals
).and().parameterTypes(params ->
params.length == 5
).and().parameterTypesAreAssignableFrom(
org.burningwave.core.classes.Methods methods =
org.burningwave.core.assembler.StaticComponentContainer.Methods;
//Filtering and obtaining a Method reference
Method method = methods.findFirst(
MethodCriteria.byScanUpTo((cls) ->
//We only analyze the ClassLoader class and not all of its hierarchy (default behavior)
cls.getName().equals(ClassLoader.class.getName())
).name(
"defineClass"::equals
).and().parameterTypes(params ->
//loading the byte code
org.burningwave.core.io.FileSystemItem fileSystemItem =
org.burningwave.core.io.FileSystemItem.ofPath(
"C:/my/pckg/MyClass.class"
);
byte[] byteCode = fileSystemItem.toByteArray();
//invoking defineClass method handle
Class<?> cls = org.burningwave.core.assembler.StaticComponentContainer.Methods.invokeDirect(
Thread.currentThread().getContextClassLoader(),