2013-05-06 6 views
5

में संसाधन फ़ाइल को पढ़ने के लिए कैसे मैं osgi और blueprint का उपयोग करता हूं, मैं अपने बंडल में फ़ाइल को पढ़ने का तरीका खोजता हूं? जैसे: mybundleosgi blueprint बंडल

  • file.json
  • OSGi-INF/खाका/blueprint.xml
  • वेब-INF
  • *

मैंने पढ़ा फ़ाइल चाहते हैं। myservice में जेसन।

उत्तर

9

ऐसा करने के लिए आसान तरीका अपने सेम में bundlecontext सुई

blueprint.xml है

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

संभव संदर्भ:

blueprintBundle बंडल के बंडल वस्तु प्रदान करता है ।

ब्लूप्रिंटबंडल कॉन्टेक्स्ट बंडल की बंडल कॉन्टेक्स्ट ऑब्जेक्ट प्रदान करता है।

ब्लूप्रिंटकंटनर बंडल के लिए ब्लूप्रिंटकॉन्टेनर ऑब्जेक्ट प्रदान करता है।

blueprintConverter बंडल कि खाका कंटेनर का प्रकार रूपांतरण सुविधा तक पहुँच प्रदान करता लिए कनवर्टर वस्तु प्रदान करता है। प्रकार रूपांतरण में अधिक जानकारी है। स्रोत: 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; 
} 
संबंधित मुद्दे