Create an OSGI service that requires custom parameters

I'm just starting to learn osgi. You must create an application that provides Search Service. The search service is platform dependent (SearchServiceLinux, SearchServiceAndroid, SearchServiceXXX ...). Also, the search service depends on the parameter that the user enters. The parameter is required.

My search service is Consumer (then the user set the i parameter by creating a new SearchService instance):

@Component(immediate = true, publicFactory = false)
@Provides(specifications = {TestConsumer.class})
@Instantiate
public class TestConsumer {
    @Requires(filter = "(factory.name=package.ISearchService)")
    private Factory mFactory;
    private ComponentInstance mSearchComponentInstance;
...
    public void userSetParameter(String pParameter) {
        Properties lProperties = new Properties();
        lProperties.put("instance.name", mFactory.getName() + "-" + pParameter);
        lProperties.put("Parameter", pParameter);

        if (mSearchComponentInstance != null) {
            mSearchComponentInstance.dispose();
        }

        try {
            mSearchComponentInstance = mFactory.createComponentInstance(lProperties);
        } catch (UnacceptableConfiguration e) {
            e.printStackTrace();
        } catch (MissingHandlerException e) {
            e.printStackTrace();
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }

My search service:

@Component
@Provides(specifications = {ISearchService.class}, strategy = "SINGLETON")
public class TestServise implements ISearchService{
    @ServiceProperty(name = "Parameter", mandatory = true)
    private int mParameter;
...

Questions: 1) Is this true program structure? Are @ServiceProperty or @Property preferable in this case? What is the best practice for OSGI Service that requires user options? Is it possible to reform the structure of the consumer to use:

@Requires (filter = "need filter for SearchService with Parameter=XXX or create this service")
ISearchService mSearchService;

2) iPOJO Event?

:

@Publishes(name = "p1", topics = "userChangeParameter")
private Publisher mPublisher;

public void userChangeParameter(String pParameter) {
    Properties lProperties = new Properties();
    lProperties.put("Parameter", pParameter);
    mPublisher.send(lProperties);
}

:

@Subscriber(name = "s0", topics = "foo")
public void subscriber(Event pEvent) {
    System.out.println("Subscriber : " + pEvent.getProperty("Parameter"));
}

3) , , ? , Apache Felix?

apache felix 4.2.1.

+4
1

​​:

@Component(
        metatype = false)
@SlingServlet(
        paths = { "/bin/test/service" }, methods = { "POST" }, extensions = { "json" },
        selectors = { "selector1", "selector2"}, generateComponent = false)
public class TestConsumer extends SlingAllMethodsServlet {
   //inject all the services here like SearchServiceLinux, etc. 
   @Reference 
   private SearchServiceLinux searchServiceLinux;
}

,

http://localhost/bin/test/service.seletor1.html

, , , seletor1 X, selector2 Y

, POST , POST, , searchParam, , searchParam .

, .

0

All Articles