1 12 package org.eclipse.update.internal.configurator; 13 14 import java.io.File ; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 import java.util.Locale ; 18 import java.util.MissingResourceException ; 19 import java.util.ResourceBundle ; 20 import java.util.StringTokenizer ; 21 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IPath; 24 import org.eclipse.core.runtime.IStatus; 25 import org.eclipse.core.runtime.MultiStatus; 26 import org.eclipse.core.runtime.Path; 27 import org.eclipse.core.runtime.Status; 28 import org.eclipse.osgi.framework.log.FrameworkLog; 29 import org.eclipse.osgi.framework.log.FrameworkLogEntry; 30 import org.eclipse.osgi.service.datalocation.Location; 31 import org.eclipse.osgi.service.resolver.PlatformAdmin; 32 import org.osgi.framework.Bundle; 33 import org.osgi.framework.BundleContext; 34 import org.osgi.framework.Filter; 35 import org.osgi.framework.InvalidSyntaxException; 36 import org.osgi.framework.ServiceReference; 37 import org.osgi.service.packageadmin.PackageAdmin; 38 import org.osgi.util.tracker.ServiceTracker; 39 40 public class Utils { 41 private static final String PROP_ARCH = "osgi.arch"; private static final String PROP_NL = "osgi.nl"; private static final String PROP_OS = "osgi.os"; private static final String PROP_WS = "osgi.ws"; private static final String PI_OSGI = "org.eclipse.osgi"; private static final String KEY_PREFIX = "%"; private static final String KEY_DOUBLE_PREFIX = "%%"; public static boolean isWindows = System.getProperty("os.name").startsWith("Win"); static FrameworkLog log; 51 private static ServiceTracker bundleTracker; 52 private static ServiceTracker instanceLocation; 53 private static ServiceTracker configurationLocation; 54 55 public static void debug(String s) { 56 if (ConfigurationActivator.DEBUG) 57 System.out.println("PlatformConfig: " + s); } 59 60 73 public static CoreException newCoreException(String s, Throwable e) { 74 75 IStatus status; 77 if (e instanceof CoreException) { 78 if (s == null) 79 s = ""; status = new MultiStatus("org.eclipse.update.configurator", 0, s, e); IStatus childrenStatus = ((CoreException) e).getStatus(); 82 ((MultiStatus) status).add(childrenStatus); 83 ((MultiStatus) status).addAll(childrenStatus); 84 } else { 85 StringBuffer completeString = new StringBuffer (""); if (s != null) 87 completeString.append(s); 88 if (e != null) { 89 completeString.append(" ["); String msg = e.getLocalizedMessage(); 91 completeString.append(msg!=null?msg:e.toString()); 92 completeString.append("]"); } 94 status = newStatus(completeString.toString(), e); 95 } 96 return new CoreException(status); 97 } 98 99 public static IStatus newStatus(String message, Throwable e) { 100 return new Status(IStatus.ERROR, "org.eclipse.update.configurator", IStatus.OK, message, e); } 102 103 public static void log(String message) { 104 log(newStatus(message, null)); 105 } 106 107 public static void log(IStatus status) { 108 if (log != null) { 109 log.log(new FrameworkLogEntry(ConfigurationActivator.PI_CONFIGURATOR, status.getMessage(), 0, status.getException(), null)); 110 } else { 111 System.out.println(status.getMessage()); 112 if (status.getException() != null) 113 status.getException().printStackTrace(); 114 } 115 } 116 117 120 static synchronized void shutdown() { 121 if (bundleTracker != null) { 122 bundleTracker.close(); 123 bundleTracker = null; 124 } 125 if (instanceLocation != null) { 126 instanceLocation.close(); 127 instanceLocation = null; 128 } 129 if (configurationLocation != null) { 130 configurationLocation.close(); 131 configurationLocation = null; 132 } 133 } 134 135 139 public static boolean isRunning() { 140 Bundle bundle = getBundle(PI_OSGI); 141 return bundle == null ? false : bundle.getState() == Bundle.ACTIVE; 142 } 143 144 147 public static boolean isValidEnvironment(String os, String ws, String arch, String nl) { 148 if (os!=null && !isMatching(os, getOS())) return false; 149 if (ws!=null && !isMatching(ws, getWS())) return false; 150 if (arch!=null && !isMatching(arch, getArch())) return false; 151 if (nl!=null && !isMatchingLocale(nl, getNL())) return false; 152 return true; 153 } 154 155 160 public static String getOS() { 161 return getContext().getProperty(PROP_OS); 162 } 163 164 169 public static String getWS() { 170 return getContext().getProperty(PROP_WS); 171 } 172 173 178 public static String getArch() { 179 return getContext().getProperty(PROP_ARCH); 180 } 181 182 187 public static String getNL() { 188 return getContext().getProperty(PROP_NL); 189 } 190 191 199 public static long getStateStamp() { 200 ServiceReference platformAdminReference = getContext().getServiceReference(PlatformAdmin.class.getName()); 201 if (platformAdminReference == null) 202 return -1; 203 PlatformAdmin admin = (PlatformAdmin) getContext().getService(platformAdminReference); 204 return admin == null ? -1 : admin.getState(false).getTimeStamp(); 205 } 206 207 212 public static synchronized Bundle getBundle(String symbolicName) { 213 if (bundleTracker == null) { 214 bundleTracker = new ServiceTracker(getContext(), PackageAdmin.class.getName(), null); 215 bundleTracker.open(); 216 } 217 PackageAdmin admin = (PackageAdmin) bundleTracker.getService(); 218 if (admin == null) 219 return null; 220 Bundle [] bundles = admin.getBundles(symbolicName, null); 221 if (bundles == null) 222 return null; 223 for (int i = 0; i < bundles.length; i++) { 225 if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) { 226 return bundles[i]; 227 } 228 } 229 return null; 230 } 231 232 235 private static BundleContext getContext() { 236 return ConfigurationActivator.getBundleContext(); 237 } 238 239 244 public static synchronized Location getConfigurationLocation() { 245 if (configurationLocation == null) { 246 Filter filter = null; 247 try { 248 filter = getContext().createFilter(Location.CONFIGURATION_FILTER); 249 } catch (InvalidSyntaxException e) { 250 } 252 configurationLocation = new ServiceTracker(getContext(), filter, null); 253 configurationLocation.open(); 254 } 255 return (Location) configurationLocation.getService(); 256 } 257 258 261 private static boolean isMatching(String candidateValues, String siteValues) { 262 if (siteValues==null) return false; 263 if ("*".equalsIgnoreCase(candidateValues)) return true; siteValues = siteValues.toUpperCase(); 265 StringTokenizer stok = new StringTokenizer (candidateValues, ","); while (stok.hasMoreTokens()) { 267 String token = stok.nextToken().toUpperCase(); 268 if (siteValues.indexOf(token)!=-1) return true; 269 } 270 return false; 271 } 272 273 276 private static boolean isMatchingLocale(String candidateValues, String locale) { 277 if (locale==null) return false; 278 if ("*".equalsIgnoreCase(candidateValues)) return true; 280 locale = locale.toUpperCase(); 281 candidateValues = candidateValues.toUpperCase(); 282 StringTokenizer stok = new StringTokenizer (candidateValues, ","); while (stok.hasMoreTokens()) { 284 String candidate = stok.nextToken(); 285 if (locale.indexOf(candidate) == 0) 286 return true; 287 if (candidate.indexOf(locale) == 0) 288 return true; 289 } 290 return false; 291 } 292 293 public static Locale getDefaultLocale() { 294 String nl = getNL(); 295 if (nl == null) 297 return Locale.getDefault(); 298 299 StringTokenizer locales = new StringTokenizer (nl,"_"); if (locales.countTokens() == 1) 302 return new Locale (locales.nextToken(), ""); else if (locales.countTokens() == 2) 304 return new Locale (locales.nextToken(), locales.nextToken()); 305 else if (locales.countTokens() == 3) 306 return new Locale (locales.nextToken(), locales.nextToken(), locales.nextToken()); 307 else 308 return Locale.getDefault(); 309 } 310 311 312 344 public static String getResourceString(ResourceBundle resourceBundle, String string) { 345 346 if (string == null) 347 return null; 348 349 String s = string.trim(); 350 351 if (s.equals("")) return string; 353 354 if (!s.startsWith(KEY_PREFIX)) 355 return string; 356 357 if (s.startsWith(KEY_DOUBLE_PREFIX)) 358 return s.substring(1); 359 360 int ix = s.indexOf(" "); String key = ix == -1 ? s : s.substring(0, ix); 362 String dflt = ix == -1 ? s : s.substring(ix + 1); 363 364 if (resourceBundle == null) 365 return dflt; 366 367 try { 368 return resourceBundle.getString(key.substring(1)); 369 } catch (MissingResourceException e) { 370 return dflt; 371 } 372 } 373 374 public static boolean isAutomaticallyStartedBundle(String bundleURL) { 375 if (bundleURL.indexOf("org.eclipse.osgi") != -1) return true; 377 378 String osgiBundles = ConfigurationActivator.getBundleContext().getProperty("osgi.bundles"); StringTokenizer st = new StringTokenizer (osgiBundles, ","); while (st.hasMoreTokens()) { 381 String token = st.nextToken().trim(); 382 int index = token.indexOf('@'); 383 if (index != -1) 384 token = token.substring(0,index); 385 if (token.startsWith("reference:file:")) { File f = new File (token.substring(15)); 387 if (bundleURL.indexOf(f.getName()) != -1) 388 return true; 389 } 390 if (bundleURL.indexOf(token) != -1) 391 return true; 392 } 393 return false; 394 } 395 396 401 public static URL makeAbsolute(URL base, URL relativeLocation) { 402 if (!"file".equals(base.getProtocol())) return relativeLocation; 405 if (relativeLocation.getProtocol() != null && !relativeLocation.getProtocol().equals(base.getProtocol())) 406 return relativeLocation; 408 IPath relativePath = new Path(relativeLocation.getPath()); 409 if (relativePath.isAbsolute()) 410 return relativeLocation; 411 try { 412 IPath absolutePath = new Path(base.getPath()).append(relativeLocation.getPath()); 413 return absolutePath.toFile().toURL(); 415 } catch (MalformedURLException e) { 416 Utils.log(e.getLocalizedMessage()); 418 return relativeLocation; 419 } 420 } 421 422 426 public static URL makeRelative(URL base, URL location) { 427 if (base == null) 428 return location; 429 if (!"file".equals(base.getProtocol())) return location; 431 if (!base.getProtocol().equals(location.getProtocol())) 432 return location; 433 IPath locationPath = new Path(location.getPath()); 434 if (!locationPath.isAbsolute()) 435 return location; 436 IPath relativePath = makeRelative(new Path(base.getPath()), locationPath); 437 try { 438 return new URL (base.getProtocol(), base.getHost(), base.getPort(), relativePath.toString()); 439 } catch (MalformedURLException e) { 440 String message = e.getMessage(); 441 if (message == null) 442 message = ""; Utils.log(Utils.newStatus(message, e)); 444 } 445 return location; 446 } 447 448 452 public static IPath makeRelative(IPath base, IPath location) { 453 if (location.getDevice() != null && !location.getDevice().equalsIgnoreCase(base.getDevice())) 454 return location; 455 int baseCount = base.segmentCount(); 456 int count = base.matchingFirstSegments(location); 457 String temp = ""; for (int j = 0; j < baseCount - count; j++) 459 temp += "../"; return new Path(temp).append(location.removeFirstSegments(count)); 461 } 462 463 467 public static String makeRelative(URL base, String absolute) { 468 try { 469 return makeRelative(base, new URL (absolute)).toExternalForm(); 470 } catch (MalformedURLException e) { 471 return absolute; 473 } 474 } 475 476 479 public static String canonicalizeURL(String url) { 480 if (!(isWindows && url.startsWith("file:"))) return url; 482 try { 483 String path = new URL (url).getPath(); 484 File file = new File (path); 486 path = file.toString().replace('\\', '/'); 487 if (Character.isUpperCase(path.charAt(0))) { 488 char[] chars = path.toCharArray(); 489 chars[0] = Character.toLowerCase(chars[0]); 490 path = new String (chars); 491 return new File (path).toURL().toExternalForm(); 492 } 493 } catch (MalformedURLException e) { 494 } 496 return url; 497 } 498 499 504 public static synchronized URL getInstallURL() { 505 if (instanceLocation == null) { 506 Filter filter = null; 507 try { 508 filter = getContext().createFilter(Location.INSTALL_FILTER); 509 } catch (InvalidSyntaxException e) { 510 } 513 instanceLocation = new ServiceTracker(getContext(), filter, null); 514 instanceLocation.open(); 515 } 516 517 Location location = (Location) instanceLocation.getService(); 518 519 if (location == null) 522 throw new IllegalStateException ("The installation location must not be null"); 524 return location.getURL(); 525 } 526 527 } 528 | Popular Tags |