Search in sources :

Example 1 with HttpServiceHandlerSpecification

use of co.cask.cdap.api.service.http.HttpServiceHandlerSpecification in project cdap by caskdata.

the class ServiceSpecificationCodec method decodeOldSpec.

private ServiceSpecification decodeOldSpec(JsonObject json) {
    String className = json.get("classname").getAsString();
    TwillSpecification twillSpec = twillSpecificationAdapter.fromJson(json.get("spec").getAsString()).getTwillSpecification();
    Map<String, HttpServiceHandlerSpecification> handlers = Maps.newHashMap();
    RuntimeSpecification handlerSpec = twillSpec.getRunnables().get(twillSpec.getName());
    Map<String, String> configs = handlerSpec.getRunnableSpecification().getConfigs();
    // Get the class names of all handlers. It is stored in the handler runnable spec configs
    List<String> handlerClasses = GSON.fromJson(configs.get("service.runnable.handlers"), new TypeToken<List<String>>() {
    }.getType());
    List<JsonObject> handlerSpecs = GSON.fromJson(configs.get("service.runnable.handler.spec"), new TypeToken<List<JsonObject>>() {
    }.getType());
    for (int i = 0; i < handlerClasses.size(); i++) {
        String handlerClass = handlerClasses.get(i);
        JsonObject spec = handlerSpecs.get(i);
        Map<String, String> properties = GSON.fromJson(spec.get("properties"), new TypeToken<Map<String, String>>() {
        }.getType());
        // Reconstruct the HttpServiceSpecification. However there is no way to determine the datasets or endpoints
        // as it is not recorded in old spec. It's ok since the spec is only used to load data from MDS during redeploy.
        handlers.put(spec.get("name").getAsString(), new HttpServiceHandlerSpecification(handlerClass, spec.get("name").getAsString(), spec.get("description").getAsString(), properties, ImmutableSet.<String>of(), ImmutableList.<ServiceHttpEndpoint>of()));
    }
    ResourceSpecification resourceSpec = handlerSpec.getResourceSpecification();
    return new ServiceSpecification(className, twillSpec.getName(), twillSpec.getName(), handlers, new Resources(resourceSpec.getMemorySize(), resourceSpec.getVirtualCores()), resourceSpec.getInstances());
}
Also used : ServiceHttpEndpoint(co.cask.cdap.api.service.http.ServiceHttpEndpoint) ServiceSpecification(co.cask.cdap.api.service.ServiceSpecification) JsonObject(com.google.gson.JsonObject) ResourceSpecification(org.apache.twill.api.ResourceSpecification) HttpServiceHandlerSpecification(co.cask.cdap.api.service.http.HttpServiceHandlerSpecification) RuntimeSpecification(org.apache.twill.api.RuntimeSpecification) TwillSpecification(org.apache.twill.api.TwillSpecification) ServiceHttpEndpoint(co.cask.cdap.api.service.http.ServiceHttpEndpoint) TypeToken(com.google.common.reflect.TypeToken) Resources(co.cask.cdap.api.Resources)

Example 2 with HttpServiceHandlerSpecification

use of co.cask.cdap.api.service.http.HttpServiceHandlerSpecification in project cdap by caskdata.

the class ServiceSpecificationCodec method deserialize.

@Override
public ServiceSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObj = (JsonObject) json;
    if (isOldSpec(jsonObj)) {
        return decodeOldSpec(jsonObj);
    }
    String className = jsonObj.get("className").getAsString();
    String name = jsonObj.get("name").getAsString();
    String description = jsonObj.get("description").getAsString();
    Map<String, HttpServiceHandlerSpecification> handlers = deserializeMap(jsonObj.get("handlers"), context, HttpServiceHandlerSpecification.class);
    Resources resources = context.deserialize(jsonObj.get("resources"), Resources.class);
    int instances = jsonObj.get("instances").getAsInt();
    return new ServiceSpecification(className, name, description, handlers, resources, instances);
}
Also used : ServiceSpecification(co.cask.cdap.api.service.ServiceSpecification) JsonObject(com.google.gson.JsonObject) Resources(co.cask.cdap.api.Resources) HttpServiceHandlerSpecification(co.cask.cdap.api.service.http.HttpServiceHandlerSpecification) ServiceHttpEndpoint(co.cask.cdap.api.service.http.ServiceHttpEndpoint)

Example 3 with HttpServiceHandlerSpecification

use of co.cask.cdap.api.service.http.HttpServiceHandlerSpecification in project cdap by caskdata.

the class DefaultHttpServiceHandlerConfigurer method createSpecification.

/**
   * Creates a {@link HttpServiceHandlerSpecification} from the parameters stored in this class.
   *
   * @return a new specification from the parameters stored in this instance
   */
public HttpServiceHandlerSpecification createSpecification() {
    List<ServiceHttpEndpoint> endpoints = new ArrayList<>();
    // Inspect the handler to grab all @UseDataset, @Property and endpoints.
    Reflections.visit(handler, handler.getClass(), new DataSetFieldExtractor(datasets), new PropertyFieldExtractor(properties), new ServiceEndpointExtractor(endpoints));
    return new HttpServiceHandlerSpecification(handler.getClass().getName(), name, "", properties, datasets, endpoints);
}
Also used : ServiceHttpEndpoint(co.cask.cdap.api.service.http.ServiceHttpEndpoint) ArrayList(java.util.ArrayList) DataSetFieldExtractor(co.cask.cdap.internal.specification.DataSetFieldExtractor) PropertyFieldExtractor(co.cask.cdap.internal.specification.PropertyFieldExtractor) HttpServiceHandlerSpecification(co.cask.cdap.api.service.http.HttpServiceHandlerSpecification)

