jackye1995 commented on a change in pull request #4073:
URL: https://github.com/apache/iceberg/pull/4073#discussion_r814607994
##########
File path: core/src/main/java/org/apache/iceberg/CatalogUtil.java
##########
@@ -275,6 +290,73 @@ public static FileIO loadFileIO(
return fileIO;
}
+ public static void initializeListeners(Map<String, String> properties) {
+ Map<String, Map<String, String>> propertiesSummary = Maps.newHashMap();
+ Map<String, String> commonProperties = Maps.newHashMap();
+ for (String key : properties.keySet()) {
+ Optional<Pair<String, String>> listenerInfo =
CatalogProperties.parseListenerCatalogProperty(key);
+ if (listenerInfo.isPresent()) {
+ String name = listenerInfo.get().first();
+ String property = listenerInfo.get().second();
+ propertiesSummary.computeIfAbsent(name, k -> Maps.newHashMap());
+ propertiesSummary.get(name).put(property, properties.get(key));
+ } else {
+ commonProperties.put(key, properties.get(key));
+ }
+ }
+
+ // inherit all common properties during listener initialization
+ propertiesSummary.forEach((k, v) -> v.putAll(commonProperties));
+
+ for (String listenerName : propertiesSummary.keySet()) {
+ Map<String, String> listenerProperties =
propertiesSummary.get(listenerName);
+ String listenerImpl =
listenerProperties.get(CatalogProperties.LISTENER_PROPERTY_IMPL);
+ ValidationException.check(listenerImpl != null,
+ "Cannot initialize listener %s, missing %s property",
+ listenerName, CatalogProperties.LISTENER_PROPERTY_IMPL);
+
+ String eventTypesString =
listenerProperties.get(CatalogProperties.LISTENER_PROPERTY_EVENT_TYPES);
+ Set<Class<?>> eventTypes = eventTypesString != null ?
Arrays.stream(eventTypesString.split(","))
+ .map(s -> {
+ try {
+ return Class.forName(s);
+ } catch (ClassNotFoundException e) {
+ throw new ValidationException(e, "Cannot find listener event
type class %s", s);
+ }
+ })
+ .collect(Collectors.toSet()) :
CatalogProperties.LISTENER_EVENT_TYPES_DEFAULT;
+
+ eventTypes.forEach(t ->
CatalogUtil.loadAndRegisterListener(listenerImpl, listenerName, t,
listenerProperties));
+ }
+ }
+
+ @VisibleForTesting
+ static <T> Listener<T> loadAndRegisterListener(
+ String listenerClass,
+ String listenerName,
+ Class<T> eventType,
+ Map<String, String> properties) {
+ DynConstructors.Ctor<Listener<T>> ctor;
+ try {
+ ctor = DynConstructors.builder(Listener.class).impl(listenerClass,
eventType).buildChecked();
+ } catch (NoSuchMethodException e) {
+ throw new IllegalArgumentException(String.format(
+ "Cannot initialize Listener, missing no-arg constructor: %s",
listenerClass), e);
+ }
+
+ Listener<T> listener;
+ try {
+ listener = ctor.newInstance();
+ } catch (ClassCastException e) {
+ throw new IllegalArgumentException(String.format(
+ "Cannot initialize Listener, %s does not implement
org.apache.iceberg.events.Listener", listenerClass), e);
+ }
+
+ listener.initialize(listenerName, properties);
+ Listeners.register(listener, eventType);
Review comment:
maybe better to register outside this method, just to make this method
cleaner
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]