KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > components > ComponentUtil


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.components;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IConfigurationElement;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.ui.PlatformUI;
20 import org.eclipse.ui.internal.components.framework.ClassIdentifier;
21 import org.eclipse.ui.internal.components.framework.ComponentException;
22 import org.eclipse.ui.internal.components.framework.ServiceFactory;
23 import org.eclipse.ui.internal.components.registry.ComponentRegistry;
24 import org.eclipse.ui.internal.components.registry.ComponentScope;
25 import org.eclipse.ui.internal.components.registry.ExtensionPointManager;
26 import org.eclipse.ui.internal.components.registry.IComponentScope;
27 import org.eclipse.ui.internal.components.registry.ServiceExtensionPoint;
28 import org.eclipse.ui.internal.misc.StatusUtil;
29 import org.osgi.framework.Bundle;
30
31
32 /**
33  * Creates an IComponent, given a set of local services.
34  *
35  * @since 3.1
36  */

37 public class ComponentUtil {
38     
39     public static final Object JavaDoc[] EMPTY_ARRAY = new Object JavaDoc[0];
40     
41     private static ComponentRegistry registry;
42     private static ExtensionPointManager uiExtensionPoints = null;
43     private static ServiceExtensionPoint partServices = null;
44
45     
46     private ComponentUtil() {
47     }
48     
49     public static ComponentRegistry getRegistry() {
50         if (registry != null) {
51             return registry;
52         }
53         registry = new ComponentRegistry();
54         Bundle uiBundle = Platform.getBundle(PlatformUI.PLUGIN_ID);
55         uiExtensionPoints = new ExtensionPointManager(uiBundle);
56         partServices = new ServiceExtensionPoint(uiExtensionPoints, ComponentUtil.registry);
57
58         return registry;
59     }
60     
61     public static void disposeRegistry() {
62       if (partServices != null) {
63           partServices.dispose();
64           partServices = null;
65       }
66       
67       if (uiExtensionPoints != null) {
68           uiExtensionPoints.dispose();
69           uiExtensionPoints = null;
70       }
71       
72       registry = null;
73     }
74     
75     public static String JavaDoc getSimpleClassName(String JavaDoc fullyQualifiedClassName) {
76         int idx = fullyQualifiedClassName.lastIndexOf('.') + 1;
77         
78         return fullyQualifiedClassName.substring(idx, fullyQualifiedClassName.length());
79     }
80     
81     public static Throwable JavaDoc getCause(Throwable JavaDoc toQuery) {
82         return StatusUtil.getCause(toQuery);
83     }
84     
85     public static Class JavaDoc loadClass(ClassIdentifier type) throws ComponentException {
86         try {
87             Bundle pluginBundle = Platform.getBundle(type.getNamespace());
88             Class JavaDoc result = pluginBundle.loadClass(type.getTypeName());
89             return result;
90         } catch (ClassNotFoundException JavaDoc e) {
91             throw new ComponentException(type.getTypeName(),
92                    NLS.bind(ComponentMessages.ComponentUtil_class_not_found,
93                             type.getNamespace(), type.getTypeName()), null);
94         }
95     }
96     
97     public static final ClassIdentifier getClassFromInitializationData(IConfigurationElement config, Object JavaDoc data) throws CoreException {
98         
99         if (!(data instanceof String JavaDoc)) {
100             String JavaDoc msg = NLS.bind(
101                     ComponentMessages.ReflectionFactory_missing_data, config.toString());
102             
103             throw new CoreException(new Status(IStatus.ERROR, config.getNamespace(), Status.OK,
104                     msg, null));
105         }
106         
107         return new ClassIdentifier(config.getNamespace(), (String JavaDoc)data);
108     }
109     
110     public static String JavaDoc getMessage(Throwable JavaDoc toQuery) {
111         String JavaDoc msg = toQuery.getMessage();
112         
113         if (msg == null) {
114             msg = toQuery.toString();
115         }
116         
117         return msg;
118     }
119     
120 // /**
121
// * Creates and initializes a component implementation, given a Class that implements
122
// * IComponent. Returns a fully-initialized component.
123
// *
124
// * @param desiredService
125
// * @param otherServices
126
// * @return a fully initialized component
127
// * @throws ComponentException if unable to create or initialize the component
128
// */
129
// public static Object createComponentImplementation(Class desiredService, IComponentProvider otherServices) throws ComponentException {
130
// try {
131
// Assert.isTrue(IComponent.class.isAssignableFrom(desiredService));
132
//
133
// IComponent component = (IComponent)desiredService.newInstance();
134
//
135
// component.init(otherServices);
136
//
137
// return component;
138
// } catch (ComponentException e) {
139
// throw new ComponentException(desiredService, e);
140
// } catch (IllegalArgumentException e) {
141
// throw new ComponentException(desiredService, e);
142
// } catch (InstantiationException e) {
143
// throw new ComponentException(desiredService, e);
144
// } catch (IllegalAccessException e) {
145
// throw new ComponentException(desiredService, e);
146
// }
147
// }
148

149     public static String JavaDoc getAttribute(IConfigurationElement element, String JavaDoc attributeId) throws CoreException {
150         String JavaDoc result = element.getAttribute(attributeId);
151         if (result == null) {
152             throw new CoreException(new Status(IStatus.ERROR, element.getNamespace(),
153                     IStatus.OK, NLS.bind(ComponentMessages.ComponentUtil_missing_attribute,
154                             new Object JavaDoc[] {element.getName(), attributeId,
155                             element.getDeclaringExtension().getExtensionPointUniqueIdentifier()}), null
156                             ));
157         }
158         
159         return result;
160     }
161     
162     public static Throwable JavaDoc getMostSpecificCause(Throwable JavaDoc exception) {
163         return getMostSpecificException(getCause(exception));
164     }
165     
166     /**
167      * @since 3.1
168      *
169      * @param exception
170      * @return
171      */

172     private static Throwable JavaDoc getMostSpecificException(Throwable JavaDoc exception) {
173         Throwable JavaDoc cause = getCause(exception);
174         if (cause == null || cause == exception) {
175             return exception;
176         }
177         
178         return getMostSpecificException(cause);
179     }
180
181     /**
182      * @param implementation
183      * @throws ComponentException
184      */

185     public static Object JavaDoc createInstance(Class JavaDoc implementation) throws ComponentException {
186         try {
187             return implementation.newInstance();
188         } catch (InstantiationException JavaDoc e) {
189             throw new ComponentException(implementation, e);
190         } catch (IllegalAccessException JavaDoc e) {
191             throw new ComponentException(implementation, e);
192         }
193     }
194
195     /**
196      * Returns meta-information about the component scope with the given ID.
197      * This is intended for introspection without activating plugins.
198      * For example, this could be used to print out a list of all services
199      * available in a particular scope. Clients who want to instantiate services
200      * in the given scope should call <code>getContext</code> instead.
201      *
202      * <i>EXPERIMENTAL</i> it is likely that this method will change in the
203      * near future.
204      *
205      * @param scope scope ID
206      * @return an IComponentScope containing meta-information about the given
207      * scope ID, or null if the given scope is unknown
208      */

209     public static IComponentScope getScope(String JavaDoc scope) {
210         return getRegistry().getScope(scope);
211     }
212
213     /**
214      * Returns a context for the given scope ID. This context can be used
215      * to instantiate components from the org.eclipse.core.components.services
216      * that are included in the given scope.
217      *
218      * <p>
219      * The resulting context will be able to create components if:
220      * <ul>
221      * <li>The component is belongs to the same scope</li>
222      * <li>The component is belongs to a scope which is included in the given scope</li>
223      * </ul>
224      *
225      * <p>
226      * Note that components that are required by the given scope will show up
227      * as dependencies in the resulting context but cannot be created by
228      * the context.
229      * </p>
230      *
231      * <p>
232      * All dependencies of the given context should be satisfied before
233      * attempting to instantiate any objects from the context.
234      * </p>
235      *
236      * @param scope ID of the scope to return
237      * @return a container context for the given scope
238      */

239     public static ServiceFactory getContext(String JavaDoc scope) {
240         IComponentScope s = getScope(scope);
241         
242         if (s == null) {
243             return null;
244         }
245         
246         return ((ComponentScope)s).getContext();
247     }
248     
249
250     
251 }
252
Popular Tags