Example #1
0
 /**
  * Serializes a component context to JSON.
  *
  * @param context The context to serialize.
  * @return The serialized context.
  */
 public static JsonObject serialize(ComponentContext<?> context) {
   return serialize(context.uri(), context.network());
 }
Example #2
0
  @SuppressWarnings("unchecked")
  private static <T extends Context<T>> T deserialize(String uri, JsonObject context) {
    ContextUri curi = new ContextUri(uri);
    NetworkContext network =
        SerializerFactory.getSerializer(Context.class)
            .deserializeObject(context, NetworkContext.class);
    if (!curi.getCluster().equals(network.cluster()) || !curi.getNetwork().equals(network.name())) {
      throw new IllegalArgumentException(
          "The given URI does not match the given context configuration");
    }

    if (curi.hasComponent()) {
      ComponentContext<?> component = network.component(curi.getComponent());
      if (component == null) {
        throw new IllegalArgumentException(
            "The URI component "
                + curi.getComponent()
                + " does not exist in the given network configuration");
      }
      if (curi.hasInstance()) {
        InstanceContext instance = component.instance(curi.getInstance());
        if (instance == null) {
          throw new IllegalArgumentException(
              "The URI instance "
                  + curi.getInstance()
                  + " does not exist in the given component configuration");
        }
        if (curi.hasEndpoint()) {
          switch (curi.getEndpoint()) {
            case ContextUri.ENDPOINT_IN:
              InputContext input = instance.input();
              if (curi.hasPort()) {
                InputPortContext inPort = input.port(curi.getPort());
                if (inPort == null) {
                  throw new IllegalArgumentException(
                      "The URI port "
                          + curi.getPort()
                          + " does not exist in the given input configuration");
                }
                return (T) inPort;
              }
              return (T) input;
            case ContextUri.ENDPOINT_OUT:
              OutputContext output = instance.output();
              if (curi.hasPort()) {
                OutputPortContext outPort = output.port(curi.getPort());
                if (outPort == null) {
                  throw new IllegalArgumentException(
                      "The URI port "
                          + curi.getPort()
                          + " does not exist in the given output configuration");
                }
                return (T) outPort;
              }
              return (T) output;
            default:
              throw new IllegalArgumentException(
                  "The URI endpoint " + curi.getEndpoint() + " is not a valid endpoint type");
          }
        }
        return (T) instance;
      }
      return (T) component;
    }
    return (T) network;
  }