Debian Codegraph @

bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/internal/p2/engine/Profile.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2007, 2023 IBM Corporation and others.
3 *
4 * This
5 * program and the accompanying materials are made available under the terms of
6 * the Eclipse Public License 2.0 which accompanies this distribution, and is
7 * available at
8 * https://www.eclipse.org/legal/epl-2.0/
9 *
10 * SPDX-License-Identifier: EPL-2.0
11 *
12 * Contributors:
13 * IBM Corporation - initial API and implementation
14 * Cloudsmith Inc. - query indexes
15 *
16 ******************************************************************************/
17 package org.eclipse.equinox.internal.p2.engine;
18
19 import java.util.*;
20 import java.util.Map.Entry;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.NullProgressMonitor;
23 import org.eclipse.equinox.internal.p2.core.helpers.OrderedProperties;
24 import org.eclipse.equinox.internal.p2.metadata.*;
25 import org.eclipse.equinox.internal.p2.metadata.index.*;
26 import org.eclipse.equinox.p2.core.IProvisioningAgent;
27 import org.eclipse.equinox.p2.engine.IProfile;
28 import org.eclipse.equinox.p2.metadata.IInstallableUnit;
29 import org.eclipse.equinox.p2.metadata.KeyWithLocale;
30 import org.eclipse.equinox.p2.metadata.expression.IEvaluationContext;
31 import org.eclipse.equinox.p2.metadata.expression.IExpression;
32 import org.eclipse.equinox.p2.metadata.index.IIndex;
33 import org.eclipse.equinox.p2.query.IQuery;
34 import org.eclipse.equinox.p2.query.IQueryResult;
35 import org.eclipse.osgi.util.NLS;
36
37 public class Profile extends IndexProvider<IInstallableUnit> implements IProfile {
38
39 /**
40 * An index that limits the candidates to those units that has profile properties
41 */
42 class ProfilePropertyIndex implements IIndex<IInstallableUnit> {
43 @Override
44 public Iterator<IInstallableUnit> getCandidates(IEvaluationContext ctx, IExpression variable, IExpression booleanExpr) {
45 return iuProperties.keySet().iterator();
46 }
47 }
48
49 private final IProvisioningAgent agent;
50 //Internal id of the profile
51 private final String profileId;
52
53 private Profile parentProfile;
54
55 private IIndex<IInstallableUnit> idIndex;
56
57 private IIndex<IInstallableUnit> propertiesIndex;
58
59 private IIndex<IInstallableUnit> capabilityIndex;
60
61 private TranslationSupport translationSupport;
62
63 /**
64 * A collection of child profiles.
65 */
66 private List<String> subProfileIds; // child profile ids
67
68 /**
69 * This storage is to be used by the touchpoints to store data.
70 */
71 private OrderedProperties storage = new OrderedProperties();
72
73 private IUMap ius = new IUMap();
74 final Map<IInstallableUnit, OrderedProperties> iuProperties = new HashMap<>();
75 private boolean changed = false;
76
77 private long timestamp;
78 private ISurrogateProfileHandler surrogateProfileHandler;
79
80 public Profile(IProvisioningAgent agent, String profileId, Profile parent, Map<String, String> properties) {
81 this.agent = agent;
82 if (profileId == null || profileId.length() == 0) {
83 throw new IllegalArgumentException(NLS.bind(Messages.Profile_Null_Profile_Id, null));
84 }
85 this.profileId = profileId;
86 setParent(parent);
87 if (properties != null)
88 storage.putAll(properties);
89
90 }
91
92 @Override
93 public String getProfileId() {
94 return profileId;
95 }
96
97 public IProfile getParentProfile() {
98 return parentProfile;
99 }
100
101 public void setParent(Profile profile) {
102 if (profile == parentProfile)
103 return;
104
105 if (parentProfile != null)
106 parentProfile.removeSubProfile(profileId);
107
108 parentProfile = profile;
109 if (parentProfile != null)
110 parentProfile.addSubProfile(profileId);
111 }
112
113 /*
114 * A profile is a root profile if it is not a sub-profile
115 * of another profile.
116 */
117 public boolean isRootProfile() {
118 return parentProfile == null;
119 }
120
121 public void addSubProfile(String subProfileId) throws IllegalArgumentException {
122 if (subProfileIds == null)
123 subProfileIds = new ArrayList<>();
124
125 if (!subProfileIds.contains(subProfileId))
126 subProfileIds.add(subProfileId);
127
128 // if (!subProfileIds.add(subProfileId))
129 // throw new IllegalArgumentException(NLS.bind(Messages.Profile_Duplicate_Child_Profile_Id, new String[] {subProfileId, this.getProfileId()}));
130 }
131
132 public void removeSubProfile(String subProfileId) throws IllegalArgumentException {
133 if (subProfileIds != null)
134 subProfileIds.remove(subProfileId);
135 }
136
137 public boolean hasSubProfiles() {
138 return subProfileIds != null && !subProfileIds.isEmpty();
139 }
140
141 public List<String> getSubProfileIds() {
142 if (subProfileIds == null)
143 return Collections.emptyList();
144 return Collections.unmodifiableList(subProfileIds);
145 }
146
147 @Override
148 public String getProperty(String key) {
149 String value = getLocalProperty(key);
150 if (value == null && parentProfile != null) {
151 value = parentProfile.getProperty(key);
152 }
153 return value;
154 }
155
156 public String getLocalProperty(String key) {
157 return storage.getProperty(key);
158 }
159
160 /**
161 * Associate the given value with the given key
162 * in the local storage of this profile.
163 */
164 public void setProperty(String key, String value) {
165 storage.setProperty(key, value);
166 changed = true;
167 }
168
169 public void removeProperty(String key) {
170 storage.remove(key);
171 changed = true;
172 }
173
174 @Override
175 public synchronized IIndex<IInstallableUnit> getIndex(String memberName) {
176 if (InstallableUnit.MEMBER_ID.equals(memberName)) {
177 if (idIndex == null)
178 idIndex = new IdIndex(ius);
179 return idIndex;
180 }
181
182 if (InstallableUnit.MEMBER_PROVIDED_CAPABILITIES.equals(memberName)) {
183 if (capabilityIndex == null)
184 capabilityIndex = new CapabilityIndex(ius.iterator());
185 return capabilityIndex;
186 }
187
188 if (InstallableUnit.MEMBER_PROFILE_PROPERTIES.equals(memberName)) {
189 if (propertiesIndex == null)
190 propertiesIndex = new ProfilePropertyIndex();
191 return propertiesIndex;
192 }
193 return null;
194 }
195
196 @Override
197 public Iterator<IInstallableUnit> everything() {
198 return ius.iterator();
199 }
200
201 @Override
202 public boolean contains(IInstallableUnit element) {
203 return ius.contains(element);
204 }
205
206 @Override
207 public Object getManagedProperty(Object client, String memberName, Object key) {
208 if (!(client instanceof IInstallableUnit))
209 return null;
210 IInstallableUnit iu = (IInstallableUnit) client;
211 if (InstallableUnit.MEMBER_PROFILE_PROPERTIES.equals(memberName) && key instanceof String)
212 return getInstallableUnitProperty(iu, (String) key);
213 if (InstallableUnit.MEMBER_TRANSLATED_PROPERTIES.equals(memberName)) {
214 synchronized (this) {
215 if (translationSupport == null)
216 translationSupport = new TranslationSupport(this);
217 return key instanceof KeyWithLocale ? translationSupport.getIUProperty(iu, (KeyWithLocale) key) : translationSupport.getIUProperty(iu, key.toString());
218 }
219 }
220 return null;
221 }
222
223 @Override
224 public IQueryResult<IInstallableUnit> available(IQuery<IInstallableUnit> query, IProgressMonitor monitor) {
225 if (surrogateProfileHandler != null)
226 return surrogateProfileHandler.queryProfile(this, query, monitor);
227 return query(query, new NullProgressMonitor());
228 }
229
230 @Override
231 public String getInstallableUnitProperty(IInstallableUnit iu, String key) {
232 OrderedProperties properties = iuProperties.get(iu);
233 if (properties == null)
234 return null;
235
236 return properties.getProperty(key);
237 }
238
239 public String setInstallableUnitProperty(IInstallableUnit iu, String key, String value) {
240 // String iuKey = createIUKey(iu);
241 OrderedProperties properties = iuProperties.get(iu);
242 if (properties == null) {
243 properties = new OrderedProperties();
244 iuProperties.put(iu, properties);
245 }
246
247 changed = true;
248 return (String) properties.setProperty(key, value);
249 }
250
251 public String removeInstallableUnitProperty(IInstallableUnit iu, String key) {
252 // String iuKey = createIUKey(iu);
253 OrderedProperties properties = iuProperties.get(iu);
254 if (properties == null)
255 return null;
256
257 String oldValue = properties.remove(key);
258 if (properties.isEmpty())
259 iuProperties.remove(iu);
260
261 changed = true;
262 return oldValue;
263 }
264
265 // private static String createIUKey(IInstallableUnit iu) {
266 // return iu.getId() + "_" + iu.getVersion().toString(); //$NON-NLS-1$
267 // }
268
269 public Map<String, String> getLocalProperties() {
270 return OrderedProperties.unmodifiableProperties(storage);
271 }
272
273 @Override
274 public Map<String, String> getProperties() {
275 if (parentProfile == null)
276 return getLocalProperties();
277
278 Map<String, String> properties = new HashMap<>(parentProfile.getProperties());
279 properties.putAll(storage);
280 return OrderedProperties.unmodifiableProperties(properties);
281 }
282
283 @Override
284 public IProvisioningAgent getProvisioningAgent() {
285 return agent;
286 }
287
288 /**
289 * Add all the properties in the map to the local properties
290 * of the profile.
291 */
292 public void addProperties(Map<String, String> properties) {
293 storage.putAll(properties);
294 changed = true;
295 }
296
297 public void addInstallableUnit(IInstallableUnit iu) {
298 iu = iu.unresolved();
299 if (ius.contains(iu))
300 return;
301
302 ius.add(iu);
303 changed = true;
304 }
305
306 public void removeInstallableUnit(IInstallableUnit iu) {
307 iu = iu.unresolved();
308 ius.remove(iu);
309 changed = true;
310 }
311
312 @Override
313 public Map<String, String> getInstallableUnitProperties(IInstallableUnit iu) {
314 OrderedProperties properties = iuProperties.get(iu);
315 if (properties == null)
316 properties = new OrderedProperties();
317
318 return OrderedProperties.unmodifiableProperties(properties);
319 }
320
321 public void clearLocalProperties() {
322 storage.clear();
323 changed = true;
324 }
325
326 public boolean isChanged() {
327 return changed;
328 }
329
330 public void setChanged(boolean isChanged) {
331 changed = isChanged;
332 }
333
334 public void clearInstallableUnits() {
335 ius.clear();
336 iuProperties.clear();
337 changed = true;
338 }
339
340 public Profile snapshot() {
341 Profile parentSnapshot = null;
342 if (parentProfile != null)
343 parentSnapshot = parentProfile.snapshot();
344
345 Profile snapshot = new Profile(agent, profileId, parentSnapshot, storage);
346 if (surrogateProfileHandler != null)
347 snapshot.setSurrogateProfileHandler(surrogateProfileHandler);
348 snapshot.setTimestamp(timestamp);
349
350 if (subProfileIds != null) {
351 for (String subProfileId : subProfileIds) {
352 snapshot.addSubProfile(subProfileId);
353 }
354 }
355
356 for (Iterator<IInstallableUnit> iter = ius.iterator(); iter.hasNext();) {
357 IInstallableUnit iu = iter.next();
358 snapshot.addInstallableUnit(iu);
359 Map<String, String> properties = getInstallableUnitProperties(iu);
360 if (properties != null)
361 snapshot.addInstallableUnitProperties(iu, properties);
362 }
363 snapshot.setChanged(false);
364 return snapshot;
365 }
366
367 public void addInstallableUnitProperties(IInstallableUnit iu, Map<String, String> properties) {
368 for (Entry<String, String> entry : properties.entrySet()) {
369 setInstallableUnitProperty(iu, entry.getKey(), entry.getValue());
370 }
371 }
372
373 public void clearInstallableUnitProperties(IInstallableUnit iu) {
374 iuProperties.remove(iu);
375 changed = true;
376 }
377
378 public void clearOrphanedInstallableUnitProperties() {
379 Set<IInstallableUnit> keys = iuProperties.keySet();
380 // Set orphans = new HashSet();
381 Collection<IInstallableUnit> toRemove = new ArrayList<>();
382 for (IInstallableUnit iu : keys) {
383 if (!ius.contains(iu))
384 toRemove.add(iu);
385 }
386
387 for (IInstallableUnit iu : toRemove) {
388 iuProperties.remove(iu);
389 }
390 // List iuKeys = new ArrayList();
391 // for (Iterator it = ius.iterator(); it.hasNext();)
392 // iuKeys.add((IInstallableUnit) it.next());
393 //
394 // iuProperties.keySet().retainAll(iuKeys);
395 }
396
397 @Override
398 public long getTimestamp() {
399 return timestamp;
400 }
401
402 public void setTimestamp(long millis) {
403 timestamp = millis;
404 }
405
406 public void setSurrogateProfileHandler(ISurrogateProfileHandler surrogateProfileHandler) {
407 this.surrogateProfileHandler = surrogateProfileHandler;
408 }
409
410 /**
411 * Prints a string representation for debugging purposes only.
412 */
413 @Override
414 public String toString() {
415 return "Profile(" + getProfileId() + ", timestamp: " + getTimestamp() + ')'; //$NON-NLS-1$ //$NON-NLS-2$
416 }
417 }