/** * Registers the provided key with the provided handler into the process's default registrations. Recommended * NOT to do this -- instead you should statically configure the registration of keys to handlers. * * {@link BaggageHandlerRegistry} instances are immutable, so adding a new registration will create and return a * new {@link BaggageHandlerRegistry} instance with the additional mapping. */ Registrations add(BagKey key, BaggageHandler<?> handler) { Map<BagKey, BaggageHandler<?>> newMapping = new TreeMap<>(keysToHandlers); newMapping.put(key, handler); return new Registrations(newMapping); }
/** * Unregisters the handler for the specified bag key. * * {@link BaggageHandlerRegistry} instances are immutable, so removing a registration will return a new * {@link BaggageHandlerRegistry} instance with the mapping removed. */ Registrations remove(BagKey key) { Map<BagKey, BaggageHandler<?>> newMapping = new TreeMap<>(keysToHandlers); if (newMapping.remove(key) != null) { return new Registrations(newMapping); } else { return this; } } }
/** * Create a {@link BaggageHandlerRegistry} instance by parsing the mappings configured in the provided * {@link Config} * * @param config a typesafe config * @return a {@link BaggageHandlerRegistry} instance with handlers loaded for the configured bag keys */ static BaggageHandlerRegistry create(Config config) { Map<BagKey, BaggageHandler<?>> mapping = new TreeMap<>(); for (Entry<String, ConfigValue> x : config.getConfig(BAGS_CONFIGURATION_KEY).entrySet()) { String bagHandlerClassName = x.getValue().unwrapped().toString(); Integer bagNumber = parseBagKey(x.getKey(), bagHandlerClassName); if (bagNumber == null) continue; BagKey key = BagKey.indexed(bagNumber); BaggageHandler<?> handler = resolveHandler(bagHandlerClassName); if (handler == null) continue; mapping.put(key, handler); } if (mapping.size() == 0) { log.warn("No baggage handlers are registered -- if this is unexpected, ensure `bag` is correctly configured"); } else { String handlersString = mapping.entrySet().stream() .map(e -> "\t" + e.getKey().toString() + ": " + e.getValue().getClass().getName().toString()) .collect(Collectors.joining("\n")); log.info(mapping.size() + " baggage handlers registered:\n" + handlersString); } return new BaggageHandlerRegistry(new Registrations(mapping)); }
/** * Create an empty {@link BaggageHandlerRegistry} */ static BaggageHandlerRegistry empty() { return new BaggageHandlerRegistry(new Registrations(new TreeMap<>())); }