You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Ryan Tandy <rt...@autonomy.com> on 2007/09/13 01:04:04 UTC

Passing a reference to a custom object between tasks

Hi,

I'm working on an Ant-based framework that makes heavy use of FTP
transmissions.  I have a class that extends
org.apache.commons.net.ftp.FTPClient, which I use to manage FTP
connections, and I would like to share instances of this class among
several of my tasks to reduce the total number of times I have to
connect to the FTP server.

My tasks that perform actual FTP work are called from the task that
establishes the connections by creating an instance of <ant> within my
Java code.  I've been using code like this in the manager task to
include a reference to the FTPClient in the <ant> (snipped for brevity):

getProject().addReference( "ftpClient", ftpClient );
Ant ant = (Ant) getProject.createTask( "ant" );
Ant.Reference ftpClientRef = new Ant.Reference();
ftpClientRef.setRefId( "ftpClient" );
ant.addReference( ftpClientRef );

and then pulling it out in the task called by the <ant>:

FTPClient ftpClient = (FTPClient)
getProject().getReference( "ftpClient" );

This code works correctly when used on a class loaded as part of Ant,
such as org.apache.commons.net.ftp.FTPClient; when I try to use it with
my custom class, distributed in the same jar as my tasks, it fails with
a ClassCastException.  We suspect that the problem may be related to how
Ant manages class loading with regards to tasks and projects.  Adding my
class to Ant's library classpath is not an option due to the framework's
range of users.

Is there some way to force Ant to load my class into its global class
loader?  Are our suspicions completely in the wrong direction?  Is this
a horrible abuse of references, for which I should be punished
endlessly?

Your input is greatly appreciated.

Thanks,
Ryan

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