Source code: org/eclipse/core/internal/runtime/AdapterFactoryProxy.java
1 /**********************************************************************
2 * Copyright (c) 2004 IBM Corporation and others. All rights reserved. This
3 * program and the accompanying materials are made available under the terms of
4 * the Common Public License v1.0 which accompanies this distribution, and is
5 * available at http://www.eclipse.org/legal/cpl-v10.html
6 *
7 * Contributors:
8 * IBM - Initial API and implementation
9 **********************************************************************/
10 package org.eclipse.core.internal.runtime;
11
12 import java.util.ArrayList;
13 import org.eclipse.core.runtime.*;
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 {
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 /**
31 * Creates a new factory proxy based on the given configuration element.
32 * Returns the new proxy, or null if the element could not be created.
33 */
34 public static AdapterFactoryProxy createProxy(IConfigurationElement element) {
35 AdapterFactoryProxy result = new AdapterFactoryProxy();
36 result.element = element;
37 if ("factory".equals(element.getName())) //$NON-NLS-1$
38 return result;
39 result.logError();
40 return null;
41 }
42
43 String getAdaptableType() {
44 //cannot return null because it can cause startup failure
45 String result = element.getAttribute("adaptableType"); //$NON-NLS-1$
46 if (result != null)
47 return result;
48 logError();
49 return ""; //$NON-NLS-1$
50 }
51
52 public Object getAdapter(Object adaptableObject, Class adapterType) {
53 if (!factoryLoaded)
54 loadFactory(false);
55 return factory == null ? null : factory.getAdapter(adaptableObject, adapterType);
56 }
57
58 public Class[] getAdapterList() {
59 if (!factoryLoaded)
60 loadFactory(false);
61 return factory == null ? null : factory.getAdapterList();
62 }
63
64 String[] getAdapterNames() {
65 IConfigurationElement[] children = element.getChildren();
66 ArrayList adapters = new ArrayList(children.length);
67 for (int i = 0; i < children.length; i++) {
68 //ignore unknown children for forward compatibility
69 if ("adapter".equals(children[i].getName())) { //$NON-NLS-1$
70 String type = children[i].getAttribute("type"); //$NON-NLS-1$
71 if (type != null)
72 adapters.add(type);
73 }
74 }
75 if (adapters.isEmpty())
76 logError();
77 return (String[]) adapters.toArray(new String[adapters.size()]);
78 }
79
80 IExtension getExtension() {
81 return element.getDeclaringExtension();
82 }
83
84 /**
85 * Loads the real adapter factory, but only if its associated plug-in is
86 * already loaded. Returns the real factory if it was successfully loaded.
87 * @param force if <code>true</code> the plugin providing the
88 * factory will be loaded if necessary, otherwise no plugin activations
89 * will occur.
90 */
91 IAdapterFactory loadFactory(boolean force) {
92 synchronized (this) {
93 if (factory != null || factoryLoaded)
94 return factory;
95 String bundleId = element.getDeclaringExtension().getNamespace();
96 if (!force && Platform.getBundle(bundleId).getState() != Bundle.ACTIVE)
97 return null;
98 //set to true to prevent repeated attempts to load a broken factory
99 factoryLoaded = true;
100 }
101 try {
102 factory = (IAdapterFactory) element.createExecutableExtension("class"); //$NON-NLS-1$
103 } catch (CoreException e) {
104 InternalPlatform.getDefault().log(e.getStatus());
105 }
106 return factory;
107 }
108
109 /**
110 * The factory extension was malformed. Log an appropriate exception
111 */
112 private void logError() {
113 String msg = Policy.bind("adapters.badAdapterFactory", element.getDeclaringExtension().getNamespace()); //$NON-NLS-1$
114 InternalPlatform.getDefault().log(new Status(IStatus.ERROR, Platform.PI_RUNTIME, 1, msg, null));
115 }
116 }