1 12 package org.eclipse.core.internal.preferences; 13 14 import java.io.*; 15 import java.lang.ref.WeakReference ; 16 import java.net.URL ; 17 import java.util.*; 18 import org.eclipse.core.internal.preferences.exchange.IProductPreferencesService; 19 import org.eclipse.core.internal.runtime.RuntimeLog; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.core.runtime.preferences.*; 22 import org.eclipse.osgi.util.NLS; 23 import org.osgi.framework.Bundle; 24 import org.osgi.framework.BundleContext; 25 import org.osgi.util.tracker.ServiceTracker; 26 27 30 public class DefaultPreferences extends EclipsePreferences { 31 private static Set loadedNodes = new HashSet(); 33 private static final String KEY_PREFIX = "%"; private static final String KEY_DOUBLE_PREFIX = "%%"; private static final IPath NL_DIR = new Path("$nl$"); 37 private static final String PROPERTIES_FILE_EXTENSION = "properties"; private static Properties productCustomization; 39 private static Properties productTranslation; 40 private static Properties commandLineCustomization; 41 private EclipsePreferences loadLevel; 42 43 private String qualifier; 45 private int segmentCount; 46 private WeakReference pluginReference; 47 48 public static String pluginCustomizationFile = null; 49 50 53 public DefaultPreferences() { 54 this(null, null); 55 } 56 57 private DefaultPreferences(EclipsePreferences parent, String name, Object context) { 58 this(parent, name); 59 this.pluginReference = new WeakReference (context); 60 } 61 62 private DefaultPreferences(EclipsePreferences parent, String name) { 63 super(parent, name); 64 65 if (parent instanceof DefaultPreferences) 66 this.pluginReference = ((DefaultPreferences) parent).pluginReference; 67 68 String path = absolutePath(); 70 segmentCount = getSegmentCount(path); 71 if (segmentCount < 2) 72 return; 73 74 qualifier = getSegment(path, 1); 76 } 77 78 84 private void applyBundleDefaults() { 85 Bundle bundle = PreferencesOSGiUtils.getDefault().getBundle(name()); 86 if (bundle == null) 87 return; 88 URL url = FileLocator.find(bundle, new Path(IPreferencesConstants.PREFERENCES_DEFAULT_OVERRIDE_FILE_NAME), null); 89 if (url == null) { 90 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 91 PrefsMessages.message("Preference default override file not found for bundle: " + bundle.getSymbolicName()); return; 93 } 94 URL transURL = FileLocator.find(bundle, NL_DIR.append(IPreferencesConstants.PREFERENCES_DEFAULT_OVERRIDE_BASE_NAME).addFileExtension(PROPERTIES_FILE_EXTENSION), null); 95 if (transURL == null && EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 96 PrefsMessages.message("Preference translation file not found for bundle: " + bundle.getSymbolicName()); applyDefaults(name(), loadProperties(url), loadProperties(transURL)); 98 } 99 100 104 private void applyCommandLineDefaults() { 105 if (commandLineCustomization == null) { 107 String filename = pluginCustomizationFile; 108 if (filename == null) { 109 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 110 PrefsMessages.message("Command-line preferences customization file not specified."); return; 112 } 113 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 114 PrefsMessages.message("Using command-line preference customization file: " + filename); commandLineCustomization = loadProperties(filename); 116 } 117 applyDefaults(null, commandLineCustomization, null); 118 } 119 120 126 private void applyDefaults(String id, Properties defaultValues, Properties translations) { 127 for (Enumeration e = defaultValues.keys(); e.hasMoreElements();) { 128 String fullKey = (String ) e.nextElement(); 129 String value = defaultValues.getProperty(fullKey); 130 if (value == null) 131 continue; 132 IPath childPath = new Path(fullKey); 133 String key = childPath.lastSegment(); 134 childPath = childPath.removeLastSegments(1); 135 String localQualifier = id; 136 if (id == null) { 137 localQualifier = childPath.segment(0); 138 childPath = childPath.removeFirstSegments(1); 139 } 140 if (name().equals(localQualifier)) { 141 value = translatePreference(value, translations); 142 if (EclipsePreferences.DEBUG_PREFERENCE_SET) 143 PrefsMessages.message("Setting default preference: " + (new Path(absolutePath()).append(childPath).append(key)) + '=' + value); ((EclipsePreferences) internalNode(childPath.toString(), false, null)).internalPut(key, value); 145 } 146 } 147 } 148 149 public IEclipsePreferences node(String childName, Object context) { 150 return internalNode(childName, true, context); 151 } 152 153 162 private void applyRuntimeDefaults() { 163 WeakReference ref = PreferencesService.getDefault().applyRuntimeDefaults(name(), pluginReference); 164 if (ref != null) 165 pluginReference = ref; 166 } 167 168 175 private void applyProductDefaults() { 176 if (productCustomization == null) { 178 BundleContext context = Activator.getContext(); 179 if (context != null) { 180 ServiceTracker productTracker = new ServiceTracker(context, IProductPreferencesService.class.getName(), null); 181 productTracker.open(); 182 IProductPreferencesService productSpecials = (IProductPreferencesService) productTracker.getService(); 183 if (productSpecials != null) { 184 productCustomization = productSpecials.getProductCustomization(); 185 productTranslation = productSpecials.getProductTranslation(); 186 } 187 productTracker.close(); 188 } else { 189 PrefsMessages.message("Product-specified preferences called before plugin is started"); } 191 if (productCustomization == null) 192 productCustomization = new Properties(); 193 } 194 if (!productCustomization.isEmpty()) 195 applyDefaults(null, productCustomization, productTranslation); 196 } 197 198 201 public void flush() { 202 } 204 205 protected IEclipsePreferences getLoadLevel() { 206 if (loadLevel == null) { 207 if (qualifier == null) 208 return null; 209 EclipsePreferences node = this; 213 for (int i = 2; i < segmentCount; i++) 214 node = (EclipsePreferences) node.parent(); 215 loadLevel = node; 216 } 217 return loadLevel; 218 } 219 220 protected EclipsePreferences internalCreate(EclipsePreferences nodeParent, String nodeName, Object context) { 221 return new DefaultPreferences(nodeParent, nodeName, context); 222 } 223 224 protected boolean isAlreadyLoaded(IEclipsePreferences node) { 225 return loadedNodes.contains(node.name()); 226 } 227 228 231 protected void load() { 232 loadDefaults(); 233 } 234 235 private void loadDefaults() { 236 applyRuntimeDefaults(); 237 applyBundleDefaults(); 238 applyProductDefaults(); 239 applyCommandLineDefaults(); 240 } 241 242 private Properties loadProperties(URL url) { 243 Properties result = new Properties(); 244 if (url == null) 245 return result; 246 InputStream input = null; 247 try { 248 input = url.openStream(); 249 result.load(input); 250 } catch (IOException e) { 251 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) { 252 PrefsMessages.message("Problem opening stream to preference customization file: " + url); e.printStackTrace(); 254 } 255 } finally { 256 if (input != null) 257 try { 258 input.close(); 259 } catch (IOException e) { 260 } 262 } 263 return result; 264 } 265 266 private Properties loadProperties(String filename) { 267 Properties result = new Properties(); 268 InputStream input = null; 269 try { 270 input = new BufferedInputStream(new FileInputStream(filename)); 271 result.load(input); 272 } catch (FileNotFoundException e) { 273 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 274 PrefsMessages.message("Preference customization file not found: " + filename); } catch (IOException e) { 276 String message = NLS.bind(PrefsMessages.preferences_loadException, filename); 277 IStatus status = new Status(IStatus.ERROR, PrefsMessages.OWNER_NAME, IStatus.ERROR, message, e); 278 RuntimeLog.log(status); 279 } finally { 280 if (input != null) 281 try { 282 input.close(); 283 } catch (IOException e) { 284 } 286 } 287 return result; 288 } 289 290 protected void loaded() { 291 loadedNodes.add(name()); 292 } 293 294 297 public void sync() { 298 } 300 301 305 private String translatePreference(String value, Properties props) { 306 value = value.trim(); 307 if (props == null || value.startsWith(KEY_DOUBLE_PREFIX)) 308 return value; 309 if (value.startsWith(KEY_PREFIX)) { 310 int ix = value.indexOf(" "); String key = ix == -1 ? value.substring(1) : value.substring(1, ix); 312 String dflt = ix == -1 ? value : value.substring(ix + 1); 313 return props.getProperty(key, dflt); 314 } 315 return value; 316 } 317 } 318 | Popular Tags |