1 11 package org.eclipse.core.internal.preferences; 12 13 import java.lang.ref.WeakReference ; 14 import java.util.*; 15 import org.eclipse.core.internal.preferences.exchange.ILegacyPreferences; 16 import org.eclipse.core.internal.runtime.RuntimeLog; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.core.runtime.preferences.*; 19 import org.eclipse.osgi.util.NLS; 20 import org.osgi.framework.Bundle; 21 22 27 public class PreferenceServiceRegistryHelper implements IRegistryChangeListener { 28 29 private static final String ELEMENT_INITIALIZER = "initializer"; private static final String ATTRIBUTE_NAME = "name"; private static final String ATTRIBUTE_CLASS = "class"; private static final String ELEMENT_SCOPE = "scope"; private static final String ELEMENT_MODIFIER = "modifier"; private final static IExtension[] EMPTY_EXTENSION_ARRAY = new IExtension[0]; 36 private static final Map scopeRegistry = Collections.synchronizedMap(new HashMap()); 37 private ListenerList modifyListeners; 38 private PreferencesService service; 39 private IExtensionRegistry registry; 40 41 45 private static IStatus createStatusError(String message, Exception e) { 46 return new Status(IStatus.ERROR, PrefsMessages.OWNER_NAME, IStatus.ERROR, message, e); 47 } 48 49 53 private static IStatus createStatusWarning(String message, Exception e) { 54 return new Status(IStatus.WARNING, PrefsMessages.OWNER_NAME, IStatus.WARNING, message, e); 55 } 56 57 60 private static void log(IStatus status) { 61 RuntimeLog.log(status); 62 } 63 64 67 public PreferenceServiceRegistryHelper(PreferencesService service, Object registryObject) { 68 super(); 69 this.service = service; 70 this.registry = (IExtensionRegistry) registryObject; 71 initializeScopes(); 72 registry.addRegistryChangeListener(this); 73 } 74 75 void stop() { 76 registry.removeRegistryChangeListener(this); 77 } 78 79 82 private void addModifyListener(IConfigurationElement element) { 83 String key = element.getAttribute(ATTRIBUTE_CLASS); 84 if (key == null) { 85 String message = NLS.bind(PrefsMessages.preferences_missingClassAttribute, element.getDeclaringExtension().getUniqueIdentifier()); 86 log(new Status(IStatus.ERROR, PrefsMessages.OWNER_NAME, IStatus.ERROR, message, null)); 87 return; 88 } 89 try { 90 Object listener = element.createExecutableExtension(ATTRIBUTE_CLASS); 91 if (!(listener instanceof PreferenceModifyListener)) { 92 log(new Status(IStatus.ERROR, PrefsMessages.OWNER_NAME, IStatus.ERROR, PrefsMessages.preferences_classCastListener, null)); 93 return; 94 } 95 modifyListeners.add(listener); 96 } catch (CoreException e) { 97 log(e.getStatus()); 98 } 99 } 100 101 106 public WeakReference applyRuntimeDefaults(String name, WeakReference pluginReference) { 107 IExtension[] extensions = getPrefExtensions(); 108 if (extensions.length == 0) { 109 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 110 PrefsMessages.message("Skipping runtime default preference customization."); return null; 112 } 113 boolean foundInitializer = false; 114 for (int i = 0; i < extensions.length; i++) { 115 IConfigurationElement[] elements = extensions[i].getConfigurationElements(); 116 for (int j = 0; j < elements.length; j++) 117 if (ELEMENT_INITIALIZER.equals(elements[j].getName())) { 118 if (name.equals(elements[j].getContributor().getName())) { 119 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) { 120 IExtension theExtension = elements[j].getDeclaringExtension(); 121 String extensionNamespace = theExtension.getContributor().getName(); 122 Bundle underlyingBundle = PreferencesOSGiUtils.getDefault().getBundle(extensionNamespace); 123 String ownerName; 124 if (underlyingBundle != null) 125 ownerName = underlyingBundle.getSymbolicName(); 126 else 127 ownerName = extensionNamespace; 128 PrefsMessages.message("Running default preference customization as defined by: " + ownerName); } 130 runInitializer(elements[j]); 131 foundInitializer = true; 133 } 134 } 135 } 136 if (foundInitializer) 137 return null; 138 139 142 Object plugin = pluginReference.get(); 144 ILegacyPreferences initService = PreferencesOSGiUtils.getDefault().getLegacyPreferences(); 145 if (initService != null) 146 plugin = initService.init(plugin, name); 147 return new WeakReference (plugin); 148 } 149 150 155 public IEclipsePreferences createNode(RootPreferences parent, String name) { 156 IScope scope = null; 157 Object value = scopeRegistry.get(name); 158 if (value instanceof IConfigurationElement) { 159 try { 160 scope = (IScope) ((IConfigurationElement) value).createExecutableExtension(ATTRIBUTE_CLASS); 161 scopeRegistry.put(name, scope); 162 } catch (ClassCastException e) { 163 log(createStatusError(PrefsMessages.preferences_classCastScope, e)); 164 return new EclipsePreferences(parent, name); 165 } catch (CoreException e) { 166 log(e.getStatus()); 167 return new EclipsePreferences(parent, name); 168 } 169 } else 170 scope = (IScope) value; 171 return scope.create(parent, name); 172 } 173 174 178 public PreferenceModifyListener[] getModifyListeners() { 179 if (modifyListeners == null) { 180 modifyListeners = new ListenerList(); 181 IExtension[] extensions = getPrefExtensions(); 182 for (int i = 0; i < extensions.length; i++) { 183 IConfigurationElement[] elements = extensions[i].getConfigurationElements(); 184 for (int j = 0; j < elements.length; j++) 185 if (ELEMENT_MODIFIER.equalsIgnoreCase(elements[j].getName())) 186 addModifyListener(elements[j]); 187 } 188 } 189 Object [] source = modifyListeners.getListeners(); 190 PreferenceModifyListener[] result = new PreferenceModifyListener[source.length]; 191 System.arraycopy(source, 0, result, 0, source.length); 192 return result; 193 } 194 195 199 private IExtension[] getPrefExtensions() { 200 IExtension[] extensionsOld = EMPTY_EXTENSION_ARRAY; 201 IExtension[] extensionsNew = EMPTY_EXTENSION_ARRAY; 202 IExtensionPoint pointOld = registry.getExtensionPoint(IPreferencesConstants.RUNTIME_NAME, IPreferencesConstants.PT_PREFERENCES); 204 if (pointOld != null) 205 extensionsOld = pointOld.getExtensions(); 206 IExtensionPoint pointNew = registry.getExtensionPoint(IPreferencesConstants.PREFERS_NAME, IPreferencesConstants.PT_PREFERENCES); 208 if (pointNew != null) 209 extensionsNew = pointNew.getExtensions(); 210 IExtension[] extensions = new IExtension[extensionsOld.length + extensionsNew.length]; 212 System.arraycopy(extensionsOld, 0, extensions, 0, extensionsOld.length); 213 System.arraycopy(extensionsNew, 0, extensions, extensionsOld.length, extensionsNew.length); 214 215 if (extensions.length == 0) { 216 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) 217 PrefsMessages.message("No extensions for org.eclipse.core.contenttype."); } 219 220 return extensions; 221 } 222 223 226 private void initializeScopes() { 227 IExtension[] extensions = getPrefExtensions(); 228 for (int i = 0; i < extensions.length; i++) { 229 IConfigurationElement[] elements = extensions[i].getConfigurationElements(); 230 for (int j = 0; j < elements.length; j++) 231 if (ELEMENT_SCOPE.equalsIgnoreCase(elements[j].getName())) 232 scopeAdded(elements[j]); 233 } 234 } 235 236 239 public void registryChanged(IRegistryChangeEvent event) { 240 IExtensionDelta[] deltasOld = event.getExtensionDeltas(IPreferencesConstants.RUNTIME_NAME, IPreferencesConstants.PT_PREFERENCES); 241 IExtensionDelta[] deltasNew = event.getExtensionDeltas(IPreferencesConstants.PREFERS_NAME, IPreferencesConstants.PT_PREFERENCES); 242 IExtensionDelta[] deltas = new IExtensionDelta[deltasOld.length + deltasNew.length]; 243 System.arraycopy(deltasOld, 0, deltas, 0, deltasOld.length); 244 System.arraycopy(deltasNew, 0, deltas, deltasOld.length, deltasNew.length); 245 246 if (deltas.length == 0) 247 return; 248 for (int i = 0; i < deltas.length; i++) { 250 IConfigurationElement[] elements = deltas[i].getExtension().getConfigurationElements(); 251 for (int j = 0; j < elements.length; j++) { 252 switch (deltas[i].getKind()) { 253 case IExtensionDelta.ADDED : 254 if (ELEMENT_SCOPE.equalsIgnoreCase(elements[j].getName())) 255 scopeAdded(elements[j]); 256 break; 257 case IExtensionDelta.REMOVED : 258 String scope = elements[j].getAttribute(ATTRIBUTE_NAME); 259 if (scope != null) 260 scopeRemoved(scope); 261 break; 262 } 263 } 264 } 265 modifyListeners = null; 267 } 268 269 272 private void runInitializer(IConfigurationElement element) { 273 AbstractPreferenceInitializer initializer = null; 274 try { 275 initializer = (AbstractPreferenceInitializer) element.createExecutableExtension(ATTRIBUTE_CLASS); 276 initializer.initializeDefaultPreferences(); 277 } catch (ClassCastException e) { 278 IStatus status = new Status(IStatus.ERROR, PrefsMessages.OWNER_NAME, IStatus.ERROR, PrefsMessages.preferences_invalidExtensionSuperclass, e); 279 log(status); 280 } catch (CoreException e) { 281 log(e.getStatus()); 282 } 283 } 284 285 289 private void scopeAdded(IConfigurationElement element) { 290 String key = element.getAttribute(ATTRIBUTE_NAME); 291 if (key == null) { 292 String message = NLS.bind(PrefsMessages.preferences_missingScopeAttribute, element.getDeclaringExtension().getUniqueIdentifier()); 293 log(createStatusWarning(message, null)); 294 return; 295 } 296 scopeRegistry.put(key, element); 297 ((RootPreferences) service.getRootNode()).addChild(key, null); 298 } 299 300 304 private void scopeRemoved(String key) { 305 IEclipsePreferences node = (IEclipsePreferences) ((RootPreferences) service.getRootNode()).getNode(key, false); 306 if (node != null) 307 ((RootPreferences) service.getRootNode()).removeNode(node); 308 else 309 ((RootPreferences) service.getRootNode()).removeNode(key); 310 scopeRegistry.remove(key); 311 } 312 313 } 314 | Popular Tags |