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

com.google.api.server.spi.tools.AnnotationApiConfigGenerator Maven / Gradle / Ivy

There is a newer version: 2.2.2
Show newest version
/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.api.server.spi.tools;

import com.google.api.server.spi.ServiceContext;
import com.google.api.server.spi.TypeLoader;
import com.google.api.server.spi.config.ApiConfigException;
import com.google.api.server.spi.config.ApiConfigLoader;
import com.google.api.server.spi.config.ApiConfigWriter;
import com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader;
import com.google.api.server.spi.config.jsonwriter.JsonConfigWriter;
import com.google.api.server.spi.config.model.ApiConfig;
import com.google.api.server.spi.config.model.ApiKey;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * Generates configuration files from annotations on endpoint classes.
 */
public class AnnotationApiConfigGenerator implements ApiConfigGenerator {
  private final ApiConfigWriter configWriter;
  private final ApiConfigLoader configLoader;

  public AnnotationApiConfigGenerator() throws ClassNotFoundException {
    this(new JsonConfigWriter(), AnnotationApiConfigGenerator.class.getClassLoader(),
        new ApiConfig.Factory());
  }

  public AnnotationApiConfigGenerator(ApiConfigWriter apiConfigWriter, ClassLoader classLoader,
      ApiConfig.Factory configFactory) throws ClassNotFoundException {
    this.configWriter = apiConfigWriter;
    this.configLoader = createConfigLoader(configFactory, new TypeLoader(classLoader));
  }

  private static ApiConfigLoader createConfigLoader(ApiConfig.Factory configFactory,
      TypeLoader typeLoader) {
    return new ApiConfigLoader(
        configFactory, typeLoader, new ApiConfigAnnotationReader(typeLoader.getAnnotationTypes()));
  }

  @Override
  public Map generateConfig(Class... serviceClasses)
      throws ApiConfigException {
    return generateConfig(ServiceContext.create(), serviceClasses);
  }

  @Override
  public Map generateConfig(
      ServiceContext serviceContext, Class... serviceClasses)
      throws ApiConfigException {

    List apiConfigs = Lists.newArrayListWithCapacity(serviceClasses.length);
    for (Class serviceClass : serviceClasses) {
      apiConfigs.add(configLoader.loadConfiguration(serviceContext, serviceClass));
    }

    Map configByApiKey = configWriter.writeConfig(apiConfigs);
    // This *must* retain the order of configByApiKey so the lily_java_api BUILD rule has
    // predictable output order.
    Map configByFileName = Maps.newLinkedHashMap();
    for (Map.Entry entry : configByApiKey.entrySet()) {
      configByFileName.put(
          entry.getKey().getApiString() + "." + configWriter.getFileExtension(), entry.getValue());
    }

    return configByFileName;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy