KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > AdapterFactoryProxy


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

9 package org.eclipse.core.internal.runtime;
10
11 import java.util.ArrayList JavaDoc;
12 import org.eclipse.core.runtime.*;
13 import org.eclipse.osgi.util.NLS;
14 import org.osgi.framework.Bundle;
15
16 /**
17  * Instances of this class represent adapter factories that have been
18  * contributed via the adapters extension point. The concrete factory is not
19  * loaded until the factory's plugin is loaded, AND until the factory is
20  * requested to supply an adapter.
21  */

22 class AdapterFactoryProxy implements IAdapterFactory, IAdapterFactoryExt {
23     private IConfigurationElement element;
24     /**
25      * The real factory. Null until the factory is loaded.
26      */

27     private IAdapterFactory factory;
28     private boolean factoryLoaded = false;
29     /**
30      * Store Id of the declaring extension. We might need it in case
31      * the owner goes away (in this case element becomes invalid).
32      */

33     private String JavaDoc ownerId;
34
35     /**
36      * Creates a new factory proxy based on the given configuration element.
37      * Returns the new proxy, or null if the element could not be created.
38      */

39     public static AdapterFactoryProxy createProxy(IConfigurationElement element) {
40         AdapterFactoryProxy result = new AdapterFactoryProxy();
41         result.element = element;
42         result.ownerId = element.getDeclaringExtension().getUniqueIdentifier();
43         if ("factory".equals(element.getName())) //$NON-NLS-1$
44
return result;
45         result.logError();
46         return null;
47     }
48
49     String JavaDoc getAdaptableType() {
50         //cannot return null because it can cause startup failure
51
String JavaDoc result = element.getAttribute("adaptableType"); //$NON-NLS-1$
52
if (result != null)
53             return result;
54         logError();
55         return ""; //$NON-NLS-1$
56
}
57
58     public Object JavaDoc getAdapter(Object JavaDoc adaptableObject, Class JavaDoc adapterType) {
59         if (!factoryLoaded)
60             loadFactory(false);
61         return factory == null ? null : factory.getAdapter(adaptableObject, adapterType);
62     }
63
64     public Class JavaDoc[] getAdapterList() {
65         if (!factoryLoaded)
66             loadFactory(false);
67         return factory == null ? null : factory.getAdapterList();
68     }
69
70     public String JavaDoc[] getAdapterNames() {
71         IConfigurationElement[] children = element.getChildren();
72         ArrayList JavaDoc adapters = new ArrayList JavaDoc(children.length);
73         for (int i = 0; i < children.length; i++) {
74             //ignore unknown children for forward compatibility
75
if ("adapter".equals(children[i].getName())) { //$NON-NLS-1$
76
String JavaDoc type = children[i].getAttribute("type"); //$NON-NLS-1$
77
if (type != null)
78                     adapters.add(type);
79             }
80         }
81         if (adapters.isEmpty())
82             logError();
83         return (String JavaDoc[]) adapters.toArray(new String JavaDoc[adapters.size()]);
84     }
85
86     IExtension getExtension() {
87         return element.getDeclaringExtension();
88     }
89
90     String JavaDoc getOwnerId() {
91         return ownerId;
92     }
93
94     /**
95      * Loads the real adapter factory, but only if its associated plug-in is
96      * already loaded. Returns the real factory if it was successfully loaded.
97      * @param force if <code>true</code> the plugin providing the
98      * factory will be loaded if necessary, otherwise no plugin activations
99      * will occur.
100      */

101     public synchronized IAdapterFactory loadFactory(boolean force) {
102         if (factory != null || factoryLoaded)
103             return factory;
104         String JavaDoc bundleId = element.getContributor().getName();
105         if (!force && Platform.getBundle(bundleId).getState() != Bundle.ACTIVE)
106             return null;
107         //set to true to prevent repeated attempts to load a broken factory
108
factoryLoaded = true;
109         try {
110             factory = (IAdapterFactory) element.createExecutableExtension("class"); //$NON-NLS-1$
111
} catch (CoreException e) {
112             InternalPlatform.getDefault().log(e.getStatus());
113         }
114         return factory;
115     }
116
117     /**
118      * The factory extension was malformed. Log an appropriate exception
119      */

120     private void logError() {
121         String JavaDoc msg = NLS.bind(Messages.adapters_badAdapterFactory, element.getContributor().getName());
122         InternalPlatform.getDefault().log(new Status(IStatus.ERROR, Platform.PI_RUNTIME, 1, msg, null));
123     }
124 }
125
Popular Tags