KickJava   Java API By Example, From Geeks To Geeks.

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


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.ui.internal.components.framework.ComponentException;
16 import org.eclipse.ui.internal.components.framework.ComponentFactory;
17 import org.eclipse.ui.internal.components.framework.ComponentHandle;
18 import org.eclipse.ui.internal.components.framework.IServiceProvider;
19 import org.eclipse.ui.internal.components.framework.ReflectionFactory;
20 import org.eclipse.ui.internal.components.framework.SingletonFactory;
21
22 /**
23  * Creates components from a named attribute of a configuration element.
24  *
25  * @since 3.1
26  */

27 public class ExecutableExtensionFactory extends ComponentFactory {
28
29     private String JavaDoc attributeId;
30     private IConfigurationElement configElement;
31
32     private ComponentFactory cachedAdapter = null;
33     
34     /**
35      * Creates a component factory based on an attribute from a configuration element. The attribute
36      * must point to a class that either implements IComponent or ComponentFactory.
37      *
38      * @param configElement configuration element
39      * @param attributeId attribute from the given configuration element that contains the name of
40      * a java class that implements either IComponent or ComponentFactory
41      * @param singleton if true, this factory will return the same instance every time. If false, this
42      * factory will create a new instance every time it is asked for a handle
43      */

44     public ExecutableExtensionFactory(IConfigurationElement configElement, String JavaDoc attributeId) {
45         this.configElement = configElement;
46         this.attributeId = attributeId;
47     }
48     
49     /* (non-Javadoc)
50      * @see org.eclipse.core.component.IComponentAdapter#createInstance(org.eclipse.core.component.IContainer)
51      */

52     public ComponentHandle createHandle(IServiceProvider availableServices) throws ComponentException {
53         // If this class is created by a factory and we've already cached an instance of the
54
// factory, use the cached factory.
55
if (cachedAdapter != null) {
56             try {
57                 ComponentHandle result = cachedAdapter.createHandle(availableServices);
58                 return result;
59             } catch (ComponentException e) {
60                 // This branch omits the classes ReflectionFactory and SingletonFactory from the dependency
61
// chain when displaying the exception message -- clients are probably interested in debugging their
62
// own classes, and probably aren't interested in classes supplied by the framework. This
63
// keeps the error messages more concise.
64
if (cachedAdapter instanceof ReflectionFactory || cachedAdapter instanceof SingletonFactory) {
65                     throw e;
66                 }
67                 
68                 throw new ComponentException(cachedAdapter.getClass(), e);
69             }
70         }
71         
72         // If we're loading a singleton object, create a new container for it.
73
// Otherwise, create a filtered view of the existing container that
74
// will only resolve services visible in this scope.
75
IServiceProvider actualContainer = availableServices;
76
77         try {
78             cachedAdapter = (ComponentFactory)configElement.createExecutableExtension("class"); //$NON-NLS-1$
79
} catch (CoreException e) {
80             throw new ComponentException(configElement, e);
81         }
82         
83         return cachedAdapter.createHandle(actualContainer);
84     }
85
86     /* (non-Javadoc)
87      * @see java.lang.Object#hashCode()
88      */

89     public int hashCode() {
90         return configElement.hashCode() + attributeId.hashCode();
91     }
92     
93     /* (non-Javadoc)
94      * @see java.lang.Object#equals(java.lang.Object)
95      */

96     public boolean equals(Object JavaDoc obj) {
97         if (!(obj instanceof ExecutableExtensionFactory)) {
98             return false;
99         }
100         ExecutableExtensionFactory t = (ExecutableExtensionFactory)obj;
101         
102         return configElement == t.configElement
103             && attributeId.equals(t.attributeId);
104     }
105
106     public void dispose() {
107         cachedAdapter = null;
108     }
109         
110 }
111
112
Popular Tags