Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 94abc281e7cf04420c3f7016c20719737bca5569 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*******************************************************************************
 * Copyright (c) 2005 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
 *******************************************************************************/
/*
 *  $$RCSfile: RegistryReader.java,v $$
 *  $$Revision: 1.3 $$  $$Date: 2005/05/18 21:58:34 $$ 
 */
package org.eclipse.jem.util;
import org.eclipse.core.runtime.*;
import org.osgi.framework.Bundle;

import org.eclipse.jem.util.logger.proxy.Logger;


/**
 * Class to read a registry. It is meant to be subclassed to provide specific function.
 * 
 * @since 1.0.0
 */
public abstract class RegistryReader {

	String pluginId;

	String extensionPointId;

	private static Bundle systemBundle;

	/**
	 * Utility method to get the plugin id of a configuation element
	 * 
	 * @param configurationElement
	 * @return plugin id of configuration element
	 * @since 1.0.0
	 */
	public static String getPluginId(IConfigurationElement configurationElement) {
		String pluginId = null;

		if (configurationElement != null) {
			IExtension extension = configurationElement.getDeclaringExtension();

			if (extension != null)
				pluginId = extension.getNamespace();
		}

		return pluginId;
	}

	/**
	 * Constructor for RegistryReader taking a registry, plugin id, and extension point id.
	 * 
	 * @param registry
	 * @param pluginID
	 * @param extensionPoint
	 * 
	 * @deprecated Use RegistryReader(plugin, extensionPoint) instead. The registry passed in is ignored.
	 * @since 1.0.0
	 */
	public RegistryReader(IPluginRegistry registry, String pluginID, String extensionPoint) {
		this(pluginID, extensionPoint);
	}

	/**
	 * Constructor for RegistryReader taking the plugin id and extension point id.
	 * 
	 * @param pluginID
	 * @param extensionPoint
	 * 
	 * @since 1.0.0
	 */
	public RegistryReader(String pluginID, String extensionPoint) {
		super();
		this.pluginId = pluginID;
		extensionPointId = extensionPoint;
	}

	private void internalReadElement(IConfigurationElement element) {
		boolean recognized = this.readElement(element);
		if (!recognized) {
			logError(element, "Error processing extension: " + element); //$NON-NLS-1$
		}
	}

	/*
	 * Logs the error in the desktop log using the provided text and the information in the configuration element.
	 */
	protected void logError(IConfigurationElement element, String text) {
		IExtension extension = element.getDeclaringExtension();
		StringBuffer buf = new StringBuffer();
		buf.append("Plugin " + extension.getNamespace() + ", extension " + extension.getExtensionPointUniqueIdentifier()); //$NON-NLS-1$ //$NON-NLS-2$
		buf.append("\n" + text); //$NON-NLS-1$
		Logger.getLogger().logError(buf.toString());
	}

	/*
	 * Logs a very common registry error when a required attribute is missing.
	 */
	protected void logMissingAttribute(IConfigurationElement element, String attributeName) {
		logError(element, "Required attribute '" + attributeName + "' not defined"); //$NON-NLS-1$ //$NON-NLS-2$
	}

	/*
	 * Implement this method to read element attributes. If this element has subelements, the reader will recursively cycle through them and call this
	 * method so don't do it here.
	 */
	public abstract boolean readElement(IConfigurationElement element);

	/**
	 * Read the extension point and parse it.
	 * 
	 * @since 1.0.0
	 */
	public void readRegistry() {
		IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(pluginId, extensionPointId);
		if (point == null)
			return;
		IConfigurationElement[] elements = point.getConfigurationElements();
		for (int i = 0; i < elements.length; i++) {
			internalReadElement(elements[i]);
		}
	}

	/**
	 * Tests to see if it is valid at this point in time to create an executable extension. A valid reason not to would be that the workspace is
	 * shutting donw.
	 * 
	 * @param element
	 * @return <code>true</code> if it is valid point to create an executable extension.
	 * 
	 * @since 1.0.0
	 */
	public static boolean canCreateExecutableExtension(IConfigurationElement element) {
		if (Platform.isRunning() && getSystemBundle().getState() != Bundle.STOPPING)
			return true;
		return false;
	}

	/**
	 * Get the system bundle
	 * 
	 * @return system bundle.
	 * 
	 * @since 1.0.0
	 */
	protected static Bundle getSystemBundle() {
		if (systemBundle == null)
			systemBundle = Platform.getBundle("org.eclipse.osgi"); //$NON-NLS-1$
		return systemBundle;
	}
}

Back to the top