1 11 12 package org.eclipse.ui.internal.intro.impl.model.util; 13 14 import java.io.IOException ; 15 import java.net.URL ; 16 17 import org.eclipse.core.runtime.FileLocator; 18 import org.eclipse.core.runtime.IConfigurationElement; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.core.runtime.IProduct; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.core.runtime.Platform; 23 import org.eclipse.ui.internal.intro.impl.util.Log; 24 import org.eclipse.ui.internal.intro.impl.util.StringUtil; 25 import org.osgi.framework.Bundle; 26 import org.osgi.framework.Constants; 27 28 31 public class BundleUtil { 32 33 private static String NL_TAG = "$nl$/"; private final static String PRODUCT_PLUGIN = "PRODUCT_PLUGIN"; private final static String PLUGINS_ROOT = "PLUGINS_ROOT/"; 37 41 public static boolean bundleHasValidState(Bundle bundle) { 42 if (bundle == null || bundle.getState() == Bundle.UNINSTALLED 43 || bundle.getState() == Bundle.INSTALLED) { 44 45 if (bundle == null) 46 Log.error("Intro tried accessing a NULL bundle.", null); else { 48 String msg = StringUtil 49 .concat("Intro tried accessing Bundle: ", getBundleHeader( bundle, Constants.BUNDLE_NAME), " vendor: ", getBundleHeader(bundle, Constants.BUNDLE_VENDOR), 52 " bundle state: ", String.valueOf(bundle.getState())).toString(); Log.error(msg, null); 54 } 55 return false; 56 } 57 58 return true; 59 } 60 61 68 public static String getBundleHeader(Bundle bundle, String key) { 69 return (String ) bundle.getHeaders().get(key); 70 } 71 72 73 public static Bundle getBundleFromConfigurationElement( 74 IConfigurationElement cfg) { 75 return Platform.getBundle(cfg.getContributor().getName()); 76 } 77 78 79 86 public static String getResourceLocation(String resource, 87 IConfigurationElement element) { 88 Bundle bundle = getBundleFromConfigurationElement(element); 89 return getResolvedResourceLocation(resource, bundle, false); 90 } 91 92 93 101 public static String getResolvedResourceLocation(String resource, 102 String pluginId) { 103 Bundle bundle = Platform.getBundle(pluginId); 104 return getResolvedResourceLocation(resource, bundle, true); 105 } 106 107 108 114 public static String getResolvedResourceLocation(String resource, 115 Bundle bundle) { 116 return getResolvedResourceLocation(resource, bundle, true); 117 } 118 119 120 public static String getResolvedResourceLocation(String base, 121 String resource, Bundle bundle) { 122 if (resource == null) 124 return null; 125 126 String fullResource = new Path(base).append(resource).toString(); 127 String resolvedResource = getResolvedResourceLocation(fullResource, 128 bundle, true); 129 130 if (resolvedResource.equals(fullResource)) 131 return resource; 133 return resolvedResource; 134 } 135 136 137 public static String getResolvedResourceLocation(String resource, 138 Bundle bundle, boolean forceNLResolve) { 139 if (resource == null) 141 return null; 142 143 if (bundle == null || !bundleHasValidState(bundle)) 144 return resource; 145 146 URL localLocation = null; 147 try { 148 int index = resource.indexOf(PLUGINS_ROOT); 150 if (index != -1) { 151 resource = resource.substring(index + PLUGINS_ROOT.length()); 152 index = resource.indexOf('/'); 153 if (index != -1) { 154 String bundleName = resource.substring(0, index); 155 if (PRODUCT_PLUGIN.equals(bundleName)) { 156 IProduct product = Platform.getProduct(); 157 if (product != null) { 158 Bundle productBundle = product.getDefiningBundle(); 159 if (productBundle != null) { 160 bundleName = productBundle.getSymbolicName(); 161 } 162 } 163 } 164 resource = resource.substring(index + 1); 165 Bundle actualBundle = Platform.getBundle(bundleName); 166 if (actualBundle != null) { 167 return getResolvedResourceLocation(resource, actualBundle, forceNLResolve); 168 } 169 } 170 } 171 172 String copyResource = resource; 174 if (forceNLResolve && !copyResource.startsWith(NL_TAG)) { 175 if (copyResource.startsWith("/") || copyResource.startsWith("\\")) copyResource = resource.substring(1); 178 copyResource = NL_TAG + copyResource; 179 } 180 IPath resourcePath = new Path(copyResource); 181 localLocation = FileLocator.find(bundle, resourcePath, null); 182 if (localLocation == null) { 183 String msg = StringUtil.concat("Could not find resource: ", resource, " in ", getBundleHeader( bundle, Constants.BUNDLE_NAME)).toString(); 189 Log.warning(msg); 190 return resource; 191 } 192 196 return toExternalForm(localLocation); 197 } catch (Exception e) { 198 String msg = StringUtil.concat("Failed to load resource: ", resource, " from ", getBundleHeader(bundle, Constants.BUNDLE_NAME)).toString(); 201 Log.error(msg, e); 202 return resource; 203 } 204 } 205 206 207 208 209 210 213 public static URL getResourceAsURL(String resource, String pluginId) { 214 Bundle bundle = Platform.getBundle(pluginId); 215 URL localLocation = FileLocator.find(bundle, new Path( 216 resource), null); 217 return localLocation; 218 } 219 220 221 222 223 224 231 public static String getResolvedBundleLocation(Bundle bundle) { 232 try { 233 URL bundleLocation = bundle.getEntry(""); if (bundleLocation == null) 235 return null; 236 240 return toExternalForm(bundleLocation); 241 } catch (IllegalStateException e) { 242 Log.error("Failed to access bundle: " + bundle.getSymbolicName(), e); 244 return null; 245 } 250 } 251 252 259 public static String getResolvedBundleLocation(String bundleId) { 260 Bundle bundle = Platform.getBundle(bundleId); 261 if (bundle == null) 262 return null; 263 return getResolvedBundleLocation(bundle); 264 } 265 266 270 271 private static String toExternalForm(URL localURL) { 272 try { 273 localURL = FileLocator.toFileURL(localURL); 274 String result = localURL.toExternalForm(); 275 if (result.startsWith("file:/")) { if (result.startsWith("file:///")==false) { result = "file:///"+result.substring(6); } 279 } 280 return result; 281 } 282 catch (IOException e) { 283 String msg = "Failed to resolve URL: " + localURL.toString(); 285 Log.error(msg, e); 286 return localURL.toString(); 287 } 288 } 289 290 } 291 | Popular Tags |