Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#735] Converters for network addresses #787

Merged
merged 1 commit into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.jboss.shamrock.deployment.util.ReflectUtil.toError;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
Expand All @@ -29,6 +31,7 @@
import org.jboss.protean.gizmo.MethodDescriptor;
import org.jboss.protean.gizmo.ResultHandle;
import org.jboss.shamrock.deployment.AccessorFinder;
import org.jboss.shamrock.deployment.annotations.BuildProducer;
import org.jboss.shamrock.deployment.annotations.BuildStep;
import org.jboss.shamrock.deployment.builditem.BytecodeRecorderObjectLoaderBuildItem;
import org.jboss.shamrock.deployment.builditem.ConfigurationBuildItem;
Expand All @@ -42,11 +45,15 @@
import org.jboss.shamrock.deployment.recording.ObjectLoader;
import org.jboss.shamrock.deployment.util.ServiceUtil;
import org.jboss.shamrock.runtime.annotations.ConfigPhase;
import org.jboss.shamrock.runtime.configuration.CidrAddressConverter;
import org.jboss.shamrock.runtime.configuration.ConverterFactory;
import org.jboss.shamrock.runtime.configuration.ExpandingConfigSource;
import org.jboss.shamrock.runtime.configuration.InetAddressConverter;
import org.jboss.shamrock.runtime.configuration.InetSocketAddressConverter;
import org.jboss.shamrock.runtime.configuration.NameIterator;
import org.jboss.shamrock.runtime.configuration.SimpleConfigurationProviderResolver;
import org.objectweb.asm.Opcodes;
import org.wildfly.common.net.CidrAddress;

/**
* Setup steps for configuration purposes.
Expand Down Expand Up @@ -81,6 +88,25 @@ public class ConfigurationSetup {

public ConfigurationSetup() {}

@BuildStep
public void setUpConverters(BuildProducer<ConfigurationCustomConverterBuildItem> configurationTypes) {
configurationTypes.produce(new ConfigurationCustomConverterBuildItem(
200,
InetSocketAddress.class,
InetSocketAddressConverter.class
));
configurationTypes.produce(new ConfigurationCustomConverterBuildItem(
200,
CidrAddress.class,
CidrAddressConverter.class
));
configurationTypes.produce(new ConfigurationCustomConverterBuildItem(
200,
InetAddress.class,
InetAddressConverter.class
));
}

/**
* Run before anything that consumes configuration; sets up the main configuration definition instance.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.jboss.shamrock.runtime.configuration;

import org.eclipse.microprofile.config.spi.Converter;
import org.wildfly.common.net.CidrAddress;
import org.wildfly.common.net.Inet;

/**
* A converter which converts a CIDR address into an instance of {@link CidrAddress}.
*/
public class CidrAddressConverter implements Converter<CidrAddress> {

@Override
public CidrAddress convert(final String value) {
if (value.isEmpty()) return null;
final CidrAddress result = Inet.parseCidrAddress(value);
if (result == null) throw new IllegalArgumentException("Failed to parse CIDR address \"" + value + "\"");
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.jboss.shamrock.runtime.configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.eclipse.microprofile.config.spi.Converter;
import org.wildfly.common.net.Inet;

/**
* A converter which produces values of type {@link InetAddress}.
*/
public class InetAddressConverter implements Converter<InetAddress> {
@Override
public InetAddress convert(final String value) {
if (value.isEmpty()) return null;
final InetAddress parsed = Inet.parseInetAddress(value);
if (parsed != null) return parsed;
try {
return InetAddress.getByName(value);
} catch (UnknownHostException e) {
throw new IllegalArgumentException("Unable to resolve \"" + value + "\"", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.jboss.shamrock.runtime.configuration;

import java.net.InetAddress;
import java.net.InetSocketAddress;

import org.eclipse.microprofile.config.spi.Converter;
import org.wildfly.common.net.Inet;

/**
* A converter which converts a socket address in the form of {@code &lt;host-or-address&gt;[:&lt;port&gt;]} into
* an instance of {@link InetSocketAddress}. If an address is given, then a resolved instance is returned, otherwise
* an unresolved instance is returned.
*/
public class InetSocketAddressConverter implements Converter<InetSocketAddress> {

@Override
public InetSocketAddress convert(final String value) {
if (value.isEmpty()) return null;
final int lastColon = value.lastIndexOf(':');
final int lastCloseBracket = value.lastIndexOf(']');
final String hostPart;
final int portPart;
if (lastColon == -1 || lastCloseBracket != -1 && lastColon < lastCloseBracket) {
// no port #
hostPart = value;
portPart = 0;
} else {
hostPart = value.substring(0, lastColon);
portPart = Integer.parseInt(value.substring(lastColon + 1));
}
InetAddress resolved = Inet.parseInetAddress(hostPart);
return resolved == null ? InetSocketAddress.createUnresolved(value, 0) : new InetSocketAddress(resolved, portPart);
}
}