Search in sources :

Example 1 with JsonConfigWriter

use of com.google.api.server.spi.config.jsonwriter.JsonConfigWriter in project endpoints-java by cloudendpoints.

the class GenApiConfigAction method genApiConfig.

/**
 * Generates API configuration for an array of service classes.
 * @param classPath Class path to load service classes and their dependencies
 * @param outputDirPath Directory to write API configuration files into
 * @param serviceClassNames Array of service class names of the API
 * @param warPath Directory or file containing a WAR layout
 * @param outputToDisk Iff {@code true}, outputs a *.api file to disk for each API.
 * @return JSON-formatted configurations for each API.
 */
public Iterable<String> genApiConfig(URL[] classPath, String outputDirPath, String warPath, List<String> serviceClassNames, boolean outputToDisk) throws ClassNotFoundException, IOException, ApiConfigException {
    File outputDir = new File(outputDirPath);
    if (!outputDir.isDirectory()) {
        throw new IllegalArgumentException(outputDirPath + " is not a directory");
    }
    ClassLoader classLoader = new URLClassLoader(classPath, getClass().getClassLoader());
    ApiConfig.Factory configFactory = new ApiConfig.Factory();
    TypeLoader typeLoader = new TypeLoader(classLoader);
    SchemaRepository schemaRepository = new SchemaRepository(typeLoader);
    ApiConfigValidator validator = new ApiConfigValidator(typeLoader, schemaRepository);
    JsonConfigWriter jsonConfigWriter = new JsonConfigWriter(typeLoader, validator);
    ApiConfigGenerator generator = new AnnotationApiConfigGenerator(jsonConfigWriter, classLoader, configFactory);
    Map<String, String> apiConfigs = generator.generateConfig(ServiceContext.create(AppEngineUtil.getApplicationId(warPath), ServiceContext.DEFAULT_API_NAME), loadClasses(classLoader, serviceClassNames));
    if (outputToDisk) {
        for (Map.Entry<String, String> entry : apiConfigs.entrySet()) {
            String apiConfigFileName = entry.getKey();
            String apiConfigFileContent = entry.getValue();
            String apiConfigFilePath = outputDir + "/" + apiConfigFileName;
            Files.write(apiConfigFileContent, new File(apiConfigFilePath), UTF_8);
            System.out.println("API configuration written to " + apiConfigFilePath);
        }
    }
    return apiConfigs.values();
}
Also used : TypeLoader(com.google.api.server.spi.TypeLoader) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) JsonConfigWriter(com.google.api.server.spi.config.jsonwriter.JsonConfigWriter) URLClassLoader(java.net.URLClassLoader) ApiConfigValidator(com.google.api.server.spi.config.validation.ApiConfigValidator) URLClassLoader(java.net.URLClassLoader) File(java.io.File) Map(java.util.Map) SchemaRepository(com.google.api.server.spi.config.model.SchemaRepository)

Aggregations

TypeLoader (com.google.api.server.spi.TypeLoader)1 JsonConfigWriter (com.google.api.server.spi.config.jsonwriter.JsonConfigWriter)1 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)1 SchemaRepository (com.google.api.server.spi.config.model.SchemaRepository)1 ApiConfigValidator (com.google.api.server.spi.config.validation.ApiConfigValidator)1 File (java.io.File)1 URLClassLoader (java.net.URLClassLoader)1 Map (java.util.Map)1