Specifying the Service Interface – Java Module System

Specifying the Service Interface

The service is represented by the interface IAdvice that is declared in package org.advice.si of the serviceinterface module (Example 19.3). It specifies the functionality that the service will provide: two abstract methods getContent() and getLocale() that return the advice (as a string) and the associated locale at (1) and (2), respectively. In other words, the advice service is locale-specific. Since the two methods have no accessibility modifier in their declaration, they are implicitly public, and as they do not provide an implementation, they are implicitly abstract.

Example 19.3 Specifying the Advice Service Interface

Click here to view code image

// File: ./src/serviceinterface/org/advice/si/IAdvice.java
package org.advice.si;
import java.util.Locale;
// Service interface
public interface IAdvice {
  String getContent();                  // (1) Returns the content of the advice.
  Locale getLocale();                   // (2) Returns the associated locale.
}

The declaration of the serviceinterface module in Figure 19.15(c) exports the package org.advice.si containing the service interface IAdvice so that service providers can implement it.

Click here to view code image

// File: ./src/serviceinterface/module-info.java
module serviceinterface {
   exports org.advice.si;
}

Implementing Service Providers

Two service providers are implemented in the serviceprovider module (Example 19.4). The package org.advice.serviceprovider contains the class Canned-Advice that implements the service interface IAdvice. The getContent() and the getLocale() methods return the canned advice and the associated UK locale at (1) and (2), respectively.

Example 19.4 Implementing the Advice Service Providers

Click here to view code image

// File:./src/serviceprovider/org/advice/serviceprovider/CannedAdvice.java
package org.advice.serviceprovider;
import org.advice.si.IAdvice;
import java.util.Locale;
public class CannedAdvice implements IAdvice {
  public String getContent() {                         // (1)
    return “Keep calm and service on!”;
  }
  public Locale getLocale() { return Locale.UK; }      // (2)
}

Click here to view code image

// File:./src/serviceprovider/org/advice/serviceprovider/VikingAdvice.java
package org.advice.serviceprovider;
import org.advice.si.IAdvice;
import java.util.Locale;
public class VikingAdvice implements IAdvice {
  public String getContent() {                                      // (3)
    return “Gi aldri opp!”;    // Never give up!
  }
  public Locale getLocale() { return new Locale(“no”, “Norway”); }  // (4)
}

The package org.advice.serviceprovider also contains the class VikingAdvice that implements the service interface IAdvice. The getContent() and the getLocale() methods return the Viking advice and the associated Norwegian locale at (3) and (4), respectively.

The serviceprovider module in Figure 19.15(d) requires the module serviceinterface in order to implement the service. The module advertizes that it implements the service in a provides-with directive at (1) below. In the directive, the service interface that is implemented is specified first, and the with clause specifies a comma-separated list of classes in this module that implement the service. This information is used by the service loader at runtime to locate and load the classes that implement this service in this module. Note that the package implementing the service is not exported, avoiding any service consumers having explicit dependency on service providers.

Click here to view code image

// File: ./src/serviceprovider/module-info.java
module serviceprovider {
  requires serviceinterface;
  provides org.advice.si.IAdvice with              // (1)
    org.advice.serviceprovider.CannedAdvice,
    org.advice.serviceprovider.VikingAdvice;
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *