EndpointsToolAction extends Action { protected URL[] computeClassPath(String warPath, String classPath) throws MalformedURLException { ImmutableList.Builder<URL> urls = ImmutableList.builder(); File warDir = new File(warPath).getAbsoluteFile(); File webInfDir = new File(warDir, "WEB-INF"); File classesDir = new File(webInfDir, "classes"); urls.add(classesDir.toURI().toURL()); File libDir = new File(webInfDir, "lib"); String[] files = libDir.list(); if (files != null) { for (String file : files) { File maybeJar = new File(libDir, file); if (maybeJar.isFile() && file.endsWith(".jar")) { urls.add(maybeJar.toURI().toURL()); } } } if (!Strings.isNullOrEmpty(classPath)) { for (String classPathEntry : classPath.split(File.pathSeparator)) { urls.add(new File(classPathEntry).toURI().toURL()); } } return urls.build().toArray(new URL[0]); } EndpointsToolAction(String name); abstract String getUsageString(); abstract boolean execute(); @Override void apply(); boolean isHelpDisplayNeeded(); void setHelpDisplayNeeded(boolean helpDisplayNeeded); String getExampleString(); void setExampleString(String exampleString); static final String OPTION_CLASS_PATH_SHORT; static final String OPTION_CLASS_PATH_LONG; static final String OPTION_OUTPUT_DIR_SHORT; static final String OPTION_OUTPUT_DIR_LONG; static final String OPTION_WAR_SHORT; static final String OPTION_WAR_LONG; static final String OPTION_LANGUAGE_SHORT; static final String OPTION_LANGUAGE_LONG; static final String OPTION_BUILD_SYSTEM_SHORT; static final String OPTION_BUILD_SYSTEM_LONG; static final String OPTION_FORMAT_SHORT; static final String OPTION_FORMAT_LONG; static final String OPTION_DEBUG; static final String OPTION_HOSTNAME_SHORT; static final String OPTION_HOSTNAME_LONG; static final String OPTION_BASE_PATH_SHORT; static final String OPTION_BASE_PATH_LONG; }
@Test public void testComputeClasspath() throws IOException { String tempDir = tmpFolder.getRoot().getAbsolutePath(); File classesDir = new File(tempDir, "WEB-INF" + File.separator + "classes"); if (!classesDir.exists()) { classesDir.mkdirs(); } File libDir = new File(tempDir, "WEB-INF" + File.separator + "lib"); if (!libDir.exists()) { libDir.mkdirs(); } File jarFileInLibDir = new File(libDir.getAbsolutePath() + File.separator + "lib.jar"); jarFileInLibDir.createNewFile(); File jarFileInClasspath = new File(tempDir + File.separator + "classpath.jar"); jarFileInClasspath.createNewFile(); TestAction action = new TestAction(); URL[] urls = action.computeClassPath(tempDir, jarFileInClasspath.getAbsolutePath()); assertEquals(new File(classesDir.getAbsolutePath() + File.separator), new File(urls[0].getFile())); assertEquals(new File(jarFileInLibDir.getAbsolutePath()), new File(urls[1].getFile())); assertEquals(new File(jarFileInClasspath.getAbsolutePath()), new File(urls[2].getFile())); }