获取bundle的绝对路径

最近碰到个小麻烦,如何从bundle获取其绝对路径。

 

解决办法如下:

 

public void start(BundleContext context) throws Exception {

    String bundleName = context.getBundle().getSymbolicName();
	log.debug("bundleName:" + bundleName);
	String bundlesInfo = System.getProperty("osgi.bundles");
	int bundleNameStart = bundlesInfo.indexOf(bundleName);
	int bundleNameEnd = bundleNameStart + bundleName.length();
	String prependedBundlePath = bundlesInfo.substring(0, bundleNameEnd);
	String prefix = "reference:file:";
	int prefixPos = prependedBundlePath.lastIndexOf(prefix);
	String bundlePath = prependedBundlePath;
	if (prefixPos >= 0) {
		bundlePath = prependedBundlePath.substring(prefixPos + prefix.length(), prependedBundlePath.length());
		}
	log.info("bundlePath=" + bundlePath);

}
 

 

你可能感兴趣的:(osgi)