下面列出了org.osgi.framework.Bundle#getVersion ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Revisionsnummer und Erstellungsdatum dieser Instanz ermitteln. Dazu wird die beim letzten
* Commit von Subversion geänderte Variable LastChangedRevision untersucht, und fürs Datum das
* von ANT beim build eingetragene Datum gesucht. Wenn diese Instanz nicht von ANT erstellt
* wurde, handelt es sich um eine Entwicklerversion, welche unter Eclipse-Kontrolle abläuft.
*
* Note: Obsoleted with change to mercurial
*
* @param plugin
*/
public static String getRevision(final boolean withDate, CoreHub plugin){
StringBuilder sb = new StringBuilder();
Bundle bundle = plugin.getBundle();
org.osgi.framework.Version v = bundle.getVersion();
sb.append("[Bundle info: ").append(v.toString());
String check = System.getProperty("inEclipse"); //$NON-NLS-1$
if (check != null && check.equals("true")) { //$NON-NLS-1$
sb.append(" (developer version)");
}
if (withDate) {
long lastModify = bundle.getLastModified();
TimeTool tt = new TimeTool(lastModify);
sb.append("; ").append(tt.toString(TimeTool.DATE_ISO));
}
sb.append("]");
return sb.toString();
}
@Override
public BindingPublisher prepare(final Bundle bundle) {
if (visited.add(bundle.getSymbolicName()) && hasComponents(bundle)) {
if ("org.ops4j.pax.url.mvn".equals(bundle.getSymbolicName())) {
return null; // false-positive, this doesn't need preparing
}
prepareDependencies(bundle);
String bundleDetails = bundle.getSymbolicName() + " [" + bundle.getVersion() + "]";
try {
BindingPublisher publisher;
log.info("ACTIVATING {}", bundleDetails);
publisher = super.prepare(bundle);
log.info("ACTIVATED {}", bundleDetails);
return publisher;
}
catch (final Exception e) {
log.warn("BROKEN {}", bundleDetails);
throw e;
}
}
// make sure we have everything we need to start
else if (bundle.getBundleContext() == context) {
prepareDependencies(bundle);
}
return null;
}
@Override
public void update(ILaunchConfigurationWorkingCopy launchConfig,
IJavaProject javaProject, List<String> programArgs, List<String> vmArgs)
throws CoreException {
// As of Eclipse Kepler(4.3), the use of -XStartOnFirstThread is a built-in launch config
// attribute so it is not necessary to make it an explicit argument. This prevents issues when
// sharing a launch config file across multiple OS platforms.
Bundle bundle = Platform.getBundle("org.eclipse.platform");
if (bundle != null) {
Version bundleVersion = bundle.getVersion();
if (bundleVersion.getMajor() == 4 && bundleVersion.getMinor() >= 3) {
updateEclipse43(launchConfig, javaProject, programArgs, vmArgs);
} else {
updateNonEclipse43(launchConfig, javaProject, programArgs, vmArgs);
}
}
}
private IStatus getSARLVersionValidity(int ignoreCauses) {
final Bundle bundle = Platform.getBundle("io.sarl.lang"); //$NON-NLS-1$
if (bundle != null) {
final Version sarlVersion = bundle.getVersion();
final Version minVersion = Utilities.parseVersion(getMinimalSARLVersion());
final Version maxVersion = Utilities.parseVersion(getMaximalSARLVersion());
final int cmp = Utilities.compareVersionToRange(sarlVersion, minVersion, maxVersion);
if (cmp < 0 && (ignoreCauses & CODE_SARL_VERSION) == 0) {
return SARLEclipsePlugin.getDefault().createStatus(IStatus.ERROR,
CODE_SARL_VERSION,
MessageFormat.format(
Messages.AbstractSREInstall_0,
sarlVersion.toString(),
minVersion.toString()));
}
if (cmp > 0 && (ignoreCauses & CODE_SARL_VERSION) == 0) {
return SARLEclipsePlugin.getDefault().createStatus(IStatus.ERROR,
CODE_SARL_VERSION,
MessageFormat.format(
Messages.AbstractSREInstall_1,
sarlVersion.toString(),
maxVersion.toString()));
}
}
return null;
}
private String getBundleInfo (Bundle bundle) {
String state = null;
switch (bundle.getState()) {
case Bundle.UNINSTALLED: state = "UNINSTALLED"; break;
case Bundle.INSTALLED: state = "INSTALLED"; break;
case Bundle.RESOLVED: state = "RESOLVED"; break;
case Bundle.STARTING: state = "STARTING"; break;
case Bundle.STOPPING: state = "STOPPING"; break;
case Bundle.ACTIVE: state = "ACTIVE"; break;
}
return bundle.getSymbolicName() + " " + bundle.getVersion() + " ["+state+"]";
}
private boolean isFragment(Bundle bundle) {
BundleRevision bundleRevision = bundle.adapt(BundleRevision.class);
if (bundleRevision == null)
throw new NullPointerException("Null bundle revision means that bundle has probably been uninstalled: " +
bundle.getSymbolicName() + ":" + bundle.getVersion());
return (bundleRevision.getTypes() & BundleRevision.TYPE_FRAGMENT) != 0;
}
private static BundleInfo bundleInfo(Object component) {
try {
Bundle bundle = FrameworkUtil.getBundle(component.getClass());
String bundleName = bundle != null ?
bundle.getSymbolicName() + ":" + bundle.getVersion() :
"From classpath";
return new BundleInfo(component.getClass().getName(), bundleName);
} catch (Exception | NoClassDefFoundError e) {
return new BundleInfo("Unavailable, reconfiguration in progress.", "");
}
}
public static BundleInfo createBundleInfo(Bundle bundle, Collection<String> classEntries) {
BundleInfo bundleInfo = new BundleInfo(bundle.getSymbolicName(), bundle.getVersion(), bundle.getLocation(), webInfUrl(bundle),
bundle.adapt(BundleWiring.class).getClassLoader());
bundleInfo.setClassEntries(classEntries);
return bundleInfo;
}
/**
* Returns the classpath entry of the tla2tool.jar (the TLA tools)
*/
public static IPath getTLAToolsClasspath() {
Bundle bundle = null;
// find the tlatools bundle among all installed bundles
// (this code assumes tlatools has a bundle shape "dir")
Bundle[] bundles = Activator.getDefault().getBundle()
.getBundleContext().getBundles();
for (int i = 0; i < bundles.length; i++) {
Bundle aBundle = bundles[i];
if ("org.lamport.tlatools".equals(aBundle.getSymbolicName())) {
// OSGi supports multiple bundles with the same symbolic name, but
// different versions. This e.g. occurs after a Toolbox update
// before the old bundle version gets purged from the
// installation directory.
// Without this explicitly version check, the Toolbox
// might accidentally use an old tla version which leads to
// undefined behavior.
// @see Bug #285 in general/bugzilla/index.html
Version otherVersion = bundle != null ? bundle.getVersion()
: Version.parseVersion("0.0.0");
if (aBundle.getVersion().compareTo(otherVersion) > 0) {
bundle = aBundle;
}
}
}
if (bundle == null)
return null;
URL local = null;
try
{
local = FileLocator.toFileURL(bundle.getEntry("/")); //$NON-NLS-1$
} catch (IOException e)
{
return null;
}
String fullPath = new File(local.getPath()).getAbsolutePath();
return Path.fromOSString(fullPath);
}
/**
* Provides a string to debug bundle information.
*
* @param bundle the bundle
* @return a debug string
*/
public static String debugString(final Bundle bundle) {
return "Bundle [getState()=" + bundle.getState() + ", getHeaders()=" + bundle.getHeaders() + ", getBundleId()="
+ bundle.getBundleId() + ", getLocation()=" + bundle.getLocation() + ", getRegisteredServices()="
+ Arrays.toString(bundle.getRegisteredServices()) + ", getServicesInUse()="
+ Arrays.toString(bundle.getServicesInUse()) + ", getSymbolicName()=" + bundle.getSymbolicName()
+ ", getLastModified()=" + bundle.getLastModified() + ", getBundleContext()="
+ bundle.getBundleContext() + ", getVersion()=" + bundle.getVersion() + "]";
}
private static String manifestVersion() {
Bundle bundle = FrameworkUtil.getBundle(ProductVersion.class);
if (bundle == null) {
String implementationVersion = ProductVersion.class.getPackage().getImplementationVersion();
if (implementationVersion == null) {
return "7.6.0";//fake version used in unit test
}
return stripSnaphot(implementationVersion);
}
Version version = bundle.getVersion();
return String.format("%s.%s.%s", version.getMajor(), version.getMinor(), version.getMicro());
}
/**
* Creates {@code ManifestElement} instances from CARBON_COMPONENT_HEADER in the given bundle.
*
* @param bundle from the which the header value should retrieved.
* @return the created list of {@code ManifestElement} instances
*/
static List<ManifestElement> getManifestElements(Bundle bundle) {
String headerValue = AccessController.doPrivileged((PrivilegedAction<String>) () ->
bundle.getHeaders().get(CARBON_COMPONENT_HEADER));
try {
return ManifestElement.parseHeader(CARBON_COMPONENT_HEADER, headerValue, bundle);
} catch (ManifestElementParserException e) {
String message = "Error occurred while parsing the " + CARBON_COMPONENT_HEADER + " header in bundle(" +
bundle.getSymbolicName() + ":" + bundle.getVersion() + "). " + "Header value: " + headerValue;
throw new StartOrderResolverException(message, e);
}
}
@Before
public void setUp() throws Exception {
Bundle bundle = Platform.getBundle(SARL_LANG_BUNDLE_NAME);
Version osgiVersion = bundle.getVersion();
this.bundleVersion = Integer.toString(osgiVersion.getMajor())
+ "." + Integer.toString(osgiVersion.getMinor())
+ "." + Integer.toString(osgiVersion.getMicro());
if (!Strings.isNullOrEmpty(osgiVersion.getQualifier())) {
this.bundleVersion += "-" + Artifact.SNAPSHOT_VERSION;
}
assertNotNullOrEmpty(this.bundleVersion);
}
private static boolean isValid(Bundle bundle) {
if (bundle == null) {
log.error("invalid bundle: NULL");
return false;
}
if (bundle.getSymbolicName() == null) {
log.error("invalid bundle: no symbolic name");
return false;
}
if (bundle.getVersion() == null) {
log.error("invalid bundle: no version");
return false;
}
return true;
}
private boolean verifyValidity(ISREInstall sre, boolean errorMessages) {
if (!sre.getValidity().isOK()) {
if (errorMessages) {
setErrorMessage(MessageFormat.format(
io.sarl.eclipse.launching.dialog.Messages.RuntimeEnvironmentTab_5,
sre.getName()));
}
return false;
}
// Check the SARL version.
final Bundle bundle = Platform.getBundle("io.sarl.lang"); //$NON-NLS-1$
if (bundle != null) {
final Version sarlVersion = bundle.getVersion();
final Version minVersion = Utilities.parseVersion(sre.getMinimalSARLVersion());
final Version maxVersion = Utilities.parseVersion(sre.getMaximalSARLVersion());
final int cmp = Utilities.compareVersionToRange(sarlVersion, minVersion, maxVersion);
if (cmp < 0) {
if (errorMessages) {
setErrorMessage(MessageFormat.format(
io.sarl.eclipse.runtime.Messages.AbstractSREInstall_0,
sarlVersion.toString(),
minVersion.toString()));
}
return false;
} else if (cmp > 0) {
if (errorMessages) {
setErrorMessage(MessageFormat.format(
io.sarl.eclipse.runtime.Messages.AbstractSREInstall_1,
sarlVersion.toString(),
maxVersion.toString()));
}
return false;
}
}
return true;
}
BsnVersion(Bundle bundle) {
this.symbolicName = bundle.getSymbolicName();
this.version = bundle.getVersion();
}
@Override
public Object getValueAt(int row, int column)
{
final Bundle b = getBundle(row);
switch (column) {
case COL_LOCATION:
return Util.shortLocation(b.getLocation());
case COL_ID:
// return Long.toString(b.getBundleId());
return Long.valueOf(b.getBundleId());
case COL_STATE:
return Util.stateName(b.getState());
case COL_STARTLEVEL: {
final BundleStartLevel bsl = b.adapt(BundleStartLevel.class);
if (null != bsl) {
try {
final int n = bsl.getStartLevel();
return Integer.valueOf(n);
} catch (final Exception e) {
// not managed, indicate with -1
return Integer.valueOf(-1);
}
} else {
// No start level adaptation available, indicate with -2
return Integer.valueOf(-2);
}
}
case COL_DESC:
return Util.getHeader(b, Constants.BUNDLE_DESCRIPTION);
case COL_NAME:
return Util.getHeader(b, Constants.BUNDLE_NAME);
case COL_VENDOR:
return Util.getHeader(b, Constants.BUNDLE_VENDOR);
case COL_SYMBOLIC_NAME:
return b.getSymbolicName();
case COL_VERSION:
return b.getVersion();
default:
return null;
}
}
public static String getVersionedId(Bundle b) {
return b.getSymbolicName() + ":" + b.getVersion();
}
@Override
public boolean apply(Bundle input) {
return input.getSymbolicName() != null && input.getVersion() != null &&
symbolicName.matcher(input.getSymbolicName()).matches() &&
(version == null || version.matcher(input.getVersion().toString()).matches());
}
/** Finds all matching bundles, in decreasing version order. */
@SuppressWarnings("deprecation")
public List<Bundle> findAll() {
boolean urlMatched = false;
List<Bundle> result = MutableList.of();
String v=null, vDep = null;
VersionRange vRange = null;
if (version!=null) {
if (isVersionRange(version)) {
vRange = VersionRange.valueOf(version);
} else {
v = BrooklynVersionSyntax.toValidOsgiVersion(version);
vDep = OsgiUtils.toOsgiVersion(version);
}
}
for (Bundle b: framework.getBundleContext().getBundles()) {
if (symbolicName!=null && !symbolicName.equals(b.getSymbolicName())) continue;
if (version!=null) {
Version bv = b.getVersion();
if (vRange != null) {
if (!vRange.includes(bv)) {
continue;
}
} else {
String bvString = bv.toString();
if (!v.equals(bvString)) {
if (!vDep.equals(bvString)) {
continue;
}
LOG.warn("Legacy inferred OSGi version string '"+vDep+"' found to match "+symbolicName+":"+version+"; switch to '"+v+"' format to avoid issues with deprecated version syntax");
}
}
}
if (!Predicates.and(predicates).apply(b)) continue;
// check url last, because if it isn't mandatory we should only clear if we find a url
// for which the other items also match
if (url!=null) {
boolean matches = url.equals(b.getLocation());
if (urlMandatory) {
if (!matches) continue;
else urlMatched = true;
} else {
if (matches) {
if (!urlMatched) {
result.clear();
urlMatched = true;
}
} else {
if (urlMatched) {
// can't use this bundle as we have previously found a preferred bundle, with a matching url
continue;
}
}
}
}
result.add(b);
}
if (symbolicName==null && url!=null && !urlMatched) {
// if we only "preferred" the url, and we did not match it, and we did not have a symbolic name,
// then clear the results list!
result.clear();
}
Collections.sort(result, new Comparator<Bundle>() {
@Override
public int compare(Bundle o1, Bundle o2) {
int r = NaturalOrderComparator.INSTANCE.compare(o1.getSymbolicName(), o2.getSymbolicName());
if (r!=0) return r;
return VersionComparator.INSTANCE.compare(o1.getVersion().toString(), o2.getVersion().toString());
}
});
return result;
}