1010import java .util .Map ;
1111import java .util .Optional ;
1212import java .util .Set ;
13- import java .util .stream .Collectors ;
1413
15- import org .eclipse .microprofile .config .Config ;
16- import org .eclipse .microprofile .config .ConfigProvider ;
14+ import org .jboss .logging .Logger ;
1715
1816import io .smallrye .stork .api .config .ServiceConfig ;
1917import io .smallrye .stork .spi .config .SimpleServiceConfig ;
2018
2119public class StorkConfigUtil {
2220
21+ private static final Logger LOGGER = Logger .getLogger (StorkConfigUtil .class .getName ());
22+
2323 public static List <ServiceConfig > toStorkServiceConfig (StorkConfiguration storkConfiguration ) {
2424 List <ServiceConfig > storkServicesConfigs = new ArrayList <>();
2525 Set <String > servicesConfigs = storkConfiguration .serviceConfiguration ().keySet ();
@@ -38,7 +38,7 @@ public static List<ServiceConfig> toStorkServiceConfig(StorkConfiguration storkC
3838 }
3939 if (serviceConfiguration .serviceRegistrar ().isPresent ()) {
4040 SimpleServiceConfig .SimpleServiceRegistrarConfig serviceRegistrarConfig = new SimpleServiceConfig .SimpleServiceRegistrarConfig (
41- serviceConfiguration .serviceRegistrar ().get ().type (),
41+ serviceConfiguration .serviceRegistrar ().get ().type (). orElse ( "" ) ,
4242 serviceConfiguration .serviceRegistrar ().get ().parameters ());
4343 builder .setServiceRegistrar (serviceRegistrarConfig );
4444 }
@@ -47,89 +47,75 @@ public static List<ServiceConfig> toStorkServiceConfig(StorkConfiguration storkC
4747 return storkServicesConfigs ;
4848 }
4949
50- public static StorkConfiguration buildDefaultRegistrationConfig (StorkConfiguration configuration , String serviceRegistrarType ) {
51- Config quarkusConfig = ConfigProvider .getConfig ();
52- List <ServiceConfig > serviceConfigs = StorkConfigUtil .toStorkServiceConfig (configuration );
53- List <ServiceConfig > registrationConfigs = serviceConfigs .stream ()
54- .filter (serviceConfig -> serviceConfig .serviceRegistrar () != null ).toList ();
55- if (registrationConfigs .isEmpty ()) {
56- String serviceName = quarkusConfig .getOptionalValue ("quarkus.application.name" , String .class )
57- .orElse ("auri-application" );
58- configuration .serviceConfiguration ().put (serviceName , new ServiceConfiguration () {
59- @ Override
60- public Optional <StorkServiceDiscoveryConfiguration > serviceDiscovery () {
61- return Optional .empty ();
62- }
50+ public static ServiceConfiguration buildDefaultRegistrarConfiguration (String serviceRegistrarType ) {
51+ return buildServiceConfigurationWithRegistrar (serviceRegistrarType , Map .of ());
52+ }
6353
64- @ Override
65- public StorkLoadBalancerConfiguration loadBalancer () {
66- return null ;
67- }
54+ public static ServiceConfiguration addRegistrarTypeIfAbsent (String serviceRegistrarType ,
55+ ServiceConfiguration serviceConfiguration ) {
56+ Map <String , String > parameters = serviceConfiguration .serviceRegistrar ()
57+ .map (StorkServiceRegistrarConfiguration ::parameters )
58+ .orElse (Map .of ());
59+ return buildServiceConfigurationWithRegistrar (serviceRegistrarType , parameters );
60+ }
6861
69- @ Override
70- public Optional <StorkServiceRegistrarConfiguration > serviceRegistrar () {
71- return Optional .of (new StorkServiceRegistrarConfiguration () {
72- @ Override
73- public String type () {
74- return serviceRegistrarType ;
75- }
76-
77- @ Override
78- public Map <String , String > parameters () {
79- return Map .of ();
80- }
81- });
82- }
83- });
84- }
85- return configuration ;
62+ private static ServiceConfiguration buildServiceConfigurationWithRegistrar (String type , Map <String , String > parameters ) {
63+ return new ServiceConfiguration () {
64+ @ Override
65+ public Optional <StorkServiceDiscoveryConfiguration > serviceDiscovery () {
66+ return Optional .empty ();
67+ }
68+
69+ @ Override
70+ public StorkLoadBalancerConfiguration loadBalancer () {
71+ return null ;
72+ }
8673
74+ @ Override
75+ public Optional <StorkServiceRegistrarConfiguration > serviceRegistrar () {
76+ return Optional .of (buildServiceRegistrarConfiguration (type , parameters ));
77+ }
78+ };
79+ }
80+
81+ private static StorkServiceRegistrarConfiguration buildServiceRegistrarConfiguration (String type ,
82+ Map <String , String > parameters ) {
83+ return new StorkServiceRegistrarConfiguration () {
84+ @ Override
85+ public Optional <String > type () {
86+ return Optional .of (type );
87+ }
88+
89+ @ Override
90+ public Map <String , String > parameters () {
91+ return parameters ;
92+ }
93+ };
8794 }
8895
8996 public static InetAddress detectAddress () {
9097 InetAddress result = null ;
9198 try {
9299 int lowest = Integer .MAX_VALUE ;
93- for ( Enumeration <NetworkInterface > nics = NetworkInterface .getNetworkInterfaces (); nics
94- .hasMoreElements (); ) {
95- NetworkInterface ifc = nics .nextElement ();
96- if (ifc .isUp ()) {
97- // this.log.trace ("Testing interface: " + ifc .getDisplayName());
98- if (ifc .getIndex () < lowest || result == null ) {
99- lowest = ifc .getIndex ();
100+ Enumeration <NetworkInterface > networkInterfaces = NetworkInterface .getNetworkInterfaces ();
101+ while ( networkInterfaces .hasMoreElements ()) {
102+ NetworkInterface networkInterface = networkInterfaces .nextElement ();
103+ if (networkInterface .isUp ()) {
104+ LOGGER . debug ("Testing interface: {} " + networkInterface .getDisplayName ());
105+ if (networkInterface .getIndex () < lowest || result == null ) {
106+ lowest = networkInterface .getIndex ();
100107 } else if (result != null ) {
101108 continue ;
102109 }
103-
104- // @formatter:off
105- // if (!ignoreInterface(ifc.getDisplayName())) {
106- // for (Enumeration<InetAddress> addrs = ifc
107- // .getInetAddresses(); addrs.hasMoreElements();) {
108- // InetAddress address = addrs.nextElement();
109- // if (address instanceof Inet4Address
110- // && !address.isLoopbackAddress()
111- // && isPreferredAddress(address)) {
112- //// this.log.trace("Found non-loopback interface: "
113- //// + ifc.getDisplayName());
114- // result = address;
115- // }
116- // }
117- // }
118- // @formatter:on
119110 }
120111 }
121112 } catch (IOException ex ) {
122- // this.log. error("Cannot get first non-loopback address", ex);
113+ LOGGER . error ("Unable to get first non-loopback address" , ex );
123114 }
124-
125- if (result != null ) {
126- return result ;
127- }
128-
129115 try {
130116 return InetAddress .getLocalHost ();
131117 } catch (UnknownHostException e ) {
132- // this.log.warn ("Unable to retrieve localhost" );
118+ LOGGER . error ("Unable to detect address" , e );
133119 }
134120
135121 return null ;
0 commit comments