Example 4 with HttpServiceHandlerSpecification

use of co.cask.cdap.api.service.http.HttpServiceHandlerSpecification in project cdap by caskdata.

the class DefaultServiceConfigurer method createHandlerSpecs.

/**
   * Constructs HttpServiceSpecifications for each of the handlers in the {@param handlers} list.
   * Also performs verifications on these handlers (that a NettyHttpService can be constructed from them).
   */
private Map<String, HttpServiceHandlerSpecification> createHandlerSpecs(List<? extends HttpServiceHandler> handlers) {
    verifyHandlers(handlers);
    Map<String, HttpServiceHandlerSpecification> handleSpecs = Maps.newHashMap();
    for (HttpServiceHandler handler : handlers) {
        DefaultHttpServiceHandlerConfigurer configurer = new DefaultHttpServiceHandlerConfigurer(handler, deployNamespace, artifactId, artifactRepository, pluginInstantiator);
        handler.configure(configurer);
        HttpServiceHandlerSpecification spec = configurer.createSpecification();
        Preconditions.checkArgument(!handleSpecs.containsKey(spec.getName()), "Handler with name %s already existed.", spec.getName());
        handleSpecs.put(spec.getName(), spec);
        addStreams(configurer.getStreams());
        addDatasetModules(configurer.getDatasetModules());
        addDatasetSpecs(configurer.getDatasetSpecs());
        addPlugins(configurer.getPlugins());
    }
    return handleSpecs;
}
Also used : HttpServiceHandler(co.cask.cdap.api.service.http.HttpServiceHandler) HttpServiceHandlerSpecification(co.cask.cdap.api.service.http.HttpServiceHandlerSpecification)

Example 5 with HttpServiceHandlerSpecification

use of co.cask.cdap.api.service.http.HttpServiceHandlerSpecification in project cdap by caskdata.

the class ServiceHttpServer method createHandlerDelegatorContexts.

private List<HandlerDelegatorContext> createHandlerDelegatorContexts(Program program, ServiceSpecification spec, BasicHttpServiceContextFactory contextFactory) {
    // Constructs all handler delegator. It is for bridging ServiceHttpHandler and HttpHandler (in netty-http).
    List<HandlerDelegatorContext> delegatorContexts = Lists.newArrayList();
    InstantiatorFactory instantiatorFactory = new InstantiatorFactory(false);
    for (Map.Entry<String, HttpServiceHandlerSpecification> entry : spec.getHandlers().entrySet()) {
        try {
            Class<?> handlerClass = program.getClassLoader().loadClass(entry.getValue().getClassName());
            @SuppressWarnings("unchecked") TypeToken<HttpServiceHandler> type = TypeToken.of((Class<HttpServiceHandler>) handlerClass);
            delegatorContexts.add(new HandlerDelegatorContext(type, instantiatorFactory, entry.getValue(), contextFactory));
        } catch (Exception e) {
            LOG.error("Could not initialize HTTP Service");
            throw Throwables.propagate(e);
        }
    }
    return delegatorContexts;
}
Also used : InstantiatorFactory(co.cask.cdap.common.lang.InstantiatorFactory) HttpServiceHandler(co.cask.cdap.api.service.http.HttpServiceHandler) HttpServiceHandlerSpecification(co.cask.cdap.api.service.http.HttpServiceHandlerSpecification) Map(java.util.Map)

Aggregations

HttpServiceHandlerSpecification (co.cask.cdap.api.service.http.HttpServiceHandlerSpecification)8 ServiceSpecification (co.cask.cdap.api.service.ServiceSpecification)5 ServiceHttpEndpoint (co.cask.cdap.api.service.http.ServiceHttpEndpoint)5 Resources (co.cask.cdap.api.Resources)2 HttpServiceHandler (co.cask.cdap.api.service.http.HttpServiceHandler)2 JsonObject (com.google.gson.JsonObject)2 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)1 FlowSpecification (co.cask.cdap.api.flow.FlowSpecification)1 FlowletConnection (co.cask.cdap.api.flow.FlowletConnection)1 FlowletDefinition (co.cask.cdap.api.flow.FlowletDefinition)1 MapReduceSpecification (co.cask.cdap.api.mapreduce.MapReduceSpecification)1 SparkSpecification (co.cask.cdap.api.spark.SparkSpecification)1 InstantiatorFactory (co.cask.cdap.common.lang.InstantiatorFactory)1 DefaultApplicationSpecification (co.cask.cdap.internal.app.DefaultApplicationSpecification)1 ServiceSpecificationCodec (co.cask.cdap.internal.app.ServiceSpecificationCodec)1 DataSetFieldExtractor (co.cask.cdap.internal.specification.DataSetFieldExtractor)1 PropertyFieldExtractor (co.cask.cdap.internal.specification.PropertyFieldExtractor)1 ApplicationId (co.cask.cdap.proto.id.ApplicationId)1 NamespaceId (co.cask.cdap.proto.id.NamespaceId)1 ProgramId (co.cask.cdap.proto.id.ProgramId)1