Building Pizza with Lookup | #2

Continuing from the last post, I will present you the code and pointers to the SVN repository at Project Kenai where the project resides..

Last time, I said I have ported the sample code to NetBeans Platform in the form of 4 modules getting plugged into the NetBeans Platform Application and I named it PizzaBeans. Today, we will just setup the service provider API and its service providers…

Service Provider Interfaces

Pizza Center Services (pizzacenter.services)

This module contains Builder and Product classes inside a package which is not exported by default, so I had to go the Project Properties and select API Versioning to make that package public, which would then be used by service provider modules to implement the desired functionality, i.e. to build the specific type of Pizza.

Service Providers

To quote from one of the John O’ Conner’s articles (see References);

“Define a service provider by implementing the service provider API. Usually, you will create a JAR file to hold your provider. To register your provider, you must create a provider configuration file in the JAR file’s META-INF/services directory.”

Here the Lookup API will come to the rescue, you will soon get to know how it enables loose-coupling amongst our modules and facilitates high cohesion. Although, I am creating probably the easiest of examples..

“The configuration file name should be the fully qualified binary name of the service’s type. The binary name is simply the fully qualified class name in which each component of the name is separated by a . character, and nested classes are separated by a $ character.”

The following two modules does the same and hence, through Lookup API one can get to know what all pizzas can be built…

Hawaiian Pizza (pizzacenter.spone)

This module contains a ConcreteBuilder class as the service provider class (the implementation of Builder class). So, this will be registered as quoted above;

Create the configuration file with a name as that of the service provider API class (i.e. Builder class) inside the “META-INF/services/” and add the fully qualified name of the above ConcreteBuilder class to the file.

Spicy Pizza (pizzacenter.sptwo)

Similarly, this module contains a ConcreteBuilder class as the service provider class (the implementation of Builder class). So, this will be registered in the similar way, as done for the Hawaiian Pizza module.

References

John O’ Conner‘s article on Creating Extensible Applications With the Java Platform is a must read, also refer to the resources I had earlier pointed to.

To be continued…

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s