Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2a0756807947700f250d777a8a1c8377b0db8c59 (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
/*******************************************************************************
 *  Copyright (c) 2010  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.core.internal;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jpt.core.JptCorePlugin;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.Bundle;

/**
 * Utilities for extension point management, validation, etc.
 */
public class XPointUtil {
	
	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);
		if (clazz == null) {
			return null;
		}
		else {
			return instantiate(pluginId, extensionPoint, clazz);
		}
    }
	
	public static <T> T instantiate(String pluginId, String extensionPoint, Class<T> clazz) {
		
		try {
			return clazz.newInstance();
		}
		catch (Exception e) {
			logFailedInstantiation(pluginId, extensionPoint, clazz.getName());
			return null;
		}
	}
	
	public 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 e) {
			logFailedClassLoad(pluginId, extensionPoint, className);
			return null;
		}
		
		if (interfaze != null && ! interfaze.isAssignableFrom(clazz)) {
			logFailedInterfaceAssignment(pluginId, extensionPoint, className, interfaze.getName());
			return null;
		}
		
		return (Class<T>) clazz;
    }
	
	public static void logDuplicateExtension(String extensionPoint, String nodeName, String value) {
		
		log(JptCoreMessages.REGISTRY_DUPLICATE, extensionPoint, nodeName, value);
	}
	
	public static void logMissingAttribute(
			IConfigurationElement configElement, String attributeName) {
		
		log(JptCoreMessages.REGISTRY_MISSING_ATTRIBUTE,
				attributeName,
				configElement.getName(),
				configElement.getDeclaringExtension().getExtensionPointUniqueIdentifier(),
				configElement.getContributor().getName());
    }
	
	public static void logInvalidValue(
			IConfigurationElement configElement, String nodeName, String invalidValue) {
		
		log(JptCoreMessages.REGISTRY_INVALID_VALUE,
				invalidValue,
				nodeName,
				configElement.getDeclaringExtension().getExtensionPointUniqueIdentifier(),
				configElement.getContributor().getName());
	}
	
	private static void logFailedClassLoad(String pluginId, String extensionPoint, String className) {
		
		log(JptCoreMessages.REGISTRY_FAILED_CLASS_LOAD,
				className,
				extensionPoint,
				pluginId);
	}
	
	private static void logFailedInterfaceAssignment(
			String pluginId, String extensionPoint, String className, String interfaceName) {
		
		log(JptCoreMessages.REGISTRY_FAILED_INTERFACE_ASSIGNMENT,
				className,
				extensionPoint,
				pluginId,
				interfaceName);
	}
	
	private static void logFailedInstantiation(String pluginId, String extensionPoint, String className) {
		
		log(JptCoreMessages.REGISTRY_FAILED_INSTANTIATION,
				className,
				extensionPoint,
				pluginId);
	}
	
	public static void log(String msg, String... bindings) {
		JptCorePlugin.log(NLS.bind(msg, bindings));
	}
	
	public static void log(Exception e) {
		JptCorePlugin.log(e);
	}
	
	
	public static final class XPointException
			extends Exception {
		
		private static final long serialVersionUID = 1L;
	}
}

Back to the top