Search in sources :

Example 1 with EventHandlerSpecification

use of com.continuuity.weave.api.EventHandlerSpecification in project weave by continuuity.

the class YarnWeavePreparer method saveWeaveSpec.

private void saveWeaveSpec(WeaveSpecification spec, final Multimap<String, LocalFile> runnableLocalFiles, Map<String, LocalFile> localFiles) throws IOException {
    // Rewrite LocalFiles inside weaveSpec
    Map<String, RuntimeSpecification> runtimeSpec = Maps.transformEntries(spec.getRunnables(), new Maps.EntryTransformer<String, RuntimeSpecification, RuntimeSpecification>() {

        @Override
        public RuntimeSpecification transformEntry(String key, RuntimeSpecification value) {
            return new DefaultRuntimeSpecification(value.getName(), value.getRunnableSpecification(), value.getResourceSpecification(), runnableLocalFiles.get(key));
        }
    });
    // Serialize into a local temp file.
    LOG.debug("Create and copy {}", Constants.Files.WEAVE_SPEC);
    Location location = createTempLocation(Constants.Files.WEAVE_SPEC);
    Writer writer = new OutputStreamWriter(location.getOutputStream(), Charsets.UTF_8);
    try {
        EventHandlerSpecification eventHandler = spec.getEventHandler();
        if (eventHandler == null) {
            eventHandler = new LogOnlyEventHandler().configure();
        }
        WeaveSpecificationAdapter.create().toJson(new DefaultWeaveSpecification(spec.getName(), runtimeSpec, spec.getOrders(), eventHandler), writer);
    } finally {
        writer.close();
    }
    LOG.debug("Done {}", Constants.Files.WEAVE_SPEC);
    localFiles.put(Constants.Files.WEAVE_SPEC, createLocalFile(Constants.Files.WEAVE_SPEC, location));
}
Also used : DefaultRuntimeSpecification(com.continuuity.weave.internal.DefaultRuntimeSpecification) EventHandlerSpecification(com.continuuity.weave.api.EventHandlerSpecification) DefaultRuntimeSpecification(com.continuuity.weave.internal.DefaultRuntimeSpecification) RuntimeSpecification(com.continuuity.weave.api.RuntimeSpecification) DefaultWeaveSpecification(com.continuuity.weave.internal.DefaultWeaveSpecification) LogOnlyEventHandler(com.continuuity.weave.internal.LogOnlyEventHandler) Maps(com.google.common.collect.Maps) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) Location(com.continuuity.weave.filesystem.Location)

Example 2 with EventHandlerSpecification

use of com.continuuity.weave.api.EventHandlerSpecification in project weave by continuuity.

the class ApplicationMasterService method createEventHandler.

private EventHandler createEventHandler(WeaveSpecification weaveSpec) {
    try {
        // Should be able to load by this class ClassLoader, as they packaged in the same jar.
        EventHandlerSpecification handlerSpec = weaveSpec.getEventHandler();
        Class<?> handlerClass = getClass().getClassLoader().loadClass(handlerSpec.getClassName());
        Preconditions.checkArgument(EventHandler.class.isAssignableFrom(handlerClass), "Class {} does not implements {}", handlerClass, EventHandler.class.getName());
        return Instances.newInstance((Class<? extends EventHandler>) handlerClass);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : EventHandler(com.continuuity.weave.api.EventHandler) EventHandlerSpecification(com.continuuity.weave.api.EventHandlerSpecification) IOException(java.io.IOException)

Example 3 with EventHandlerSpecification

use of com.continuuity.weave.api.EventHandlerSpecification in project weave by continuuity.

the class WeaveSpecificationCodec method serialize.

@Override
public JsonElement serialize(WeaveSpecification src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject json = new JsonObject();
    json.addProperty("name", src.getName());
    json.add("runnables", context.serialize(src.getRunnables(), new TypeToken<Map<String, RuntimeSpecification>>() {
    }.getType()));
    json.add("orders", context.serialize(src.getOrders(), new TypeToken<List<WeaveSpecification.Order>>() {
    }.getType()));
    EventHandlerSpecification eventHandler = src.getEventHandler();
    if (eventHandler != null) {
        json.add("handler", context.serialize(eventHandler, EventHandlerSpecification.class));
    }
    return json;
}
Also used : JsonObject(com.google.gson.JsonObject) List(java.util.List) WeaveSpecification(com.continuuity.weave.api.WeaveSpecification) DefaultWeaveSpecification(com.continuuity.weave.internal.DefaultWeaveSpecification) EventHandlerSpecification(com.continuuity.weave.api.EventHandlerSpecification) DefaultEventHandlerSpecification(com.continuuity.weave.internal.DefaultEventHandlerSpecification) Map(java.util.Map)

Example 4 with EventHandlerSpecification

use of com.continuuity.weave.api.EventHandlerSpecification in project weave by continuuity.

the class WeaveSpecificationCodec method deserialize.

@Override
public WeaveSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    String name = jsonObj.get("name").getAsString();
    Map<String, RuntimeSpecification> runnables = context.deserialize(jsonObj.get("runnables"), new TypeToken<Map<String, RuntimeSpecification>>() {
    }.getType());
    List<WeaveSpecification.Order> orders = context.deserialize(jsonObj.get("orders"), new TypeToken<List<WeaveSpecification.Order>>() {
    }.getType());
    JsonElement handler = jsonObj.get("handler");
    EventHandlerSpecification eventHandler = null;
    if (handler != null && !handler.isJsonNull()) {
        eventHandler = context.deserialize(handler, EventHandlerSpecification.class);
    }
    return new DefaultWeaveSpecification(name, runnables, orders, eventHandler);
}
Also used : TypeToken(com.google.common.reflect.TypeToken) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) EventHandlerSpecification(com.continuuity.weave.api.EventHandlerSpecification) DefaultEventHandlerSpecification(com.continuuity.weave.internal.DefaultEventHandlerSpecification) RuntimeSpecification(com.continuuity.weave.api.RuntimeSpecification) DefaultWeaveSpecification(com.continuuity.weave.internal.DefaultWeaveSpecification)

Aggregations

EventHandlerSpecification (com.continuuity.weave.api.EventHandlerSpecification)4 DefaultWeaveSpecification (com.continuuity.weave.internal.DefaultWeaveSpecification)3 RuntimeSpecification (com.continuuity.weave.api.RuntimeSpecification)2 DefaultEventHandlerSpecification (com.continuuity.weave.internal.DefaultEventHandlerSpecification)2 JsonObject (com.google.gson.JsonObject)2 EventHandler (com.continuuity.weave.api.EventHandler)1 WeaveSpecification (com.continuuity.weave.api.WeaveSpecification)1 Location (com.continuuity.weave.filesystem.Location)1 DefaultRuntimeSpecification (com.continuuity.weave.internal.DefaultRuntimeSpecification)1 LogOnlyEventHandler (com.continuuity.weave.internal.LogOnlyEventHandler)1 Maps (com.google.common.collect.Maps)1 TypeToken (com.google.common.reflect.TypeToken)1 JsonElement (com.google.gson.JsonElement)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 List (java.util.List)1 Map (java.util.Map)1