2013-05-06 79 views

回答

9

要做到這一點,簡單的方法是在你的bean注入的BundleContext

blueprint.xml

<bean id="plugin" class="com.timactive.MyBean" init-method="start"> 
    <property name="bcontext" ref="blueprintBundleContext"></property> 
</bean> 

可能參考:

blueprintBundle 提供包的Bundle對象。

blueprintBundleContext 提供了bundle的BundleContext對象。

blueprintContainer 爲包提供BlueprintContainer對象。

blueprintConverter 爲提供對Blueprint容器類型轉換功能的訪問的包提供了Converter對象。類型轉換具有更多信息。 來源:http://www.ibm.com/developerworks/opensource/library/os-osgiblueprint/

而在你的類:

import org.osgi.framework.Bundle; 
import org.osgi.framework.BundleContext 
public class MyBean { 

    public BundleContext bcontext; 
    public boolean start(){ 
    try { 
    Bundle bundle = bcontext.getBundle(); 
    InputStream is = bundle.getEntry("/file.json").openStream(); 
    String jsondb = readFile(is); 

    } catch (IOException e) { 
       LOG.error("The file treefield.json not found", e); 
       return(false); 
      } 

     } 

     return(true); 
    } 

    private String readFile(InputStream is) throws IOException { 
     java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); 
     return s.hasNext() ? s.next() : ""; 
    } 
    public void setBcontext(BundleContext bcontext) { 
    this.bcontext = bcontext; 
}