Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 80c65b5990293f517fa49ed49ccf0bfbeb282028 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.core.internal.adapter;

import java.util.Iterator;
import java.util.List;
import org.eclipse.core.internal.runtime.AdapterManager;
import org.eclipse.core.internal.runtime.IAdapterManagerProvider;
import org.eclipse.core.runtime.*;

/**
 * Portions of the AdapterManager that deal with the Eclipse extension registry
 * were moved into this class.
 */
public final class AdapterManagerListener implements IRegistryEventListener, IAdapterManagerProvider {
	public static final String ADAPTER_POINT_ID = "org.eclipse.core.runtime.adapters"; //$NON-NLS-1$

	private AdapterManager theAdapterManager;

	/**
	 * Constructs a new adapter manager.
	 */
	public AdapterManagerListener() {
		theAdapterManager = AdapterManager.getDefault();
		theAdapterManager.registerLazyFactoryProvider(this);
	}

	/**
	 * Loads adapters registered with the adapters extension point from
	 * the plug-in registry.  Note that the actual factory implementations
	 * are loaded lazily as they are needed.
	 */
	@Override
	public boolean addFactories(AdapterManager adapterManager) {
		IExtensionPoint point = RegistryFactory.getRegistry().getExtensionPoint(ADAPTER_POINT_ID);
		if (point == null)
			return false;

		boolean factoriesAdded = false;
		IExtension[] extensions = point.getExtensions();
		for (int i = 0; i < extensions.length; i++) {
			IConfigurationElement[] elements = extensions[i].getConfigurationElements();
			for (int j = 0; j < elements.length; j++) {
				AdapterFactoryProxy proxy = AdapterFactoryProxy.createProxy(elements[j]);
				if (proxy != null) {
					adapterManager.registerFactory(proxy, proxy.getAdaptableType());
					factoriesAdded = true;
				}
			}
		}
		RegistryFactory.getRegistry().addListener(this, ADAPTER_POINT_ID);
		return factoriesAdded;
	}

	private void registerExtension(IExtension extension) {
		IConfigurationElement[] elements = extension.getConfigurationElements();
		for (int j = 0; j < elements.length; j++) {
			AdapterFactoryProxy proxy = AdapterFactoryProxy.createProxy(elements[j]);
			if (proxy != null)
				theAdapterManager.registerFactory(proxy, proxy.getAdaptableType());
		}
	}

	@Override
	public synchronized void added(IExtension[] extensions) {
		for (int i = 0; i < extensions.length; i++)
			registerExtension(extensions[i]);
		theAdapterManager.flushLookup();
	}

	@Override
	public synchronized void removed(IExtension[] extensions) {
		theAdapterManager.flushLookup();
		for (int i = 0; i < extensions.length; i++) {
			for (Iterator it = theAdapterManager.getFactories().values().iterator(); it.hasNext();) {
				for (Iterator it2 = ((List) it.next()).iterator(); it2.hasNext();) {
					IAdapterFactory factory = (IAdapterFactory) it2.next();
					if (!(factory instanceof AdapterFactoryProxy))
						continue;
					if (((AdapterFactoryProxy) factory).originatesFrom(extensions[i]))
						it2.remove();
				}
			}
		}
	}

	@Override
	public synchronized void added(IExtensionPoint[] extensionPoints) {
		// nothing to do
	}

	@Override
	public synchronized void removed(IExtensionPoint[] extensionPoints) {
		// all extensions should have been removed by this point by #removed(IExtension[] extensions)
		// nothing to do
	}

	/*
	 * Shuts down the listener by removing the registry change listener. Should only be
	 * invoked during platform shutdown.
	 */
	public synchronized void stop() {
		RegistryFactory.getRegistry().removeListener(this);
	}
}

Back to the top