2012-09-21 57 views
0

我有一些應用程序,什麼叫一些jar庫,什麼叫Felix框架,並添加了一些捆綁軟件包的目錄。Felix - 如何正確地重新註冊捆綁更新服務?

project scheme

「顯示幫助」 按鈕的代碼:

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 { 
    } 
} 

串上更新「註冊I_AboutForm」顯示。

爲什麼Felix找不到重新註冊的服務?

回答

1

的解決方案是增加了更新的束代碼調用

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

,因爲這在OSGi API Documentation描述:

如果這束已出口由另一個 束導入任何包,這些軟件包必須保持導出狀態,直到 FrameworkWiring.refreshBundles方法被調用或 框架重新啓動。

0

找不到服務的原因是接口AboutFormInterface仍然連線到更新軟件包的先前版本。

其中一個解決方案是在第三個Bundle中定義接口,以便在更新期間保持穩定。