ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dominique Devienne <DDevie...@lgc.com>
Subject RE: Preventing Parent Classloading
Date Fri, 10 Oct 2003 14:28:15 GMT
Here's something that might work. The code you sketched below, which only
depends on Cocoon classes, could be make part of Cocoon itself in an
existing or new JAR from Cocoon/WEB-INF/lib, as a new main() entry point.

This way, you could then just use the Java task within your own task (I do
this all the time in my own custom task), calling that entry point, and
setting the classpath explicitly. Play as well with the attributes to
include or not the system and Ant runtime.

Here's some more example code to help. Good luck, --DD

public class JaxbSchemaCompiler extends Task {

    /** The &lt;java&gt; task to run the Jaxb schema compiler. */
    private Java _java;

    // Override Task#init
    public void init() throws BuildException {
        _java = new Java();
        TaskUtils.configureTask(this, _java);
    }

    // Override Task#execute
    public void execute() throws BuildException {
        ...
    }
    ....
}

public class TaskUtils {

    private TaskUtils() {}

     /**
     * Configures a helper task to be used within another task.
     *
     * @param  parent the parent (custom) task using the helper task.
     * @param  helper the helper task to configure for use.
     * @return the configured helper task for call chaining.
     */
    public static Task configureTask(Task parent, Task helper) {
        // Make helper share attributes of the parent
        helper.setProject(parent.getProject());
        helper.setTaskName(parent.getTaskName());
        helper.setOwningTarget(parent.getOwningTarget());

        // Initialize (and return) the helper
        helper.init();
        return helper;
    }
    ...
}

> -----Original Message-----
> From: Upayavira [mailto:uv@upaya.co.uk]
> Sent: Thursday, October 09, 2003 6:00 PM
> To: Ant Developers List
> Subject: Re: Preventing Parent Classloading
> 
> Dominique Devienne wrote:
> 
> >Sounds like the CL you create delegates to the Ant CL, thus your problem.
> >Create your own CL by making it delegate to the Root/Bootstrap CL (by
> >setting the parent loader to null), and the classes will always be loaded
> >from your classes (may duplicate-load the some classes Ant loaded in its
> CL,
> >but seems alright for you).
> >
> >
> Firstly, many thanks for your help with the DynamicConfigurator stuff.
> It worked a treat.
> 
> As to class loading, It is gradually getting worse. I've just realised
> that my IDE had included all of the Cocoon classes into the classpath
> for Ant, so all of Cocoon's classes were there all along! :-(
> 
> As soon as I remove these jars from Ant's classpath, it stops working at
> all. So I'm now pretty lost.
> 
> All I want to do is this:
> 
> In my Task, create a classloader that will ONLY load from
> Cocoon/WEB-INF/lib, and then run the following:
>         CocoonBean cocoon = new CocoonBean();
>         OutputStreamListener listener = new
> OutputStreamListener(System.out);
>         cocoon.addListener(listener);
>         BeanConfigurator.configure(xconf, cocoon, "", uriGroup, listener);
> 
>         System.out.println(getProlog(Constants.NAME, Constants.VERSION,
> Constants.YEAR));
> 
>         cocoon.initialize();
>         cocoon.process();
>         cocoon.dispose();
> 
>         listener.complete();
>         return (listener.isSuccessful() ? 0 : 1);
> 
> All of the classes mentioned are from Cocoon, none from Ant. Then I'd
> switch my classloader back to the Ant one, and I'd be sorted.
> 
> Presumably this is a pretty straightforward requirement? Are you able to
> suggest places I can look for examples? I've tried looking at the Java
> class, but I quickly get out of my depth.
> 
> Do I need to load all of my classes using introspection, or can I refer
> to them by name as I have above? Do all includes for a class need to be
> available to the classloader that loads that class?
> 
> >I'm no class loading expert, but at first glance this should work. --DD
> >
> >
> I've not even reached beginner level when it comes to class loading I'm
> afraid!
> 
> Thanks again,
> 
> Upayavira
> 
> >>-----Original Message-----
> >>From: Upayavira [mailto:uv@upaya.co.uk]
> >>Sent: Thursday, October 09, 2003 2:39 PM
> >>To: dev@ant.apache.org
> >>Subject: Preventing Parent Classloading
> >>
> >>I'm working on a CocoonTask. Cocoon has a significant bundle of jars,
> >>some of which overlap with Ant's.
> >>
> >>In my task, I set up a class loader responsible for loading Cocoon
> >>classes, which works, apart from when those same classes exist in Ant,
> >>when the Ant ones are loaded instead of Cocoon's.Ant has a different
> >>version of org.apache.log.Hierarchy which is incompatible with the one
> >>in Cocoon, and thus resulting in an IllegalAccessError.
> >>
> >>To quote from the javadocs for ClassLoader:
> >>
> >>"When requested to find a class or resource, a ClassLoader instance will
> >>delegate the search for the class or resource to its parent class loader
> >>before attempting to find the class or resource itself. "
> >>
> >>How can I reverse this, so that the Cocoon ClassLoader is used in
> >>preference? Or prevent the delegation to the parent class loader?
> >>
> >>Thanks in advance.
> >>
> >>Regards, Upayavira

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message