开发者

How instantiate several OSGi services?

开发者 https://www.devze.com 2023-02-25 11:26 出处:网络
In the context of an Eclipse RCP application I decided to use OSGi services to provide \"Interfaces\" out of a plugin (i.e a bundle).

In the context of an Eclipse RCP application I decided to use OSGi services to provide "Interfaces" out of a plugin (i.e a bundle).

In one of my plugin I have the following Parser interface:

public interface Parser {

    public void start(File file);

    public boolean hasNext();

    public Object next();
}

Consumer plugins will use this interface to parse files. Because several parsing can be done in the same time and because an implementation of this interface w开发者_StackOverflowill need several "state" private field each consumer of this service must use a dedicated service instance.

In this case, the default solution provided by manu OSGi tutorials consisting in registering ONE service instance in the start method of the parser bundle doesn't work. What is the best solution to handle such a solution ? I can create a ParserFactory service with one unique method:

public Parser create(File file);

??

Any comment is welcome,


As you're suggesting, I would change your service interface to be a provider of Parsers.

And your Parser is just an Iterator, so maybe something like

public interface ParserFactory<T> {

  /** Iterating on the returned object
   *  provides Ts parsed from the InputStream.
   *
   *  @param input must be closed by the returned object 
   *   when done iterating. 
   */
  Iterable<T> createParser(InputStream input);
}

Using an InputStream or Reader also makes it more flexible that requiring a File.


Have a look at the OSGi ServiceFactory; this allows you to instantiate services for different requesting bundles. You can read more about it in section 5.6 of the core specification.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号