Debian Codegraph @

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

raw
1 /*******************************************************************************
2 * Copyright (c) 2007, 2017 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 * Ericsson AB - Bug 400011 - [shared] Cleanup the SurrogateProfileHandler code
14 * Red Hat, Inc. - fragments support added., Bug 460967
15 *******************************************************************************/
16 package org.eclipse.equinox.internal.p2.engine;
17
18 import java.io.File;
19 import java.lang.ref.SoftReference;
20 import java.net.*;
21 import java.util.*;
22 import org.eclipse.core.runtime.*;
23 import org.eclipse.equinox.internal.p2.core.helpers.*;
24 import org.eclipse.equinox.p2.core.IProvisioningAgent;
25 import org.eclipse.equinox.p2.core.ProvisionException;
26 import org.eclipse.equinox.p2.engine.IProfile;
27 import org.eclipse.equinox.p2.metadata.*;
28 import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription;
29 import org.eclipse.equinox.p2.metadata.expression.ExpressionUtil;
30 import org.eclipse.equinox.p2.metadata.expression.IMatchExpression;
31 import org.eclipse.equinox.p2.query.*;
32 import org.eclipse.equinox.p2.repository.IRepositoryManager;
33 import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
34 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
35 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
36 import org.eclipse.osgi.service.datalocation.Location;
37 import org.eclipse.osgi.util.NLS;
38
39 public class SurrogateProfileHandler implements ISurrogateProfileHandler {
40
41 private static final String NATIVE_TOUCHPOINT_TYPE = "org.eclipse.equinox.p2.native"; //$NON-NLS-1$
42 private static final String P2_ENGINE_DIR = "p2/" + EngineActivator.ID + "/"; //$NON-NLS-1$//$NON-NLS-2$
43 private static final String OSGI_INSTALL_AREA = "osgi.install.area"; //$NON-NLS-1$
44 private static final String ECLIPSE_INI_IGNORED = "eclipse.ini.ignored"; //$NON-NLS-1$
45 private static final String IU_LOCKED = Integer.toString(IProfile.LOCK_UNINSTALL | IProfile.LOCK_UPDATE);
46 private static final String PROP_SURROGATE = "org.eclipse.equinox.p2.surrogate"; //$NON-NLS-1$
47 private static final String PROP_BASE = "org.eclipse.equinox.p2.base"; //$NON-NLS-1$
48 private static final String STRICT = "STRICT"; //$NON-NLS-1$
49 private static final String PROP_INCLUSION_RULES = "org.eclipse.equinox.p2.internal.inclusion.rules"; //$NON-NLS-1$
50
51 private final IProvisioningAgent agent;
52
53 private SimpleProfileRegistry profileRegistry;
54
55 private SoftReference<IProfile> cachedProfile;
56
57 private static void addSharedProfileBaseIUs(final IProfile sharedProfile, final Profile userProfile) {
58 IQuery<IInstallableUnit> rootIUQuery = QueryUtil.createMatchQuery( //
59 "profileProperties[$0] == 'true' || (touchpointType != null && touchpointType.id == $1)", //$NON-NLS-1$
60 IProfile.PROP_PROFILE_ROOT_IU, NATIVE_TOUCHPOINT_TYPE);
61 IQueryResult<IInstallableUnit> rootIUs = sharedProfile.query(rootIUQuery, null);
62 for (IInstallableUnit iu : rootIUs) {
63 userProfile.addInstallableUnit(iu);
64 userProfile.addInstallableUnitProperties(iu, sharedProfile.getInstallableUnitProperties(iu));
65 String profileLockedIUSystemProperty = EngineActivator.getContext()
66 .getProperty(IProfile.PROP_PROFILE_LOCKED_IU);
67 if (profileLockedIUSystemProperty == null) {
68 userProfile.setInstallableUnitProperty(iu, IProfile.PROP_PROFILE_LOCKED_IU, IU_LOCKED);
69 } else {
70 String installableUnitProperty = userProfile.getInstallableUnitProperty(iu,
71 IProfile.PROP_PROFILE_LOCKED_IU);
72 int locked = installableUnitProperty == null ? IProfile.LOCK_NONE
73 : Integer.parseInt(installableUnitProperty);
74 userProfile.setInstallableUnitProperty(iu, IProfile.PROP_PROFILE_LOCKED_IU,
75 Integer.toString(locked | Integer.parseInt(profileLockedIUSystemProperty)));
76 }
77 userProfile.setInstallableUnitProperty(iu, PROP_BASE, Boolean.TRUE.toString());
78 }
79
80 IInstallableUnit sharedProfileIU = createSharedProfileIU(sharedProfile);
81 userProfile.addInstallableUnit(sharedProfileIU);
82 userProfile.setInstallableUnitProperty(sharedProfileIU, PROP_INCLUSION_RULES, STRICT);
83 userProfile.setInstallableUnitProperty(sharedProfileIU, PROP_BASE, Boolean.TRUE.toString());
84 }
85
86 private static IInstallableUnit createSharedProfileIU(final IProfile sharedProfile) {
87 InstallableUnitDescription iuDescription = new InstallableUnitDescription();
88 iuDescription.setId("SharedProfile_" + sharedProfile.getProfileId()); //$NON-NLS-1$
89 iuDescription.setVersion(Version.createOSGi(1, 0, 0, Long.toString(sharedProfile.getTimestamp())));
90
91 ArrayList<IProvidedCapability> iuCapabilities = new ArrayList<>();
92 IProvidedCapability selfCapability = MetadataFactory.createProvidedCapability(IInstallableUnit.NAMESPACE_IU_ID, iuDescription.getId(), iuDescription.getVersion());
93 iuCapabilities.add(selfCapability);
94 iuDescription.addProvidedCapabilities(iuCapabilities);
95
96 ArrayList<IRequirement> iuRequirements = new ArrayList<>();
97 IQueryResult<IInstallableUnit> allIUs = sharedProfile.query(QueryUtil.createIUAnyQuery(), null);
98 for (IInstallableUnit iu : allIUs) {
99 IMatchExpression<IInstallableUnit> iuMatcher = ExpressionUtil.getFactory().matchExpression(ExpressionUtil.parse("id == $0 && version == $1"), iu.getId(), iu.getVersion()); //$NON-NLS-1$
100 iuRequirements.add(MetadataFactory.createRequirement(iuMatcher, null, 0, 1, true));
101 }
102 iuDescription.addRequirements(iuRequirements);
103 iuDescription.setProperty(IInstallableUnit.PROP_NAME, NLS.bind(Messages.Shared_Profile, null));
104
105 IInstallableUnit sharedProfileIU = MetadataFactory.createInstallableUnit(iuDescription);
106 return sharedProfileIU;
107 }
108
109 private static void updateProperties(final IProfile sharedProfile, Profile userProfile) {
110 Location installLocation = ServiceHelper.getService(EngineActivator.getContext(), Location.class, Location.INSTALL_FILTER);
111 File installFolder = URLUtil.toFile(installLocation.getURL());
112 if (installFolder == null) {
113 // fallback: use only path of the URL if the protocol is not 'file'
114 installFolder = new File(installLocation.getURL().getPath());
115 }
116
117 if (Boolean.parseBoolean(sharedProfile.getProperty(IProfile.PROP_ROAMING))) {
118 userProfile.setProperty(IProfile.PROP_INSTALL_FOLDER, installFolder.getAbsolutePath());
119 userProfile.setProperty(IProfile.PROP_SHARED_CACHE, installFolder.getAbsolutePath());
120 userProfile.setProperty(IProfile.PROP_ROAMING, Boolean.FALSE.toString());
121 } else {
122 String cache = sharedProfile.getProperty(IProfile.PROP_CACHE);
123 if (cache != null)
124 userProfile.setProperty(IProfile.PROP_SHARED_CACHE, cache);
125 }
126
127 Location configurationLocation = ServiceHelper.getService(EngineActivator.getContext(), Location.class, Location.CONFIGURATION_FILTER);
128 File configurationFolder = URLUtil.toFile(configurationLocation.getURL());
129 if (configurationFolder == null) {
130 // fallback: use only path of the URL if the protocol is not 'file'
131 configurationFolder = new File(configurationLocation.getURL().getPath());
132 }
133 userProfile.setProperty(IProfile.PROP_CONFIGURATION_FOLDER, configurationFolder.getAbsolutePath());
134
135 // We need to check that the configuration folder is not a file system root.
136 // some of the profiles resources are stored as siblings to the configuration folder.
137 // also see bug 230384
138 if (configurationFolder.getParentFile() == null)
139 throw new IllegalArgumentException("Configuration folder must not be a file system root."); //$NON-NLS-1$
140
141 userProfile.setProperty(IProfile.PROP_CACHE, configurationFolder.getParentFile().getAbsolutePath());
142
143 File launcherConfigFile = new File(configurationFolder, ECLIPSE_INI_IGNORED);
144 userProfile.setProperty(IProfile.PROP_LAUNCHER_CONFIGURATION, launcherConfigFile.getAbsolutePath());
145 }
146
147 public SurrogateProfileHandler(IProvisioningAgent agent) {
148 this.agent = agent;
149 }
150
151 private synchronized SimpleProfileRegistry getProfileRegistry() {
152 if (profileRegistry == null) {
153 String installArea = EngineActivator.getContext().getProperty(OSGI_INSTALL_AREA);
154 try {
155 URL registryURL = new URL(installArea + P2_ENGINE_DIR + SimpleProfileRegistry.DEFAULT_STORAGE_DIR);
156 File sharedRegistryDirectory = URIUtil.toFile(URIUtil.toURI(registryURL));
157 profileRegistry = new SimpleProfileRegistry(agent, sharedRegistryDirectory, null, false);
158 } catch (MalformedURLException e) {
159 //this is not possible because we know the above URL is valid
160 } catch (URISyntaxException e) {
161 //this is not possible because we know the above URL is valid
162 }
163 }
164 return profileRegistry;
165 }
166
167 // this method must not try to lock the profile registry
168 private IProfile getSharedProfile(String id) {
169 SimpleProfileRegistry registry = getProfileRegistry();
170 long[] timestamps = registry.listProfileTimestamps(id);
171 if (timestamps.length == 0)
172 return null;
173
174 long currentTimestamp = timestamps[timestamps.length - 1];
175
176 //see if we have a cached profile
177 if (cachedProfile != null) {
178 IProfile profile = cachedProfile.get();
179 if (profile != null && profile.getProfileId().equals(id) && profile.getTimestamp() == currentTimestamp)
180 return profile;
181 }
182
183 final Profile profile = (Profile) registry.getProfile(id, currentTimestamp);
184 if (profile != null)
185 cachedProfile = new SoftReference<>(profile);
186
187 if (!EngineActivator.EXTENDED) {
188 return profile;
189 }
190
191 setUpRepos();
192 return profile;
193 }
194
195 /**
196 * Removes repositories from fragments locations as they might be obsolete and adds them back.
197 */
198 private void setUpRepos() {
199 //clean old junk
200 IMetadataRepositoryManager metaManager = agent.getService(IMetadataRepositoryManager.class);
201 URI[] knownRepositories = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_LOCAL);
202 for (URI uri : knownRepositories) {
203 if ("true".equals(metaManager.getRepositoryProperty(uri, EngineActivator.P2_FRAGMENT_PROPERTY))) { //$NON-NLS-1$
204 metaManager.removeRepository(uri);
205 }
206 }
207
208 IArtifactRepositoryManager artifactManager = agent.getService(IArtifactRepositoryManager.class);
209 knownRepositories = artifactManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_LOCAL);
210 for (URI uri : knownRepositories) {
211 if ("true".equals(artifactManager.getRepositoryProperty(uri, EngineActivator.P2_FRAGMENT_PROPERTY))) { //$NON-NLS-1$
212 artifactManager.removeRepository(uri);
213 }
214 }
215
216 File[] fragments = EngineActivator.getExtensionsDirectories();
217 for (File f : fragments) {
218 metaManager.addRepository(f.toURI());
219 metaManager.setRepositoryProperty(f.toURI(), EngineActivator.P2_FRAGMENT_PROPERTY, Boolean.TRUE.toString());
220 artifactManager.addRepository(f.toURI());
221 artifactManager.setRepositoryProperty(f.toURI(), EngineActivator.P2_FRAGMENT_PROPERTY, Boolean.TRUE.toString());
222 }
223 }
224
225 @Override
226 public IProfile createProfile(String id) {
227 final Profile sharedProfile = (Profile) getSharedProfile(id);
228 if (sharedProfile == null)
229 return null;
230
231 if (!EngineActivator.EXTENDED) {
232 Profile userProfile = new Profile(agent, id, null, sharedProfile.getProperties());
233 userProfile.setProperty(PROP_SURROGATE, Boolean.TRUE.toString());
234 userProfile.setSurrogateProfileHandler(this);
235 updateProperties(sharedProfile, userProfile);
236 addSharedProfileBaseIUs(sharedProfile, userProfile);
237
238 return userProfile;
239 }
240
241 File[] extensionLocations = EngineActivator.getExtensionsDirectories();
242 Set<IInstallableUnit> added = new HashSet<>();
243 for (File extension : extensionLocations) {
244 try {
245 IMetadataRepositoryManager metaManager = agent.getService(IMetadataRepositoryManager.class);
246 IMetadataRepository repo = metaManager.loadRepository(extension.toURI(), new NullProgressMonitor());
247 Set<IInstallableUnit> installableUnits = repo.query(QueryUtil.createIUAnyQuery(), new NullProgressMonitor()).toUnmodifiableSet();
248 for (IInstallableUnit unit : installableUnits) {
249 Collection<IProvidedCapability> capabilities = unit.getProvidedCapabilities();
250 boolean featureOrBundle = false;
251 for (IProvidedCapability cap : capabilities) {
252 if ("org.eclipse.equinox.p2.eclipse.type".equals(cap.getNamespace())) { //$NON-NLS-1$
253 if ("bundle".equals(cap.getName()) //$NON-NLS-1$
254 || "source".equals(cap.getName()) //$NON-NLS-1$
255 || "feature".equals(cap.getName())) { //$NON-NLS-1$
256 featureOrBundle = true;
257 }
258 }
259 }
260 if (Boolean.TRUE.equals(Boolean.valueOf(unit.getProperties().get("org.eclipse.equinox.p2.type.group")))) { //$NON-NLS-1$
261 featureOrBundle = true;
262 }
263 if (featureOrBundle && !added.contains(unit)) {
264 added.add(unit);
265 sharedProfile.addInstallableUnit(unit);
266 }
267
268 Map<String, String> iuProperties = unit.getProperties();
269 if (iuProperties != null && !iuProperties.isEmpty()) {
270 sharedProfile.addInstallableUnitProperties(unit, iuProperties);
271 }
272 }
273 } catch (ProvisionException e) {
274 LogHelper.log(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.SurrogateProfileHandler_1, extension), e));
275 }
276 }
277
278 Profile userProfile = new Profile(agent, id, null, sharedProfile.getProperties());
279 userProfile.setProperty(PROP_SURROGATE, Boolean.TRUE.toString());
280 userProfile.setSurrogateProfileHandler(this);
281 updateProperties(sharedProfile, userProfile);
282 addSharedProfileBaseIUs(sharedProfile, userProfile);
283
284 return userProfile;
285 }
286
287 @Override
288 public boolean isSurrogate(IProfile profile) {
289 return Boolean.parseBoolean(profile.getProperty(PROP_SURROGATE));
290 }
291
292 @Override
293 public IQueryResult<IInstallableUnit> queryProfile(IProfile profile, IQuery<IInstallableUnit> query, IProgressMonitor monitor) {
294 IProfile sharedProfile = getSharedProfile(profile.getProfileId());
295 if (sharedProfile == null)
296 return profile.query(query, monitor);
297
298 // TODO: Should consider using a sequenced iterator here instead of collecting
299 Collector<IInstallableUnit> result = new Collector<>();
300 result.addAll(sharedProfile.query(query, monitor));
301 result.addAll(profile.query(query, monitor));
302 return result;
303 }
304 }