@@ -15,25 +15,26 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Iterator;
import java.util.List;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.ant.launcher.AntClassLoader;
import org.apache.ant.launcher.AntLoader;
import org.apache.ant.launcher.AntLoader;
import org.apache.ant.project.DefaultProjectEngine;
import org.apache.ant.project.LogTargetToListenerAdapter ;
import org.apache.ant.project.Project;
import org.apache.ant.project.Project;
import org.apache.ant.project.ProjectBuilder;
import org.apache.ant.project.ProjectBuilder;
import org.apache.ant.project.ProjectEngine;
import org.apache.ant.project.ProjectEngine;
import org.apache.ant.project.ProjectListener;
import org.apache.ant.project.ProjectListener;
import org.apache.ant.project.LogTargetToListenerAdapter;
import org.apache.ant.tasklet.JavaVersion;
import org.apache.ant.tasklet.JavaVersion;
import org.apache.ant.tasklet.TaskletContext;
import org.apache.ant.tasklet.TaskletContext;
import org.apache.ant.tasklet.engine.TaskletEngine;
import org.apache.ant.tasklet.engine.TaskletEngine;
import org.apache.ant.tasklet.engine.TskDeployer;
import org.apache.avalon.Disposable;
import org.apache.avalon.Disposable;
import org.apache.avalon.Initializable;
import org.apache.avalon.Initializable;
import org.apache.avalon.camelot.CamelotUtil;
import org.apache.avalon.camelot.Deployer;
import org.apache.avalon.camelot.DeploymentException;
import org.apache.avalon.camelot.DeploymentException;
import org.apache.avalon.util.ObjectUtil;
import org.apache.avalon.util.ObjectUtil;
import org.apache.avalon.util.StringUtil;
import org.apache.avalon.util.StringUtil;
@@ -41,7 +42,6 @@ import org.apache.avalon.util.cli.AbstractMain;
import org.apache.avalon.util.cli.CLOption;
import org.apache.avalon.util.cli.CLOption;
import org.apache.avalon.util.cli.CLOptionDescriptor;
import org.apache.avalon.util.cli.CLOptionDescriptor;
import org.apache.avalon.util.io.ExtensionFileFilter;
import org.apache.avalon.util.io.ExtensionFileFilter;
import org.apache.avalon.util.io.FileUtil;
import org.apache.log.Category;
import org.apache.log.Category;
import org.apache.log.LogKit;
import org.apache.log.LogKit;
import org.apache.log.Logger;
import org.apache.log.Logger;
@@ -67,21 +67,11 @@ public class Main
protected final static String DEFAULT_LOGLEVEL = "WARN";
protected final static String DEFAULT_LOGLEVEL = "WARN";
//Some defaults for file locations/names
//Some defaults for file locations/names
protected final static String DEFAULT_LIB_DIRECTORY = "lib";
protected final static String DEFAULT_TASKLIB_DIRECTORY = DEFAULT_LIB_DIRECTORY;
protected final static String DEFAULT_FILENAME = "build.xmk";
protected final static String DEFAULT_FILENAME = "build.xmk";
//some constants that define the classes to be loaded to perform
//particular services
protected final static String DEFAULT_ENGINE =
"org.apache.ant.project.DefaultProjectEngine";
protected final static String DEFAULT_LISTENER =
protected final static String DEFAULT_LISTENER =
"org.apache.ant.project.DefaultProjectListener";
"org.apache.ant.project.DefaultProjectListener";
protected final static String DEFAULT_BUILDER =
"org.apache.ant.project.DefaultProjectBuilder";
//defines for the Command Line options
//defines for the Command Line options
private static final int HELP_OPT = 'h';
private static final int HELP_OPT = 'h';
private static final int QUIET_OPT = 'q';
private static final int QUIET_OPT = 'q';
@@ -91,8 +81,6 @@ public class Main
private static final int DEFINE_OPT = 'D';
private static final int DEFINE_OPT = 'D';
private static final int VERSION_OPT = 1;
private static final int VERSION_OPT = 1;
private static final int LISTENER_OPT = 2;
private static final int LISTENER_OPT = 2;
private static final int BIN_DIR_OPT = 3;
private static final int LIB_DIR_OPT = 4;
private static final int TASKLIB_DIR_OPT = 5;
private static final int TASKLIB_DIR_OPT = 5;
private static final int INCREMENTAL_OPT = 6;
private static final int INCREMENTAL_OPT = 6;
private static final int HOME_DIR_OPT = 7;
private static final int HOME_DIR_OPT = 7;
@@ -102,8 +90,7 @@ public class Main
{
{
HELP_OPT, QUIET_OPT, VERBOSE_OPT, FILE_OPT,
HELP_OPT, QUIET_OPT, VERBOSE_OPT, FILE_OPT,
LOG_LEVEL_OPT, VERSION_OPT, LISTENER_OPT,
LOG_LEVEL_OPT, VERSION_OPT, LISTENER_OPT,
DEFINE_OPT
//BIN_DIR_OPT, LIB_DIR_OPT, TASKLIB_DIR_OPT, HOME_DIR_OPT
DEFINE_OPT //TASKLIB_DIR_OPT, HOME_DIR_OPT
};
};
//incompatable options for other logging options
//incompatable options for other logging options
@@ -112,15 +99,8 @@ public class Main
QUIET_OPT, VERBOSE_OPT, LOG_LEVEL_OPT
QUIET_OPT, VERBOSE_OPT, LOG_LEVEL_OPT
};
};
protected Logger m_logger;
protected ProjectListener m_listener;
protected ProjectListener m_listener;
protected File m_binDir;
protected File m_homeDir;
protected File m_homeDir;
protected File m_libDir;
protected File m_taskLibDir;
protected File m_buildFile;
protected File m_userDir;
/**
/**
* Main entry point called to run standard Ant.
* Main entry point called to run standard Ant.
@@ -134,13 +114,13 @@ public class Main
try { main.execute( args ); }
try { main.execute( args ); }
catch( final AntException ae )
catch( final AntException ae )
{
{
main.m_logger .error( "Error: " + ae.getMessage() );
main.m_logger .debug( "Exception..." + StringUtil.printStackTrace( ae ) );
main.getLogger() .error( "Error: " + ae.getMessage() );
main.getLogger() .debug( "Exception..." + StringUtil.printStackTrace( ae ) );
}
}
catch( final Throwable throwable )
catch( final Throwable throwable )
{
{
main.m_logger .error( "Error: " + throwable );
main.m_logger .debug( "Exception..." + StringUtil.printStackTrace( throwable ) );
main.getLogger() .error( "Error: " + throwable );
main.getLogger() .debug( "Exception..." + StringUtil.printStackTrace( throwable ) );
}
}
}
}
@@ -151,7 +131,7 @@ public class Main
protected CLOptionDescriptor[] createCLOptions()
protected CLOptionDescriptor[] createCLOptions()
{
{
//TODO: localise
//TODO: localise
final CLOptionDescriptor[] options = new CLOptionDescriptor[ 13 ];
final CLOptionDescriptor[] options = new CLOptionDescriptor[ 11 ];
options[0] =
options[0] =
new CLOptionDescriptor( "help",
new CLOptionDescriptor( "help",
@@ -202,33 +182,21 @@ public class Main
INFO_OPT_INCOMPAT );
INFO_OPT_INCOMPAT );
options[7] =
options[7] =
new CLOptionDescriptor( "bin-dir",
CLOptionDescriptor.ARGUMENT_REQUIRED,
BIN_DIR_OPT,
"the listener for log events." );
options[8] =
new CLOptionDescriptor( "lib-dir",
CLOptionDescriptor.ARGUMENT_REQUIRED,
LIB_DIR_OPT,
"the lib directory to scan for jars/zip files." );
options[9] =
new CLOptionDescriptor( "task-lib-dir",
new CLOptionDescriptor( "task-lib-dir",
CLOptionDescriptor.ARGUMENT_REQUIRED,
CLOptionDescriptor.ARGUMENT_REQUIRED,
TASKLIB_DIR_OPT,
TASKLIB_DIR_OPT,
"the task lib directory to scan for .tsk files." );
"the task lib directory to scan for .tsk files." );
options[10 ] =
options[8] =
new CLOptionDescriptor( "incremental",
new CLOptionDescriptor( "incremental",
CLOptionDescriptor.ARGUMENT_DISALLOWED,
CLOptionDescriptor.ARGUMENT_DISALLOWED,
INCREMENTAL_OPT,
INCREMENTAL_OPT,
"Run in incremental mode" );
"Run in incremental mode" );
options[11 ] =
options[9] =
new CLOptionDescriptor( "ant-home",
new CLOptionDescriptor( "ant-home",
CLOptionDescriptor.ARGUMENT_REQUIRED,
CLOptionDescriptor.ARGUMENT_REQUIRED,
HOME_DIR_OPT,
HOME_DIR_OPT,
"Specify ant home directory" );
"Specify ant home directory" );
options[12 ] =
options[10 ] =
new CLOptionDescriptor( "define",
new CLOptionDescriptor( "define",
CLOptionDescriptor.ARGUMENTS_REQUIRED_2,
CLOptionDescriptor.ARGUMENTS_REQUIRED_2,
DEFINE_OPT,
DEFINE_OPT,
@@ -249,11 +217,8 @@ public class Main
final ArrayList targets = new ArrayList();
final ArrayList targets = new ArrayList();
String filename = null;
String filename = null;
String listenerName = null;
String listenerName = null;
String builderName = null;
String logLevel = null;
String logLevel = null;
String binDir = null;
String homeDir = null;
String homeDir = null;
String libDir = null;
String taskLibDir = null;
String taskLibDir = null;
boolean incremental = false;
boolean incremental = false;
HashMap defines = new HashMap();
HashMap defines = new HashMap();
@@ -268,8 +233,6 @@ public class Main
case HELP_OPT: usage(); return;
case HELP_OPT: usage(); return;
case VERSION_OPT: System.out.println( VERSION ); return;
case VERSION_OPT: System.out.println( VERSION ); return;
case FILE_OPT: filename = option.getArgument(); break;
case FILE_OPT: filename = option.getArgument(); break;
case BIN_DIR_OPT: binDir = option.getArgument(); break;
case LIB_DIR_OPT: libDir = option.getArgument(); break;
case HOME_DIR_OPT: homeDir = option.getArgument(); break;
case HOME_DIR_OPT: homeDir = option.getArgument(); break;
case TASKLIB_DIR_OPT: taskLibDir = option.getArgument(); break;
case TASKLIB_DIR_OPT: taskLibDir = option.getArgument(); break;
case VERBOSE_OPT: logLevel = "INFO"; break;
case VERBOSE_OPT: logLevel = "INFO"; break;
@@ -284,64 +247,63 @@ public class Main
}
}
}
}
if( null == logLevel ) logLevel = getDefaultLogLevel();
if( null == listenerName ) listenerName = getDefaultListener();
if( null == filename ) filename = getDefaultFilename();
if( null == libDir ) libDir = getDefaultLibDir();
if( null == taskLibDir ) taskLibDir = getDefaultTaskLibDir();
if( null == builderName ) builderName = getBuilderNameFor( filename );
setupLogger( logLevel ); //handle logging...
setupListener( listenerName ); //handle listener..
setupDefaultAntDirs();
//try to auto-discover the location of ant so that
//can populate classpath with libs/tasks and gain access
//to antRun
if( null == binDir && null == homeDir )
{
m_homeDir = getDefaultHomeDir();
m_binDir = m_homeDir.getParentFile();
}
else if( null == binDir ) // && null != homeDir
{
m_homeDir = getHomeDir( homeDir );
m_binDir = new File( m_homeDir, "bin" );
}
else
if( null == logLevel ) logLevel = DEFAULT_LOGLEVEL;
if( null == listenerName ) listenerName = DEFAULT_LISTENER;
if( null == filename ) filename = DEFAULT_FILENAME;
//handle logging...
setLogger( createLogger( logLevel ) );
//if ant home not set then use system property ant.home
//that was set up by launcher.
if( null == homeDir ) homeDir = System.getProperty( "ant.home" );
final Properties properties = new Properties();
properties.setProperty( "ant.home", homeDir );
if( null != taskLibDir ) properties.setProperty( "ant.path.task-lib", taskLibDir );
m_homeDir = (new File( homeDir )).getAbsoluteFile();
if( !m_homeDir.isDirectory() )
{
{
m_binDir = getBinDir( binDir );
m_homeDir = m_binDir.getParentFile();
throw new AntException( "ant-home (" + m_homeDir + ") is not a directory" );
}
}
m_libDir = getLibDir( m_homeDir, libDir );
m_taskLibDir = getTaskLibDir( m_homeDir, taskLibDir );
m_buildFile = getFile( filename );
m_logger.warn( "Ant Build File: " + m_buildFile );
m_logger.info( "Ant Home Directory: " + m_homeDir );
m_logger.info( "Ant Bin Directory: " + m_binDir );
m_logger.debug( "Ant Lib Directory: " + m_libDir );
m_logger.debug( "Ant Task Lib Directory: " + m_taskLibDir );
final File libDir = new File( m_homeDir, "lib" );
final File buildFile = (new File( filename )).getCanonicalFile();
if( !buildFile.isFile() )
{
throw new AntException( "File " + buildFile + " is not a file or doesn't exist" );
}
//setup classloader so that it will correctly load
//setup classloader so that it will correctly load
//the Project/ProjectBuilder/ProjectEngine and all dependencies
//the Project/ProjectBuilder/ProjectEngine and all dependencies
setupContextClassLoader( m_libDir );
final ClassLoader classLoader = createClassLoader( libDir );
Thread.currentThread().setContextClassLoader( classLoader );
final Project project = getProject( builderName, m_buildFile );
setupProjectContext( project, defines );
//handle listener..
final ProjectListener listener = createListener( listenerName );
final ProjectEngine engine = getProjectEngine();
getLogger().warn( "Ant Build File: " + buildFile );
getLogger().info( "Ant Home Directory: " + m_homeDir );
//getLogger().info( "Ant Bin Directory: " + m_binDir );
//getLogger().debug( "Ant Lib Directory: " + m_libDir );
//getLogger().debug( "Ant Task Lib Directory: " + m_taskLibDir );
//make sure Engine is sweet...
if( engine instanceof Initializable )
{
((Initializable)engine).init();
}
final AntEngine antEngine = new DefaultAntEngine();
setupLogger( antEngine );
antEngine.setProperties( properties );
antEngine.init();
engine.addProjectListener( m_listener );
final ProjectBuilder builder = antEngine.getProjectBuilder();
//create the project
final Project project = builder.build( buildFile );
setupProjectContext( project, defines );
deployDefaultTaskLibs( engine, m_taskLibDir );
final ProjectEngine engine = antEngine.getProjectEngine();
engine.addProjectListener( listener );
BufferedReader reader = null;
BufferedReader reader = null;
@@ -366,44 +328,7 @@ public class Main
}
}
//shutdown engine gracefully if needed
if( engine instanceof Disposable )
{
((Disposable)engine).dispose();
}
}
/**
* Deploy all tasklibs in tasklib directory into ProjectEngine.
*
* @param engine the ProjectEngine
* @param taskLibDirectory the directory to look for .tsk files
*/
protected void deployDefaultTaskLibs( final ProjectEngine engine,
final File taskLibDirectory )
{
final ExtensionFileFilter filter = new ExtensionFileFilter( ".tsk" );
final File[] files = taskLibDirectory.listFiles( filter );
final TskDeployer deployer = engine.getTaskletEngine().getTskDeployer();
for( int i = 0; i < files.length; i++ )
{
final String name = files[ i ].getName();
try
{
deployer.deploy( name.substring( 0, name.length() - 4 ),
files[ i ].toURL() );
}
catch( final MalformedURLException mue ) {}
catch( final DeploymentException de )
{
throw new AntException( "Failed to deploy task library " + files[ i ],
de );
}
}
antEngine.dispose();
}
}
/**
/**
@@ -436,22 +361,11 @@ public class Main
}
}
catch( final AntException ae )
catch( final AntException ae )
{
{
m_logger .error( "BUILD FAILED" );
m_logger .error( "Reason:\n" + StringUtil.printStackTrace( ae, 5, true ) );
getLogger() .error( "BUILD FAILED" );
getLogger() .error( "Reason:\n" + StringUtil.printStackTrace( ae, 5, true ) );
}
}
}
}
/**
* Setup Logger for a particular log-level.
* This is in seperate method so it can be overidden if sub-classed.
*
* @param logLevel the log-level
*/
protected void setupLogger( final String logLevel )
{
m_logger = createLogger( logLevel );
}
/**
/**
* Create Logger of appropriate log-level.
* Create Logger of appropriate log-level.
*
*
@@ -479,39 +393,47 @@ public class Main
*
*
* @param listenerName the name of project listener
* @param listenerName the name of project listener
*/
*/
protected void setupListener( final String listenerName )
protected ProjectListener createListener( final String listenerName )
throws AntException
{
{
m_listener = createListener( listenerName );
m_logger.addLogTarget( new LogTargetToListenerAdapter( m_listener ) );
}
ProjectListener result = null;
/**
* Make sure classloader is setup correctly so can do Class.forName safely
*
* @param libDir the directory to grab all the lib files from
*/
protected void setupContextClassLoader( final File libDir )
{
setupClassLoader( libDir );
Thread.currentThread().setContextClassLoader( AntLoader.getLoader() );
try { result = (ProjectListener)ObjectUtil.createObject( listenerName ); }
catch( final Throwable t )
{
throw new AntException( "Error creating the listener " + listenerName +
" due to " + StringUtil.printStackTrace( t, 5, true ),
t );
}
getLogger().addLogTarget( new LogTargetToListenerAdapter( result ) );
return result;
}
}
/**
/**
* Setup classloader so that the *current* classloader has access to parsers etc.
* This is a bit of a hack as it assumes that AntLoader was used to load this file
* but it is the only way to add to current classloader safely.
* Try to load all extra zipz/jars from lib directory into CURRENT classloader.
*
*
* @param libDir the directory of lib files to add
* @param libDir the directory of lib files to add
*/
*/
protected void setup ClassLoader( final File libDir )
protected ClassLoader create ClassLoader( final File libDir )
{
{
final ClassLoader candidate = getClass().getClassLoader();
if( !(candidate instanceof AntClassLoader) )
{
getLogger().warn( "Warning: Unable to add entries from " +
"lib-path to classloader" );
return candidate;
}
final AntClassLoader classLoader = (AntClassLoader)candidate;
final ExtensionFileFilter filter =
final ExtensionFileFilter filter =
new ExtensionFileFilter( new String[] { ".jar", ".zip" } );
new ExtensionFileFilter( new String[] { ".jar", ".zip" } );
final File[] files = libDir.listFiles( filter );
final File[] files = libDir.listFiles( filter );
final AntLoader classLoader = AntLoader.getLoader();
for( int i = 0; i < files.length; i++ )
for( int i = 0; i < files.length; i++ )
{
{
//except for a few *special* files add all the
//except for a few *special* files add all the
@@ -524,28 +446,8 @@ public class Main
catch( final MalformedURLException mue ) {}
catch( final MalformedURLException mue ) {}
}
}
}
}
}
/**
* Using a specified builder create a project from a particular file.
*
* @param builderName the name of the builder class
* @param file the file
* @return the newly created Project
* @exception AntException if an error occurs
* @exception IOException if an error occurs
*/
protected Project getProject( final String builderName, final File file )
throws AntException, IOException
{
m_logger.debug( "Ant Project Builder: " + builderName );
final ProjectBuilder builder = createBuilder( builderName );
builder.setLogger( m_logger );
//create the project
final Project project = builder.build( file );
return project;
return classLoader;
}
}
/**
/**
@@ -562,12 +464,11 @@ public class Main
{
{
//put these values into defines so that they overide
//put these values into defines so that they overide
//user-defined proeprties
//user-defined proeprties
defines.put( AntContextResources.HOME_DIR, m_homeDir );
defines.put( AntContextResources.BIN_DIR, m_binDir );
defines.put( AntContextResources.LIB_DIR, m_libDir );
defines.put( AntContextResources.TASKLIB_DIR, m_taskLibDir );
//defines.put( AntContextResources.USER_DIR, m_userDir );
defines.put( TaskletContext.JAVA_VERSION, getJavaVersion() );
//defines.put( AntContextResources.HOME_DIR, m_homeDir );
//defines.put( AntContextResources.BIN_DIR, m_binDir );
//defines.put( AntContextResources.LIB_DIR, m_libDir );
//defines.put( AntContextResources.TASKLIB_DIR, m_taskLibDir );
//defines.put( TaskletContext.JAVA_VERSION, getJavaVersion() );
final TaskletContext context = project.getContext();
final TaskletContext context = project.getContext();
addToContext( context, defines );
addToContext( context, defines );
@@ -593,330 +494,5 @@ public class Main
context.setProperty( key, value );
context.setProperty( key, value );
}
}
}
}
/**
* Helper method to retrieve current JVM version.
* Basically stolen from original Ant sources.
*
* @return the current JVM version
*/
protected JavaVersion getJavaVersion()
{
JavaVersion version = JavaVersion.JAVA1_0;
try
{
Class.forName( "java.lang.Void" );
version = JavaVersion.JAVA1_1;
Class.forName( "java.lang.ThreadLocal" );
version = JavaVersion.JAVA1_2;
Class.forName( "java.lang.StrictMath" );
version = JavaVersion.JAVA1_3;
}
catch( final ClassNotFoundException cnfe ) {}
return version;
}
/**
* Create and configure project engine
*
* @return the ProjectEngine
*/
protected ProjectEngine getProjectEngine()
{
final ProjectEngine engine = createProjectEngine();
engine.setLogger( m_logger );
return engine;
}
/**
* Create the project engine.
* This is seperate method so that it can be overidden in a sub-class.
*
* @return the new ProjectEngine
*/
protected ProjectEngine createProjectEngine()
{
return (ProjectEngine)createObject( DEFAULT_ENGINE, "project-engine" );
}
protected File getHomeDir( final String homeDir )
throws AntException
{
final File file = (new File( homeDir )).getAbsoluteFile();
checkDirectory( file, "ant-home" );
return file;
}
protected File getBinDir( final String binDir )
throws AntException
{
File file = (new File( binDir )).getAbsoluteFile();
if( !file.isDirectory() ) file = file.getParentFile();
checkDirectory( file, "bin-dir" );
return file;
}
protected File getLibDir( final File antHome, String libDir )
throws AntException
{
return resolveDirectory( antHome, libDir, "lib-dir" );
}
protected File getTaskLibDir( final File antHome, final String taskLibDir )
throws AntException
{
return resolveDirectory( antHome, taskLibDir, "task-lib-dir" );
}
protected File resolveDirectory( final File antHome, final String dir, final String name )
throws AntException
{
final File file = FileUtil.resolveFile( antHome, dir );
checkDirectory( file, name );
return file;
}
protected void checkDirectory( final File file, final String name )
{
if( !file.exists() )
{
throw new AntException( name + " (" + file + ") does not exist" );
}
else if( !file.isDirectory() )
{
throw new AntException( name + " (" + file + ") is not a directory" );
}
}
protected ProjectListener createListener( final String listenerName )
throws AntException
{
try { return (ProjectListener)createObject( listenerName, "listener" ); }
catch( final ClassCastException cce )
{
throw new AntException( "Aparently the listener named " + listenerName +
" does not implement the ProjectListener interface",
cce );
}
}
protected void setupDefaultAntDirs()
{
final String os = System.getProperty( "os.name" );
final String userDir = System.getProperty( "user.home" );
m_userDir =
(new File( getUserLocationFor( os, userDir ) )).getAbsoluteFile();
}
/**
* Retrieve default bin-dir value if possible (Otherwise throw an exception).
*
* Lookup OS specific places for ant to be.
* /opt/ant on *BSD ?
* /usr/local/ant on linux ?
* /Program Files/Ant on Win32 ?
*
* @return bin directory
*/
protected File getDefaultHomeDir()
throws AntException
{
if( null != m_userDir )
{
try
{
checkDirectory( m_userDir, null );
return m_userDir;
}
catch( final AntException ae ) {}
}
final String os = System.getProperty( "os.name" );
final File candidate =
(new File( getSystemLocationFor( os ) )).getAbsoluteFile();
checkDirectory( candidate, "ant-home" );
return candidate;
}
/**
* This determins a mapping from an OS specific place to ants home directory.
* In later versions the mapping should be read from configuration file.
*
* @param os the name of OS
* @return the location of directory
*/
protected String getUserLocationFor( final String os, final String userDir )
{
if( os.startsWith( "Windows" ) )
{
return userDir + "\\Ant";
}
else if( '/' == File.separatorChar )
{
if( os.startsWith( "Linux" ) ) return userDir + "/ant";
else return userDir + "/opt/ant";
}
else
{
return userDir + File.separator + "ant";
}
}
/**
* This determins a mapping from an OS specific place to ants home directory.
* In later versions the mapping should be read from configuration file.
*
* @param os the name of OS
* @return the location of directory
*/
protected String getSystemLocationFor( final String os )
{
if( os.startsWith( "Windows" ) )
{
return "\\Program Files\\Ant";
}
else if( '/' == File.separatorChar )
{
if( os.startsWith( "Linux" ) ) return "/usr/local/ant";
else return "/opt/ant";
}
else
{
return File.separator + "ant";
}
}
protected String getDefaultLibDir()
{
return DEFAULT_LIB_DIRECTORY;
}
protected String getDefaultTaskLibDir()
{
return DEFAULT_TASKLIB_DIRECTORY;
}
/**
* Retrieve default filename. Overide this in base classes to change default.
*
* @return the default filename
*/
protected String getDefaultFilename()
{
return DEFAULT_FILENAME;
}
/**
* Retrieve default logelevel. Overide this in base classes to change default.
*
* @return the default loglevel
*/
protected String getDefaultLogLevel()
{
return DEFAULT_LOGLEVEL;
}
/**
* Retrieve default listener. Overide this in base classes to change default.
*
* @return the default listener
*/
protected String getDefaultListener()
{
return DEFAULT_LISTENER;
}
/**
* Get File object for filename.
* Check that file exists and is not a directory.
*
* @param filename the filename
* @return the file object
* @exception AntException if an error occurs
*/
protected File getFile( final String filename )
throws AntException, IOException
{
final File file = (new File( filename )).getCanonicalFile();
if( !file.exists() )
{
throw new AntException( "File " + file + " does not exist." );
}
if( file.isDirectory() )
{
throw new AntException( "File " + file + " is a directory." );
}
return file;
}
/**
* Create instance of Builder based on classname.
*
* @param builderName builder class name
* @return the ProjectBuilder
* @exception AntException if an error occurs
*/
protected ProjectBuilder createBuilder( final String builderName )
throws AntException
{
try { return (ProjectBuilder)createObject( builderName, "builder" ); }
catch( final ClassCastException cce )
{
throw new AntException( "Aparently the builder named " + builderName +
" does not implement the ProjectBuilder interface",
cce );
}
}
/**
* Helper method to create object and throw an apporpriate AntException if creation failed.
*
* @param objectName the classname of object
* @param type the type of object being created (ie builder|listener)
* @return the created object
* @exception AntException if an error occurs
*/
protected Object createObject( final String objectName, final String type )
throws AntException
{
try
{
return ObjectUtil.createObject( objectName );
}
catch( final IllegalAccessException iae )
{
throw new AntException( "Non-public constructor for " + type + " " + objectName,
iae );
}
catch( final InstantiationException ie )
{
throw new AntException( "Error instantiating class for " + type + " " + objectName,
ie );
}
catch( final ClassNotFoundException cnfe )
{
throw new AntException( "Could not find the class for " + type + " " + objectName,
cnfe );
}
}
/**
* Retrieve class name of builder for file.
* Eventually this will look in a registry of file extentions to BuilderNames.
*
* @param filename the filename
* @return the name of Class for Builder
* @exception AntException if an error occurs
*/
protected String getBuilderNameFor( final String filename )
throws AntException
{
return DEFAULT_BUILDER;
}
}
}