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();
}
Aggregations