org.apache.tools.ant.AntClassLoader Java Examples

The following examples show how to use org.apache.tools.ant.AntClassLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MakeNBM.java    From netbeans with Apache License 2.0 6 votes vote down vote up
static void validateAgainstAUDTDs(InputSource input, final Path updaterJar, final Task task) throws IOException, SAXException {
    XMLUtil.parse(input, true, false, XMLUtil.rethrowHandler(), new EntityResolver() {
        ClassLoader loader = new AntClassLoader(task.getProject(), updaterJar);
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            String remote = "http://www.netbeans.org/dtds/";
            if (systemId.startsWith(remote)) {
                String rsrc = "org/netbeans/updater/resources/" + systemId.substring(remote.length());
                URL u = loader.getResource(rsrc);
                if (u != null) {
                    return new InputSource(u.toString());
                } else {
                    task.log(rsrc + " not found in " + updaterJar, Project.MSG_WARN);
                }
            }
            return null;
        }
    });
}
 
Example #2
Source File: RootLoaderRef.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void execute() throws BuildException {
    if (taskClasspath == null || taskClasspath.size() == 0) {
        throw new BuildException("no classpath given");
    }
    Project project = getProject();
    String[] list = taskClasspath.list();
    LoaderConfiguration lc = new LoaderConfiguration();
    for (String s : list) {
        if (s.matches(".*ant-[^/]*jar$")) {
            continue;
        }
        if (s.matches(".*commons-logging-[^/]*jar$")) {
            continue;
        }
        if (s.matches(".*xerces-[^/]*jar$")) {
            continue;
        }
        lc.addFile(s);
    }
    AntClassLoader loader = AccessController.doPrivileged((PrivilegedAction<AntClassLoader>) () -> new AntClassLoader(new RootLoader(lc), true));
    project.addReference(name, loader);
}
 
Example #3
Source File: MXJC2Task.java    From mxjc with MIT License 6 votes vote down vote up
private void doXJC() throws BuildException {
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    try {
        if (classpath != null) {
            for (String pathElement : classpath.list()) {
                try {
                    options.classpaths.add(new File(pathElement).toURI().toURL());
                } catch (MalformedURLException ex) {
                    log("Classpath for XJC task not setup properly: " + pathElement);
                }
            }
        }
        // set the user-specified class loader so that XJC will use it.
        Thread.currentThread().setContextClassLoader(new AntClassLoader(getProject(),classpath));
        _doXJC();
    } finally {
        // restore the context class loader
        Thread.currentThread().setContextClassLoader(old);
    }
}
 
Example #4
Source File: DfsTask.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the path for the parent-last ClassLoader, intended to be used for
 * {@link org.apache.hadoop.conf.Configuration Configuration}.
 * @param confpath The path to search for resources, classes, etc. before
 * parent ClassLoaders.
 */
public void setConf(String confpath) {
  confloader = AccessController.doPrivileged(
      new PrivilegedAction<AntClassLoader>() {
        @Override
        public AntClassLoader run() {
          return new AntClassLoader(getClass().getClassLoader(), false);
        }
      });
  confloader.setProject(getProject());
  if (null != confpath)
    confloader.addPathElement(confpath);
}
 
Example #5
Source File: DfsTask.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the path for the parent-last ClassLoader, intended to be used for
 * {@link org.apache.hadoop.conf.Configuration Configuration}.
 * @param confpath The path to search for resources, classes, etc. before
 * parent ClassLoaders.
 */
public void setConf(String confpath) {
  confloader = AccessController.doPrivileged(
      new PrivilegedAction<AntClassLoader>() {
        @Override
        public AntClassLoader run() {
          return new AntClassLoader(getClass().getClassLoader(), false);
        }
      });
  confloader.setProject(getProject());
  if (null != confpath)
    confloader.addPathElement(confpath);
}
 
