1 11 package org.eclipse.core.internal.preferences; 12 13 import java.io.*; 14 import java.util.*; 15 import org.eclipse.core.internal.runtime.MetaDataKeeper; 16 import org.eclipse.core.runtime.IPath; 17 import org.eclipse.core.runtime.Path; 18 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 19 import org.eclipse.osgi.service.datalocation.Location; 20 21 24 public class InstancePreferences extends EclipsePreferences { 25 26 private String qualifier; 28 private int segmentCount; 29 private IEclipsePreferences loadLevel; 30 private IPath location; 31 private static Set loadedNodes = new HashSet(); 33 private static boolean initialized = false; 34 private static IPath baseLocation; 35 36 private static IPath getBaseLocation() { 37 if (baseLocation == null) { 42 Location instanceLocation = PreferencesOSGiUtils.getDefault().getInstanceLocation(); 43 if (instanceLocation != null && (instanceLocation.isSet() || instanceLocation.allowsDefault())) 44 baseLocation = MetaDataKeeper.getMetaArea().getStateLocation(IPreferencesConstants.RUNTIME_NAME); 45 } 46 return baseLocation; 47 } 48 49 52 public InstancePreferences() { 53 this(null, null); 54 } 55 56 private InstancePreferences(EclipsePreferences parent, String name) { 57 super(parent, name); 58 59 initializeChildren(); 60 61 String path = absolutePath(); 63 segmentCount = getSegmentCount(path); 64 if (segmentCount < 2) 65 return; 66 67 qualifier = getSegment(path, 1); 69 70 } 73 74 protected boolean isAlreadyLoaded(IEclipsePreferences node) { 75 return loadedNodes.contains(node.name()); 76 } 77 78 protected void loaded() { 79 loadedNodes.add(name()); 80 } 81 82 87 protected void loadLegacy() { 88 IPath path = new Path(absolutePath()); 89 if (path.segmentCount() != 2) 90 return; 91 if (PreferencesOSGiUtils.getDefault().getInstanceLocation() == null) { 93 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 94 PrefsMessages.message("Cannot load Legacy plug-in preferences since instance location is not set."); return; 96 } 97 String bundleName = path.segment(1); 98 File prefFile = null; 101 Location instanceLocation = PreferencesOSGiUtils.getDefault().getInstanceLocation(); 102 if (instanceLocation != null && instanceLocation.isSet()) 103 prefFile = MetaDataKeeper.getMetaArea().getPreferenceLocation(bundleName, false).toFile(); 104 if (prefFile == null) { 105 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 106 PrefsMessages.message("Cannot load legacy values because instance location is not set."); return; 108 } 109 if (!prefFile.exists()) { 110 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 112 PrefsMessages.message("Legacy plug-in preference file not found: " + prefFile); return; 114 } 115 116 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 117 PrefsMessages.message("Loading legacy preferences from " + prefFile); 119 InputStream input = null; 121 Properties values = new Properties(); 122 try { 123 input = new BufferedInputStream(new FileInputStream(prefFile)); 124 values.load(input); 125 } catch (IOException e) { 126 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 128 PrefsMessages.message("IOException encountered loading legacy preference file " + prefFile); return; 130 } finally { 131 if (input != null) { 132 try { 133 input.close(); 134 } catch (IOException e) { 135 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) { 137 PrefsMessages.message("IOException encountered closing legacy preference file " + prefFile); e.printStackTrace(); 139 } 140 } 141 } 142 } 143 144 for (Iterator i = values.keySet().iterator(); i.hasNext();) { 146 String key = (String ) i.next(); 147 String value = values.getProperty(key); 148 if (value != null) { 150 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 151 PrefsMessages.message("Loaded legacy preference: " + key + " -> " + value); Object oldValue = internalPut(key, value); 154 if (!value.equals(oldValue)) 155 makeDirty(); 156 } 157 } 158 159 if (!prefFile.delete()) 161 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 163 PrefsMessages.message("Unable to delete legacy preferences file: " + prefFile); } 165 166 protected IPath getLocation() { 167 if (location == null) 168 location = computeLocation(getBaseLocation(), qualifier); 169 return location; 170 } 171 172 175 protected IEclipsePreferences getLoadLevel() { 176 if (loadLevel == null) { 177 if (qualifier == null) 178 return null; 179 IEclipsePreferences node = this; 183 for (int i = 2; i < segmentCount; i++) 184 node = (IEclipsePreferences) node.parent(); 185 loadLevel = node; 186 } 187 return loadLevel; 188 } 189 190 194 protected void initializeChildren() { 195 if (initialized || parent == null) 196 return; 197 try { 198 synchronized (this) { 199 String [] names = computeChildren(getBaseLocation()); 200 for (int i = 0; i < names.length; i++) 201 addChild(names[i], null); 202 } 203 } finally { 204 initialized = true; 205 } 206 } 207 208 protected EclipsePreferences internalCreate(EclipsePreferences nodeParent, String nodeName, Object context) { 209 return new InstancePreferences(nodeParent, nodeName); 210 } 211 } 212 | Popular Tags |