| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004, 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 (Pascal Rapicault) - reading preferences from base in shared install |
| 14 |
*******************************************************************************/ |
| 15 |
package org.eclipse.equinox.internal.p2.engine; |
| 16 |
|
| 17 |
import java.io.File; |
| 18 |
import java.util.*; |
| 19 |
import org.eclipse.core.internal.preferences.EclipsePreferences; |
| 20 |
import org.eclipse.core.runtime.*; |
| 21 |
import org.eclipse.core.runtime.jobs.Job; |
| 22 |
import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
| 23 |
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; |
| 24 |
import org.eclipse.equinox.internal.p2.core.helpers.Tracing; |
| 25 |
import org.eclipse.equinox.p2.core.IAgentLocation; |
| 26 |
import org.eclipse.equinox.p2.core.IProvisioningAgent; |
| 27 |
import org.eclipse.equinox.p2.engine.IProfileRegistry; |
| 28 |
import org.osgi.framework.*; |
| 29 |
import org.osgi.service.prefs.BackingStoreException; |
| 30 |
|
| 31 |
/** |
| 32 |
* A preference implementation that stores preferences in the engine's profile |
| 33 |
* data area. There is one preference file per profile, with an additional file |
| 34 |
* that is used when there is no currently running profile. |
| 35 |
*/ |
| 36 |
public class ProfilePreferences extends EclipsePreferences { |
| 37 |
private class SaveJob extends Job { |
| 38 |
IProvisioningAgent agent; |
| 39 |
|
| 40 |
SaveJob(IProvisioningAgent agent) { |
| 41 |
super(Messages.ProfilePreferences_saving); |
| 42 |
setSystem(true); |
| 43 |
this.agent = agent; |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public boolean belongsTo(Object family) { |
| 48 |
return family == PROFILE_SAVE_JOB_FAMILY; |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
protected IStatus run(IProgressMonitor monitor) { |
| 53 |
try { |
| 54 |
doSave(agent); |
| 55 |
} catch (IllegalStateException e) { |
| 56 |
if (Tracing.DEBUG_PROFILE_PREFERENCES) { |
| 57 |
Tracing.debug("Attempt to save preferences after agent has been stopped"); //$NON-NLS-1$ |
| 58 |
e.printStackTrace(); |
| 59 |
} |
| 60 |
//ignore - this means the provisioning agent has already been stopped, and since |
| 61 |
//this job is joined during agent stop, it means this job has been scheduled after the |
| 62 |
//agent stopped and therefore can't have any interesting changes to save |
| 63 |
} catch (BackingStoreException e) { |
| 64 |
LogHelper.log(new Status(IStatus.WARNING, EngineActivator.ID, "Exception saving profile preferences", e)); //$NON-NLS-1$ |
| 65 |
} catch (RuntimeException e) { |
| 66 |
LogHelper.log(new Status(IStatus.WARNING, EngineActivator.ID, "Exception saving profile preferences", e)); //$NON-NLS-1$ |
| 67 |
} |
| 68 |
return Status.OK_STATUS; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// cache which nodes have been loaded from disk |
| 73 |
private static Set<String> loadedNodes = Collections.synchronizedSet(new HashSet<>()); |
| 74 |
|
| 75 |
public static final Object PROFILE_SAVE_JOB_FAMILY = new Object(); |
| 76 |
|
| 77 |
private static final long SAVE_SCHEDULE_DELAY = 500; |
| 78 |
|
| 79 |
//private IPath location; |
| 80 |
protected IEclipsePreferences loadLevel; |
| 81 |
protected Object profileLock; |
| 82 |
protected String qualifier; |
| 83 |
|
| 84 |
private SaveJob saveJob; |
| 85 |
protected int segmentCount; |
| 86 |
|
| 87 |
public ProfilePreferences() { |
| 88 |
this(null, null); |
| 89 |
} |
| 90 |
|
| 91 |
public ProfilePreferences(EclipsePreferences nodeParent, String nodeName) { |
| 92 |
super(nodeParent, nodeName); |
| 93 |
|
| 94 |
//path is /profile/{agent location}/{profile id}/qualifier |
| 95 |
|
| 96 |
// cache the segment count |
| 97 |
String path = absolutePath(); |
| 98 |
segmentCount = getSegmentCount(path); |
| 99 |
|
| 100 |
if (segmentCount <= 2) |
| 101 |
return; |
| 102 |
|
| 103 |
if (segmentCount == 3) |
| 104 |
profileLock = new Object(); |
| 105 |
|
| 106 |
if (segmentCount < 4) |
| 107 |
return; |
| 108 |
// cache the qualifier |
| 109 |
qualifier = getQualifierSegment(); |
| 110 |
} |
| 111 |
|
| 112 |
private boolean containsProfile(IProfileRegistry profileRegistry, String profileId) { |
| 113 |
if (profileId == null || profileRegistry == null) |
| 114 |
return false; |
| 115 |
return profileRegistry.containsProfile(profileId); |
| 116 |
} |
| 117 |
|
| 118 |
/* |
| 119 |
* Create an Engine phase to save profile preferences |
| 120 |
*/ |
| 121 |
protected void doSave(IProvisioningAgent agent) throws BackingStoreException { |
| 122 |
synchronized (((ProfilePreferences) parent).profileLock) { |
| 123 |
String profileId = getProfileIdSegment(); |
| 124 |
IProfileRegistry registry = agent.getService(IProfileRegistry.class); |
| 125 |
//can't save anything without a profile registry |
| 126 |
if (registry == null) |
| 127 |
return; |
| 128 |
if (!containsProfile(registry, profileId)) { |
| 129 |
//use the default location for the self profile, otherwise just do nothing and return |
| 130 |
if (IProfileRegistry.SELF.equals(profileId)) { |
| 131 |
IPath location = getDefaultLocation(agent); |
| 132 |
if (location != null) { |
| 133 |
super.save(location); |
| 134 |
return; |
| 135 |
} |
| 136 |
} |
| 137 |
if (Tracing.DEBUG_PROFILE_PREFERENCES) |
| 138 |
Tracing.debug("Not saving preferences since there is no file for node: " + absolutePath()); //$NON-NLS-1$ |
| 139 |
return; |
| 140 |
} |
| 141 |
super.save(getProfileLocation(registry, profileId)); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Returns a reference to the agent service corresponding to the given encoded |
| 147 |
* agent location. Never returns null; throws an exception if the agent could not be found. |
| 148 |
*/ |
| 149 |
protected IProvisioningAgent getAgent(String segment) throws BackingStoreException { |
| 150 |
String locationString = SlashEncode.decode(segment); |
| 151 |
Exception failure = null; |
| 152 |
IProvisioningAgent result = null; |
| 153 |
try { |
| 154 |
String filter = "(locationURI=" + encodeForFilter(locationString) + ')'; //$NON-NLS-1$ |
| 155 |
final BundleContext context = EngineActivator.getContext(); |
| 156 |
if (context != null) { |
| 157 |
Collection<ServiceReference<IProvisioningAgent>> refs = context.getServiceReferences(IProvisioningAgent.class, filter); |
| 158 |
if (!refs.isEmpty()) { |
| 159 |
ServiceReference<IProvisioningAgent> ref = refs.iterator().next(); |
| 160 |
result = EngineActivator.getContext().getService(ref); |
| 161 |
EngineActivator.getContext().ungetService(ref); |
| 162 |
return result; |
| 163 |
} |
| 164 |
} |
| 165 |
} catch (InvalidSyntaxException e) { |
| 166 |
failure = e; |
| 167 |
} |
| 168 |
throw new BackingStoreException("Unable to determine provisioning agent from location: " + segment, failure); //$NON-NLS-1$ |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Encodes a string so that it is suitable for use as a value for a filter property. |
| 173 |
* Any reserved filter characters are escaped. |
| 174 |
*/ |
| 175 |
private String encodeForFilter(String string) { |
| 176 |
StringBuilder result = new StringBuilder(string.length()); |
| 177 |
char[] input = string.toCharArray(); |
| 178 |
for (char element : input) { |
| 179 |
switch (element) { |
| 180 |
case '(' : |
| 181 |
case ')' : |
| 182 |
case '*' : |
| 183 |
case '\\' : |
| 184 |
result.append('\\'); |
| 185 |
//fall through |
| 186 |
default : |
| 187 |
result.append(element); |
| 188 |
} |
| 189 |
} |
| 190 |
return result.toString(); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Returns the preference file to use when there is no active profile. |
| 195 |
*/ |
| 196 |
private IPath getDefaultLocation(IProvisioningAgent agent) { |
| 197 |
//use engine agent location for preferences if there is no self profile |
| 198 |
IAgentLocation location = agent.getService(IAgentLocation.class); |
| 199 |
if (location == null) { |
| 200 |
LogHelper.log(new Status(IStatus.WARNING, EngineActivator.ID, "Agent location service not available", new RuntimeException())); //$NON-NLS-1$ |
| 201 |
return null; |
| 202 |
} |
| 203 |
IPath dataArea = IPath.fromOSString(URIUtil.toFile(location.getDataArea(EngineActivator.ID)).getAbsolutePath()); |
| 204 |
return computeLocation(dataArea, qualifier); |
| 205 |
} |
| 206 |
|
| 207 |
@Override |
| 208 |
protected IEclipsePreferences getLoadLevel() { |
| 209 |
if (loadLevel == null) { |
| 210 |
if (qualifier == null) |
| 211 |
return null; |
| 212 |
// Make it relative to this node rather than navigating to it from the root. |
| 213 |
// Walk backwards up the tree starting at this node. |
| 214 |
// This is important to avoid a chicken/egg thing on startup. |
| 215 |
IEclipsePreferences node = this; |
| 216 |
for (int i = 4; i < segmentCount; i++) |
| 217 |
node = (EclipsePreferences) node.parent(); |
| 218 |
loadLevel = node; |
| 219 |
} |
| 220 |
return loadLevel; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Returns the location of the preference file for the given profile. |
| 225 |
*/ |
| 226 |
private IPath getProfileLocation(IProfileRegistry registry, String profileId) { |
| 227 |
SimpleProfileRegistry profileRegistry = (SimpleProfileRegistry) registry; |
| 228 |
File profileDataDirectory = profileRegistry.getProfileDataDirectory(profileId); |
| 229 |
return computeLocation(IPath.fromOSString(profileDataDirectory.getAbsolutePath()), qualifier); |
| 230 |
} |
| 231 |
|
| 232 |
@Override |
| 233 |
protected EclipsePreferences internalCreate(EclipsePreferences nodeParent, String nodeName, Object context) { |
| 234 |
if (nodeName.equals("shared") && segmentCount == 1) { //$NON-NLS-1$ |
| 235 |
return new SharedProfilePreferences(nodeParent, nodeName); |
| 236 |
} |
| 237 |
return new ProfilePreferences(nodeParent, nodeName); |
| 238 |
} |
| 239 |
|
| 240 |
@Override |
| 241 |
protected boolean isAlreadyLoaded(IEclipsePreferences node) { |
| 242 |
return loadedNodes.contains(node.absolutePath()); |
| 243 |
} |
| 244 |
|
| 245 |
protected boolean isAlreadyLoaded(String path) { |
| 246 |
return loadedNodes.contains(path); |
| 247 |
} |
| 248 |
|
| 249 |
/* |
| 250 |
* Create an Engine phase to load profile preferences |
| 251 |
*/ |
| 252 |
@Override |
| 253 |
protected void load() throws BackingStoreException { |
| 254 |
synchronized (((ProfilePreferences) parent).profileLock) { |
| 255 |
IProvisioningAgent agent = getAgent(getAgentLocationSegment()); |
| 256 |
if (agent == null) |
| 257 |
return; |
| 258 |
IProfileRegistry registry = agent.getService(IProfileRegistry.class); |
| 259 |
String profileId = getProfileIdSegment(); |
| 260 |
if (!containsProfile(registry, profileId)) { |
| 261 |
//use the default location for the self profile, otherwise just do nothing and return |
| 262 |
if (IProfileRegistry.SELF.equals(profileId)) { |
| 263 |
IPath location = getDefaultLocation(agent); |
| 264 |
if (location != null) { |
| 265 |
load(location); |
| 266 |
return; |
| 267 |
} |
| 268 |
} |
| 269 |
if (Tracing.DEBUG_PROFILE_PREFERENCES) |
| 270 |
Tracing.debug("Not loading preferences since there is no file for node: " + absolutePath()); //$NON-NLS-1$ |
| 271 |
return; |
| 272 |
} |
| 273 |
load(getProfileLocation(registry, profileId)); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
@Override |
| 278 |
protected void loaded() { |
| 279 |
loadedNodes.add(name()); |
| 280 |
} |
| 281 |
|
| 282 |
@Override |
| 283 |
public void removeNode() throws BackingStoreException { |
| 284 |
super.removeNode(); |
| 285 |
loadedNodes.remove(this.absolutePath()); |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Schedules the save job. This method is synchronized to protect lazily initialization |
| 290 |
* of the save job instance. |
| 291 |
*/ |
| 292 |
@Override |
| 293 |
protected synchronized void save() throws BackingStoreException { |
| 294 |
try { |
| 295 |
IProvisioningAgent agent = getAgent(getAgentLocationSegment()); |
| 296 |
if (saveJob == null || saveJob.agent != agent) |
| 297 |
saveJob = new SaveJob(agent); |
| 298 |
} catch (BackingStoreException e) { |
| 299 |
if (Tracing.DEBUG_PROFILE_PREFERENCES) |
| 300 |
e.printStackTrace(); |
| 301 |
//get agent has already gone away so we can't save preferences |
| 302 |
//TODO see bug 300450 |
| 303 |
} |
| 304 |
//only schedule a save if the engine bundle is still running |
| 305 |
BundleContext context = EngineActivator.getContext(); |
| 306 |
if (context == null || saveJob == null) |
| 307 |
return; |
| 308 |
try { |
| 309 |
if (context.getBundle().getState() == Bundle.ACTIVE) |
| 310 |
saveJob.schedule(SAVE_SCHEDULE_DELAY); |
| 311 |
} catch (IllegalStateException e) { |
| 312 |
//bundle has been stopped concurrently, so don't save |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
protected String getQualifierSegment() { |
| 317 |
return getSegment(absolutePath(), 3); |
| 318 |
} |
| 319 |
|
| 320 |
protected String getProfileIdSegment() { |
| 321 |
return getSegment(absolutePath(), 2); |
| 322 |
} |
| 323 |
|
| 324 |
protected String getAgentLocationSegment() { |
| 325 |
return getSegment(absolutePath(), 1); |
| 326 |
} |
| 327 |
|
| 328 |
} |