2016-09-09 11 views
3

OSGIプログラムを作成するためにbndtoolsを使用しようとしています。ここに私の前のコードがあり、それはfelixコンソールでうまくいくかもしれません。OSGIでJavaプロパティファイルを使用する方法宣言的サービスAnnotations

package com.buaa.ate.service.data.command; 
import org.osgi.service.component.annotations.Component; 
import org.osgi.service.component.annotations.Reference; 
import org.apache.felix.service.command.CommandProcessor; 
import com.buaa.ate.service.api.data.Publisher; 

@Component(
     service=PublishCommand.class, 
     property={ 
       CommandProcessor.COMMAND_SCOPE + ":String=example", 
       CommandProcessor.COMMAND_FUNCTION + ":String=publish", 
     } 
) 
public class PublishCommand { 

    private Publisher publishSvc; 
    @Reference 
    public void setPublisher(Publisher publishSvc) { 
     this.publishSvc = publishSvc; 
    } 
    public void publish(String content) { 
     publishSvc.start(); 
     long result = publishSvc.publish(content); 
     System.out.println(result); 
     publishSvc.stop(); 
    } 
} 

は今、私はこのような注釈変更したい:

package com.buaa.ate.service.data.command; 
import org.osgi.service.component.annotations.Component; 
import org.osgi.service.component.annotations.Reference; 
import org.apache.felix.service.command.CommandProcessor; 
import com.buaa.ate.service.api.data.Publisher; 

@Component(
     service=PublishCommand.class, 
     properties="com/buaa/ate/service/data/command/config.properties" 
) 
public class PublishCommand { 

    private Publisher publishSvc; 
    @Reference 
    public void setPublisher(Publisher publishSvc) { 
     this.publishSvc = publishSvc; 
    } 
    public void publish(String content) { 
     publishSvc.start(); 
     long result = publishSvc.publish(content); 
     System.out.println(result); 
     publishSvc.stop(); 
    } 
} 

をそして、これは私のプロパティファイルです:

osgi.command.scope\:String:example 
osgi.command.function\:String:publish 

: はconfig.properties

それは、このようなコンテンツですプログラムを実行するときに、 '何かを公開する'というコマンドを入力し、問題が発生します。

'gogo: CommandNotFoundException: Command not found: publish' 

問題を解決するにはどうすればよいですか。

答えて

2

まあ、問題を解決するのが簡単だと分かりました。

プロパティこのコンポーネントの

パブリック抽象java.lang.Stringで[]プロパティ

プロパティ:これは、OSGiのJavaDocの一部です。 各プロパティ文字列は "key = value"として指定されています。プロパティ値の型はkey:type = valueとしてキーで指定できます。型は、Component Descriptionのproperty要素のtype属性でサポートされているプロパティ型のいずれかでなければなりません。

複数の値を持つプロパティを指定するには、複数のキーと値のペアを使用します。たとえば、 "foo = bar"、 "foo = baz"などです。

参照: "コンポーネント説明のプロパティ要素。"

デフォルト:{}

だからconfig.propertiesに 'タイプ' プロパティを追加し、コードがうまく機能することができます。 current properties file

をそして、それはこのような内容です:ここでは、現在のプロパティファイルさ

osgi.command.scope=example 
osgi.command.scope\:type:String 
osgi.command.function=publish 
osgi.command.function\:type:String 

プログラムは今うまく機能することができます。

関連する問題