2013-05-18 137 views
0

我是Apache Felix的新用户。我想创建基于Apache Felix框架的简单桌面应用程序。我发现了this简单的例子,如何创建一个简单的主要方法作为应用程序启动框架,但我不知道如何添加服务到应用程序。如何将服务添加到桌面Apache Felix应用程序

package com.paint.servicebased; 

import java.util.Map; 
import java.util.ServiceLoader; 

import org.osgi.framework.Bundle; 
import org.osgi.framework.BundleContext; 
import org.osgi.framework.BundleException; 
import org.osgi.framework.launch.Framework; 
import org.osgi.framework.launch.FrameworkFactory; 

/** 
* This class provides a static {@code main()} method so that the bundle can be 
* run as a stand-alone host application. In such a scenario, the application 
* creates its own embedded OSGi framework instance and interacts with the 
* internal extensions to providing drawing functionality. To successfully 
* launch the stand-alone application, it must be run from this bundle's 
* installation directory using "{@code java -jar}". The locations of any 
* additional extensions that have to be started, have to be passed as command 
* line arguments to this method. 
*/ 
public class Application 
{ 

    private static Framework m_framework = null; 

    /** 
    * Enables the bundle to run as a stand-alone application. When this static 
    * {@code main()} method is invoked, the application creates its own 
    * embedded OSGi framework instance and interacts with the internal 
    * extensions to provide drawing functionality. To successfully launch as a 
    * stand-alone application, this method should be invoked from the bundle's 
    * installation directory using "{@code java -jar}". The location of any 
    * extension that shall be installed can be passed as parameters. 
    * <p> 
    * For example if you build the bundles inside your workspace, maven will 
    * create a target directory in every project. To start the application from 
    * within your IDE you should pass: 
    * <p> 
    * 
    * <pre> 
    * {@code file:../servicebased.circle/target/servicebased.circle-1.0.0.jar 
    * file:../servicebased.square/target/servicebased.square-1.0.0.jar 
    * file:../servicebased.triangle/target/servicebased.triangle-1.0.0.jar} 
    * </pre> 
    * 
    * @param args The locations of additional bundles to start. 
    * 
    */ 
    public static void main(String[] args) 
    { 
     // Args should never be null if the application is run from the command 
     // line. 
     // Check it anyway. 
     String[] locations = args != null ? args : new String[0]; 

     // Print welcome banner. 
     System.out.println("\nWelcome to My Launcher"); 
     System.out.println("======================\n"); 

     try 
     { 
      Map<String, String> config = ConfigUtil.createConfig(); 
      m_framework = createFramework(config); 
      m_framework.init(); 
      m_framework.start(); 
      installAndStartBundles(locations); 
      m_framework.waitForStop(0); 
      System.exit(0); 
     } 
     catch (Exception ex) 
     { 
      System.err.println("Could not create framework: " + ex); 
      ex.printStackTrace(); 
      System.exit(-1); 
     } 
    } 

    /** 
    * Util method for creating an embedded Framework. Tries to create a 
    * {@link FrameworkFactory} which is then be used to create the framework. 
    * 
    * @param config the configuration to create the framework with 
    * @return a Framework with the given configuration 
    */ 
    private static Framework createFramework(Map<String, String> config) 
    { 
     ServiceLoader<FrameworkFactory> factoryLoader = ServiceLoader 
       .load(FrameworkFactory.class); 
     for (FrameworkFactory factory : factoryLoader) 
     { 
      return factory.newFramework(config); 
     } 
     throw new IllegalStateException(
       "Unable to load FrameworkFactory service."); 
    } 

    /** 
    * Installs and starts all bundles used by the application. Therefore the 
    * host bundle will be started. The locations of extensions for the host 
    * bundle can be passed in as parameters. 
    * 
    * @param bundleLocations the locations where extension for the host bundle 
    * are located. Must not be {@code null}! 
    * @throws BundleException if something went wrong while installing or 
    * starting the bundles. 
    */ 
    private static void installAndStartBundles(String... bundleLocations) 
      throws BundleException 
    { 
     BundleContext bundleContext = m_framework.getBundleContext(); 
     Activator hostActivator = new Activator(); 
     hostActivator.start(bundleContext); 
     for (String location : bundleLocations) 
     { 
      Bundle addition = bundleContext.installBundle(location); 
      addition.start(); 
     } 
    } 
} 

如何添加位于外部JAR文件中的服务并将其用于将用于启动应用程序的主jar文件?

+0

当你开始使用OSGi时,从更高的层次开始,从bndtools开始,它为您提供开箱即用的OSGi。如果你觉得你想了解它是如何工作的,那么很好,但是,然后阅读规范或许多关于OSGi的较低级别书籍的书籍之一。我不认为Stackoverflow问题旨在帮助您在信息丰富时开始使用技术。 –

回答

1

我已经开始了video,它解释了服务和服务动态。在视频中,您还可以看到如何使用BndTools(我强烈建议)。为了注册和使用服务,我们使用Apache Felix DependencyManager。

相关问题