1 11 12 package org.eclipse.core.internal.plugins; 13 14 import java.util.*; 15 import java.util.ArrayList ; 16 import org.eclipse.core.internal.runtime.InternalPlatform; 17 import org.eclipse.core.runtime.*; 18 import org.osgi.framework.*; 19 20 23 public class PluginRegistry implements IPluginRegistry { 24 private IExtensionRegistry extRegistry; 25 private RegistryListener listener; 26 27 protected WeakHashMap descriptors = new WeakHashMap(); 29 public PluginRegistry() { 30 extRegistry = InternalPlatform.getDefault().getRegistry(); 31 listener = new RegistryListener(); 32 InternalPlatform.getDefault().getBundleContext().addBundleListener(listener); 33 } 34 35 public void close() { 36 InternalPlatform.getDefault().getBundleContext().removeBundleListener(listener); 37 listener = null; 38 descriptors = null; 39 } 40 41 44 public IConfigurationElement[] getConfigurationElementsFor(String uniqueId) { 45 return extRegistry.getConfigurationElementsFor(uniqueId); 46 } 47 48 51 public IConfigurationElement[] getConfigurationElementsFor(String pluginId, String pointId) { 52 return extRegistry.getConfigurationElementsFor(pluginId, pointId); 53 } 54 55 58 public IConfigurationElement[] getConfigurationElementsFor(String pluginId, String pointId, String extensionId) { 59 return extRegistry.getConfigurationElementsFor(pluginId, pointId, extensionId); 60 } 61 62 65 public IExtension getExtension(String xptUniqueId, String extUniqueId) { 66 return extRegistry.getExtension(xptUniqueId, extUniqueId); 67 } 68 69 72 public IExtension getExtension(String pluginId, String xptSimpleId, String extId) { 73 return extRegistry.getExtension(pluginId, xptSimpleId, extId); 74 } 75 76 79 public IExtensionPoint getExtensionPoint(String xptUniqueId) { 80 return extRegistry.getExtensionPoint(xptUniqueId); 81 } 82 83 86 public IExtensionPoint getExtensionPoint(String plugin, String xpt) { 87 return extRegistry.getExtensionPoint(plugin, xpt); 88 } 89 90 93 public IExtensionPoint[] getExtensionPoints() { 94 return extRegistry.getExtensionPoints(); 95 } 96 97 100 public IPluginDescriptor getPluginDescriptor(String plugin) { 101 Bundle correspondingBundle = InternalPlatform.getDefault().getBundle(plugin); 102 if (correspondingBundle == null) 103 return null; 104 return getPluginDescriptor(correspondingBundle); 105 } 106 107 private PluginDescriptor getPluginDescriptor(Bundle bundle) { 108 if (InternalPlatform.getDefault().isFragment(bundle) || descriptors == null) { 109 return null; 110 } 111 synchronized(descriptors) { 112 PluginDescriptor correspondingDescriptor = (PluginDescriptor) descriptors.get(bundle); 113 if (bundle != null) { 114 if (correspondingDescriptor == null || correspondingDescriptor.getBundle() != bundle) { 116 correspondingDescriptor = new PluginDescriptor(bundle); 118 descriptors.put(bundle, correspondingDescriptor); 119 } 120 return correspondingDescriptor; 121 } 122 if (correspondingDescriptor != null) 124 descriptors.remove(bundle); 125 } 126 return null; 127 } 128 129 132 public IPluginDescriptor[] getPluginDescriptors(String plugin) { 133 Bundle[] bundles = InternalPlatform.getDefault().getBundles(plugin, null); 134 if (bundles == null) 135 return new IPluginDescriptor[0]; 136 IPluginDescriptor[] results = new IPluginDescriptor[bundles.length]; 137 int added = 0; 138 for (int i = 0; i < bundles.length; i++) { 139 PluginDescriptor desc = getPluginDescriptor(bundles[i]); 140 if (desc != null) 141 results[added++] = desc; 142 } 143 if (added == bundles.length) 144 return results; 145 146 if (added == 0) 147 return new IPluginDescriptor[0]; 148 149 IPluginDescriptor[] toReturn = new IPluginDescriptor[added]; 150 System.arraycopy(results, 0, toReturn, 0, added); 151 return toReturn; 152 } 153 154 157 public IPluginDescriptor getPluginDescriptor(String pluginId, PluginVersionIdentifier version) { 158 Bundle[] bundles = InternalPlatform.getDefault().getBundles(pluginId, version.toString()); 159 if (bundles == null) 160 return null; 161 162 return getPluginDescriptor(bundles[0]); 163 } 164 165 168 public IPluginDescriptor[] getPluginDescriptors() { 169 Bundle[] bundles = InternalPlatform.getDefault().getBundleContext().getBundles(); 170 ArrayList pds = new ArrayList (bundles.length); 171 for (int i = 0; i < bundles.length; i++) { 172 boolean isFragment = InternalPlatform.getDefault().isFragment(bundles[i]); 173 if (!isFragment && bundles[i].getSymbolicName() != null && (bundles[i].getState() == Bundle.RESOLVED || bundles[i].getState() == Bundle.STARTING || bundles[i].getState() == Bundle.ACTIVE)) 174 pds.add(getPluginDescriptor(bundles[i])); 175 } 176 IPluginDescriptor[] result = new IPluginDescriptor[pds.size()]; 177 return (IPluginDescriptor[]) pds.toArray(result); 178 } 179 180 void logError(IStatus status) { 181 InternalPlatform.getDefault().log(status); 182 if (InternalPlatform.DEBUG) 183 System.out.println(status.getMessage()); 184 } 185 186 public class RegistryListener implements BundleListener { 187 public void bundleChanged(BundleEvent event) { 188 if (descriptors == null) 189 return; 190 191 synchronized(descriptors) { 192 if (event.getType() == BundleEvent.UNINSTALLED || event.getType() == BundleEvent.UNRESOLVED) { 193 descriptors.remove(event.getBundle()); 194 } 195 } 196 } 197 } 198 } 199 | Popular Tags |