OSGI环境中 ,可以通过BundleContext 来获取环境中的bundle, 对bundle我们可以进行start,stop,uninstall,update; 安装可以通过bundleContext.install(路径);来安装; 但插件没有提供refresh的方法,在运行中的bundle通过update操作可能不会更新,安装补丁后,也需要刷新宿主,这样通过程序来控制得特殊处理;实际上提供了refresh方法,但比较不好找而已;
首先在你的Activator 中实现ServiceTrackerCustomizer接口
实现一个跟踪器,这样在程序中可以通过
首先在你的Activator 中实现ServiceTrackerCustomizer接口
public class Activator implements BundleActivator, ServiceTrackerCustomizer {
private ServiceTracker packageAdminTracker;
private static PackageAdmin packageAdmin;
private static BundleContext context;
public static BundleContext getContext() {
return context;
}
public static PackageAdmin getPackageAdminInstance() {
return packageAdmin;
}
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
Activator.resumePluginRegistry = new ArrayList();
packageAdminTracker = new ServiceTracker(context, PackageAdmin.class.getName(), this);
packageAdminTracker.open();
}
public void stop(BundleContext bundleContext) throws Exception {
packageAdminTracker.close();
packageAdminTracker = null;
}
public Object addingService(ServiceReference reference) {
synchronized (Activator.class) {
packageAdmin = (PackageAdmin) context.getService(reference);
}
return packageAdmin;
}
public void modifiedService(ServiceReference reference, Object service) {
}
public void removedService(ServiceReference reference, Object service) {
synchronized (Activator.class) {
context.ungetService(reference);
packageAdmin = null;
}
}
}
实现一个跟踪器,这样在程序中可以通过
Activator.getPackageAdminInstance().refreshPackages(new Bundle[] { bundle });
来更新你的要更新的插件