2013-05-23 10 views
5

का उपयोग करके मैं कुछ मॉड्यूल बना रहा हूं जिन्हें मैं ओएसजीआई पुस्तकालयों के लिए किसी भी वास्तविक निर्भरता के बिना ओएसजीआई बंडलों के रूप में उजागर करना चाहता हूं। ऐसा लगता है कि घोषणात्मक सेवाओं के विकल्प का उपयोग करना संभव है।एक एम्बेडेड ओएसजीआई कंटेनर

हालांकि, क्योंकि मैं ओएसजीआई (कम से कम बंडल बनाने वाली तरफ) के लिए नया हूं, मैं यह जांचना चाहता हूं कि यह सब काम करता है या नहीं, इस अंत में मैं एक छोटा एम्बेडेड ओएसजीआई वातावरण स्थापित करना चाहता हूं।

वर्तमान में मेरे पास एक एकल बंडल है जो एक एपीआई निर्यात करता है और एक इंटरफेस का एक स्टब कार्यान्वयन भी प्रदान करता है।

और एम्बेडेड फेलिक्स कार्यान्वयन काम करने के लिए लगता है ठीक से लेकिन वहाँ दो समस्याएं हैं:

मैं एक वातावरण स्थापित करने के लिए निम्नलिखित ट्यूटोरियल का पालन किया है

Bundle bundle = felix.getBundleContext().installBundle("/path/to/bundle.jar") 
bundle.start(); 
System.out.println(bundle.getRegisteredServices()); 

यह null प्रिंट करता है, जबकि बंडल प्रतीत होता है ठीक है, ऐसा लगता है कि यह किसी भी सेवा का पर्दाफाश नहीं करता है।

दूसरा मैं सोच रहा हूं कि मुझे घोषणात्मक सेवाओं को थोड़ा सा चलाने और चलाने के लिए कुछ खास करना है या नहीं। मेरे Maven निर्भरता हैं:

<dependencies> 
    <dependency> 
     <groupId>org.apache.felix</groupId> 
     <artifactId>org.apache.felix.framework</artifactId> 
     <version>4.2.1</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.felix</groupId> 
     <artifactId>org.apache.felix.scr</artifactId> 
     <version>1.6.2</version> 
    </dependency> 
</dependencies> 

ईमेल थ्रेड के आधार पर पाया गया: http://mail-archives.apache.org/mod_mbox/felix-users/201111.mbox/%[email protected]%3E

मैं फेलिक्स स्टार्टअप गुणों के बंडल जोड़ने की कोशिश की:

map.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "org.apache.felix.scr; version=1.6.2"); 

हालांकि इस थोड़ा आशावादी लगता है पहली नजर में। मैं एम्बेडेड फ़ेलिक्स इंजन के लिए घोषणात्मक सेवाएं कैसे सक्षम करूं?

उत्तर

6

दोनों समस्याओं का समाधान अपने स्वयं के बंडलों को लोड करने से पहले "स्क्रू" जार (घोषणात्मक सेवाओं को पार्स करने के लिए उपयोग किया जाता है) को लोड करना था।

क्योंकि जार मेरी Maven भंडार में स्थित है और यह पार प्रणाली काम करना चाहिए, कोड की निम्न बिट जहाँ भी यह रहता है से scr जार लोड करता है:

URL url = getClass().getClassLoader().getResource("org/apache/felix/scr/ScrService.class"); 
    String jarPath = url.toURI().getSchemeSpecificPart().replaceAll("!.*", ""); 
    framework.getBundleContext().installBundle(jarPath).start(); 

इस बिट मैं अपने खुद के बंडल लोड और बाद इसमें सेवाओं का पता लगाया गया था।

एक sidenote पर, आप प्रारंभिक नक्शा करने के लिए कुछ गुण जोड़कर लॉगिंग सक्षम कर सकते हैं:

map.put("ds.showtrace", "true"); 
    map.put("ds.showerrors", "true"); 

अधिक गुण http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html

भविष्य में संदर्भ के लिए में पाया जा सकता है, यहाँ सभी कोड मैं प्रयोग किया जाता है यह उठो और

private void initialize() throws BundleException, URISyntaxException { 
    Map<String, String> map = new HashMap<String, String>(); 

    // make sure the cache is cleaned 
    map.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT); 

    // more properties available at: http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html 
    map.put("ds.showtrace", "true"); 
    map.put("ds.showerrors", "true"); 

    System.out.println("Building OSGi Framework"); 
    FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next(); 
    Framework framework = frameworkFactory.newFramework(map); 

    System.out.println("Starting OSGi Framework"); 
    framework.start(); 

    // declarative services dependency is necessary, otherwise they won't be picked up! 
    loadScrBundle(framework); 

    framework.getBundleContext().installBundle("file:/path/to/myBundle.jar").start(); 

    ServiceReference reference = framework.getBundleContext().getServiceReference("my.Interface"); 
    System.out.println(framework.getBundleContext().getService(reference)); 

    for (Bundle bundle : framework.getBundleContext().getBundles()) { 
     System.out.println("Bundle: " + bundle.getSymbolicName()); 
     if (bundle.getRegisteredServices() != null) { 
      for (ServiceReference serviceReference : bundle.getRegisteredServices()) 
       System.out.println("\tRegistered service: " + serviceReference); 
     } 
    } 
} 

private void loadScrBundle(Framework framework) throws URISyntaxException, BundleException { 
    URL url = getClass().getClassLoader().getResource("org/apache/felix/scr/ScrService.class"); 
    if (url == null) 
     throw new RuntimeException("Could not find the class org.apache.felix.scr.ScrService"); 
    String jarPath = url.toURI().getSchemeSpecificPart().replaceAll("!.*", ""); 
    System.out.println("Found declarative services implementation: " + jarPath); 
    framework.getBundleContext().installBundle(jarPath).start(); 
} 
+0

हाय @nablex। मेरे पास एक समान प्रश्न है [** यहां: ** अपाचे मैवेन असेंबली प्लगइन ओएसजीआई बंडलों के साथ काम नहीं कर रहा है] (http://stackoverflow.com/questions/34886012/apache-maven-assembly-plugin-not-working-with-osgi -बंडल्स) और उम्मीद कर रहे थे कि आप एक नज़र डालें और देखें कि क्या आप मदद कर सकते हैं। मैं वास्तव में अटक गया हूं और आप जो भी मदद कर सकते हैं उसकी सराहना करेंगे। पहले ही, आपका बहुत धन्यवाद। –

1

चलाने के लिए मैं pax exam पर एक नज़र लेने के लिए सुझाव देते हैं। यह एक ओएसजीआई कंटेनर में आपके बंडल का परीक्षण करने की अनुमति देता है। अच्छी बात यह है कि यह जूनिट के साथ एकीकृत करता है ताकि आपका परीक्षण सामान्य परीक्षणों के समान दिखता हो। कुछ उदाहरणों के लिए Apache Karaf Tests देखें।

संबंधित मुद्दे