1 11 package org.eclipse.core.internal.registry.osgi; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.net.URL ; 16 import java.util.ResourceBundle ; 17 import javax.xml.parsers.SAXParserFactory ; 18 import org.eclipse.core.internal.registry.*; 19 import org.eclipse.core.internal.runtime.ResourceTranslator; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.core.runtime.spi.*; 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 39 40 public class RegistryStrategyOSGI extends RegistryStrategy { 41 42 45 private Object token; 46 47 50 protected boolean DEBUG; 51 52 55 protected boolean DEBUG_REGISTRY_EVENTS; 56 57 60 private ServiceTracker xmlTracker = null; 61 62 66 private boolean trackTimestamp; 67 68 75 public RegistryStrategyOSGI(File [] theStorageDir, boolean[] cacheReadOnly, Object key) { 76 super(theStorageDir, cacheReadOnly); 77 token = key; 78 79 BundleContext context = Activator.getContext(); 82 if (context != null) 83 trackTimestamp = "true".equalsIgnoreCase(context.getProperty(IRegistryConstants.PROP_CHECK_CONFIG)); else 85 trackTimestamp = false; 86 } 87 88 91 public final String translate(String key, ResourceBundle resources) { 92 return ResourceTranslator.getResourceString(null, key, resources); 93 } 94 95 98 101 private static float DEFAULT_BUNDLECACHE_LOADFACTOR = 0.75f; 102 103 108 private static int DEFAULT_BUNDLECACHE_SIZE = 200; 109 110 118 private ReferenceMap bundleMap = new ReferenceMap(ReferenceMap.SOFT, DEFAULT_BUNDLECACHE_SIZE, DEFAULT_BUNDLECACHE_LOADFACTOR); 119 120 private Bundle getBundle(String id) { 122 if (id == null) 123 return null; 124 long OSGiId; 125 try { 126 OSGiId = Long.parseLong(id); 127 } catch (NumberFormatException e) { 128 return null; 129 } 130 Bundle bundle = (Bundle ) bundleMap.get((int) OSGiId); 134 if (bundle != null) 135 return bundle; 136 bundle = Activator.getContext().getBundle(OSGiId); 137 bundleMap.put((int) OSGiId, bundle); 138 return bundle; 139 } 140 141 144 147 public Object createExecutableExtension(RegistryContributor contributor, String className, String overridenContributorName) throws CoreException { 148 Bundle contributingBundle; 149 if (overridenContributorName != null && !overridenContributorName.equals("")) contributingBundle = OSGIUtils.getDefault().getBundle(overridenContributorName); 151 else 152 contributingBundle = getBundle(contributor.getId()); 153 154 if (contributingBundle == null) 155 throwException(NLS.bind(RegistryMessages.plugin_loadClassError, "UNKNOWN BUNDLE", className), new InvalidRegistryObjectException()); 157 Class classInstance = null; 159 try { 160 classInstance = contributingBundle.loadClass(className); 161 } catch (Exception e1) { 162 throwException(NLS.bind(RegistryMessages.plugin_loadClassError, contributingBundle.getSymbolicName(), className), e1); 163 } catch (LinkageError e) { 164 throwException(NLS.bind(RegistryMessages.plugin_loadClassError, contributingBundle.getSymbolicName(), className), e); 165 } 166 167 Object result = null; 169 try { 170 result = classInstance.newInstance(); 171 } catch (Exception e) { 172 throwException(NLS.bind(RegistryMessages.plugin_instantiateClassError, contributingBundle.getSymbolicName(), className), e); 173 } catch (LinkageError e1) { 174 throwException(NLS.bind(RegistryMessages.plugin_instantiateClassError, contributingBundle.getSymbolicName(), className), e1); 175 } 176 return result; 177 } 178 179 private void throwException(String message, Throwable exception) throws CoreException { 180 throw new CoreException(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, IRegistryConstants.PLUGIN_ERROR, message, exception)); 181 } 182 183 186 189 private EclipseBundleListener pluginBundleListener = null; 190 191 194 public void onStart(IExtensionRegistry registry) { 195 super.onStart(registry); 196 if (!(registry instanceof ExtensionRegistry)) 197 return; 198 ExtensionRegistry theRegistry = (ExtensionRegistry) registry; 199 200 pluginBundleListener = new EclipseBundleListener(theRegistry, token, this); 202 Activator.getContext().addBundleListener(pluginBundleListener); 203 204 if (!theRegistry.filledFromCache()) 211 pluginBundleListener.processBundles(Activator.getContext().getBundles()); 212 } 213 214 217 public void onStop(IExtensionRegistry registry) { 218 if (pluginBundleListener != null) 219 Activator.getContext().removeBundleListener(pluginBundleListener); 220 if (xmlTracker != null) { 221 xmlTracker.close(); 222 xmlTracker = null; 223 } 224 super.onStop(registry); 225 } 226 227 230 233 public boolean cacheUse() { 234 return !"true".equals(RegistryProperties.getProperty(IRegistryConstants.PROP_NO_REGISTRY_CACHE)); } 236 237 240 public boolean cacheLazyLoading() { 241 return !("true".equalsIgnoreCase(RegistryProperties.getProperty(IRegistryConstants.PROP_NO_LAZY_CACHE_LOADING))); } 243 244 247 public long getContributionsTimestamp() { 248 if (!checkContributionsTimestamp()) 249 return 0; 250 RegistryTimestamp expectedTimestamp = new RegistryTimestamp(); 251 BundleContext context = Activator.getContext(); 252 Bundle [] allBundles = context.getBundles(); 253 for (int i = 0; i < allBundles.length; i++) { 254 URL pluginManifest = EclipseBundleListener.getExtensionURL(allBundles[i], false); 255 if (pluginManifest == null) 256 continue; 257 long timestamp = getExtendedTimestamp(allBundles[i], pluginManifest); 258 expectedTimestamp.add(timestamp); 259 } 260 return expectedTimestamp.getContentsTimestamp(); 261 } 262 263 public boolean checkContributionsTimestamp() { 264 return trackTimestamp; 265 } 266 267 public long getExtendedTimestamp(Bundle bundle, URL pluginManifest) { 268 if (pluginManifest == null) 269 return 0; 270 try { 271 return pluginManifest.openConnection().getLastModified() + bundle.getBundleId(); 272 } catch (IOException e) { 273 if (debug()) { 274 System.out.println("Unable to obtain timestamp for the bundle " + bundle.getSymbolicName()); e.printStackTrace(); 276 } 277 return 0; 278 } 279 } 280 281 284 public SAXParserFactory getXMLParser() { 285 if (xmlTracker == null) { 286 xmlTracker = new ServiceTracker(Activator.getContext(), SAXParserFactory .class.getName(), null); 287 xmlTracker.open(); 288 } 289 return (SAXParserFactory ) xmlTracker.getService(); 290 } 291 } 292 | Popular Tags |