Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2bf1b5c92d80ab077cf832e5779c4c637c696f64 (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*******************************************************************************
 * Copyright (c) 2007, 2010 Intel Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * Intel Corporation - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.tcmodification.extension;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;
import org.eclipse.cdt.managedbuilder.internal.core.IRealBuildObjectAssociation;
import org.eclipse.cdt.managedbuilder.internal.tcmodification.IObjectSet;
import org.eclipse.cdt.managedbuilder.internal.tcmodification.Messages;
import org.eclipse.cdt.managedbuilder.internal.tcmodification.ObjectSet;
import org.eclipse.cdt.managedbuilder.internal.tcmodification.ObjectSetList;
import org.eclipse.cdt.managedbuilder.internal.tcmodification.PerTypeMapStorage;
import org.eclipse.cdt.managedbuilder.internal.tcmodification.TcModificationUtil;
import org.eclipse.cdt.managedbuilder.internal.tcmodification.extension.MatchObjectElement.PatternElement;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;

public class RulesManager {
	private static RulesManager fInstance;
	private static final String EXTENSION_POINT_ID = ManagedBuilderCorePlugin.getUniqueIdentifier()
			+ ".tcModificationInfo"; //$NON-NLS-1$

	private ConflictDefinition[] fConflictDefinitions;

	private Map<MatchObjectElement, IObjectSet> fMatchObjectMap = new HashMap<MatchObjectElement, IObjectSet>();
	private PerTypeMapStorage<IRealBuildObjectAssociation, Set<IRealBuildObjectAssociation>> fObjToChildSuperClassMap;
	private StarterJob fStarter;
	private boolean fIsStartInited;

	private class StarterJob extends Job {

		private StarterJob() {
			super(Messages.getString("RulesManager.1")); //$NON-NLS-1$
			setSystem(true);
		}

		@Override
		protected IStatus run(IProgressMonitor monitor) {
			try {
				loadExtensions();
			} finally {
				fStarter = null;
			}
			return Status.OK_STATUS;
		}

	}

	private RulesManager() {
	}

	public static RulesManager getInstance() {
		if (fInstance == null)
			fInstance = getInstanceSynch();
		return fInstance;
	}

	public synchronized static RulesManager getInstanceSynch() {
		if (fInstance == null)
			fInstance = new RulesManager();
		return fInstance;
	}

	public void start() {
		if (fIsStartInited)
			return;

		synchronized (this) {
			if (fIsStartInited)
				return;

			fIsStartInited = true;
		}

		fStarter = new StarterJob();
		fStarter.schedule();
	}

	private void loadExtensions() {
		IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(EXTENSION_POINT_ID);
		if (extensionPoint == null) {
			fConflictDefinitions = new ConflictDefinition[0];
		} else {
			IExtension[] extensions = extensionPoint.getExtensions();
			List<ConflictDefinition> conflictDefs = new ArrayList<ConflictDefinition>();
			for (int i = 0; i < extensions.length; ++i) {
				IExtension extension = extensions[i];
				IConfigurationElement[] elements = extension.getConfigurationElements();
				for (int k = 0; k < elements.length; k++) {
					IConfigurationElement el = elements[k];
					String elName = el.getName();
					if (ConflictDefinitionElement.ELEMENT_NAME.equals(elName)) {
						try {
							ConflictDefinitionElement cde = new ConflictDefinitionElement(el);
							ConflictDefinition cd = resolve(cde);
							if (cd != null)
								conflictDefs.add(cd);
						} catch (IllegalArgumentException e) {
							ManagedBuilderCorePlugin.log(e);
						}
					}
				}
			}

			fConflictDefinitions = conflictDefs.toArray(new ConflictDefinition[conflictDefs.size()]);
		}
	}

	private ConflictDefinition resolve(ConflictDefinitionElement el) throws IllegalArgumentException {
		MatchObjectElement mos[] = el.getMatchObjects();
		if (mos.length != 2)
			throw new IllegalArgumentException();

		IObjectSet oss[] = new IObjectSet[mos.length];

		for (int i = 0; i < mos.length; i++) {
			oss[i] = resolve(mos[i]);
			if (oss[i].getNumObjects() == 0) {
				//no conflicts
				return null;
			}
		}

		return new ConflictDefinition(new ObjectSetList(oss));
	}

	private IObjectSet resolve(MatchObjectElement el) {
		IObjectSet oSet = fMatchObjectMap.get(el);
		if (oSet == null) {
			int type = el.getObjectType();
			PatternElement[] patterns = el.getPatterns();
			HashSet<IRealBuildObjectAssociation> objectsSet = new HashSet<IRealBuildObjectAssociation>();
			for (int i = 0; i < patterns.length; i++) {
				PatternElement pattern = patterns[i];
				processPattern(type, pattern, objectsSet);
			}
			oSet = new ObjectSet(type, objectsSet);
			fMatchObjectMap.put(el, oSet);
		}
		return oSet;
	}

	private IRealBuildObjectAssociation[] getObjectsForId(int objType, String id, int idType) {
		if (idType == PatternElement.TYPE_ID_EXACT_MATCH) {
			IRealBuildObjectAssociation obj = TcModificationUtil.getObjectById(objType, id);
			if (obj != null)
				return new IRealBuildObjectAssociation[] { obj };
			return new IRealBuildObjectAssociation[0];
		}

		IRealBuildObjectAssociation[] allObjs = TcModificationUtil.getExtensionObjects(objType);
		Pattern pattern = Pattern.compile(id);
		List<IRealBuildObjectAssociation> list = new ArrayList<IRealBuildObjectAssociation>();

		for (int i = 0; i < allObjs.length; i++) {
			if (pattern.matcher(allObjs[i].getId()).matches())
				list.add(allObjs[i]);
		}

		return list.toArray(new IRealBuildObjectAssociation[list.size()]);
	}

	private Set<IRealBuildObjectAssociation> processPattern(int objType, PatternElement el,
			Set<IRealBuildObjectAssociation> set) {
		if (set == null)
			set = new HashSet<IRealBuildObjectAssociation>();

		String ids[] = el.getIds();
		if (el.getSearchType() == PatternElement.TYPE_SEARCH_EXTENSION_OBJECT) {
			for (int i = 0; i < ids.length; i++) {
				IRealBuildObjectAssociation objs[] = getObjectsForId(objType, ids[i], el.getIdType());
				for (int k = 0; k < objs.length; k++) {
					set.add(objs[k].getRealBuildObject());
				}
			}
		} else if (el.getSearchType() == PatternElement.TYPE_SEARCH_ALL_EXTENSION_SUPERCLASSES) {
			IRealBuildObjectAssociation[] allReal = TcModificationUtil.getRealObjects(objType);
			for (int i = 0; i < ids.length; i++) {
				IRealBuildObjectAssociation[] objs = getObjectsForId(objType, ids[i], el.getIdType());
				for (int k = 0; k < objs.length; k++) {
					IRealBuildObjectAssociation obj = objs[k];

					set.add(obj.getRealBuildObject());

					Set<IRealBuildObjectAssociation> childRealSet = getChildSuperClassRealSet(obj, allReal);

					set.addAll(childRealSet);
					//					for(int k = 0; k < allReal.length; k++){
					//						IRealBuildObjectAssociation otherReal = allReal[k];
					//						if(otherReal == obj || set.contains(otherReal))
					//							continue;
					//
					//						if("tcm.derive.tc1".equals(otherReal.getId())){
					//							int f = 0; f++;
					//						}
					//						IRealBuildObjectAssociation[] identics = otherReal.getIdenticBuildObjects();
					//						for(int j = 0; j < identics.length; j++){
					//							IRealBuildObjectAssociation identic = identics[j];
					//							for(identic = identic.getSuperClassObject(); identic != null; identic = identic.getSuperClassObject()){
					//								if(identic == obj){
					//									set.add(identic.getRealBuildObject());
					//								}
					//							}
					//						}
					//					}
				}
			}
		}

		return set;
	}

	private Set<IRealBuildObjectAssociation> getChildSuperClassRealSet(IRealBuildObjectAssociation obj,
			IRealBuildObjectAssociation[] all) {
		if (fObjToChildSuperClassMap == null)
			fObjToChildSuperClassMap = new PerTypeMapStorage<IRealBuildObjectAssociation, Set<IRealBuildObjectAssociation>>();

		if (all == null)
			all = TcModificationUtil.getExtensionObjects(obj.getType());

		Map<IRealBuildObjectAssociation, Set<IRealBuildObjectAssociation>> map = fObjToChildSuperClassMap
				.getMap(obj.getType(), true);
		Set<IRealBuildObjectAssociation> set = map.get(obj);
		if (set == null) {
			set = createChildSuperClassRealSet(obj, all, null);
			map.put(obj, set);
		}

		return set;
	}

	private static Set<IRealBuildObjectAssociation> createChildSuperClassRealSet(IRealBuildObjectAssociation obj,
			IRealBuildObjectAssociation[] all, Set<IRealBuildObjectAssociation> set) {
		if (set == null)
			set = new HashSet<IRealBuildObjectAssociation>();

		if (all == null)
			all = TcModificationUtil.getExtensionObjects(obj.getType());

		for (int i = 0; i < all.length; i++) {
			IRealBuildObjectAssociation cur = all[i];
			for (IRealBuildObjectAssociation el = cur.getSuperClassObject(); el != null; el = el
					.getSuperClassObject()) {
				if (el == obj) {
					IRealBuildObjectAssociation realQuickTest = null;
					for (IRealBuildObjectAssociation found = cur; found != obj; found = found.getSuperClassObject()) {
						IRealBuildObjectAssociation real = found.getRealBuildObject();
						if (real != realQuickTest) {
							set.add(real);
							realQuickTest = real;
						}
					}
				}
			}
		}

		return set;
	}

	public ObjectSetListBasedDefinition[] getRules(int ruleType) {
		checkInitialization();
		return fConflictDefinitions.clone();
	}

	private void checkInitialization() {
		if (!fIsStartInited)
			throw new IllegalStateException();

		StarterJob starter = fStarter;

		if (starter != null) {
			try {
				starter.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

Back to the top