1 11 12 package org.eclipse.ui.internal.intro.universal.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.Path; 21 import org.eclipse.core.runtime.Platform; 22 import org.osgi.framework.Bundle; 23 import org.osgi.framework.Constants; 24 25 28 public class BundleUtil { 29 30 private static String NL_TAG = "$nl$/"; 32 33 37 public static boolean bundleHasValidState(Bundle bundle) { 38 if (bundle == null || bundle.getState() == Bundle.UNINSTALLED 39 || bundle.getState() == Bundle.INSTALLED) { 40 41 if (bundle == null) 42 Log.error("Universal Welcome tried accessing a NULL bundle.", null); else { 44 String msg = StringUtil 45 .concat("Universal Welcome tried accessing Bundle: ", getBundleHeader( bundle, Constants.BUNDLE_NAME), " vendor: ", getBundleHeader(bundle, Constants.BUNDLE_VENDOR), 48 " bundle state: ", String.valueOf(bundle.getState())).toString(); Log.error(msg, null); 50 } 51 return false; 52 } 53 54 return true; 55 } 56 57 64 public static String getBundleHeader(Bundle bundle, String key) { 65 return (String ) bundle.getHeaders().get(key); 66 } 67 68 69 public static Bundle getBundleFromConfigurationElement( 70 IConfigurationElement cfg) { 71 return Platform.getBundle(cfg.getNamespaceIdentifier()); 72 } 73 74 75 82 public static String getResourceLocation(String resource, 83 IConfigurationElement element) { 84 Bundle bundle = getBundleFromConfigurationElement(element); 85 return getResolvedResourceLocation(resource, bundle, false); 86 } 87 88 89 97 public static String getResolvedResourceLocation(String resource, 98 String pluginId) { 99 Bundle bundle = Platform.getBundle(pluginId); 100 return getResolvedResourceLocation(resource, bundle, true); 101 } 102 103 104 110 public static String getResolvedResourceLocation(String resource, 111 Bundle bundle) { 112 return getResolvedResourceLocation(resource, bundle, true); 113 } 114 115 116 public static String getResolvedResourceLocation(String base, 117 String resource, Bundle bundle) { 118 if (resource == null) 120 return null; 121 122 String fullResource = new Path(base).append(resource).toString(); 123 String resolvedResource = getResolvedResourceLocation(fullResource, 124 bundle, true); 125 126 if (resolvedResource.equals(fullResource)) 127 return resource; 129 return resolvedResource; 130 } 131 132 133 public static String getResolvedResourceLocation(String resource, 134 Bundle bundle, boolean forceNLResolve) { 135 if (resource == null) 137 return null; 138 139 if (bundle == null || !bundleHasValidState(bundle)) 140 return resource; 141 142 URL localLocation = null; 143 try { 144 String copyResource = resource; 146 if (forceNLResolve && !copyResource.startsWith(NL_TAG)) { 147 if (copyResource.startsWith("/") || copyResource.startsWith("\\")) copyResource = resource.substring(1); 150 copyResource = NL_TAG + copyResource; 151 } 152 IPath resourcePath = new Path(copyResource); 153 localLocation = FileLocator.find(bundle, resourcePath, null); 154 if (localLocation == null) { 155 String msg = StringUtil.concat("Could not find resource: ", resource, " in ", getBundleHeader( bundle, Constants.BUNDLE_NAME)).toString(); 161 Log.warning(msg); 162 return resource; 163 } 164 168 return toExternalForm(localLocation); 169 } catch (Exception e) { 170 String msg = StringUtil.concat("Failed to load resource: ", resource, " from ", getBundleHeader(bundle, Constants.BUNDLE_NAME)).toString(); 173 Log.error(msg, e); 174 return resource; 175 } 176 } 177 178 private static String toExternalForm(URL localURL) { 179 try { 180 localURL = FileLocator.toFileURL(localURL); 181 String result = localURL.toExternalForm(); 182 if (result.startsWith("file:/")) { if (result.startsWith("file:///")==false) { result = "file:///"+result.substring(6); } 186 } 187 return result; 188 } 189 catch (IOException e) { 190 String msg = "Failed to resolve URL: " + localURL.toString(); 192 Log.error(msg, e); 193 return localURL.toString(); 194 } 195 } 196 197 198 199 200 201 204 public static URL getResourceAsURL(String resource, String pluginId) { 205 Bundle bundle = Platform.getBundle(pluginId); 206 URL localLocation = FileLocator.find(bundle, new Path( 207 resource), null); 208 return localLocation; 209 } 210 211 212 213 214 215 222 public static String getResolvedBundleLocation(Bundle bundle) { 223 try { 224 URL bundleLocation = bundle.getEntry(""); if (bundleLocation == null) 226 return null; 227 231 return toExternalForm(bundleLocation); 232 } catch (IllegalStateException e) { 233 Log.error("Failed to access bundle: " + bundle.getSymbolicName(), e); 235 return null; 236 } 241 } 242 243 250 public static String getResolvedBundleLocation(String bundleId) { 251 Bundle bundle = Platform.getBundle(bundleId); 252 if (bundle == null) 253 return null; 254 return getResolvedBundleLocation(bundle); 255 } 256 257 } 258 | Popular Tags |