KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > core > ant > InternalProject


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * Portions Copyright 2000-2004 The Apache Software Foundation
4  * All rights reserved. This program and the accompanying materials are made
5  * available under the terms of the Apache Software License v2.0 which
6  * accompanies this distribution and is available at
7  * http://www.apache.org/licenses/LICENSE-2.0.
8  *
9  * Contributors:
10  * IBM Corporation - derived implementation
11  *******************************************************************************/

12
13 package org.eclipse.ant.internal.core.ant;
14
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.lang.reflect.Constructor JavaDoc;
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19 import java.text.MessageFormat JavaDoc; // can't use ICU, ant build script
20
import java.util.Enumeration JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.Project;
26 import org.apache.tools.ant.ProjectComponent;
27
28 /**
29  * A subclass of Project to facilitate "faster" parsing with
30  * less garbage generated. This class is not used on Ant 1.6 and newer
31  * due to the improvements in lazy loading of these Ant versions.
32  *
33  * Only three tasks are loaded (property, taskdef and
34  * typedef: three tasks that can be defined outside of a target on Ant 1.5.1 or older).
35  *
36  * Datatypes are loaded if requested.
37  *
38  * Derived from the original Ant Project class
39  */

40 public class InternalProject extends Project {
41
42     private Hashtable JavaDoc typeNameToClass = null;
43     
44     public InternalProject() {
45         super();
46     }
47
48     /* (non-Javadoc)
49      * @see org.apache.tools.ant.Project#init()
50      */

51     public void init() throws BuildException {
52         setJavaVersionProperty();
53
54         try {
55             Class JavaDoc taskClass = Class.forName("org.apache.tools.ant.taskdefs.Property"); //$NON-NLS-1$
56
addTaskDefinition("property", taskClass); //$NON-NLS-1$
57
taskClass = Class.forName("org.apache.tools.ant.taskdefs.Typedef"); //$NON-NLS-1$
58
addTaskDefinition("typedef", taskClass); //$NON-NLS-1$
59
taskClass = Class.forName("org.apache.tools.ant.taskdefs.Taskdef"); //$NON-NLS-1$
60
addTaskDefinition("taskdef", taskClass); //$NON-NLS-1$
61
} catch (NoClassDefFoundError JavaDoc e) {
62             throw new BuildException(InternalAntMessages.InternalAntRunner_Missing_Class, e);
63         } catch (ClassNotFoundException JavaDoc c) {
64             throw new BuildException(InternalAntMessages.InternalAntRunner_Missing_Class, c);
65         }
66
67         setSystemProperties();
68     }
69
70     /* (non-Javadoc)
71      * @see org.apache.tools.ant.Project#createDataType(java.lang.String)
72      */

73     public Object JavaDoc createDataType(String JavaDoc typeName) throws BuildException {
74         if (typeNameToClass == null) {
75             initializeTypes();
76         }
77         Class JavaDoc typeClass = (Class JavaDoc) typeNameToClass.get(typeName);
78
79         if (typeClass == null) {
80             return null;
81         }
82
83         Throwable JavaDoc thrown = null;
84         try {
85             Constructor JavaDoc ctor = null;
86             boolean noArg = false;
87             // DataType can have a "no arg" constructor or take a single
88
// Project argument.
89
try {
90                 ctor = typeClass.getConstructor(new Class JavaDoc[0]);
91                 noArg = true;
92             } catch (NoSuchMethodException JavaDoc nse) {
93                 ctor = typeClass.getConstructor(new Class JavaDoc[] { Project.class });
94                 noArg = false;
95             }
96
97             Object JavaDoc o = null;
98             if (noArg) {
99                 o = ctor.newInstance(new Object JavaDoc[0]);
100             } else {
101                 o = ctor.newInstance(new Object JavaDoc[] { this });
102             }
103             if (o instanceof ProjectComponent) {
104                 ((ProjectComponent) o).setProject(this);
105             }
106             return o;
107         } catch (InvocationTargetException JavaDoc ite) {
108             thrown = ite.getTargetException();
109         } catch (IllegalArgumentException JavaDoc e) {
110             thrown = e;
111         } catch (InstantiationException JavaDoc e) {
112             thrown = e;
113         } catch (IllegalAccessException JavaDoc e) {
114             thrown = e;
115         } catch (NoSuchMethodException JavaDoc nse) {
116             thrown = nse;
117         } catch (NoClassDefFoundError JavaDoc ncdfe) {
118             thrown = ncdfe;
119         }
120         if (thrown != null) {
121             String JavaDoc message= MessageFormat.format(InternalAntMessages.InternalProject_0, new String JavaDoc[]{typeName, thrown.toString()});
122             throw new BuildException(message, thrown);
123         }
124         // this line is actually unreachable
125
return null;
126     }
127
128     /**
129      * Initialize the mapping of data type name to data type classname
130      */

131     private void initializeTypes() {
132         typeNameToClass = new Hashtable JavaDoc(18);
133         String JavaDoc dataDefs = "/org/apache/tools/ant/types/defaults.properties"; //$NON-NLS-1$
134
try {
135             Properties JavaDoc props = new Properties JavaDoc();
136             InputStream JavaDoc in = Project.class.getResourceAsStream(dataDefs);
137             if (in == null) {
138                 return;
139             }
140             props.load(in);
141             in.close();
142
143             Enumeration JavaDoc enumeration = props.propertyNames();
144             while (enumeration.hasMoreElements()) {
145                 String JavaDoc typeName = (String JavaDoc) enumeration.nextElement();
146                 String JavaDoc className = props.getProperty(typeName);
147                 try {
148                     Class JavaDoc typeClass= Class.forName(className);
149                     typeNameToClass.put(typeName, typeClass);
150                 } catch (NoClassDefFoundError JavaDoc e) {
151                     //ignore
152
} catch (ClassNotFoundException JavaDoc c) {
153                     //ignore
154
}
155             }
156         } catch (IOException JavaDoc ioe) {
157             return;
158         }
159
160     }
161     
162     /* (non-Javadoc)
163      * @see org.apache.tools.ant.Project#getDataTypeDefinitions()
164      */

165     public Hashtable JavaDoc getDataTypeDefinitions() {
166         if (typeNameToClass == null) {
167             initializeTypes();
168         }
169         return typeNameToClass;
170     }
171     
172     /* (non-Javadoc)
173      * @see org.apache.tools.ant.Project#addDataTypeDefinition(java.lang.String, java.lang.Class)
174      */

175     public void addDataTypeDefinition(String JavaDoc typeName, Class JavaDoc typeClass) {
176         getDataTypeDefinitions();
177         typeNameToClass.put(typeName, typeClass);
178     }
179 }
180
Popular Tags