cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Upayavira ...@upaya.co.uk>
Subject Re: Ant Class Loading & CocoonTask
Date Thu, 09 Oct 2003 20:44:21 GMT
Stefano Mazzocchi wrote:

>
> On Thursday, Oct 9, 2003, at 21:45 Europe/Rome, Upayavira wrote:
>
>> Stefano Mazzocchi wrote:
>>
>>>
>>> On Thursday, Oct 9, 2003, at 14:19 Europe/Rome, Upayavira wrote:
>>>
>>>> I have written a Cocoon Ant task, which is really neat, and, as 
>>>> Marc was suggesting, it shares its config code with the CLI.
>>>>
>>>> However, I'm having some classloading problems with it. I set up an 
>>>> AntClassLoader which points only to WEB-INF/lib, and it 
>>>> successfully loads in CocoonBean and associated classes. When it 
>>>> gets into the initialisation phase, however, it gets a Logger from 
>>>> Ant's copy of Velocity, rather than from a Cocoon jar.
>>>>
>>>> How can I force the class loader to ignore Ant's classpath and just 
>>>> use Cocoon's for everything?
>>>
>>>
>>>
>>> Did you try forcing the classloader in the task' thread context? you 
>>> can basically ask the current classloader, wrap it with yours and 
>>> set that one in (this is what the ParanoidCocoonServlet does, BTW, 
>>> Sylvain was also able to get classes directly from Eclipse without 
>>> the need redeploy)
>>>
>>> that might trigger security exceptions in protected environments, 
>>> but ant is never used under a security manager (AFAIK).
>>
>>
>> Below is the relevant code snippet (note the consciously hard coded 
>> path!). I just create an AntClassLoader and off I go. But it tries to 
>> load org.apache.log.Hierarchy from Ant's copy of Velocity, rather 
>> than our own library. The         loader.setThreadContextLoader() 
>> does force the classloader in the task's thread context, I think. But 
>> it is still getting me a class from Ant. The AntClassLoader does wrap 
>> the thread class loader, as I think you're saying.
>>
>> I tried extending the AntClassLoader and copying the 
>> paranoidclassloader's loadClass method across into my subclass, but 
>> my loadClass method was never called.
>>
>> Any ideas? This is frustrating because I think I'm sooooo close...
>>
>>        AntClassLoader loader = null;
>>        Project project = getProject();
>>              Path path = new Path(project);
>>              FileSet classes = new FileSet();
>>        classes.setDir(new File("d:/documents/cocoon/cocoon 
>> dev/build/eclipse/classes"));
>>        path.addFileset(classes);
>>              FileSet fileSet = new FileSet();
>>              fileSet.setDir(getLibDir(xconf));
>>        fileSet.setIncludes("*.jar");
>>              path.addFileset(fileSet);
>>
>>        loader = new AntClassLoader(project.getCoreLoader(), project,
>>                                                    path, false);
>> //loader.setParent(null); uncommenting this makes no difference
>>             loader.setIsolated(true);
>>        loader.setThreadContextLoader();
>>          try {
>>            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();
>>            int exitCode = (listener.isSuccessful() ? 0 : 1);
>>        } catch (Exception e){
>>            System.out.println("Exception: "+e.getMessage());
>>            e.printStackTrace();
>>        }
>>        loader.resetThreadContextLoader();
>>
>> Regards, Upayavira
>>
>
> Don't know how the AntClassLoader works, but I don't know if you are 
> setting the classloader at all.
>
> You should be doing something like this
>
>   Thread thread = Thread.currentThread();
>   ClassLoader oldCL= thread.getContextClassLoader();
>   ClassLoader newCL = new YourClassloader(oldCL);
>   thread.setContextClassLoader(newCL);
>
> HTH

That's pretty much what the Ant stuff does. And it's got a 'parentFirst' 
option which defines the order in which parent/child should be queried, 
but it doesn't seem to work. I'll keep on on the Ant list.

Oh, and I'm soooo close :-(

Regards, Upayavira



Mime
View raw message