1

What should be done if a lookup returns null? I am using Lookup.getDefault().lookup() of org.openide.util.Lookup which is used for finding instances of objects. The general pattern is to pass a Class object and get back an instance of that class or null.

I am using it in the code below:

StreamingServer server = Lookup.getDefault().lookup(StreamingServer.class);
ServerControllerFactory controllerFactory = Lookup.getDefault().lookup(ServerControllerFactory.class);
StreamingController controller = Lookup.getDefault().lookup(StreamingController.class);

Since no istances are found, null is returned for each of these, 'server', 'controllerFactory', 'controller'. I need to handle this situation so I can use these objects to access methods, as in :

controllerFactory.createServerController(graph)

server.register(serverController, context)

How can I accomplish this?

dev2d
  • 4,245
  • 3
  • 31
  • 54
user1727270
  • 303
  • 1
  • 3
  • 10

2 Answers2

0

You need to create a META-INF/services folder in your Eclipse source folder. Then, for each class, create a text file in the META-INF/services folder. The name of the text file is the full name (includes the package location) of the class type, and the contents of the text file is the full name (includes the package location) of the implementing class (which is not necessarily the same name of the class).

For example, for the ServerControllerFactory class, you will create a text file named org.gephi.streaming.server.ServerControllerFactory and the text this file will contain is org.gephi.streaming.server.impl.ServerControllerFactory

cs61c.ta
  • 21
  • 2
0

For me, the class that I was trying to call lookup on just needed the @ServiceProvider decoration like this:

@ServiceProvider(service = MyClassHere.class)
public final class MyClassHere ....
kmort
  • 2,848
  • 2
  • 32
  • 54