private void appendClasspath(Path path, Class c, String expectedJar) { File location = LoaderUtils.getClassSource( c ); if (location != null) { path.createPath().setLocation( location ); log( String.format( "Implicitly adding '%s' to CLASSPATH", location ), Project.MSG_VERBOSE ); } else { log( String.format( "Couldn't found classpath for '%s'", c ), Project.MSG_WARN ); log( String.format( "Make sure you have '%s' in the <classpath/> of taskdef of GreenPepper", expectedJar ) ); } }
/** * Get the constructor with not argument of an helper from its class name. * It'll first try the thread class loader, then Class.forName() will load * from the same loader that loaded this class. * * @param helperClass * The name of the class to create an instance of. Must not be * <code>null</code>. * * @return the constructor of the specified class. * * @exception BuildException * if the class cannot be found or if a constructor with no * argument cannot be found. */ private Constructor<? extends ProjectHelper> getHelperConstructor(String helperClass) throws BuildException { ClassLoader classLoader = LoaderUtils.getContextClassLoader(); try { Class<?> clazz = null; if (classLoader != null) { try { clazz = classLoader.loadClass(helperClass); } catch (ClassNotFoundException ex) { // try next method } } if (clazz == null) { clazz = Class.forName(helperClass); } return clazz.asSubclass(ProjectHelper.class).getConstructor(); } catch (Exception e) { throw new BuildException(e); } }
/** * Sets the current thread's context loader to this classloader, storing * the current loader value for later resetting. */ public void setThreadContextLoader() { if (isContextLoaderSaved) { throw new BuildException("Context loader has not been reset"); } if (LoaderUtils.isContextLoaderAvailable()) { savedContextLoader = LoaderUtils.getContextClassLoader(); ClassLoader loader = this; if (project != null && "only".equals(project.getProperty("build.sysclasspath"))) { loader = this.getClass().getClassLoader(); } LoaderUtils.setContextClassLoader(loader); isContextLoaderSaved = true; } }
/** * Resets the current thread's context loader to its original value. */ public void resetThreadContextLoader() { if (LoaderUtils.isContextLoaderAvailable() && isContextLoaderSaved) { LoaderUtils.setContextClassLoader(savedContextLoader); savedContextLoader = null; isContextLoaderSaved = false; } }
/** * Check the path for multiple different versions of * ant. * @param cmd command to execute */ private void checkForkedPath(final CommandlineJava cmd) { if (forkedPathChecked) { return; } forkedPathChecked = true; if (!cmd.haveClasspath()) { return; } try (AntClassLoader loader = AntClassLoader.newAntClassLoader(null, getProject(), cmd.createClasspath(getProject()), true)) { final String projectResourceName = LoaderUtils.classNameToResource(Project.class.getName()); URL previous = null; try { for (final Enumeration<URL> e = loader.getResources(projectResourceName); e.hasMoreElements();) { final URL current = e.nextElement(); if (previous != null && !urlEquals(current, previous)) { log("WARNING: multiple versions of ant detected " + "in path for junit " + LINE_SEP + " " + previous + LINE_SEP + " and " + current, Project.MSG_WARN); return; } previous = current; } } catch (final Exception ex) { // Ignore exception } } }
/** * Implementation of addClasspathEntry. * * @param resource resource that one wants to lookup * @return true if something was in fact added * @since Ant 1.7.1 */ private boolean addClasspathResource(String resource) { /* * pre Ant 1.6 this method used to call getClass().getResource * while Ant 1.6 will call ClassLoader.getResource(). * * The difference is that Class.getResource expects a leading * slash for "absolute" resources and will strip it before * delegating to ClassLoader.getResource - so we now have to * emulate Class's behavior. */ if (resource.startsWith("/")) { resource = resource.substring(1); } else { resource = "org/apache/tools/ant/taskdefs/optional/junit/" + resource; } final File f = LoaderUtils.getResourceSource(JUnitTask.class.getClassLoader(), resource); if (f != null) { log("Found " + f.getAbsolutePath(), Project.MSG_DEBUG); antRuntimeClasses.createPath().setLocation(f); return true; } else { log("Couldn\'t find " + resource, Project.MSG_DEBUG); return false; } }
/** * Search for the given resource and add the directory or archive * that contains it to the classpath. * * <p>Doesn't work for archives in JDK 1.1 as the URL returned by * getResource doesn't contain the name of the archive.</p> * @param resource the resource name to search for */ protected void addClasspathEntry(String resource) { /* * pre Ant 1.6 this method used to call getClass().getResource * while Ant 1.6 will call ClassLoader.getResource(). * * The difference is that Class.getResource expects a leading * slash for "absolute" resources and will strip it before * delegating to ClassLoader.getResource - so we now have to * emulate Class's behavior. */ if (resource.startsWith("/")) { resource = resource.substring(1); } else { resource = "org/apache/tools/ant/taskdefs/optional/" + resource; } File f = LoaderUtils.getResourceSource(getClass().getClassLoader(), resource); if (f != null) { log("Found " + f.getAbsolutePath(), Project.MSG_DEBUG); createClasspath().setLocation(f); } else { log("Couldn\'t find " + resource, Project.MSG_VERBOSE); } }
/** * Search for the given resource and add the directory or archive * that contains it to the classpath. * * <p>Doesn't work for archives in JDK 1.1 as the URL returned by * getResource doesn't contain the name of the archive.</p> * * @param resource resource that one wants to lookup * @since Ant 1.6 */ private void addClasspathEntry(String resource) { /* * pre Ant 1.6 this method used to call getClass().getResource * while Ant 1.6 will call ClassLoader.getResource(). * * The difference is that Class.getResource expects a leading * slash for "absolute" resources and will strip it before * delegating to ClassLoader.getResource - so we now have to * emulate Class's behavior. */ if (resource.startsWith("/")) { resource = resource.substring(1); } else { resource = "org/apache/tools/ant/taskdefs/optional/jdepend/" + resource; } File f = LoaderUtils.getResourceSource(getClass().getClassLoader(), resource); if (f == null) { log("Couldn\'t find " + resource, Project.MSG_DEBUG); } else { log("Found " + f.getAbsolutePath(), Project.MSG_DEBUG); runtimeClasses.createPath().setLocation(f); } }
private void collectProjectHelpers() { // First, try the system property Constructor<? extends ProjectHelper> projectHelper = getProjectHelperBySystemProperty(); registerProjectHelper(projectHelper); // A JDK1.3 'service' (like in JAXP). That will plug a helper // automatically if in CLASSPATH, with the right META-INF/services. try { ClassLoader classLoader = LoaderUtils.getContextClassLoader(); if (classLoader != null) { Enumeration<URL> resources = classLoader.getResources(ProjectHelper.SERVICE_ID); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); URLConnection conn = resource.openConnection(); conn.setUseCaches(false); projectHelper = getProjectHelperByService(conn.getInputStream()); registerProjectHelper(projectHelper); } } InputStream systemResource = ClassLoader.getSystemResourceAsStream(ProjectHelper.SERVICE_ID); if (systemResource != null) { projectHelper = getProjectHelperByService(systemResource); registerProjectHelper(projectHelper); } } catch (Exception e) { System.err.println("Unable to load ProjectHelper from service " + ProjectHelper.SERVICE_ID + " (" + e.getClass().getName() + ": " + e.getMessage() + ")"); if (DEBUG) { e.printStackTrace(System.err); //NOSONAR } } }
private void addResourceSource(Path classpath, String resource) { final File f = LoaderUtils.getResourceSource( ErrorProneExternalCompilerAdapter.class.getClassLoader(), resource); if (f != null) { attributes.log("Found " + f.getAbsolutePath(), Project.MSG_DEBUG); classpath.createPath().setLocation(f); } else { attributes.log("Couldn't find " + resource, Project.MSG_DEBUG); } }
private static boolean mustSplit() { return LoaderUtils.getResourceSource(FTPTask.class.getClassLoader(), "/org/apache/commons/net/" + "ftp/FTP.class") == null; }
@Test public void testJunitOnCpArguments() throws Exception { final File tmp = new File(System.getProperty("java.io.tmpdir")); //NOI18N final File workDir = new File(tmp, String.format("%s_testJCP%d", //NOI18N getClass().getName(), System.currentTimeMillis() / 1000)); workDir.mkdirs(); try { final File modulesDir = new File(workDir, "modules"); //NOI18N modulesDir.mkdirs(); final Project project = new Project(); project.init(); project.setBaseDir(workDir); final MockCommandLauncher mockProcLauncher = new MockCommandLauncher(); project.addReference( MagicNames.ANT_VM_LAUNCHER_REF_ID, mockProcLauncher); JUnitTask task = new JUnitTask(); task.setDir(workDir); task.setFork(true); task.setProject(project); final File junit = LoaderUtils.getResourceSource( JUnitTask.class.getClassLoader(), "junit/framework/Test.class"); //NOI18N final Path cp = new Path(project); cp.setPath(junit.getAbsolutePath()); task.createClasspath().add(cp); final Path mp = new Path(project); mp.setPath(modulesDir.getName()); task.createModulepath().add(mp); task.addTest(new JUnitTest("org.apache.tools.ant.taskdefs.optional.junit.TestTest")); task.execute(); assertNotNull(mockProcLauncher.cmd); String resCp = null; String resMp = null; Set<String> resExports = new TreeSet<>(); for (int i = 1; i < mockProcLauncher.cmd.length; i++) { if ("-classpath".equals(mockProcLauncher.cmd[i])) { //NOI18N resCp = mockProcLauncher.cmd[++i]; } else if ("--module-path".equals(mockProcLauncher.cmd[i])) { //NOI18N resMp = mockProcLauncher.cmd[++i]; } else if (mockProcLauncher.cmd[i].equals("--add-exports")) { //NOI18N resExports.add(mockProcLauncher.cmd[++i]); } else if (JUnitTestRunner.class.getName().equals(mockProcLauncher.cmd[i])) { break; } } assertTrue("No exports", resExports.isEmpty()); if (project.getProperty(MagicNames.BUILD_SYSCLASSPATH) == null && System.getProperty(MagicNames.BUILD_SYSCLASSPATH) == null) { assertEquals("Expected classpath", cp.toString(), resCp); } assertEquals("Expected modulepath", mp.toString(), resMp); } finally { delete(workDir); } }
@Test public void testJunitOnMpArguments() throws Exception { final File tmp = new File(System.getProperty("java.io.tmpdir")); //NOI18N final File workDir = new File(tmp, String.format("%s_testJMP%d", //NOI18N getClass().getName(), System.currentTimeMillis() / 1000)); workDir.mkdirs(); try { final File modulesDir = new File(workDir, "modules"); //NOI18N modulesDir.mkdirs(); final Project project = new Project(); project.init(); project.setBaseDir(workDir); final MockCommandLauncher mockProcLauncher = new MockCommandLauncher(); project.addReference( MagicNames.ANT_VM_LAUNCHER_REF_ID, mockProcLauncher); JUnitTask task = new JUnitTask(); task.setDir(workDir); task.setFork(true); task.setProject(project); final File junit = LoaderUtils.getResourceSource( JUnitTask.class.getClassLoader(), "junit/framework/Test.class"); //NOI18N final Path mp = new Path(project); mp.add(new Path(project, junit.getAbsolutePath())); mp.add(new Path(project, modulesDir.getName())); task.createModulepath().add(mp); task.addTest(new JUnitTest("org.apache.tools.ant.taskdefs.optional.junit.TestTest")); //NOI18N task.execute(); assertNotNull(mockProcLauncher.cmd); String resCp = null; String resMp = null; Set<String> resExports = new TreeSet<>(); for (int i = 1; i < mockProcLauncher.cmd.length; i++) { if ("-classpath".equals(mockProcLauncher.cmd[i])) { //NOI18N resCp = mockProcLauncher.cmd[++i]; } else if ("--module-path".equals(mockProcLauncher.cmd[i])) { //NOI18N resMp = mockProcLauncher.cmd[++i]; } else if (mockProcLauncher.cmd[i].equals("--add-exports")) { //NOI18N resExports.add(mockProcLauncher.cmd[++i]); } else if (JUnitTestRunner.class.getName().equals(mockProcLauncher.cmd[i])) { break; } } assertTrue("No exports", resExports.isEmpty()); if (project.getProperty(MagicNames.BUILD_SYSCLASSPATH) == null && System.getProperty(MagicNames.BUILD_SYSCLASSPATH) == null) { assertNull("No classpath", resCp); } assertEquals("Expected modulepath", mp.toString(), resMp); } finally { delete(workDir); } }
/** * JDK1.1 compatible access to the context class loader. Cut & paste from JAXP. * * @deprecated since 1.6.x. * Use LoaderUtils.getContextClassLoader() * * @return the current context class loader, or <code>null</code> * if the context class loader is unavailable. */ public static ClassLoader getContextClassLoader() { return LoaderUtils.isContextLoaderAvailable() ? LoaderUtils.getContextClassLoader() : null; }