Example #6
Source File: DfsTask.java    From RDFS with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the path for the parent-last ClassLoader, intended to be used for
 * {@link org.apache.hadoop.conf.Configuration Configuration}.
 * @param confpath The path to search for resources, classes, etc. before
 * parent ClassLoaders.
 */
public void setConf(String confpath) {
  confloader = new AntClassLoader(getClass().getClassLoader(), false);
  confloader.setProject(getProject());
  if (null != confpath)
    confloader.addPathElement(confpath);
}
 
Example #7
Source File: DfsTask.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the path for the parent-last ClassLoader, intended to be used for
 * {@link org.apache.hadoop.conf.Configuration Configuration}.
 * @param confpath The path to search for resources, classes, etc. before
 * parent ClassLoaders.
 */
public void setConf(String confpath) {
  confloader = new AntClassLoader(getClass().getClassLoader(), false);
  confloader.setProject(getProject());
  if (null != confpath)
    confloader.addPathElement(confpath);
}
 
Example #8
Source File: DatabaseTaskBase.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void execute() throws BuildException
{
    initLogging();

    if (!hasCommands())
    {
        _log.info("No sub tasks specified, so there is nothing to do.");
        return;
    }

    ClassLoader sysClassLoader = (ClassLoader)AccessController.doPrivileged(new PrivilegedAction()
    {
        public Object run()
        {
            try
            {
                ClassLoader    contextClassLoader = Thread.currentThread().getContextClassLoader();
                AntClassLoader newClassLoader     = new AntClassLoader(getClass().getClassLoader(), true);

                // we're changing the thread classloader so that we can access resources
                // from the classpath used to load this task's class
                Thread.currentThread().setContextClassLoader(newClassLoader);
                return contextClassLoader;
            }
            catch (SecurityException ex)
            {
                throw new BuildException("Could not change the context clas loader", ex);
            }
        }
    });

    try
    {
        executeCommands(readModel());
    }
    finally
    {
        if ((getDataSource() != null) && isShutdownDatabase())
        {
            getPlatform().shutdownDatabase();
        }
        // rollback of our classloader change
        Thread.currentThread().setContextClassLoader(sysClassLoader);
    }
}
 
Example #9
Source File: DatabaseTaskBase.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void execute() throws BuildException
{
    initLogging();

    if (!hasCommands())
    {
        _log.info("No sub tasks specified, so there is nothing to do.");
        return;
    }

    ClassLoader sysClassLoader = (ClassLoader)AccessController.doPrivileged(new PrivilegedAction()
    {
        public Object run()
        {
            try
            {
                ClassLoader    contextClassLoader = Thread.currentThread().getContextClassLoader();
                AntClassLoader newClassLoader     = new AntClassLoader(getClass().getClassLoader(), true);

                // we're changing the thread classloader so that we can access resources
                // from the classpath used to load this task's class
                Thread.currentThread().setContextClassLoader(newClassLoader);
                return contextClassLoader;
            }
            catch (SecurityException ex)
            {
                throw new BuildException("Could not change the context clas loader", ex);
            }
        }
    });

    try
    {
        executeCommands(readModel());
    }
    finally
    {
        if ((getDataSource() != null) && isShutdownDatabase())
        {
            getPlatform().shutdownDatabase();
        }
        // rollback of our classloader change
        Thread.currentThread().setContextClassLoader(sysClassLoader);
    }
}
 
