1 12 package org.eclipse.core.internal.preferences; 13 14 import java.util.Set ; 15 import java.util.TreeSet ; 16 import org.eclipse.core.runtime.preferences.ConfigurationScope; 17 import org.osgi.framework.*; 18 import org.osgi.service.prefs.BackingStoreException; 19 import org.osgi.service.prefs.Preferences; 20 21 31 public class OSGiPreferencesServiceManager implements ServiceFactory, BundleListener { 32 33 private static final String ORG_ECLIPSE_CORE_INTERNAL_PREFERENCES_OSGI = "org.eclipse.core.internal.preferences.osgi"; 35 private Preferences prefBundles; 37 38 public OSGiPreferencesServiceManager(BundleContext context) { 39 40 context.addBundleListener(this); 41 42 prefBundles = new ConfigurationScope().getNode(ORG_ECLIPSE_CORE_INTERNAL_PREFERENCES_OSGI); 44 45 try { 47 48 Bundle[] allBundles = context.getBundles(); 50 Set bundleQualifiers = new TreeSet (); 51 for (int i = 0; i < allBundles.length; i++) { 52 bundleQualifiers.add(getQualifier(allBundles[i])); 53 } 54 55 String [] prefsBundles = prefBundles.keys(); 57 58 for (int i = 0; i < prefsBundles.length; i++) { 60 if (!bundleQualifiers.contains(prefsBundles[i])) { 61 removePrefs(prefsBundles[i]); 62 } 63 } 64 65 } catch (BackingStoreException e) { 66 } 68 } 69 70 73 public Object getService(Bundle bundle, ServiceRegistration registration) { 74 String qualifier = getQualifier(bundle); 75 Preferences bundlesNode = getBundlesNode(); 77 bundlesNode.put(qualifier, ""); try { 79 bundlesNode.flush(); 80 } catch (BackingStoreException e) { 81 } 83 return new OSGiPreferencesServiceImpl(new ConfigurationScope().getNode(getQualifier(bundle))); 85 } 86 87 90 private String getQualifier(Bundle bundle) { 91 String qualifier = "org.eclipse.core.runtime.preferences.OSGiPreferences." + bundle.getBundleId(); return qualifier; 93 } 94 95 98 public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) { 99 try { 100 new ConfigurationScope().getNode(getQualifier(bundle)).flush(); 102 } catch (BackingStoreException e) { 103 } 105 } 106 107 110 public void bundleChanged(BundleEvent event) { 111 if (event.getType() == BundleEvent.UNINSTALLED) { 112 try { 113 removePrefs(getQualifier(event.getBundle())); 114 } catch (BackingStoreException e) { 115 } 117 } 118 119 } 120 121 protected void removePrefs(String qualifier) throws BackingStoreException { 122 new ConfigurationScope().getNode(qualifier).removeNode(); 125 126 Preferences bundlesNode = getBundlesNode(); 128 bundlesNode.remove(qualifier); 129 bundlesNode.flush(); 130 } 131 132 private Preferences getBundlesNode() { 133 try { 134 if (prefBundles == null || !prefBundles.nodeExists("")) { prefBundles = new ConfigurationScope().getNode(ORG_ECLIPSE_CORE_INTERNAL_PREFERENCES_OSGI); 136 } 137 return prefBundles; 138 } catch (BackingStoreException e) { 139 } 141 return null; 142 } 143 } 144 | Popular Tags |