Search in sources :

Example 1 with AntClassLoader

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;
}
Also used : MalformedURLException(java.net.MalformedURLException) AntClassLoader(org.apache.tools.ant.AntClassLoader) URL(java.net.URL) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyResourceLoader(groovy.lang.GroovyResourceLoader) RootLoader(org.codehaus.groovy.tools.RootLoader) AntClassLoader(org.apache.tools.ant.AntClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 2 with AntClassLoader

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;
}
Also used : ArrayList(java.util.ArrayList) AntClassLoader(org.apache.tools.ant.AntClassLoader) IOException(java.io.IOException) URL(java.net.URL) StringTokenizer(java.util.StringTokenizer) URLClassLoader(java.net.URLClassLoader) AntClassLoader(org.apache.tools.ant.AntClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Example 3 with AntClassLoader

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;
}
Also used : ModuleFactory(com.puppycrawl.tools.checkstyle.ModuleFactory) Checker(com.puppycrawl.tools.checkstyle.Checker) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) RootModule(com.puppycrawl.tools.checkstyle.api.RootModule) PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) AntClassLoader(org.apache.tools.ant.AntClassLoader) AntClassLoader(org.apache.tools.ant.AntClassLoader) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) BuildException(org.apache.tools.ant.BuildException) Properties(java.util.Properties) PackageObjectFactory(com.puppycrawl.tools.checkstyle.PackageObjectFactory)

Example 4 with AntClassLoader

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());
    }
}
Also used : Attribute(org.eclipse.jetty.ant.types.Attribute) AntClassLoader(org.apache.tools.ant.AntClassLoader) URLClassLoader(java.net.URLClassLoader) WebAppClassLoader(org.eclipse.jetty.webapp.WebAppClassLoader) AntClassLoader(org.apache.tools.ant.AntClassLoader) WebXmlConfiguration(org.eclipse.jetty.webapp.WebXmlConfiguration) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) JettyWebXmlConfiguration(org.eclipse.jetty.webapp.JettyWebXmlConfiguration) WebAppClassLoader(org.eclipse.jetty.webapp.WebAppClassLoader) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException)

Example 5 with AntClassLoader

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));
}
Also used : Pattern(java.util.regex.Pattern) Resource(org.eclipse.jetty.util.resource.Resource) AntClassLoader(org.apache.tools.ant.AntClassLoader) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) WebAppClassLoader(org.eclipse.jetty.webapp.WebAppClassLoader) AntClassLoader(org.apache.tools.ant.AntClassLoader) URLClassLoader(java.net.URLClassLoader) PatternMatcher(org.eclipse.jetty.util.PatternMatcher) File(java.io.File)

Aggregations

AntClassLoader (org.apache.tools.ant.AntClassLoader)12 BuildException (org.apache.tools.ant.BuildException)8 URL (java.net.URL)6 URLClassLoader (java.net.URLClassLoader)5 File (java.io.File)4 IOException (java.io.IOException)4 MalformedURLException (java.net.MalformedURLException)3 ArrayList (java.util.ArrayList)3 Project (org.apache.tools.ant.Project)3 Function (com.google.common.base.Function)2 GroovyClassLoader (groovy.lang.GroovyClassLoader)2 GroovyResourceLoader (groovy.lang.GroovyResourceLoader)2 WebAppClassLoader (org.eclipse.jetty.webapp.WebAppClassLoader)2 SuiteHint (com.carrotsearch.ant.tasks.junit4.balancers.SuiteHint)1 AggregatedQuitEvent (com.carrotsearch.ant.tasks.junit4.events.aggregated.AggregatedQuitEvent)1 AggregatedStartEvent (com.carrotsearch.ant.tasks.junit4.events.aggregated.AggregatedStartEvent)1 AggregatedEventListener (com.carrotsearch.ant.tasks.junit4.listeners.AggregatedEventListener)1 FilterExpressionParser (com.carrotsearch.randomizedtesting.FilterExpressionParser)1 Node (com.carrotsearch.randomizedtesting.FilterExpressionParser.Node)1 EventBus (com.google.common.eventbus.EventBus)1