KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > EarlyStartupRunnable


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.ui.internal;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.core.runtime.IExtension;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.jface.util.SafeRunnable;
23 import org.eclipse.ui.IStartup;
24 import org.osgi.framework.Bundle;
25
26 /**
27  * A utility class used to call #earlyStartup on the proper instance for a given
28  * configuration element. There are a few steps to the process in order to
29  * ensure compatibility with pre-3.0 plugins.
30  *
31  * @since 3.0
32  */

33 public class EarlyStartupRunnable extends SafeRunnable {
34
35     private static final String JavaDoc EXTENSION_CLASS = "org.eclipse.core.runtime.IExtension"; //$NON-NLS-1$
36

37 // private static final String PLUGIN_DESC_CLASS = "org.eclipse.core.runtime.IPluginDescriptor"; //$NON-NLS-1$
38

39     private static final String JavaDoc GET_PLUGIN_METHOD = "getPlugin"; //$NON-NLS-1$
40

41     private static final String JavaDoc GET_DESC_METHOD = "getDeclaringPluginDescriptor"; //$NON-NLS-1$
42

43     private static final String JavaDoc PI_RUNTIME_COMPATIBILITY = "org.eclipse.core.runtime.compatibility"; //$NON-NLS-1$
44

45     private IExtension extension;
46
47     /**
48      * @param extension
49      * must not be null
50      */

51     public EarlyStartupRunnable(IExtension extension) {
52         this.extension = extension;
53     }
54
55     public void run() throws Exception JavaDoc {
56         IConfigurationElement[] configElements = extension
57                 .getConfigurationElements();
58
59         // look for the startup tag in each element and run the extension
60
boolean foundAtLeastOne = false;
61         for (int i = 0; i < configElements.length; ++i) {
62             IConfigurationElement element = configElements[i];
63             if (element != null
64                     && element.getName()
65                             .equals(IWorkbenchConstants.TAG_STARTUP)) {
66                 runEarlyStartup(getExecutableExtension(element));
67                 foundAtLeastOne = true;
68             }
69         }
70
71         // if no startup tags were found, then try the plugin object
72
if (!foundAtLeastOne) {
73             runEarlyStartup(getPluginForCompatibility());
74         }
75     }
76
77     public void handleException(Throwable JavaDoc exception) {
78         IStatus status = new Status(IStatus.ERROR, extension.getNamespace(), 0,
79                 "Unable to execute early startup code for an extension", //$NON-NLS-1$
80
exception);
81         WorkbenchPlugin.log("Unhandled Exception", status); //$NON-NLS-1$
82
}
83
84     private void runEarlyStartup(Object JavaDoc executableExtension) {
85         if (executableExtension != null
86                 && executableExtension instanceof IStartup) {
87             ((IStartup) executableExtension).earlyStartup();
88         } else {
89             IStatus status = new Status(IStatus.ERROR,
90                     extension.getNamespace(), 0,
91                     "startup class must implement org.eclipse.ui.IStartup", //$NON-NLS-1$
92
null);
93             WorkbenchPlugin.log("Bad extension specification", status); //$NON-NLS-1$
94
}
95     }
96
97     /**
98      * In 3.0 the class attribute is a mandatory element of the startup element.
99      * However, 2.1 plugins should still be able to run if the compatibility
100      * bundle is loaded.
101      *
102      * @return an executable extension for this startup element or null if an
103      * extension (or plugin) could not be found
104      */

105     private Object JavaDoc getExecutableExtension(IConfigurationElement element)
106             throws CoreException {
107
108         String JavaDoc classname = element.getAttribute(IWorkbenchConstants.TAG_CLASS);
109
110         // if class attribute is absent then try to use the compatibility
111
// bundle to return the plugin object
112
if (classname == null || classname.length() <= 0) {
113             return getPluginForCompatibility();
114         }
115
116         // otherwise the 3.0 runtime should be able to do it
117
return WorkbenchPlugin.createExtension(element,
118                 IWorkbenchConstants.TAG_CLASS);
119     }
120
121     /**
122      * If the compatiblity bundle is loaded, then return the plugin object for
123      * the extension on this runnable. Return null if the compatibility bundle
124      * is not loaded or the plugin object cannot be created.
125      */

126     private Object JavaDoc getPluginForCompatibility() {
127         // make sure the compatibility bundle is available
128
Bundle compatBundle = Platform.getBundle(PI_RUNTIME_COMPATIBILITY);
129         if (compatBundle == null) {
130             return null;
131         }
132
133         // use reflection to try to access the plugin object
134
try {
135             // IPluginDescriptor pluginDesc =
136
// extension.getDeclaringPluginDescriptor();
137
Class JavaDoc extensionClass = compatBundle.loadClass(EXTENSION_CLASS);
138             Method JavaDoc getDescMethod = extensionClass.getDeclaredMethod(
139                     GET_DESC_METHOD, new Class JavaDoc[0]);
140             Object JavaDoc pluginDesc = getDescMethod.invoke(extension, new Object JavaDoc[0]);
141             if (pluginDesc == null) {
142                 return null;
143             }
144
145             // Plugin plugin = pluginDesc.getPlugin();
146
Class JavaDoc pluginDescClass = pluginDesc.getClass();
147             Method JavaDoc getPluginMethod = pluginDescClass.getDeclaredMethod(
148                     GET_PLUGIN_METHOD, new Class JavaDoc[0]);
149             return getPluginMethod.invoke(pluginDesc, new Object JavaDoc[0]);
150         } catch (ClassNotFoundException JavaDoc e) {
151             handleException(e);
152         } catch (IllegalAccessException JavaDoc e) {
153             handleException(e);
154         } catch (InvocationTargetException JavaDoc e) {
155             handleException(e);
156         } catch (NoSuchMethodException JavaDoc e) {
157             handleException(e);
158         }
159
160         return null;
161     }
162 }
163
Popular Tags