public static void activateBundlesFromFile(BundleContext context, String bundleStartupFileName) throws IOException, BundleException { Map<String, Version> bundleMap = readBundleActivationFile(context.getBundle(), bundleStartupFileName); Map<String, Bundle> startupBundleMap = new HashMap<String, Bundle>(); for (Bundle b : context.getBundles()) { String symbolicName = b.getSymbolicName(); if (bundleMap.containsKey(symbolicName)) { Version version = b.getVersion(); Version reqVersion = bundleMap.get(symbolicName); if (version.getMajor() == reqVersion.getMajor() && version.getMinor() >= reqVersion.getMinor()) { if (startupBundleMap.containsKey(symbolicName)) { Bundle previousBundle = startupBundleMap.get(symbolicName); if (version.compareTo(previousBundle.getVersion()) <= 0) { break; } } startupBundleMap.put(symbolicName, b); } } } for (Bundle startupBundle : startupBundleMap.values()) { logger.log(Level.INFO, "Starting bundle: " + startupBundle); startupBundle.start(); } }
/** * Compares v1 <-> v2, if v1 is greater than v2, then result will be > 0 if v2 is greater than v1 * the result will be < 0 * * @param v1 * @param v2 * @return */ public static int compareVersions(Version v1, Version v2) { if (v2 == v1) { // quicktest return 0; } int result = v1.getMajor() - v2.getMajor(); if (result != 0) { return result; } result = v1.getMinor() - v2.getMinor(); if (result != 0) { return result; } result = v1.getMicro() - v2.getMicro(); if (result != 0) { return result; } return v1.getQualifier().compareTo(v2.getQualifier()); }
/** Note: Used by Grails tooling */ public static boolean isGroovyVersionDisabledOrMissing(SpecifiedVersion version) { BundleDescription disabledBundle = null; disabledBundle = getDisabledBundleDescription(version); if (disabledBundle != null) { return true; } Bundle[] active = Platform.getBundles("org.codehaus.groovy", version.toVersionString()); if (active == null) { return true; } // getBundles returns bundles with version >= specified version, // so must do one more check to see if there is a bundle where the // major.minor version matches for (Bundle bundle : active) { Version bundleVersion = bundle.getVersion(); if (bundleVersion.getMajor() == version.majorVersion && bundleVersion.getMajor() == version.majorVersion) { return false; } } // no bundle with specifed version has been found return true; }
/** * Pings the usage start server. * * @param pluginName the name of the plugin to appear in the stats * @param pluginVersion the {@link Version} of the plugin. */ public static void pingUsageServer(String pluginName, Version pluginVersion) { String versionString = String.format( "%1$d.%2$d.%3$d", pluginVersion.getMajor(), pluginVersion.getMinor(), pluginVersion.getMicro()); SdkStatsService.ping(pluginName, versionString); }
/** * Given a precise version create a version range suitable for an import package specification. * Currently an input version of M.N.U.Q will result in an output range "[M.N.U.Q, M+1)" following * the version usage recommended by OSGi (a package that is not backwards compatible must * increment the major number in its version number). * * @param version an OSGi compatibel version on string form. * @return a quoted version range starting with the given version (inclusive) ending with the next * major version (exclusive). If the specified version is <code>null</code> or an empty string * a <code>null</code> is returned. */ private static String toImportRange(final String version) throws IllegalArgumentException { if (null == version || 0 == version.length()) { return null; } final Version vStart = Version.parseVersion(version); final Version vEnd = new Version(vStart.getMajor() + 1, 0, 0, null); return "\"[" + vStart.toString() + "," + vEnd.toString() + ")\""; }
private static URL findNextBestProfile(String javaEdition, Version javaVersion) { URL result = null; int minor = javaVersion.getMinor(); do { result = findInSystemBundle( javaEdition + javaVersion.getMajor() + "." + minor + PROFILE_EXT); // $NON-NLS-1$ minor = minor - 1; } while (result == null && minor > 0); return result; }
public static String getTargetVersionString() { IMonitorModelBase model = MonitorRegistry.findModel(IPDEBuildConstants.BUNDLE_OSGI); if (model == null) return ICoreConstants.TARGET37; String version = model.getMonitorBase().getVersion(); if (VersionUtil.validateVersion(version).getSeverity() == IStatus.OK) { Version vid = new Version(version); int major = vid.getMajor(); int minor = vid.getMinor(); if (major == 3 && minor == 0) return ICoreConstants.TARGET30; if (major == 3 && minor == 1) return ICoreConstants.TARGET31; if (major == 3 && minor == 2) return ICoreConstants.TARGET32; if (major == 3 && minor == 3) return ICoreConstants.TARGET33; if (major == 3 && minor == 4) return ICoreConstants.TARGET34; if (major == 3 && minor == 5) return ICoreConstants.TARGET35; if (major == 3 && minor == 6) return ICoreConstants.TARGET36; } return ICoreConstants.TARGET37; }
public long getLastModifiedTime(String file) { if (fBundleVersion == -1) { Version v = SVCorePlugin.getDefault().getBundle().getVersion(); // Ensure the version is always really big to work around the temporary // current problem of having existing versions out in the wild fBundleVersion = v.getMicro(); fBundleVersion |= v.getMinor() << 16; fBundleVersion |= v.getMajor() << 24; fBundleVersion |= (8L << 48L); } if (fBundleVersion < fBundle.getLastModified()) { System.out.println( "computed bundle version older than BundleVersion: " + fBundleVersion + " " + fBundle.getLastModified()); } return fBundleVersion; }
Map<String, Result> install( final Collection<Patch> patches, boolean simulate, boolean synchronous) { try { // Compute individual patch results final Map<String, Result> results = new LinkedHashMap<String, Result>(); final Map<Bundle, String> toUpdate = new HashMap<Bundle, String>(); Map<String, BundleUpdate> allUpdates = new HashMap<String, BundleUpdate>(); for (Patch patch : patches) { String startup = readFully(new File(System.getProperty("karaf.base"), "etc/startup.properties")); String overrides = readFully(new File(System.getProperty("karaf.base"), "etc/overrides.properties")); List<BundleUpdate> updates = new ArrayList<BundleUpdate>(); Bundle[] allBundles = bundleContext.getBundles(); for (String url : patch.getBundles()) { JarInputStream jis = new JarInputStream(new URL(url).openStream()); jis.close(); Manifest manifest = jis.getManifest(); Attributes att = manifest != null ? manifest.getMainAttributes() : null; String sn = att != null ? att.getValue(Constants.BUNDLE_SYMBOLICNAME) : null; String vr = att != null ? att.getValue(Constants.BUNDLE_VERSION) : null; if (sn == null || vr == null) { continue; } Version v = VersionTable.getVersion(vr); VersionRange range = null; if (patch.getVersionRange(url) == null) { // default version range starts with x.y.0 as the lower bound Version lower = new Version(v.getMajor(), v.getMinor(), 0); // We can't really upgrade with versions such as 2.1.0 if (v.compareTo(lower) > 0) { range = new VersionRange(false, lower, v, true); } } else { range = new VersionRange(patch.getVersionRange(url)); } if (range != null) { for (Bundle bundle : allBundles) { Version oldV = bundle.getVersion(); if (bundle.getBundleId() != 0 && stripSymbolicName(sn).equals(stripSymbolicName(bundle.getSymbolicName())) && range.contains(oldV)) { String location = bundle.getLocation(); BundleUpdate update = new BundleUpdateImpl(sn, v.toString(), url, oldV.toString(), location); updates.add(update); // Merge result BundleUpdate oldUpdate = allUpdates.get(sn); if (oldUpdate != null) { Version upv = VersionTable.getVersion(oldUpdate.getNewVersion()); if (upv.compareTo(v) < 0) { allUpdates.put(sn, update); toUpdate.put(bundle, url); } } else { toUpdate.put(bundle, url); } } } } else { System.err.printf( "Skipping bundle %s - unable to process bundle without a version range configuration%n", url); } } if (!simulate) { new Offline(new File(System.getProperty("karaf.base"))) .applyConfigChanges(((PatchImpl) patch).getPatch()); } Result result = new ResultImpl( patch, simulate, System.currentTimeMillis(), updates, startup, overrides); results.put(patch.getId(), result); } // Apply results System.out.println("Bundles to update:"); for (Map.Entry<Bundle, String> e : toUpdate.entrySet()) { System.out.println( " " + e.getKey().getSymbolicName() + "/" + e.getKey().getVersion().toString() + " with " + e.getValue()); } if (simulate) { System.out.println("Running simulation only - no bundles are being updated at this time"); } else { System.out.println( "Installation will begin. The connection may be lost or the console restarted."); } System.out.flush(); if (!simulate) { Thread thread = new Thread() { public void run() { try { applyChanges(toUpdate); for (Patch patch : patches) { Result result = results.get(patch.getId()); ((PatchImpl) patch).setResult(result); saveResult(result); } } catch (Exception e) { e.printStackTrace(System.err); System.err.flush(); } } }; if (synchronous) { thread.run(); } else { thread.start(); } } return results; } catch (Exception e) { throw new PatchException(e); } }
/** @return the major and minor parts of the given version */ private static Version toMajorMinorVersion(Version version) { return new Version(version.getMajor(), version.getMinor(), 0); }