GenApiConfigAction extends EndpointsToolAction { 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.asCharSink(new File(apiConfigFilePath), UTF_8).write(apiConfigFileContent); System.out.println("API configuration written to " + apiConfigFilePath); } } return apiConfigs.values(); } GenApiConfigAction(); @Override boolean execute(); Iterable<String> genApiConfig(
      URL[] classPath, String outputDirPath, String warPath, List<String> serviceClassNames,
      boolean outputToDisk); @Override String getUsageString(); static final String NAME; }
@Test public void testGenApiConfig() throws Exception { tool.execute( new String[]{GenApiConfigAction.NAME, option(EndpointsToolAction.OPTION_CLASS_PATH_SHORT), "classPath", option(EndpointsToolAction.OPTION_OUTPUT_DIR_SHORT), "outputDir", "MyService", "MyService2"}); assertFalse(usagePrinted); assertThat(Lists.newArrayList(classPath)) .containsExactly(new File("classPath").toURI().toURL(), new File(new File(EndpointsToolAction.DEFAULT_WAR_PATH).getAbsoluteFile(), "/WEB-INF/classes") .toURI() .toURL()); assertEquals("outputDir", outputDirPath); assertEquals(EndpointsToolAction.DEFAULT_WAR_PATH, warPath); assertStringsEqual(Arrays.asList("MyService", "MyService2"), serviceClassNames); assertTrue(outputToDisk); }