use of org.apache.tools.ant.AntClassLoader in project groovy by apache.
the class Groovyc method buildClassLoaderFor.
protected GroovyClassLoader buildClassLoaderFor() {
// GROOVY-5044
if (!fork && !getIncludeantruntime()) {
throw new IllegalArgumentException("The includeAntRuntime=false option is not compatible with fork=false");
}
ClassLoader parent = getIncludeantruntime() ? getClass().getClassLoader() : new AntClassLoader(new RootLoader(EMPTY_URL_ARRAY, null), getProject(), getClasspath());
if (parent instanceof AntClassLoader) {
AntClassLoader antLoader = (AntClassLoader) parent;
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 loader = new GroovyClassLoader(parent, configuration);
if (!forceLookupUnnamedFiles) {
// in normal case we don't need to do script lookups
loader.setResourceLoader(new GroovyResourceLoader() {
public URL loadGroovySource(String filename) throws MalformedURLException {
return null;
}
});
}
return loader;
}
use of org.apache.tools.ant.AntClassLoader in project tomcat by apache.
the class JspC method initClassLoader.
/**
* Initializes the classloader as/if needed for the given
* compilation context.
* @return the classloader that will be used
* @throws IOException If an error occurs
*/
protected ClassLoader initClassLoader() throws IOException {
classPath = getClassPath();
ClassLoader jspcLoader = getClass().getClassLoader();
if (jspcLoader instanceof AntClassLoader) {
classPath += File.pathSeparator + ((AntClassLoader) jspcLoader).getClasspath();
}
// Turn the classPath into URLs
ArrayList<URL> urls = new ArrayList<>();
StringTokenizer tokenizer = new StringTokenizer(classPath, File.pathSeparator);
while (tokenizer.hasMoreTokens()) {
String path = tokenizer.nextToken();
try {
File libFile = new File(path);
urls.add(libFile.toURI().toURL());
} catch (IOException ioe) {
// therefore we have permission to freak uot
throw new RuntimeException(ioe.toString());
}
}
File webappBase = new File(uriRoot);
if (webappBase.exists()) {
File classes = new File(webappBase, "/WEB-INF/classes");
try {
if (classes.exists()) {
classPath = classPath + File.pathSeparator + classes.getCanonicalPath();
urls.add(classes.getCanonicalFile().toURI().toURL());
}
} catch (IOException ioe) {
// therefore we have permission to freak out
throw new RuntimeException(ioe.toString());
}
File lib = new File(webappBase, "/WEB-INF/lib");
if (lib.exists() && lib.isDirectory()) {
String[] libs = lib.list();
if (libs != null) {
for (int i = 0; i < libs.length; i++) {
if (libs[i].length() < 5)
continue;
String ext = libs[i].substring(libs[i].length() - 4);
if (!".jar".equalsIgnoreCase(ext)) {
if (".tld".equalsIgnoreCase(ext)) {
log.warn("TLD files should not be placed in " + "/WEB-INF/lib");
}
continue;
}
try {
File libFile = new File(lib, libs[i]);
classPath = classPath + File.pathSeparator + libFile.getAbsolutePath();
urls.add(libFile.getAbsoluteFile().toURI().toURL());
} catch (IOException ioe) {
// therefore we have permission to freak out
throw new RuntimeException(ioe.toString());
}
}
}
}
}
URL[] urlsA = new URL[urls.size()];
urls.toArray(urlsA);
loader = new URLClassLoader(urlsA, this.getClass().getClassLoader());
return loader;
}
use of org.apache.tools.ant.AntClassLoader in project checkstyle by checkstyle.
the class CheckstyleAntTask method createRootModule.
/**
* Creates new instance of the root module.
* @return new instance of the root module
*/
private RootModule createRootModule() {
final RootModule rootModule;
try {
final Properties props = createOverridingProperties();
final Configuration config = ConfigurationLoader.loadConfiguration(configLocation, new PropertiesExpander(props), omitIgnoredModules);
final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
final ModuleFactory factory = new PackageObjectFactory(Checker.class.getPackage().getName() + ".", moduleClassLoader);
rootModule = (RootModule) factory.createModule(config.getName());
rootModule.setModuleClassLoader(moduleClassLoader);
if (rootModule instanceof Checker) {
final ClassLoader loader = new AntClassLoader(getProject(), classpath);
((Checker) rootModule).setClassLoader(loader);
}
rootModule.configure(config);
} catch (final CheckstyleException ex) {
throw new BuildException(String.format(Locale.ROOT, "Unable to create Root Module: " + "configLocation {%s}, classpath {%s}.", configLocation, classpath), ex);
}
return rootModule;
}
use of org.apache.tools.ant.AntClassLoader in project jetty.project by eclipse.
the class AntWebAppContext method doStart.
/**
*
*/
public void doStart() {
try {
TaskLog.logWithTimestamp("Starting web application " + this.getDescriptor());
if (jettyEnvXml != null && jettyEnvXml.exists())
envConfiguration.setJettyEnvXml(Resource.toURL(jettyEnvXml));
ClassLoader parentLoader = this.getClass().getClassLoader();
if (parentLoader instanceof AntClassLoader)
parentLoader = new AntURLClassLoader((AntClassLoader) parentLoader);
setClassLoader(new WebAppClassLoader(parentLoader, this));
if (attributes != null && attributes.getAttributes() != null) {
for (Attribute a : attributes.getAttributes()) setAttribute(a.getName(), a.getValue());
}
//apply a context xml file if one was supplied
if (contextXml != null) {
XmlConfiguration xmlConfiguration = new XmlConfiguration(Resource.toURL(contextXml));
TaskLog.log("Applying context xml file " + contextXml);
xmlConfiguration.configure(this);
}
super.doStart();
} catch (Exception e) {
TaskLog.log(e.toString());
}
}
use of org.apache.tools.ant.AntClassLoader in project jetty.project by eclipse.
the class AntWebInfConfiguration method preConfigure.
@Override
public void preConfigure(final WebAppContext context) throws Exception {
//Make a temp directory for the webapp if one is not already set
resolveTempDirectory(context);
//Extract webapp if necessary
unpack(context);
//Apply an initial ordering to the jars which governs which will be scanned for META-INF
//info and annotations. The ordering is based on inclusion patterns.
String tmp = (String) context.getAttribute(WEBINF_JAR_PATTERN);
Pattern webInfPattern = (tmp == null ? null : Pattern.compile(tmp));
tmp = (String) context.getAttribute(CONTAINER_JAR_PATTERN);
Pattern containerPattern = (tmp == null ? null : Pattern.compile(tmp));
//Apply ordering to container jars - if no pattern is specified, we won't
//match any of the container jars
PatternMatcher containerJarNameMatcher = new PatternMatcher() {
public void matched(URI uri) throws Exception {
context.getMetaData().addContainerResource(Resource.newResource(uri));
}
};
ClassLoader loader = context.getClassLoader();
if (loader != null) {
loader = loader.getParent();
if (loader != null) {
URI[] containerUris = null;
if (loader instanceof URLClassLoader) {
URL[] urls = ((URLClassLoader) loader).getURLs();
if (urls != null) {
containerUris = new URI[urls.length];
int i = 0;
for (URL u : urls) {
try {
containerUris[i] = u.toURI();
} catch (URISyntaxException e) {
containerUris[i] = new URI(u.toString().replaceAll(" ", "%20"));
}
i++;
}
}
} else if (loader instanceof AntClassLoader) {
AntClassLoader antLoader = (AntClassLoader) loader;
String[] paths = antLoader.getClasspath().split(new String(new char[] { File.pathSeparatorChar }));
if (paths != null) {
containerUris = new URI[paths.length];
int i = 0;
for (String p : paths) {
File f = new File(p);
containerUris[i] = f.toURI();
i++;
}
}
}
containerJarNameMatcher.match(containerPattern, containerUris, false);
}
}
//Apply ordering to WEB-INF/lib jars
PatternMatcher webInfJarNameMatcher = new PatternMatcher() {
@Override
public void matched(URI uri) throws Exception {
context.getMetaData().addWebInfJar(Resource.newResource(uri));
}
};
List<Resource> jars = findJars(context);
//Convert to uris for matching
URI[] uris = null;
if (jars != null) {
uris = new URI[jars.size()];
int i = 0;
for (Resource r : jars) {
uris[i++] = r.getURI();
}
}
//null is inclusive, no pattern == all jars match
webInfJarNameMatcher.match(webInfPattern, uris, true);
//No pattern to appy to classes, just add to metadata
context.getMetaData().setWebInfClassesDirs(findClassDirs(context));
}
Aggregations