checkWidgets
is a simple operation that has a parameter that is the head member of a
substitution group. This operation demonstrates how to deal with individual parameters that are members of a substitution group.
The consumer must ensure that the parameter is a valid member of the substitution group. The service must properly determine
which member of the substitution group was sent in the request.
The generated method signature uses the Java class supporting the type of the substitution group's head element. Because the member elements of a substitution group are either of the same type as the head element or of a type derived from the head element's type, the Java classes generated to support the members of the substitution group inherit from the Java class generated to support the head element. Java's type hierarchy natively supports using subclasses in place of the parent class.
Because of how FUSE Services Framework generates the types for a substitution group and Java's type hierarchy, the client can invoke
checkWidgets()
without using any special code. When developing the logic to invoke
checkWidgets()
you can pass in an object of one of the classes generated to support the widget
substitution group.
Example 15.14 shows a consumer invoking
checkWidgets()
.
Example 15.14. Consumer Invoking checkWidgets()
System.out.println("What type of widgets do you want to order?"); System.out.println("1 - Normal"); System.out.println("2 - Wood"); System.out.println("3 - Plastic"); System.out.println("Selection [1-3]"); String selection = reader.readLine(); String trimmed = selection.trim(); char widgetType = trimmed.charAt(0); switch (widgetType) { case '1': { WidgetType widget = new WidgetType(); ... break; } case '2': { WoodWidgetType widget = new WoodWidgetType(); ... break; } case '3': { PlasticWidgetType widget = new PlasticWidgetType(); ... break; } default : System.out.println("Invaid Widget Selection!!"); } proxy.checkWidgets(widgets);
The service's implementation of checkWidgets()
gets a widget description as a
WidgetType
object, checks the inventory of widgets, and returns the number of widgets in
stock. Because all of the classes used to implement the substitution group inherit from the same base class, you can implement
checkWidgets()
without using any JAXB specific APIs.
All of the classes generated to support the members of the substitution group for widget
extend the WidgetType
class. Because of this fact, you can use instanceof
to
determine what type of widget was passed in and simply cast the widgetPart
object into the more
restrictive type if appropriate. Once you have the proper type of object, you can check the inventory of the right kind of
widget.
Example 15.15 shows a possible implementation.
Example 15.15. Service Implementation of checkWidgets()
public int checkWidgets(WidgetType widgetPart) { if (widgetPart instanceof WidgetType) { return checkWidgetInventory(widgetType); } else if (widgetPart instanceof WoodWidgetType) { WoodWidgetType widget = (WoodWidgetType)widgetPart; return checkWoodWidgetInventory(widget); } else if (widgetPart instanceof PlasticWidgetType) { PlasticWidgetType widget = (PlasticWidgetType)widgetPart; return checkPlasticWidgetInventory(widget); } }