package org.eclipse.core.internal.preferences;
import java.io.*;
import java.util.*;
import org.eclipse.core.internal.runtime.MetaDataKeeper;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.osgi.service.datalocation.Location;
public class InstancePreferences extends EclipsePreferences {
private String qualifier;
private int segmentCount;
private IEclipsePreferences loadLevel;
private IPath location;
private static Set<String> loadedNodes = Collections.synchronizedSet(new HashSet<String>());
private static boolean initialized = false;
private static IPath baseLocation;
static IPath getBaseLocation() {
if (baseLocation == null) {
Location instanceLocation = PreferencesOSGiUtils.getDefault().getInstanceLocation();
if (instanceLocation != null && (instanceLocation.isSet() || instanceLocation.allowsDefault()))
baseLocation = MetaDataKeeper.getMetaArea().getStateLocation(IPreferencesConstants.RUNTIME_NAME);
}
return baseLocation;
}
public InstancePreferences() {
this(null, null);
}
private InstancePreferences(EclipsePreferences parent, String name) {
super(parent, name);
initializeChildren();
String path = absolutePath();
segmentCount = getSegmentCount(path);
if (segmentCount < 2)
return;
qualifier = getSegment(path, 1);
}
@Override
protected boolean isAlreadyLoaded(IEclipsePreferences node) {
return loadedNodes.contains(node.name());
}
@Override
protected void loaded() {
loadedNodes.add(name());
}
@Override
protected void loadLegacy() {
IPath path = new Path(absolutePath());
if (path.segmentCount() != 2)
return;
if (PreferencesOSGiUtils.getDefault().getInstanceLocation() == null) {
if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
PrefsMessages.message("Cannot load Legacy plug-in preferences since instance location is not set.");
return;
}
String bundleName = path.segment(1);
File prefFile = null;
Location instanceLocation = PreferencesOSGiUtils.getDefault().getInstanceLocation();
if (instanceLocation != null && instanceLocation.isSet())
prefFile = MetaDataKeeper.getMetaArea().getPreferenceLocation(bundleName, false).toFile();
if (prefFile == null) {
if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
PrefsMessages.message("Cannot load legacy values because instance location is not set.");
return;
}
if (!prefFile.exists()) {
if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
PrefsMessages.message("Legacy plug-in preference file not found: " + prefFile);
return;
}
if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
PrefsMessages.message("Loading legacy preferences from " + prefFile);
InputStream input = null;
Properties values = new Properties();
try {
input = new BufferedInputStream(new FileInputStream(prefFile));
values.load(input);
} catch (IOException e) {
if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
PrefsMessages.message("IOException encountered loading legacy preference file " + prefFile);
return;
} catch (IllegalArgumentException e) {
if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
PrefsMessages.message("IllegalArgumentException encountered loading legacy preference file " + prefFile);
return;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) {
PrefsMessages.message("IOException encountered closing legacy preference file " + prefFile);
e.printStackTrace();
}
}
}
}
for (Iterator<?> i = values.keySet().iterator(); i.hasNext();) {
String key = (String) i.next();
String value = values.getProperty(key);
if (value != null) {
if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
PrefsMessages.message("Loaded legacy preference: " + key + " -> " + value);
Object oldValue = internalPut(key, value);
if (!value.equals(oldValue))
makeDirty();
}
}
if (!prefFile.delete())
if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
PrefsMessages.message("Unable to delete legacy preferences file: " + prefFile);
}
@Override
protected IPath getLocation() {
if (location == null)
location = computeLocation(getBaseLocation(), qualifier);
return location;
}
@Override
protected IEclipsePreferences getLoadLevel() {
if (loadLevel == null) {
if (qualifier == null)
return null;
IEclipsePreferences node = this;
for (int i = 2; i < segmentCount; i++)
node = (IEclipsePreferences) node.parent();
loadLevel = node;
}
return loadLevel;
}
protected void initializeChildren() {
if (initialized || parent == null)
return;
try {
synchronized (this) {
String[] names = computeChildren(getBaseLocation());
for (String n : names) {
addChild(n, null);
}
}
} finally {
initialized = true;
}
}
@Override
protected EclipsePreferences internalCreate(EclipsePreferences nodeParent, String nodeName, Object context) {
return new InstancePreferences(nodeParent, nodeName);
}
}