Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0674bd8a8e3bd832b3b1ea7f9fc8a96f97605d23 (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
/*******************************************************************************
 * Copyright (c) 2009-2012 Mia-Software.
 * 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:
 *    Gregoire Dupe (Mia-Software)
 *    Nicolas Bros (Mia-Software)
 *    Nicolas Bros (Mia-Software) - Bug 375054 - Add validation warning for overlay on EClass
 *******************************************************************************/
package org.eclipse.emf.facet.common.sdk.core.internal;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.facet.common.sdk.core.internal.exported.CommonConstants;
import org.eclipse.emf.facet.common.sdk.core.internal.exported.IEmfFacetProjectBuilder;
import org.eclipse.emf.facet.util.core.Logger;

/**
 * A builder for EMF Facet projects, that delegates to children builders provided by the extension point
 * "builderRegistration".
 * 
 * @author Gregoire Dupe (Mia-Software)
 */
public class EmfFacetProjectBuilder extends IncrementalProjectBuilder {

	private static ArrayList<BuilderDescriptor> emffacetBuilders = null;

	public EmfFacetProjectBuilder() {
		super();
		if (EmfFacetProjectBuilder.emffacetBuilders == null) {
			EmfFacetProjectBuilder.emffacetBuilders = new ArrayList<BuilderDescriptor>();
			synchronized (EmfFacetProjectBuilder.emffacetBuilders) {
				final IConfigurationElement[] configs = Platform.getExtensionRegistry()
						.getConfigurationElementsFor(CommonConstants.BUILDER_EXTENSION_POINT_ID);
				for (final IConfigurationElement config : configs) {
					try {
						final String id = config.getDeclaringExtension().getUniqueIdentifier();
						final IEmfFacetProjectBuilder builderInst = (IEmfFacetProjectBuilder) config
								.createExecutableExtension("class"); //$NON-NLS-1$
						final BuilderDescriptor builderDescriptor = new BuilderDescriptor(
								builderInst, id);
						EmfFacetProjectBuilder.emffacetBuilders.add(builderDescriptor);
						for (final IConfigurationElement depends : config.getChildren("depends")) { //$NON-NLS-1$
							builderDescriptor.getDependsOn().add(depends.getAttribute("builder")); //$NON-NLS-1$
						}
					} catch (final Exception e) {
						Logger.logError(e, Activator.getDefault());
					}
				}
				EmfFacetProjectBuilder.emffacetBuilders = sortBuilders(EmfFacetProjectBuilder.emffacetBuilders);
			}
		}
		validateBuilderDependencies();
	}

	/**
	 * Sort builders by dependencies : first those that depend on nothing, then those that depend on those already in
	 * the list, etc.
	 */
	private static ArrayList<BuilderDescriptor> sortBuilders(final ArrayList<BuilderDescriptor> builders) {
		final ArrayList<BuilderDescriptor> result = new ArrayList<EmfFacetProjectBuilder.BuilderDescriptor>();
		final List<BuilderDescriptor> remaining = new LinkedList<EmfFacetProjectBuilder.BuilderDescriptor>();
		remaining.addAll(builders);

		while (!remaining.isEmpty()) {
			boolean stuck = true;
			final ListIterator<BuilderDescriptor> listIterator = remaining.listIterator();
			while (listIterator.hasNext()) {
				final BuilderDescriptor builder = listIterator.next();
				// if it depends only on builders that are already in the list
				// before it, then add it here
				if (dependendsOnlyOn(builder, result)) {
					result.add(builder);
					listIterator.remove();
					stuck = false;
				}
			}
			if (stuck) {
				throw new RuntimeException("Cannot order builders due to incoherent dependencies"); //$NON-NLS-1$
			}
		}

		return result;
	}

	/** Whether the given builder only depends on those in the list. */
	private static boolean dependendsOnlyOn(final BuilderDescriptor builder, final ArrayList<BuilderDescriptor> list) {
		final List<String> dependsOn = builder.getDependsOn();
		for (final String dep : dependsOn) {
			boolean found = false;
			for (final BuilderDescriptor prev : list) {
				if (prev.getId().equals(dep)) {
					found = true;
					break;
				}
			}
			if (!found) {
				return false;
			}
		}
		return true;
	}

	private static void validateBuilderDependencies() {
		final List<String> idList = new ArrayList<String>();
		for (final BuilderDescriptor descriptor : EmfFacetProjectBuilder.emffacetBuilders) {
			idList.add(descriptor.getId());
		}
		for (final BuilderDescriptor descriptor : EmfFacetProjectBuilder.emffacetBuilders) {
			for (final String dependsOn : descriptor.getDependsOn()) {
				if (!idList.contains(dependsOn)) {
					Logger.logWarning("The builder " + descriptor.getId() + " depends on " //$NON-NLS-1$//$NON-NLS-2$
							+ dependsOn + " which does not exist.", Activator //$NON-NLS-1$
							.getDefault());
				}
			}
		}

	}

	@Override
	protected void clean(final IProgressMonitor monitor) throws CoreException {
		synchronized (EmfFacetProjectBuilder.emffacetBuilders) {
			final Iterator<BuilderDescriptor> builders = EmfFacetProjectBuilder.emffacetBuilders
					.iterator();
			while (builders.hasNext()) {
				final BuilderDescriptor builder = builders.next();
				builder.getBuilderInst().clean(this, monitor);
			}
		}
	}

	@Override
	protected IProject[] build(final int kind, @SuppressWarnings("rawtypes") final Map args, final IProgressMonitor monitor)
			throws CoreException {
		synchronized (EmfFacetProjectBuilder.emffacetBuilders) {
			final Iterator<BuilderDescriptor> builders = EmfFacetProjectBuilder.emffacetBuilders
					.iterator();
			while (builders.hasNext()) {
				final BuilderDescriptor builder = builders.next();
				builder.getBuilderInst().build(this, kind, args, monitor);
			}
		}
		return null;
	}

	/**
	 * This class is used to hold the builder id, the builder instance and the builder dependencies in a single
	 * structure.
	 */
	private class BuilderDescriptor {
		private final IEmfFacetProjectBuilder builderInst;

		private final List<String> dependsOn = new ArrayList<String>();

		private final String id;

		/**
		 * @param builderInst
		 *            an instance of the described builder
		 * @param id
		 *            the id of the described builder
		 */
		public BuilderDescriptor(final IEmfFacetProjectBuilder builderInst, final String id) {
			this.builderInst = builderInst;
			this.id = id;
		}

		public String getId() {
			return this.id;
		}

		/** @return the list of dependencies on other EmfFacet builders. */
		public List<String> getDependsOn() {
			return this.dependsOn;
		}

		/** @return the instance of the described builder */
		public IEmfFacetProjectBuilder getBuilderInst() {
			return this.builderInst;
		}

		@Override
		public String toString() {
			return this.id + "(" + this.builderInst.getClass().getName() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

}

Back to the top