Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5942fb531f0dbe1f01054ce0eff89751b8b66f75 (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
/*******************************************************************************
 * Copyright (c) 2010, 2011 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.core.internal.utility;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jpt.common.core.JptCommonCorePlugin;
import org.eclipse.jpt.common.core.internal.JptCommonCoreMessages;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.Bundle;

/**
 * Utilities for extension point management, validation, etc.
 */
public class XPointTools {
	
	public static String findRequiredAttribute(
			IConfigurationElement configElement, String attributeName)
			throws XPointException {
		
		String val = configElement.getAttribute(attributeName);
		if (val == null) {
			logMissingAttribute(configElement, attributeName);
			throw new XPointException();
		}
		return val;
	}
	
	public static <T> T instantiate(String pluginId, String extensionPoint, String className, Class<T> interfaze) {
		Class<T> clazz = loadClass(pluginId, extensionPoint, className, interfaze);
		return (clazz == null) ? null : instantiate(pluginId, extensionPoint, clazz);
    }
	
	/**
	 * Instantiate the specified class.
	 */
	public static <T> T instantiate(String pluginId, String extensionPoint, Class<T> clazz) {
		try {
			return clazz.newInstance();
		} catch (Exception ex) {
			logFailedInstantiation(ex, pluginId, extensionPoint, clazz.getName());
			return null;
		}
	}

	/**	
	 * Load the specified class and cast it to the specified interface.
	 */
	private static <T> Class<T> loadClass(String pluginId, String extensionPoint, String className, Class<T> interfaze) {
		Bundle bundle = Platform.getBundle(pluginId);

		Class<?> clazz;
		try {
			clazz = bundle.loadClass(className);
		} catch (Exception ex) {
			logFailedClassLoad(ex, pluginId, extensionPoint, className);
			return null;
		}
		
		if (interfaze.isAssignableFrom(clazz)) {
			@SuppressWarnings("unchecked")
			Class<T> clazzT = (Class<T>) clazz;
			return clazzT;
		}
		
		logFailedInterfaceAssignment(pluginId, extensionPoint, className, interfaze.getName());
		return null;
    }
	
	public static void logDuplicateExtension(String extensionPoint, String nodeName, String value) {
		log(JptCommonCoreMessages.REGISTRY_DUPLICATE, extensionPoint, nodeName, value);
	}
	
	public static void logMissingAttribute(IConfigurationElement configElement, String attributeName) {
		log(JptCommonCoreMessages.REGISTRY_MISSING_ATTRIBUTE,
				attributeName,
				configElement.getName(),
				configElement.getDeclaringExtension().getExtensionPointUniqueIdentifier(),
				configElement.getContributor().getName());
    }
	
	public static void logInvalidValue(
			IConfigurationElement configElement, String nodeName, String invalidValue) {
		
		log(JptCommonCoreMessages.REGISTRY_INVALID_VALUE,
				invalidValue,
				nodeName,
				configElement.getDeclaringExtension().getExtensionPointUniqueIdentifier(),
				configElement.getContributor().getName());
	}
	
	private static void logFailedClassLoad(Exception ex, String pluginId, String extensionPoint, String className) {
		log(ex, JptCommonCoreMessages.REGISTRY_FAILED_CLASS_LOAD,
				className,
				extensionPoint,
				pluginId);
	}
	
	private static void logFailedInterfaceAssignment(
			String pluginId, String extensionPoint, String className, String interfaceName) {
		
		log(JptCommonCoreMessages.REGISTRY_FAILED_INTERFACE_ASSIGNMENT,
				className,
				extensionPoint,
				pluginId,
				interfaceName);
	}
	
	private static void logFailedInstantiation(Exception ex, String pluginId, String extensionPoint, String className) {
		log(ex, JptCommonCoreMessages.REGISTRY_FAILED_INSTANTIATION,
				className,
				extensionPoint,
				pluginId);
	}
	
	public static void log(String msg, String... bindings) {
		JptCommonCorePlugin.log(NLS.bind(msg, bindings));
	}
	
	public static void log(Throwable ex, String msg, String... bindings) {
		JptCommonCorePlugin.log(NLS.bind(msg, bindings), ex);
	}
	
	public static void log(Throwable ex) {
		JptCommonCorePlugin.log(ex);
	}
	
	
	public static final class XPointException extends Exception {
		private static final long serialVersionUID = 1L;
	}
}

Back to the top