2013-05-18 2 views
0

Apache Felix를 처음 사용했습니다. Apache Felix 프레임 워크를 기반으로 간단한 데스크탑 애플리케이션을 만들고 싶습니다. 내가 프레임 워크를 응용 프로그램으로 시작하는 간단한 기본 방법을 만드는 방법을 간단한 예제를 this 찾았지만 응용 프로그램에 서비스를 추가하는 방법을 모르겠습니다. 내가 외부 JAR 파일에있는 서비스를 추가하고 응용 프로그램을 시작하는 데 사용되는 주요 jar 파일에 사용할 수있는 방법데스크탑 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(); 
     } 
    } 
} 

?

+0

OSGi로 시작하는 경우 더 높은 수준에서 시작하는 것이 좋습니다 (예 : bndtools로 시작하면 OSGi를 즉시 사용할 수 있습니다. 모든 것이 어떻게 작동하는지 이해하고 싶다면 OSGi에 대한 많은 책 중 하나를 읽으십시오. Stackoverflow 질문은 정보가 풍부 할 때 기술을 시작하는 데 도움이 될 것이라고 생각하지 않습니다. –

답변

1

서비스 및 서비스 역학을 설명하는 video을 시작했습니다. 비디오에서 BndTools 사용 방법을 볼 수 있습니다. 서비스 등록 및 사용을 위해 우리는 Apache Felix DependencyManager를 사용합니다.

관련 문제