Manur
Manur

Reputation: 8783

Use of Lookup API outside of NetBeans Platform

I'm trying to evaluate whether it's appropriate for our shop to use the NetBeans Lookup API without the whole NetBeans Platform.

So far, I managed to create a project with this code :

 for (SomeInterface si : Lookup.getDefault().lookupAll(SomeInterface.class)) {
     si.doSomething();
 }

I also created a couple of other projects, each with an AnImplementation class implementing SomeInterface, and the accompanying file META-INF/services/path.to.SomeInterface containing a line referencing the class (eg. "other.path.to.AnImplementation").

When I add these implementing projects to the libraries (dependencies) of the main project in the NetBeans IDE, it works fine and I can see the successive results of doSomething() from both implementations.

My question is how to make that work without referencing the sub-projects in the main project ; the jars of the sub-projects wouldn't be included in the generated jar of the main project when building, and one would be able to add or remove them at will, altering the result of the above code.

If I'm not mistaken, this is the behavior advertised in the Lookup API documentation. Thanks in advance.

Edit: For now, my conclusion is that without the NetBeans Platform (or OSGi ?) it's not possible to detect which service providers are present at startup time. You need to reference their jars in your classpath, and thus to identify them before startup. Feel free to prove me wrong.

Upvotes: 2

Views: 1666

Answers (2)

Tim Sparg
Tim Sparg

Reputation: 3304

You have to reference the sub-project in your calling application, as this puts it on the classpath - If the jar/library is not on the classpath then APIs like the Lookup and ServiceLoader wont be able to find it.

If you use OSGI or the NetBeans platform these systems allow you to change the classpath at runtime.

Geertjans blog has an entry about exactly this(using the Lookup API outside of the NetBeans platform), in his blog he also references John O'Connors blog which contrasts the ServiceLoader and Lookup APIs

EDIT

I've just seen Jon Skeets' answer to a similar question. You can use the -Djava.ext.dirs=lib property to set a folder (In this case 'libs') as the place where it must lookup jars for your classpath.

Upvotes: 4

Waldheinz
Waldheinz

Reputation: 10497

In my understanding you don't have to bundle all the modules together with the main project for this to work. All you need is to make sure that your modules are on the classpath when starting the application, because the global Lookup uses the ServiceLoader mechanism under the hood. Based on your question I recommend considering if

  • using the ServiceLoader directly is a better match for you problem or
  • some DI framework like Guice is worth a try or
  • if OSGI offers something useful for you as well and use that.

Don't get me wrong, I absolutely love NetBeans and the NetBeans platform, but in my opinion using the Lookup alone is of limited use because of the possibilities listed above.

Upvotes: 1

Related Questions