4

I'm trying to use bndtools to create my OSGI program. Here is my previous code, and it can work well with the felix console.

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();
    }
}

Now, I want to change the annotation like this:

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();
    }
}

And this is my properties file: config.properties

It's content like this:

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

When I run the program, input the command 'publish something', and then the problem happens:

'gogo: CommandNotFoundException: Command not found: publish'

So, what should I do to fix the problem?

Wubin
  • 141
  • 2
  • 11

1 Answers1

3

Well, I just realize that it's so easy to fix the problem. This is a part of the osgi javadoc:

property

public abstract java.lang.String[] property

Properties for this Component. Each property string is specified as "key=value". The type of the property value can be specified in the key as key:type=value. The type must be one of the property types supported by the type attribute of the property element of a Component Description.

To specify a property with multiple values, use multiple key, value pairs. For example, "foo=bar", "foo=baz".

See Also: "The property element of a Component Description."

Default:{}

So I add the 'type' property to config.properties, and then the code can work well. Here is the current properties file: current properties file

And it's content like this:

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

The program can work well now.

Wubin
  • 141
  • 2
  • 11