private static JsonObject serialize(String uri, NetworkContext context) { return new JsonObject() .putString("uri", uri) .putObject( "context", SerializerFactory.getSerializer(Context.class).serializeToObject(context)); }
@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; }
/** * Configuration helper methods. * * @author <a href="http://github.com/kuujo">Jordan Halterman</a> */ public final class Configs { private static final Serializer serializer = SerializerFactory.getSerializer(Config.class); /** * Creates a network configuration from json. * * @param config A json network configuration. * @return A network configuration. */ public static NetworkConfig createNetwork(JsonObject config) { return serializer.deserializeObject(config, NetworkConfig.class); } /** * Creates a component configuration from json. * * @param config A json component configuration. * @return A component configuration. */ @SuppressWarnings("unchecked") public static <T extends ComponentConfig<T>> T createComponent(JsonObject config) { return (T) serializer.deserializeObject(config, ComponentConfig.class); } /** * Creates a connection configuration from json. * * @param config A json connection configuration. * @return A connection configuration. */ public static ConnectionConfig createConnection(JsonObject config) { return serializer.deserializeObject(config, ConnectionConfig.class); } /** * Merges two network configurations into a single configuraiton. * * @param base The base configuration. * @param merge The configuration to merge. * @return The combined configuration. */ public static NetworkConfig mergeNetworks(NetworkConfig base, NetworkConfig merge) { if (!base.getName().equals(merge.getName())) { throw new IllegalArgumentException("Cannot merge networks of different names."); } for (ComponentConfig<?> component : merge.getComponents()) { if (!base.hasComponent(component.getName())) { base.addComponent(component); } } for (ConnectionConfig connection : merge.getConnections()) { boolean exists = false; for (ConnectionConfig existing : base.getConnections()) { if (existing.equals(connection)) { exists = true; break; } } if (!exists) { base.createConnection(connection); } } return base; } /** * Unmerges one network configuration from another. * * @param base The base network configuration. * @param unmerge The configuration to extract. * @return The cleaned configuration. */ public static NetworkConfig unmergeNetworks(NetworkConfig base, NetworkConfig unmerge) { if (!base.getName().equals(unmerge.getName())) { throw new IllegalArgumentException("Cannot merge networks of different names."); } for (ComponentConfig<?> component : unmerge.getComponents()) { base.removeComponent(component.getName()); } for (ConnectionConfig connection : unmerge.getConnections()) { base.destroyConnection(connection); } return base; } }