| 832 | } |
| 833 | |
| 834 | public static void initComponents(Protocol p, ProtocolConfiguration cfg) throws Exception { |
| 835 | // copy the props and weed out properties from non-components |
| 836 | Map<String,String> properties=cfg != null? cfg.getProperties() : new HashMap<>(); |
| 837 | Map<String,String> props=new HashMap<>(); |
| 838 | |
| 839 | // first a bit of sanity checking: no duplicate components in p |
| 840 | final Map<String,Class<?>> prefixes=new HashMap<>(); |
| 841 | final Ref<Exception> ex=new Ref<>(null); |
| 842 | Util.forAllComponentTypes(p.getClass(), (cl, prefix) -> { |
| 843 | if(ex.isSet()) |
| 844 | return; |
| 845 | if(prefix == null || prefix.trim().isEmpty()) { |
| 846 | ex.set(new IllegalArgumentException(String.format("component (class=%s) in %s must have a prefix", |
| 847 | cl.getSimpleName(), p.getName()))); |
| 848 | return; |
| 849 | } |
| 850 | if(prefixes.containsKey(prefix)) |
| 851 | ex.set(new IllegalArgumentException(String.format("multiple components (class=%s) in %s have same prefix '%s'", |
| 852 | cl.getSimpleName(), p.getName(), prefix))); |
| 853 | else |
| 854 | prefixes.put(prefix, cl); |
| 855 | }); |
| 856 | if(ex.isSet()) |
| 857 | throw ex.get(); |
| 858 | |
| 859 | Util.forAllComponentTypes(p.getClass(), (c, prefix) -> { |
| 860 | String key=prefix + "."; |
| 861 | properties.entrySet().stream().filter(e -> e.getKey().startsWith(key)) |
| 862 | .forEach(e -> props.put(e.getKey(), e.getValue())); |
| 863 | }); |
| 864 | ex.set(null); |
| 865 | |
| 866 | // StackType ip_version=Util.getIpStackType(); |
| 867 | InetAddress resolved_addr=p.getTransport() != null? p.getTransport().getBindAddress() : null; |
| 868 | final StackType ip_version=resolved_addr instanceof Inet6Address? StackType.IPv6 : StackType.IPv4; |
| 869 | Util.forAllComponents(p, (comp,prefix) -> { |
| 870 | try { |
| 871 | if(ex.isSet()) |
| 872 | return; |
| 873 | Map<String,String> m=new HashMap<>(); |
| 874 | String key=prefix + "."; |
| 875 | props.entrySet().stream().filter(e -> e.getKey().startsWith(key)) |
| 876 | .forEach(e -> m.put(e.getKey().substring(prefix.length() + 1), e.getValue())); |
| 877 | props.keySet().removeIf(k -> k.startsWith(key)); |
| 878 | if(!m.isEmpty()) { |
| 879 | Configurator.initializeAttrs(comp, m, ip_version); |
| 880 | if(!m.isEmpty()) { |
| 881 | String fmt="the following properties in %s:%s (%s) are not recognized: %s"; |
| 882 | ex.set(new IllegalArgumentException(String.format(fmt, p.getName(), prefix, |
| 883 | comp.getClass().getSimpleName(), m))); |
| 884 | } |
| 885 | } |
| 886 | Configurator.setDefaultAddressValues(comp, ip_version); |
| 887 | if(comp instanceof Lifecycle) |
| 888 | ((Lifecycle)comp).init(); |
| 889 | } |
| 890 | catch(Exception e) { |
| 891 | throw new IllegalArgumentException(String.format("failed initializing component %s in protocol %s: %s", |