0

我有一些应用程序,调用了一些 jar 库,调用了 Felix 框架并从 bundles 目录添加了一些包。

项目方案

“显示帮助”按钮的代码:

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           

        for (Bundle qqq : context.getBundles()) {
            if ("ihtika2.I_AboutForm".equals(qqq.getSymbolicName())) {
                System.out.println("I_AboutForm state: "+qqq.getState());
            }
        }

        try {
            ServiceReference[] refs = context.getServiceReferences(
                    AboutFormInterface.class.getName(), "(Funct=*)");
            if (refs == null) {
                System.out.println("Not Found AboutForm on show");
            } else {
                AboutFormInterface AboutForm = (AboutFormInterface) context.getService(refs[0]);
                AboutForm.showWindow(context);
            }
        } catch (Exception ex) {
            Ini.logger.fatal("Error on showing AboutForm", ex);
        }

    }  

In first click (before update) service founded correctly.

The code of the "Update" button:

  try {
            ServiceReference[] refs = context.getServiceReferences(
                    UpdaterInt.class.getName(), "(Funct=*)");
            if (refs == null) {
                System.out.println("Not Found UpdaterInt on demand");
            } else {
                UpdaterInt UpdaterLocal = (UpdaterInt) context.getService(refs[0]);
                UpdaterLocal.update();
            }
        } catch (Exception ex) {
            Ini.logger.fatal("Error on showing AboutForm", ex);
        }

更新程序包的代码:

public void update() {

        System.out.println("Stage 1");

        int x = 0;


        Map<String, Bundle> bundlesList = new HashMap<String, Bundle>();

        for (Bundle singleBundle : context.getBundles()) {
            bundlesList.put(singleBundle.getLocation(), singleBundle);
            System.out.println(singleBundle.getLocation());
        }

        System.out.println("+++++++++++++++++++++++++++++");

        Map<String, Bundle> treeMap = new TreeMap<String, Bundle>(bundlesList);
        for (String str : treeMap.keySet()) {
            Bundle singleBundle = treeMap.get(str);
            System.out.println(str + " " + singleBundle.getSymbolicName());
            if (!"khartn.Updater".equals(singleBundle.getSymbolicName())
                    && !"org.apache.felix.framework".equals(singleBundle.getSymbolicName())) {
                try {
                    System.out.println("state0 " + singleBundle.getState());
                    singleBundle.stop();
                    System.out.println("state1 " + singleBundle.getState());
                    singleBundle.update();
                    System.out.println("state1.1 " + singleBundle.getState());
                    singleBundle.start();

                    synchronized (this) {
                        try {
                            this.wait(250);
                        } catch (InterruptedException ex) {
                            ex.printStackTrace();
                        }
                    }
                    System.out.println("state2 " + singleBundle.getState());
                } catch (BundleException ex) {
                    ex.printStackTrace();
                }
                System.out.println("***");
            }
        }


    }

在更新主 JFrame 时正确更新 - 它隐藏和显示。但是在第二次点击“显示帮助”时,Felix 找不到“显示帮助”的服务。“显示帮助”服务注册码:

package ihtika2.i_aboutform;

import ihtika2.i_aboutform.service.AboutForm;
import ihtika2.i_aboutform.service.AboutFormInterface;
import java.util.Hashtable;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

public class Activator implements BundleActivator {

    @Override
    public void start(BundleContext context) throws Exception {
        Hashtable<String, String> props = new Hashtable<String, String>();
        props.put("Funct", "AboutForm");
        System.out.println("Registering I_AboutForm");
         context.registerService(AboutFormInterface.class.getName(), new AboutForm(), props);

    }

    @Override
    public void stop(BundleContext context) throws Exception {
    }
}

更新时显示字符串“Registering I_AboutForm”。

为什么Felix找不到重新注册的服务?

4

2 回答 2

1

解决方案是将更新程序的捆绑代码调用添加到

context.getBundle(0).adapt(FrameworkWiring.class).refreshBundles(bundles, null);

OSGi API 文档中所述:

如果此包导出了由另一个包导入的任何包,则这些包必须保持导出状态,直到 FrameworkWiring.refreshBundles调用该方法或重新启动框架。

于 2012-09-21T08:59:34.973 回答
0

未找到服务的原因是接口AboutFormInterface仍然连接到更新捆绑包的先前版本。

解决方案之一是在第三个 Bundle 中定义接口,以便在更新期间保持稳定。

于 2015-12-31T02:24:26.660 回答