Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8fb7be029ec78436e94b4741bd158722cb9cbd5b (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*******************************************************************************
 * Copyright (c) 2011, 2012 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.runtime.persistence.internal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.eclipse.core.expressions.EvaluationContext;
import org.eclipse.core.expressions.EvaluationResult;
import org.eclipse.core.expressions.Expression;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.tcf.te.runtime.extensions.AbstractExtensionPointManager;
import org.eclipse.tcf.te.runtime.extensions.ExecutableExtensionProxy;
import org.eclipse.tcf.te.runtime.persistence.activator.CoreBundleActivator;


/**
 */
public class PersistenceDelegateBindingExtensionPointManager extends AbstractExtensionPointManager<PersistenceDelegateBinding> {

	/*
	 * Thread save singleton instance creation.
	 */
	private static class LazyInstance {
		public static PersistenceDelegateBindingExtensionPointManager instance = new PersistenceDelegateBindingExtensionPointManager();
	}

	/**
	 * Constructor.
	 */
	PersistenceDelegateBindingExtensionPointManager() {
		super();
	}

	/**
	 * Returns the singleton instance of the extension point manager.
	 */
	public static PersistenceDelegateBindingExtensionPointManager getInstance() {
		return LazyInstance.instance;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.extensions.AbstractExtensionPointManager#getExtensionPointId()
	 */
	@Override
	protected String getExtensionPointId() {
		return "org.eclipse.tcf.te.runtime.persistence.bindings"; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.extensions.AbstractExtensionPointManager#getConfigurationElementName()
	 */
	@Override
	protected String getConfigurationElementName() {
		return "binding"; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.extensions.AbstractExtensionPointManager#doCreateExtensionProxy(org.eclipse.core.runtime.IConfigurationElement)
	 */
	@Override
	protected ExecutableExtensionProxy<PersistenceDelegateBinding> doCreateExtensionProxy(IConfigurationElement element) throws CoreException {
		return new ExecutableExtensionProxy<PersistenceDelegateBinding>(element) {
			/* (non-Javadoc)
			 * @see org.eclipse.tcf.te.runtime.extensions.ExecutableExtensionProxy#newInstance()
			 */
			@Override
			public PersistenceDelegateBinding newInstance() {
				PersistenceDelegateBinding instance = new PersistenceDelegateBinding();
				try {
					instance.setInitializationData(getConfigurationElement(), null, null);
				} catch (CoreException e) {
					IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(),
									e.getLocalizedMessage(), e);
					Platform.getLog(CoreBundleActivator.getContext().getBundle()).log(status);
				}
				return instance;
			}
		};
	}

	/**
	 * Returns the applicable persistence delegate bindings for the given delegate context.
	 *
	 * @param context The delegate context or <code>null</code>.
	 * @return The list of applicable editor page bindings or an empty array.
	 */
	public PersistenceDelegateBinding[] getApplicableBindings(Object context, Object container) {
		List<PersistenceDelegateBinding> applicable = new ArrayList<PersistenceDelegateBinding>();

		for (PersistenceDelegateBinding binding : getBindings()) {
			Expression enablement = binding.getEnablement();

			// The binding is applicable by default if no expression is specified.
			boolean isApplicable = enablement == null;

			if (enablement != null) if (context != null) {
				// Set the default variable to the delegate context.
				EvaluationContext evalContext = new EvaluationContext(null, context);
				evalContext.addVariable("context", context); //$NON-NLS-1$
				if (context instanceof Class) evalContext.addVariable("contextClass", ((Class<?>)context).getName()); //$NON-NLS-1$
				else evalContext.addVariable("contextClass", context.getClass().getName()); //$NON-NLS-1$
				if (container != null) {
					evalContext.addVariable("container", container); //$NON-NLS-1$
					if (container instanceof Class) evalContext.addVariable("containerClass", ((Class<?>)container).getName()); //$NON-NLS-1$
					else evalContext.addVariable("containerClass", container.getClass().getName()); //$NON-NLS-1$
				}
				// Allow plugin activation
				evalContext.setAllowPluginActivation(true);
				// Evaluate the expression
				try {
					isApplicable = enablement.evaluate(evalContext).equals(EvaluationResult.TRUE);
				} catch (CoreException e) {
					IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(),
									e.getLocalizedMessage(), e);
					Platform.getLog(CoreBundleActivator.getContext().getBundle()).log(status);
				}
			}
			else // The enablement is false by definition if no delegate context is given.
				isApplicable = false;

			// Add the binding if applicable
			if (isApplicable) applicable.add(binding);
		}

		// Sort the applicable bindings by priority
		Collections.sort(applicable, new SortByPriority());

		if (applicable.size() > 1) {
			List<PersistenceDelegateBinding> overwritten = new ArrayList<PersistenceDelegateBinding>();
			for (PersistenceDelegateBinding candidate : applicable)
				for (PersistenceDelegateBinding overwriter : applicable) {
					String[] overwrites = overwriter.getOverwrites();
					if (overwrites != null && Arrays.asList(overwrites).contains(candidate.getId()))
						overwritten.add(candidate);
				}

			for (PersistenceDelegateBinding toRemove : overwritten)
				applicable.remove(toRemove);
		}

		return applicable.toArray(new PersistenceDelegateBinding[applicable.size()]);
	}

	/**
	 * Persistence delegate binding sort by priority comparator implementation.
	 */
	/* default */ static class SortByPriority implements Comparator<PersistenceDelegateBinding> {

		/* (non-Javadoc)
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		@Override
		public int compare(PersistenceDelegateBinding o1, PersistenceDelegateBinding o2) {

			if (o1 != null && o2 != null) {
				String p1 = o1.getPriority();
				if (p1 == null || "".equals(p1)) p1 = "normal"; //$NON-NLS-1$ //$NON-NLS-2$
				String p2 = o2.getPriority();
				if (p2 == null || "".equals(p1)) p2 = "normal"; //$NON-NLS-1$ //$NON-NLS-2$

				int i1 = 0;
				if ("lowest".equalsIgnoreCase(p1)) i1 = -3; //$NON-NLS-1$
				if ("lower".equalsIgnoreCase(p1)) i1 = -2; //$NON-NLS-1$
				if ("low".equalsIgnoreCase(p1)) i1 = -1; //$NON-NLS-1$
				if ("high".equalsIgnoreCase(p1)) i1 = 1; //$NON-NLS-1$
				if ("higher".equalsIgnoreCase(p1)) i1 = 2; //$NON-NLS-1$
				if ("highest".equalsIgnoreCase(p1)) i1 = 3; //$NON-NLS-1$

				int i2 = 0;
				if ("lowest".equalsIgnoreCase(p2)) i2 = -3; //$NON-NLS-1$
				if ("lower".equalsIgnoreCase(p2)) i2 = -2; //$NON-NLS-1$
				if ("low".equalsIgnoreCase(p2)) i2 = -1; //$NON-NLS-1$
				if ("high".equalsIgnoreCase(p2)) i2 = 1; //$NON-NLS-1$
				if ("higher".equalsIgnoreCase(p2)) i2 = 2; //$NON-NLS-1$
				if ("highest".equalsIgnoreCase(p2)) i2 = 3; //$NON-NLS-1$

				if (i1 < i2) return 1;
				if (i1 > i2) return -1;
			}

			return 0;
		}
	}

	/**
	 * Returns the list of all contributed persistence delegate bindings.
	 *
	 * @return The list of contributed persistence delegate bindings, or an empty array.
	 */
	public PersistenceDelegateBinding[] getBindings() {
		List<PersistenceDelegateBinding> contributions = new ArrayList<PersistenceDelegateBinding>();
		Collection<ExecutableExtensionProxy<PersistenceDelegateBinding>> persistenceDelegateBindings = getExtensions().values();
		for (ExecutableExtensionProxy<PersistenceDelegateBinding> persistenceDelegateBinding : persistenceDelegateBindings) {
			PersistenceDelegateBinding instance = persistenceDelegateBinding.getInstance();
			if (instance != null && !contributions.contains(instance)) contributions.add(instance);
		}

		return contributions.toArray(new PersistenceDelegateBinding[contributions.size()]);
	}

	/**
	 * Returns the persistence delegate binding identified by its unique id. If no persistence
	 * delegate binding with the specified id is registered, <code>null</code> is returned.
	 *
	 * @param id The unique id of the persistence delegate binding or <code>null</code>
	 *
	 * @return The persistence delegate binding instance or <code>null</code>.
	 */
	public PersistenceDelegateBinding getBinding(String id) {
		PersistenceDelegateBinding contribution = null;
		if (getExtensions().containsKey(id)) {
			ExecutableExtensionProxy<PersistenceDelegateBinding> proxy = getExtensions().get(id);
			// Get the extension instance
			contribution = proxy.getInstance();
		}

		return contribution;
	}
}

Back to the top