Author: peterreilly
Date: Wed Sep 20 15:41:51 2006
New Revision: 448385

URL: http://svn.apache.org/viewvc?view=rev&rev=448385
Log:
Performance: bugzilla  25778
    Minor optimization for ComponentHelper.initDefaultDefinitions


Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/ComponentHelper.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/ComponentHelper.java
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/ComponentHelper.java?view=diff&rev=448385&r1=448384&r2=448385
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/ComponentHelper.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/ComponentHelper.java Wed Sep 
20 15:41:51 2006
@@ -124,6 +124,10 @@
      * contrived work here to enable this early.
      */
     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
@@ -713,35 +717,19 @@
      * Load ant's tasks.
      */
     private void initTasks() {
-        ClassLoader classLoader = null;
-        classLoader = getClassLoader(classLoader);
-        String dataDefs = MagicNames.TASKDEF_PROPERTIES_RESOURCE;
-
-        InputStream in = null;
-        try {
-            Properties props = new Properties();
-            in = this.getClass().getResourceAsStream(dataDefs);
-            if (in == null) {
-                throw new BuildException(ERROR_NO_TASK_LIST_LOAD);
-            }
-            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);
-                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);
+        ClassLoader classLoader = getClassLoader(null);
+        Properties props = getDefaultDefinitions(false);
+        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);
         }
     }
 
@@ -753,44 +741,60 @@
         }
         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.
      */
     private void initTypes() {
-        ClassLoader classLoader = null;
-        classLoader = getClassLoader(classLoader);
-        String dataDefs = MagicNames.TYPEDEFS_PROPERTIES_RESOURCE;
-
-        InputStream in = null;
-        try {
-            Properties props = new Properties();
-            in = this.getClass().getResourceAsStream(dataDefs);
-            if (in == null) {
-                throw new BuildException(ERROR_NO_TYPE_LIST_LOAD);
-            }
-            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
-                }
-            }
+        ClassLoader classLoader = getClassLoader(null);
+        Properties props = getDefaultDefinitions(true);
+        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);
         }
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to