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

io.scalecube.services.ServiceRegistration Maven / Gradle / Ivy

package io.scalecube.services;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.StringJoiner;

public class ServiceRegistration implements Externalizable {

  private static final long serialVersionUID = 1L;

  private String namespace;
  private Map tags;
  private Collection methods;

  /**
   * Constructor for SerDe.
   *
   * @deprecated exposed only for de/serialization purpose.
   */
  public ServiceRegistration() {}

  /**
   * Create a new service registration.
   *
   * @param namespace the namespace to use
   * @param tags tags
   * @param methods a collection of service method definitions
   */
  public ServiceRegistration(
      String namespace, Map tags, Collection methods) {
    this.namespace = Objects.requireNonNull(namespace, "ServiceRegistration.namespace is required");
    this.tags = Collections.unmodifiableMap(new HashMap<>(tags));
    this.methods = Collections.unmodifiableList(new ArrayList<>(methods));
  }

  public String namespace() {
    return namespace;
  }

  public Map tags() {
    return tags;
  }

  public Collection methods() {
    return methods;
  }

  @Override
  public String toString() {
    return new StringJoiner(", ", ServiceRegistration.class.getSimpleName() + "[", "]")
        .add("namespace=" + namespace)
        .add("tags=" + tags)
        .add("methods(" + methods.size() + ")")
        .toString();
  }

  @Override
  public void writeExternal(ObjectOutput out) throws IOException {
    // namespace
    out.writeUTF(namespace);

    // tags
    out.writeInt(tags.size());
    for (Entry entry : tags.entrySet()) {
      out.writeUTF(entry.getKey());
      out.writeUTF(entry.getValue());
    }

    // methods
    out.writeInt(methods.size());
    for (ServiceMethodDefinition method : methods) {
      out.writeObject(method);
    }
  }

  @Override
  public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    // namespace
    namespace = in.readUTF();

    // tags
    int tagsSize = in.readInt();
    Map tags = new HashMap<>(tagsSize);
    for (int i = 0; i < tagsSize; i++) {
      String key = in.readUTF();
      String value = in.readUTF();
      tags.put(key, value);
    }
    this.tags = Collections.unmodifiableMap(tags);

    // methods
    int methodsSize = in.readInt();
    List methods = new ArrayList<>(methodsSize);
    for (int i = 0; i < methodsSize; i++) {
      methods.add((ServiceMethodDefinition) in.readObject());
    }
    this.methods = Collections.unmodifiableList(methods);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy