package org.apache.tools.ant; import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Constructor; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import org.apache.tools.ant.helper.ProjectHelper2; import org.apache.tools.ant.util.LoaderUtils; /** * Repository of {@link ProjectHelper} found in the classpath or via * some System properties. *
See the ProjectHelper documentation in the manual.
* * @since Ant 1.8.0 */ public class ProjectHelperRepository { private static final String DEBUG_PROJECT_HELPER_REPOSITORY = "ant.project-helper-repo.debug"; // The message log level is not accessible here because everything // is instanciated statically private static final boolean DEBUG = "true".equals(System.getProperty(DEBUG_PROJECT_HELPER_REPOSITORY)); private static ProjectHelperRepository instance = new ProjectHelperRepository(); private List/*null.
*
* @return a new instance of the specified class.
*
* @exception BuildException
* if the class cannot be found or cannot be appropriate
* instantiated.
*/
private ProjectHelper newHelper(String helperClass) throws BuildException {
ClassLoader classLoader = LoaderUtils.getContextClassLoader();
try {
Class clazz = null;
if (classLoader != null) {
try {
clazz = classLoader.loadClass(helperClass);
} catch (ClassNotFoundException ex) {
// try next method
}
}
if (clazz == null) {
clazz = Class.forName(helperClass);
}
return ((ProjectHelper) clazz.newInstance());
} catch (Exception e) {
throw new BuildException(e);
}
}
/**
* Get the helper that will be able to parse the specified file. The helper
* will be chosen among the ones found in the classpath
*
* @return the first ProjectHelper that fit the requirement (never null).
*/
public ProjectHelper getProjectHelper(File buildFile) throws BuildException {
Iterator it = getHelpers();
while (it.hasNext()) {
ProjectHelper helper = (ProjectHelper) it.next();
if (helper.supportsBuildFile(buildFile)) {
if (DEBUG) {
System.out.println("ProjectHelper "
+ helper.getClass().getName()
+ " selected for the file "
+ buildFile);
}
return helper;
}
}
throw new RuntimeException("BUG: at least the ProjectHelper2 should "
+ "have supported the file " + buildFile);
}
/**
* Get an iterator on the list of project helpers configured. The iterator
* will always return at least one element as there will always be the
* default project helper configured.
*
* @return an iterator of {@link ProjectHelper}
*/
public Iterator getHelpers() {
return new ConstructingIterator(helpers.iterator());
}
private static class ConstructingIterator implements Iterator {
private final Iterator nested;
ConstructingIterator(Iterator nested) {
this.nested = nested;
}
public boolean hasNext() {
return nested.hasNext();
}
public Object next() {
Constructor c = (Constructor) nested.next();
try {
return c.newInstance(NO_OBJECT);
} catch (Exception e) {
throw new BuildException("Failed to invoke no-arg constructor"
+ " on " + c.getName());
}
}
public void remove() {
throw new UnsupportedOperationException("remove is not supported");
}
}
}