Browse Source

Performance: bugzilla 25778

Minor optimization for ComponentHelper.initDefaultDefinitions



git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@448385 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Reilly 19 years ago
parent
commit
b912cb97f4
1 changed files with 66 additions and 62 deletions
  1. +66
    -62
      src/main/org/apache/tools/ant/ComponentHelper.java

+ 66
- 62
src/main/org/apache/tools/ant/ComponentHelper.java View File

@@ -124,6 +124,10 @@ public class ComponentHelper {
* contrived work here to enable this early. * contrived work here to enable this early.
*/ */
private static final String ANT_PROPERTY_TASK = "property"; private static final String ANT_PROPERTY_TASK = "property";
// {tasks, types}
private static Properties[] defaultDefinitions = new Properties[2];



/** /**
* Find a project component for a specific project, creating * Find a project component for a specific project, creating
@@ -713,35 +717,19 @@ public class ComponentHelper {
* Load ant's tasks. * Load ant's tasks.
*/ */
private void initTasks() { private void initTasks() {
ClassLoader classLoader = null; ClassLoader classLoader = getClassLoader(null);
classLoader = getClassLoader(classLoader); Properties props = getDefaultDefinitions(false);
String dataDefs = MagicNames.TASKDEF_PROPERTIES_RESOURCE; Enumeration e = props.propertyNames();

while (e.hasMoreElements()) {
InputStream in = null; String name = (String) e.nextElement();
try { String className = props.getProperty(name);
Properties props = new Properties(); AntTypeDefinition def = new AntTypeDefinition();
in = this.getClass().getResourceAsStream(dataDefs); def.setName(name);
if (in == null) { def.setClassName(className);
throw new BuildException(ERROR_NO_TASK_LIST_LOAD); def.setClassLoader(classLoader);
} def.setAdaptToClass(Task.class);
props.load(in); def.setAdapterClass(TaskAdapter.class);

antTypeTable.put(name, def);
Enumeration e = props.propertyNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
String className = props.getProperty(name);
AntTypeDefinition def = new AntTypeDefinition();
def.setName(name);
def.setClassName(className);
def.setClassLoader(classLoader);
def.setAdaptToClass(Task.class);
def.setAdapterClass(TaskAdapter.class);
antTypeTable.put(name, def);
}
} catch (IOException ex) {
throw new BuildException(ERROR_NO_TASK_LIST_LOAD);
} finally {
FileUtils.close(in);
} }
} }


@@ -753,44 +741,60 @@ public class ComponentHelper {
} }
return classLoader; return classLoader;
} }
/**
* Load default task or type definitions - just the names,
* no class loading.
* Caches results between calls to reduce overhead.
* @param type true for typedefs, false for taskdefs
* @return a mapping from definition names to class names
* @throws BuildException if there was some problem loading
* or parsing the definitions list
*/
private static synchronized Properties getDefaultDefinitions(boolean type)
throws BuildException {
int idx = type ? 1 : 0;
if (defaultDefinitions[idx] == null) {
String resource = type
? MagicNames.TYPEDEFS_PROPERTIES_RESOURCE
: MagicNames.TASKDEF_PROPERTIES_RESOURCE;
String errorString = type
? ERROR_NO_TYPE_LIST_LOAD
: ERROR_NO_TASK_LIST_LOAD;
InputStream in = null;
try {
in = ComponentHelper.class.getResourceAsStream(
resource);
if (in == null) {
throw new BuildException(errorString);
}
Properties p = new Properties();
p.load(in);
defaultDefinitions[idx] = p;
} catch (IOException e) {
throw new BuildException(errorString, e);
} finally {
FileUtils.close(in);
}
}
return defaultDefinitions[idx];
}


/** /**
* Load ant's datatypes. * Load ant's datatypes.
*/ */
private void initTypes() { private void initTypes() {
ClassLoader classLoader = null; ClassLoader classLoader = getClassLoader(null);
classLoader = getClassLoader(classLoader); Properties props = getDefaultDefinitions(true);
String dataDefs = MagicNames.TYPEDEFS_PROPERTIES_RESOURCE; Enumeration e = props.propertyNames();

while (e.hasMoreElements()) {
InputStream in = null; String name = (String) e.nextElement();
try { String className = props.getProperty(name);
Properties props = new Properties(); AntTypeDefinition def = new AntTypeDefinition();
in = this.getClass().getResourceAsStream(dataDefs); def.setName(name);
if (in == null) { def.setClassName(className);
throw new BuildException(ERROR_NO_TYPE_LIST_LOAD); def.setClassLoader(classLoader);
} antTypeTable.put(name, def);
props.load(in);

Enumeration e = props.propertyNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
String className = props.getProperty(name);
AntTypeDefinition def = new AntTypeDefinition();
def.setName(name);
def.setClassName(className);
def.setClassLoader(classLoader);
antTypeTable.put(name, def);
}
} catch (IOException ex) {
throw new BuildException(ERROR_NO_TYPE_LIST_LOAD);
} finally {
if (in != null) {
try {
in.close();
} catch (Exception ignore) {
// ignore
}
}
} }
} }




||||||
x
 
000:0
Loading…
Cancel
Save