Example #10
Source File: Groovyc.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void doForkCommandLineList(List<String> commandLineList, Path classpath, String separator) {
    if (forkedExecutable != null && !forkedExecutable.isEmpty()) {
        commandLineList.add(forkedExecutable);
    } else {
        String javaHome;
        if (forkJavaHome != null) {
            javaHome = forkJavaHome.getPath();
        } else {
            javaHome = System.getProperty("java.home");
        }
        commandLineList.add(javaHome + separator + "bin" + separator + "java");
    }

    String[] bootstrapClasspath;
    ClassLoader loader = getClass().getClassLoader();
    if (loader instanceof AntClassLoader) {
        bootstrapClasspath = ((AntClassLoader) loader).getClasspath().split(File.pathSeparator);
    } else {
        Class<?>[] bootstrapClasses = {
                FileSystemCompilerFacade.class,
                FileSystemCompiler.class,
                ParseTreeVisitor.class,
                ClassVisitor.class,
                CommandLine.class,
        };
        bootstrapClasspath = Arrays.stream(bootstrapClasses).map(Groovyc::getLocation)
                .map(uri -> new File(uri).getAbsolutePath()).distinct().toArray(String[]::new);
    }
    if (bootstrapClasspath.length > 0) {
        commandLineList.add("-classpath");
        commandLineList.add(getClasspathRelative(bootstrapClasspath));
    }

    if (memoryInitialSize != null && !memoryInitialSize.isEmpty()) {
        commandLineList.add("-Xms" + memoryInitialSize);
    }
    if (memoryMaximumSize != null && !memoryMaximumSize.isEmpty()) {
        commandLineList.add("-Xmx" + memoryMaximumSize);
    }
    if (targetBytecode != null) {
        commandLineList.add("-Dgroovy.target.bytecode=" + targetBytecode);
    }
    if (!"*.groovy".equals(getScriptExtension())) {
        String tmpExtension = getScriptExtension();
        if (tmpExtension.startsWith("*."))
            tmpExtension = tmpExtension.substring(1);
        commandLineList.add("-Dgroovy.default.scriptExtension=" + tmpExtension);
    }

    commandLineList.add(FileSystemCompilerFacade.class.getName());
    commandLineList.add("--classpath");
    if (includeAntRuntime) {
        classpath.addExisting(new Path(getProject()).concatSystemClasspath("last"));
    }
    if (includeJavaRuntime) {
        classpath.addJavaRuntime();
    }
    commandLineList.add(getClasspathRelative(classpath.list()));
    if (forceLookupUnnamedFiles) {
        commandLineList.add("--forceLookupUnnamedFiles");
    }
}
 
Example #11
Source File: Groovyc.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected GroovyClassLoader buildClassLoaderFor() {
    if (fork) {
        throw new GroovyBugError("Cannot use Groovyc#buildClassLoaderFor() for forked compilation");
    }
    // GROOVY-5044
    if (!getIncludeantruntime()) {
        throw new IllegalArgumentException("The includeAntRuntime=false option is not compatible with fork=false");
    }

    ClassLoader loader = getClass().getClassLoader();
    if (loader instanceof AntClassLoader) {
        AntClassLoader antLoader = (AntClassLoader) loader;
        String[] pathElm = antLoader.getClasspath().split(File.pathSeparator);
        List<String> classpath = configuration.getClasspath();
        /*
         * Iterate over the classpath provided to groovyc, and add any missing path
         * entries to the AntClassLoader.  This is a workaround, since for some reason
         * 'directory' classpath entries were not added to the AntClassLoader' classpath.
         */
        for (String cpEntry : classpath) {
            boolean found = false;
            for (String path : pathElm) {
                if (cpEntry.equals(path)) {
                    found = true;
                    break;
                }
            }
            /*
             * fix for GROOVY-2284
             * seems like AntClassLoader doesn't check if the file
             * may not exist in the classpath yet
             */
            if (!found && new File(cpEntry).exists()) {
                try {
                    antLoader.addPathElement(cpEntry);
                } catch (BuildException e) {
                    log.warn("The classpath entry " + cpEntry + " is not a valid Java resource");
                }
            }
        }
    }

    GroovyClassLoader groovyLoader = AccessController.doPrivileged(
            (PrivilegedAction<GroovyClassLoader>) () -> new GroovyClassLoader(loader, configuration));
    if (!forceLookupUnnamedFiles) {
        // in normal case we don't need to do script lookups
        groovyLoader.setResourceLoader(filename -> null);
    }
    return groovyLoader;
}