2013-05-06 15 views

cevap

9

, kolay yöntem

blueprint.xml sizin fasulye BundleContext enjekte etmektir

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

olası referans:

blueprintBundle Paketin Paketi nesnesi sağlar .

blueprintBundleContext Paketteki BundleContext nesnesini sağlar.

blueprintContainer Grup için BlueprintContainer nesnesini sağlar.

blueprintConverter Blueprint Konteyner tipi dönüşüm tesisine erişim sağlar paket için Dönüştürücü nesnesi sağlar. Tür dönüşümü daha fazla bilgi içeriyor. kaynak: http://www.ibm.com/developerworks/opensource/library/os-osgiblueprint/

Ve sınıfında:

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