KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > core > AntCorePlugin


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.core;
12
13 import java.net.URL JavaDoc;
14 import java.net.URLClassLoader JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.ant.internal.core.AntClassLoader;
19 import org.eclipse.ant.internal.core.AntCoreUtil;
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.core.runtime.IExtensionPoint;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.core.runtime.Plugin;
25 import org.eclipse.core.runtime.Status;
26 import org.osgi.framework.BundleContext;
27
28 /**
29  * The plug-in runtime class for the Ant Core plug-in.
30  * Clients may not instantiate or subclass this class.
31  */

32 public class AntCorePlugin extends Plugin {
33
34     /**
35      * Status code indicating an unexpected internal error.
36      * @since 2.1
37      */

38     public static final int INTERNAL_ERROR = 120;
39     
40     /**
41      * The single instance of this plug-in runtime class.
42      */

43     private static AntCorePlugin plugin;
44
45     /**
46      * The preferences class for this plug-in.
47      */

48     private AntCorePreferences preferences;
49     
50     /**
51      * Unique identifier constant (value <code>"org.eclipse.ant.core"</code>)
52      * for the Ant Core plug-in.
53      */

54     public static final String JavaDoc PI_ANTCORE = "org.eclipse.ant.core"; //$NON-NLS-1$
55

56     /**
57      * Simple identifier constant (value <code>"antTasks"</code>)
58      * for the Ant tasks extension point.
59      */

60     public static final String JavaDoc PT_TASKS = "antTasks"; //$NON-NLS-1$
61

62     /**
63      * Simple identifier constant (value <code>"extraClasspathEntries"</code>)
64      * for the extra classpath entries extension point.
65      */

66     public static final String JavaDoc PT_EXTRA_CLASSPATH = "extraClasspathEntries"; //$NON-NLS-1$
67

68     /**
69      * Simple identifier constant (value <code>"antTypes"</code>)
70      * for the Ant types extension point.
71      */

72     public static final String JavaDoc PT_TYPES = "antTypes"; //$NON-NLS-1$
73

74     /**
75      * Simple identifier constant (value <code>"antProperties"</code>)
76      * for the Ant properties extension point.
77      *
78      * @since 3.0
79      */

80     public static final String JavaDoc PT_PROPERTIES = "antProperties"; //$NON-NLS-1$
81

82     /**
83      * Simple identifier constant (value <code>"org.eclipse.ant.core.antBuildFile"</code>)
84      * for the content type of an Ant BuildFile
85      *
86      * @since 3.0
87      */

88     public static final String JavaDoc ANT_BUILDFILE_CONTENT_TYPE = PI_ANTCORE + ".antBuildFile"; //$NON-NLS-1$
89

90     /**
91      * Simple identifier constant (value <code>"class"</code>)
92      * of a tag that appears in Ant extensions.
93      */

94     public static final String JavaDoc CLASS = "class"; //$NON-NLS-1$
95

96     /**
97      * Simple identifier constant (value <code>"name"</code>)
98      * of a tag that appears in Ant extensions.
99      */

100     public static final String JavaDoc NAME = "name"; //$NON-NLS-1$
101

102     /**
103      * Simple identifier constant (value <code>"library"</code>)
104      * of a tag that appears in Ant extensions.
105      */

106     public static final String JavaDoc LIBRARY = "library"; //$NON-NLS-1$
107

108     /**
109      * Simple identifier constant (value <code>"headless"</code>) of a tag
110      * that appears in Ant extensions.
111      * @since 2.1
112      */

113     public static final String JavaDoc HEADLESS = "headless"; //$NON-NLS-1$
114

115     /**
116      * Simple identifier constant (value <code>"eclipseRuntime"</code>) of a tag
117      * that appears in Ant extensions.
118      * @since 3.0
119      */

120     public static final String JavaDoc ECLIPSE_RUNTIME = "eclipseRuntime"; //$NON-NLS-1$
121

122     /**
123      * Simple identifier constant (value <code>"uri"</code>) of a tag
124      * that appears in Ant extensions.
125      * @since 3.2
126      */

127     public static final String JavaDoc URI = "uri"; //$NON-NLS-1$
128

129     /**
130      * Simple identifier constant (value <code>"value"</code>) of a tag
131      * that appears in Ant extensions.
132      * @since 3.0
133      */

134     public static final String JavaDoc VALUE = "value"; //$NON-NLS-1$
135

136     /**
137      * Key to access the <code>IProgressMonitor</code> reference. When a
138      * progress monitor is passed to the <code>AntRunner.run(IProgressMonitor)</code>
139      * method, the object is available as a reference for the current
140      * Ant project.
141      */

142     public static final String JavaDoc ECLIPSE_PROGRESS_MONITOR = "eclipse.progress.monitor"; //$NON-NLS-1$
143

144     /**
145      * Status code indicating an error occurred running a build.
146      * @since 2.1
147      */

148     public static final int ERROR_RUNNING_BUILD = 1;
149     
150     /**
151      * Status code indicating an error occurred due to a malformed URL.
152      * @since 2.1
153      */

154     public static final int ERROR_MALFORMED_URL = 2;
155     
156     /**
157      * Status code indicating an error occurred as a library was not specified
158      * @since 2.1
159      */

160     public static final int ERROR_LIBRARY_NOT_SPECIFIED = 3;
161
162     /**
163      * Constructs an instance of this plug-in runtime class.
164      * <p>
165      * An instance of this plug-in runtime class is automatically created
166      * when the facilities provided by the Ant Core plug-in are required.
167      * <b>Clients must never explicitly instantiate a plug-in runtime class.</b>
168      * </p>
169      * @since 3.1
170      *
171      */

172     public AntCorePlugin() {
173         plugin = this;
174     }
175
176     /* (non-Javadoc)
177      * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
178      * @since 3.1
179      */

180     public void stop(BundleContext context) throws Exception JavaDoc {
181         super.stop(context);
182         AntCoreUtil.setBundleContext(null);
183         if (preferences != null) {
184             savePluginPreferences();
185         }
186     }
187     
188     /* (non-Javadoc)
189      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
190      * @since 3.1
191      */

192     public void start(BundleContext context) throws Exception JavaDoc {
193         super.start(context);
194         AntCoreUtil.setBundleContext(context);
195     }
196
197     /**
198      * Given an extension point name, extract its extensions and return them
199      * as a List.
200      * @param pointName The name of the extension point
201      * @return The list of the extensions
202      */

203     private List JavaDoc extractExtensions(String JavaDoc pointName) {
204         IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(AntCorePlugin.PI_ANTCORE, pointName);
205         if (extensionPoint == null) {
206             return null;
207         }
208         IConfigurationElement[] extensions = extensionPoint.getConfigurationElements();
209         return Arrays.asList(extensions);
210     }
211
212     /**
213      * Returns an object representing this plug-in's preferences.
214      *
215      * @return the Ant core object representing the preferences for this plug-in.
216      */

217     public AntCorePreferences getPreferences() {
218         if (preferences == null) {
219             preferences = new AntCorePreferences(extractExtensions(PT_TASKS), extractExtensions(PT_EXTRA_CLASSPATH), extractExtensions(PT_TYPES), extractExtensions(PT_PROPERTIES), false);
220         }
221         return preferences;
222     }
223     
224     /**
225      * Set this plug-in's preferences for running headless based on the
226      * headless parameter.
227      * This method is public for testing purposes only. It should not
228      * be called outside of the Ant integration framework.
229      * @param headless Whether or not to mark that the plug-in is running headless or not
230      */

231     public void setRunningHeadless(boolean headless) {
232         preferences = new AntCorePreferences(extractExtensions(PT_TASKS), extractExtensions(PT_EXTRA_CLASSPATH), extractExtensions(PT_TYPES), extractExtensions(PT_PROPERTIES), headless);
233     }
234
235     /**
236      * Returns this plug-in instance.
237      *
238      * @return the single instance of this plug-in runtime class
239      */

240     public static AntCorePlugin getPlugin() {
241         return plugin;
242     }
243     
244     /**
245      * Returns a new class loader to use when executing Ant builds.
246      *
247      * @return the new class loader
248      */

249     public ClassLoader JavaDoc getNewClassLoader() {
250         AntCorePreferences corePreferences = getPreferences();
251         return getNewClassLoader(false, corePreferences.getURLs());
252     }
253     
254     /**
255      * Returns a new class loader to use when executing Ant builds.
256      * @param urls the URLs that define the classpath of the new classloader
257      * @return the new class loader
258      * @since 3.1
259      */

260     public URLClassLoader JavaDoc getNewClassLoader(URL JavaDoc[] urls) {
261         return getNewClassLoader(false, urls);
262     }
263     
264     /**
265      * Returns a new class loader to use when executing Ant builds or
266      * other applications such as parsing or code proposal determination.
267      *
268      * @param allowLoading whether to allow plug-in classloaders associated
269      * with the new classloader to load Apache Ant classes or resources.
270      * @return the new class loader
271      */

272     public ClassLoader JavaDoc getNewClassLoader(boolean allowLoading) {
273         AntCorePreferences corePreferences = getPreferences();
274         URL JavaDoc[] urls = corePreferences.getURLs();
275         return getNewClassLoader(allowLoading, urls);
276     }
277         
278     /**
279      * Returns a new class loader to use when executing Ant builds or
280      * other applications such as parsing or code proposal determination.
281      *
282      * @param allowLoading whether to allow plug-in classloaders associated
283      * with the new classloader to load Apache Ant classes or resources.
284      * @param urls the URLs that define the classpath of the new classloader
285      * @return the new class loader
286      * @since 3.1
287      */

288     public URLClassLoader JavaDoc getNewClassLoader(boolean allowLoading, URL JavaDoc[] urls) {
289         AntCorePreferences corePreferences = getPreferences();
290         ClassLoader JavaDoc[] pluginLoaders = corePreferences.getPluginClassLoaders();
291         AntClassLoader loader= new AntClassLoader(urls, pluginLoaders);
292         loader.allowPluginClassLoadersToLoadAnt(allowLoading);
293         return loader;
294     }
295     
296     /**
297      * Logs the specified throwable with this plug-in's log.
298      *
299      * @param t throwable to log
300      * @since 2.1
301      */

302     public static void log(Throwable JavaDoc t) {
303         IStatus status= new Status(IStatus.ERROR, PI_ANTCORE, INTERNAL_ERROR, "Error logged from Ant Core: ", t); //$NON-NLS-1$
304
getPlugin().getLog().log(status);
305     }
306 }
307
Popular Tags