Creating the services

Services do things. That is, services expose common ways of using the object model. A service itself is a grouping of functionality for a specific purpose. The BeNOW Service API was written to provide service functionality and is used and exposed by the Web API.

So far, we've implemented the object model for the music library project, and now we'll define the services for common use of this object model. The services provides access to high level functionality, such as creating an album, adding tracks, getting albums, etc.

We will eventually be using the music library services during web page construction, but they should not be specific to anything web related. The web pages will be showing results from the services, but the services are not tied to the web directly. The services can be used from java directly, remotely or via a page. We'll see all of these usages, but for now, let's build the services.

Note: Services are more fully described in the Creating Services and Creating Secure Services tutorials. The security concepts introduced here may make more sense after reading the Security Tutorial.

Interfaces

The AlbumService defines methods for working with the Albums, and has the following points of interest The PerformerService defines methods for working with the Performers, be they Artists or Groups. The @AnonymousAccess and @RequiresPermission annotations are provided similarly to the AlbumService.


So, service interfaces are definitions of available functionality, and are regular interfaces which extend the Service interface and include @ParamName and security annotations.

At this point, we have definition without implementation, however. Let's complete the services so that they do something useful.

Implementations

AlbumServiceImpl defines the processing for the methods declared in the AlbumService.
public class AlbumServiceImpl implements AlbumService { ... }
Most of the processing is routine, but it is using the repository. The update() method of JSQLObject descendants is used to add the object to the repository, as shown
@Override
public Album createAlbum(String title, Performer performer) {
  AlbumImpl a = new AlbumImpl(title, performer);
  a.update();
  return a;
}
and the remove() method is used to remove objects from the repository:
@Override
public boolean deleteAlbum(Album album) {
  return ((AlbumImpl) album).remove();
}

@Publish must be provided for the class, as in the service. By providing the @Publish annotation, you've indicated that the service is ready to be used. In the same way, PerformerServiceImpl is an implementation of the PerformerService and is marked with @Publish, indicating that it is ready to be used.

About the only new thing in the implementation is the use of lists within repository objects. Note that the object is updated after a contained list is modified:

@Override
public void removePerformerLink(Performer performer, URL link) {
  PerformerImpl p = (PerformerImpl) performer;
  p.removeLink(link);
  p.update();
}


All code, including these new services, can be compiled via ant:

ant jar
For those who have not used ant, it guides compilation and other tasks via it's build file, build.xml. The standard ant build file used in web projects has the following targets: clean,compile,deploy, jar and javadoc. Check out the build file and the ant website for more information. The ant manual can be very useful when modifying ant build files.

With the services now defined and implemented, the Service API does the rest of the work in publishing and allowing invocation. Lets now use the services.