OSGI:捆绑包更新后服务不可用

OSGI:捆绑包更新后服务不可用,osgi,updates,equinox,osgi-bundle,Osgi,Updates,Equinox,Osgi Bundle,我有一个Bundle p(rovider=它实现了Bundle I(Interface)中定义的接口Bundle U(ser)应该使用此服务。在我启动应用程序后,一切正常,我的所有服务都可以使用。但是当我更新Bundle p时,它的非服务性问题可以得到解决 更新方法如下所示: this._bundle.stop(); this._bundle.update(new FileInputStream(updateFile)); this._bundle.start(); 这是我的所有包的Bundl

我有一个Bundle p(rovider=它实现了Bundle I(Interface)中定义的接口Bundle U(ser)应该使用此服务。在我启动应用程序后,一切正常,我的所有服务都可以使用。但是当我更新Bundle p时,它的非服务性问题可以得到解决

更新方法如下所示:

this._bundle.stop();
this._bundle.update(new FileInputStream(updateFile));
this._bundle.start();
这是我的所有包的BundleActivator,它还应处理服务注册和服务引用:

public abstract class BundleActivator implements org.osgi.framework.BundleActivator, BundleListener, ServiceListener
{
/**
 * Bundle Context.
 */
protected BundleContext context;

/**
 * Services which are registered by this bundle.
 */
protected HashSet<ServiceRegistration> serviceRegistrations = new HashSet<ServiceRegistration>();

/**
 * Service references used by this bundle.
 */
protected HashMap<String, ServiceReference> serviceReferences = new HashMap<String, ServiceReference>();

/**
 * Perform this method after Bundle has started.
 *
 * @param bc BundleContext.
 */
protected abstract void afterStart(BundleContext bc);

/**
 * Perform this method before Bundle is going to be stoped.
 *
 * @param bc BundleContext.
 */
protected abstract void beforeStop(BundleContext bc);

/**
 * Perform this method after Bundle has changed.
 *
 * @param be BundleEvent
 */
protected abstract void afterBundleChanged(BundleEvent be);

/**
 * Returns the bundle context for this bundle.
 *
 * @return bundle context
 */
public BundleContext getContext() {
    return this.context;
}

/**
 * Registeres a service.
 *
 * @param clazz      interface
 * @param service    service
 * @param properties properties
 *
 * @return service registration
 */
public ServiceRegistration registerService(Class clazz, Object service, Dictionary<String, ?> properties) {
    return this.registerService(clazz.getCanonicalName(), service, properties);
}

/**
 * Registeres a service.
 *
 * @param clazz      interface
 * @param service    service
 * @param properties properties
 *
 * @return service registration
 */
public ServiceRegistration registerService(String clazz, Object service, Dictionary<String, ?> properties) {
    ServiceRegistration retval = this.context.registerService(clazz, service, properties);
    System.out.println("registered service: " + retval.toString() + " for " + clazz);
    this.serviceRegistrations.add(retval);
    return retval;
}

/**
 * Returns a registered service.
 *
 * @param clazz interface
 *
 * @return service instance
 */
public Object getService(Class clazz) {
    if (clazz == null) {
        return null;
    }
    return this.getService(clazz.getCanonicalName());
}

/**
 * Returns a registered service.
 *
 * @param clazz interface
 *
 * @return service instance
 */
public Object getService(String clazz) {
    if (clazz == null) {
        return null;
    }
    System.out.println("Class: " + clazz);
    ServiceReference sr = this.context.getServiceReference(clazz);
    System.out.println("SR: " + sr);
    if (sr == null) {
        if (this.serviceReferences.containsKey(clazz)) {
            System.out.println("Unget service");
            this.context.ungetService(this.serviceReferences.get(clazz));
            this.serviceReferences.remove(clazz);
        }
        sr = this.context.getServiceReference(clazz);
        System.out.println("SR: " + sr);
        if (sr == null) {
            return null;
        }
    }

    try {
        this.context.addServiceListener(this, "(objectClass=" + clazz + ")");
    } catch (InvalidSyntaxException ex) {
        Logger.getLogger(BundleActivator.class.getName()).log(Level.SEVERE, null, ex);
    }

    this.serviceReferences.put(clazz, sr);
    return this.context.getService(sr);
}

@Override
public void start(BundleContext bc) throws Exception {
    ContextRegistry.getInstance().add(bc);

    this.context = bc;
    this.context.addBundleListener(this);
    this.afterStart(bc);
    System.out.println("Balindoo bundle activated: " + this.getClass().getPackage().getName());
}

@Override
public void stop(BundleContext bc) throws Exception {
    this.beforeStop(bc);

    for (ServiceRegistration sr : this.serviceRegistrations) {
        this.context.ungetService(sr.getReference());
        sr.unregister();
    }
    this.serviceRegistrations.clear();

    for (ServiceReference sr : this.serviceReferences.values()) {
        this.context.ungetService(sr);
    }
    this.serviceReferences.clear();

    ContextRegistry.getInstance().remove(bc);
    this.context.removeBundleListener(this);
    this.context = null;
    System.out.println("Balindoo bundle deactivated: " + this.getClass().getPackage().getName());
}

@Override
public void bundleChanged(BundleEvent be) {
    String name = be.getBundle().getSymbolicName();
    if (name.startsWith("com.vaadin")) {
        if (be.getType() == BundleEvent.STARTED && !ResourceProvider.getInstance().hasBundle(be.getBundle())) {
            ResourceProvider.getInstance().add(be.getBundle());
        } else if (be.getType() == BundleEvent.STOPPED && ResourceProvider.getInstance().hasBundle(be.getBundle())) {
            ResourceProvider.getInstance().remove(be.getBundle());
        }
    }

    for (ServiceReference sr : this.serviceReferences.values()) {
        this.context.ungetService(sr);
    }
    this.serviceReferences.clear();

    HashSet<Bundle> thisBundle = new HashSet<>();
    thisBundle.add(this.context.getBundle());
    thisBundle.add(be.getBundle());

    FrameworkWiring wiring = this.context.getBundle().adapt(FrameworkWiring.class);
    if (wiring != null) {
        System.out.println("FrameworkWiring:\n\tthis:\t" + this.context.getBundle().getSymbolicName() + "\n\tto  :\t" + be.getBundle().getSymbolicName());
        wiring.refreshBundles(thisBundle);
    }

    this.afterBundleChanged(be);
}

@Override
public void serviceChanged(ServiceEvent event) {
    switch (event.getType()) {
        case ServiceEvent.UNREGISTERING:
            System.out.println("unregister service");
            if (this.serviceReferences.containsValue(event.getServiceReference())) {
                //Remove
            }
            this.context.ungetService(event.getServiceReference());
            break;
    }
}
接口捆绑包的清单:

Manifest-Version: 1.0
Bnd-LastModified: 1381156992131
Build-Jdk: 1.7.0_21
Built-By: nspecht
Bundle-Activator: org.company.example.data.api.impl.BundleActivator
Bundle-Description: Example Bundle org.company.example.data.api 1.0.0
Bundle-ManifestVersion: 2
Bundle-Name: org.company.example.data.api
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-SymbolicName: org.company.org.company.example.data.api
Bundle-Vendor: Company Inc.
Bundle-Version: 1.0.0
Created-By: Apache Maven Bundle Plugin
Eclipse-BuddyPolicy: registered
Export-Package: org.company.example.data.api;uses:="org.company.example.data.api.model";version="1.0.0",org.company.example.data.api.impl;uses:="org.osgi.framework,org.company.utils.modulemanager.generic";version="1.0.0",org.company.example.data.api.model;uses:="org.company.utils.data.api";version="1.0.0"
Import-Package: org.company.utils.data.api;version="[1.0,2)",org.company.utils.modulemanager.generic;version="[1.0,2)",org.osgi.framework;version="[1.6,2)"
Tool: Bnd-1.50.0
至少捆绑U的接口:

Manifest-Version: 1.0
Bnd-LastModified: 1381157008373
Build-Jdk: 1.7.0_21
Built-By: nspecht
Bundle-Activator: org.company.example.webui.impl.BundleActivator
Bundle-Description: Example Bundle org.company.example.webui 1.0.0
Bundle-ManifestVersion: 2
Bundle-Name: org.company.example.webui
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-SymbolicName: org.company.org.company.example.webui
Bundle-Vendor: Company Inc.
Bundle-Version: 1.0.0
Created-By: Apache Maven Bundle Plugin
Eclipse-BuddyPolicy: registered
Export-Package: org.company.example.webui.impl;uses:="org.company.utils.webui,org.company.example.data.api,org.company.example.webui.menu,org.osgi.framework,org.company.utils.webui.menu,org.company.example.webui.view,org.company.utils.modulemanager.generic,org.company.utils.modulemanager.exception";version="1.0.0",org.company.example.webui.menu;uses:="org.company.utils.webui,org.company.utils.webui.generics,org.company.utils.modulemanager.translation,org.company.example.webui.view,org.company.utils.webui.menu";version="1.0.0",org.company.example.webui.i18n;version="1.0.0",org.company.example.webui.view;uses:="org.company.example.data.api,com.vaadin.server,org.company.utils.modulemanager.translation,org.company.example.data.api.model,com.vaadin.ui,org.company.example.webui.impl,com.vaadin.event,org.company.utils.webui.exception,com.vaadin.data,com.vaadin.data.util,com.vaadin.navigator,org.company.utils.modulemanager.exception";version="1.0.0"
Import-Package: com.vaadin.data;version="[7.1,8)",com.vaadin.data.util;version="[7.1,8)",com.vaadin.event;version="[7.1,8)",com.vaadin.navigator;version="[7.1,8)",com.vaadin.server;version="[7.1,8)",com.vaadin.ui;version="[7.1,8)",org.company.example.data.api;version="[1.0,2)",org.company.example.data.api.model;version="[1.0,2)",org.company.utils.modulemanager.exception;version="[1.0,2)",org.company.utils.modulemanager.generic;version="[1.0,2)",org.company.utils.modulemanager.translation;version="[1.0,2)",org.company.utils.webui;version="[1.0,2)",org.company.utils.webui.exception;version="[1.0,2)",org.company.utils.webui.generics;version="[1.0,2)",org.company.utils.webui.menu;version="[1.0,2)",org.osgi.framework;version="[1.6,2)"
Tool: Bnd-1.50.0

您很可能还需要“刷新”您的消费捆绑包(U),因为通常消费捆绑包会保留对原始捆绑包p的引用,因此它不会看到您的新服务。因此,您需要将捆绑包U带回“解析”状态状态。在GOGO shell上,您通常只需发出刷新命令。

您很可能还需要“刷新”您的消费捆绑包(U),因为通常消费捆绑包会保留对原始捆绑包p的引用,因此它不会看到您的新服务。因此,您需要将捆绑包U带回“解析”状态状态。在GOGO shell上,您通常只需发出刷新命令。

检查谁正在导出包含服务接口I的包。p和U必须连接到同一个包。为了确保U可以使用服务对象,框架验证U是否连接到与p相同的包

似乎在更新p之后,新的p'连接到与U不同的软件包版本。因此,当p'注册服务时,它来自软件包的不同版本


如果包包含在p中并由p导出,而p不同时导入相同的包,则可能会发生这种情况。服务提供商应同时导出和导入服务接口包。请参见检查谁正在导出包含服务接口I的包。p和U必须连接到同一个包。为了确保U可以n使用服务对象,框架验证U是否连接到与P相同的包

似乎在更新p之后,新的p'连接到与U不同的软件包版本。因此,当p'注册服务时,它来自软件包的不同版本


如果包包含在p中并由p导出,而p不导入相同的包,则可能会发生这种情况。服务提供商应同时导出和导入服务接口包。请参见

My package p正在导入和导出接口所在的包。您的捆绑包p正在导入和导出包Age。也许你可以共享清单?你应该确保更新后捆绑包P和捆绑包U连接到服务接口的同一个包。我更新了3个manifest.MF文件。我在上一篇文章中犯了一个错误。捆绑包P-不是-导入接口所在的捆绑包。我将其作为依赖项添加,并告诉maven to导出每个名为org.company.example.data.*的包(实际上包括org.company.example.data.api)。这可能是我的错误吗?在更改包和包名称后,它可以工作。感谢您的提示。我的包P正在导入和导出接口所在的包。您的意思是您的包P正在导入和导出包。也许您可以共享清单?您应该确保更新包P和包U后e连接到服务接口的同一个包。我更新了3个MANIFEST.MF文件。我在上一篇文章中犯了一个错误。捆绑包P-不是-导入接口所在的捆绑包。我将其添加为依赖项,并告诉maven导出每个名为org.company.example.data的包。*(实际上包括org.company.example.data.api)。这可能是我的错误吗?在更改捆绑包和包名称后,它可以工作。感谢您的提示。调用刷新命令没有帮助。我还尝试停止和启动U,但也不起作用调用刷新命令没有帮助。我还尝试停止和启动U,但也不起作用
Manifest-Version: 1.0
Bnd-LastModified: 1381157008373
Build-Jdk: 1.7.0_21
Built-By: nspecht
Bundle-Activator: org.company.example.webui.impl.BundleActivator
Bundle-Description: Example Bundle org.company.example.webui 1.0.0
Bundle-ManifestVersion: 2
Bundle-Name: org.company.example.webui
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-SymbolicName: org.company.org.company.example.webui
Bundle-Vendor: Company Inc.
Bundle-Version: 1.0.0
Created-By: Apache Maven Bundle Plugin
Eclipse-BuddyPolicy: registered
Export-Package: org.company.example.webui.impl;uses:="org.company.utils.webui,org.company.example.data.api,org.company.example.webui.menu,org.osgi.framework,org.company.utils.webui.menu,org.company.example.webui.view,org.company.utils.modulemanager.generic,org.company.utils.modulemanager.exception";version="1.0.0",org.company.example.webui.menu;uses:="org.company.utils.webui,org.company.utils.webui.generics,org.company.utils.modulemanager.translation,org.company.example.webui.view,org.company.utils.webui.menu";version="1.0.0",org.company.example.webui.i18n;version="1.0.0",org.company.example.webui.view;uses:="org.company.example.data.api,com.vaadin.server,org.company.utils.modulemanager.translation,org.company.example.data.api.model,com.vaadin.ui,org.company.example.webui.impl,com.vaadin.event,org.company.utils.webui.exception,com.vaadin.data,com.vaadin.data.util,com.vaadin.navigator,org.company.utils.modulemanager.exception";version="1.0.0"
Import-Package: com.vaadin.data;version="[7.1,8)",com.vaadin.data.util;version="[7.1,8)",com.vaadin.event;version="[7.1,8)",com.vaadin.navigator;version="[7.1,8)",com.vaadin.server;version="[7.1,8)",com.vaadin.ui;version="[7.1,8)",org.company.example.data.api;version="[1.0,2)",org.company.example.data.api.model;version="[1.0,2)",org.company.utils.modulemanager.exception;version="[1.0,2)",org.company.utils.modulemanager.generic;version="[1.0,2)",org.company.utils.modulemanager.translation;version="[1.0,2)",org.company.utils.webui;version="[1.0,2)",org.company.utils.webui.exception;version="[1.0,2)",org.company.utils.webui.generics;version="[1.0,2)",org.company.utils.webui.menu;version="[1.0,2)",org.osgi.framework;version="[1.6,2)"
Tool: Bnd-1.50.0