All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nstream.adapter.nats.NatsConsumerLoader Maven / Gradle / Ivy

The newest version!
package nstream.adapter.nats;

import io.nats.client.PullSubscribeOptions;
import io.nats.client.api.ConsumerConfiguration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.SortedMap;
import java.util.regex.Pattern;
import static swim.util.Severity.warning;

public final class NatsConsumerLoader {

  private static final Map> KNOWN_FIELDS = Map.ofEntries(
      Map.entry("name", String.class),
      Map.entry("configuration", ConsumerConfiguration.class),
      Map.entry("stream", String.class),
      Map.entry("durable", String.class),
      Map.entry("bind", Boolean.TYPE),
      Map.entry("fastBind", Boolean.TYPE),
      Map.entry("messageAlarmTime", Long.TYPE)
  );

  private NatsConsumerLoader() {
  }

  private static void updateMap(String name, String value, Map result) {
    if ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) {
      result.put(name, Boolean.parseBoolean(value));
      warning("[WARN] NatsConsumerLoader treated unknown property " + name + " as boolean");
    } else {
      try {
        result.put(name, Long.parseLong(value));
        warning("[WARN] NatsConsumerLoader treated unknown property " + name + " as long");
      } catch (Exception e1) {
        result.put(name, value);
        warning("[WARN] NatsConsumerLoader treated unknown property " + name + " as String");
      }
    }
  }

  public static PullSubscribeOptions toLoadableConf(Properties properties) {

    final PullSubscribeOptions.Builder psoBuilder = PullSubscribeOptions.builder();

    final Map result = new HashMap<>(properties.size());
    properties.forEach((k, v) -> {
      final String name = k.toString();
      final String value = v.toString();
      if (KNOWN_FIELDS.containsKey(name)) {
        updateMap(name, value, KNOWN_FIELDS.get(name), result);
      } else {
        updateMap(name, value, result);
      }
    });

    // Dynamically apply PullSubscribeOptions parameters
    result.forEach((key, value) -> {
      switch (key) {
        case "name":
          psoBuilder.name((String) value);
          break;
        case "configuration":
          psoBuilder.configuration((ConsumerConfiguration) value);
          break;
        case "stream":
          psoBuilder.stream((String) value);
          break;
        case "durable":
          psoBuilder.durable((String) value);
          break;
        case "bind":
          psoBuilder.bind((Boolean) value);
          break;
        case "fastBind":
          psoBuilder.fastBind((Boolean) value);
          break;
        case "messageAlarmTime":
          psoBuilder.messageAlarmTime((Long) value);
          break;
        default:
          break; // FIXME
      }
    });

    return psoBuilder.build();
  }

  private static void updateMap(String name, String value, Class knownClass,
                                Map result) {
    if (knownClass == String.class) {
      result.put(name, value);
    } else if (knownClass == Integer.TYPE) {
      result.put(name, Integer.parseInt(value));
    } else if (knownClass == Long.TYPE) {
      result.put(name, Long.parseLong(value));
    } else if (knownClass == Boolean.TYPE) {
      result.put(name, Boolean.parseBoolean(value));
    } else if (knownClass == Set.class) {
      if (value != null && !value.isEmpty()) {
        result.put(name, Set.of(value.split(",")));
      }
    } else if (knownClass == Pattern.class) {
      result.put(name, Pattern.compile(value));
    } else if (knownClass == SortedMap.class) {
      throw new UnsupportedOperationException("not yet implemented");
    } else { // FIXME
      throw new UnsupportedOperationException("not yet implemented");
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy