C H A P T E R  4

Developing XML Operations

XML operations provide an efficient way to create a web service interface for existing business components that were not designed for web service access.

For example, suppose you are creating a web service to enable a customer to place an order, but the business component has methods to check inventory, check customer credit, ship an order, and process billing information. You can combine those methods into a single XML operation to place an order.

XML operations are, along with direct method calls, the building blocks of your web service. Each XML operation defines a response to a particular client request.

This chapter explains how you create and edit XML operations. It also provides a description of the tools you use for this job. For conceptual information on XML operations, see XML Operations.


Overview of Tools

The tools you use to develop XML operations are available to you from the Explorer and Source Editor.

You perform the initial step of creating an XML operation definition in the Explorer by selecting a node, to indicate where you want the operation created and then opening the New From Template wizard. This procedure is the same as the procedure for creating a class or any other object in the Explorer. (See Creating an XML Operation for explanation of how to do this.)

To further develop your XML operation, you access tools from either the Explorer or the Source Editor. The same commands are available from either window. The Source Editor is generally more convenient to use because it lets you issue commands and view results of the commands in the same window. FIGURE 4-2 and FIGURE 4-1 show XML operations displayed in the Source Editor.

 FIGURE 4-1 XML Operation Source Editor, Showing Complex Input

Screenshot showing the source editor for XML operations has three panes: Data Source, XML Input Format, and XML Output Format.

 FIGURE 4-2 XML Operation Source Editor, Showing Complex Output

Screenshot showing the source editor for XML operations has three panes: Data Source, XML Input Format, and XML Output Format.

The Source Editor displays an XML operation in three panes:

The Data Source Pane

The Data Source pane displays an XML operation in a tree view. The nodes of the structure represent XML input document elements, methods called by the XML operation, parameters to these methods, return values of the methods, and organizational nodes. Each type of node has its own commands and properties.

You can edit an XML operation by:

At the top level, the Data Source pane contains two organizational nodes: the Input Document Elements node and the Methods node.

Input Document Elements Node

The XML input document contains the data that specifies the client request. It is represented as the Input Document Elements node. By expanding this node, you can browse and edit the XML input document elements. These elements are represented as subnodes. FIGURE 4-3 shows a cropped view of the Source Editor with the Input Document Elements node expanded.

 FIGURE 4-3 The Input Document Elements Node

Screenshot of the source editor Data Source pane, showing an expanded Input Documents Elements node.[ D ]

From these nodes, you can perform the following types of editing operations on the XML input document:

Methods Node

From the Methods node, you can:

Executing Methods and Returning Data

XML operations execute methods on other runtime objects. You specify which methods your XML operation executes by adding methods calls to the Methods node. By adding method calls, you can program your XML operation to return data or perform other types of processing.

You can also delete and rearrange the order in which methods are called. The XML operation executes the methods in order from top to bottom.

Adding, reordering, or deleting methods affects the XML output document by adding, reordering, and deleting the elements corresponding to the return values. Such changes are displayed in the XML output pane as you make them. For more information on these topics, see Adding a Method to an XML Operation, Reordering a Method or Input Document Element, and Deleting a Method or Input Document Element.

Providing Parameter Values

If a method takes parameters, the parameters are listed under the Parameters node for that method. By default, the XML operation obtains a value for each parameter by mapping elements of the XML input document to like-named parameters.

You can, however, remap XML input elements to method parameters in any way you want. You might need to do this if two methods in your XML operation take like-named parameters. In such a situation, the XML operation by default maps both parameters to the same XML input element. If this is not appropriate, you can create a new input element and remap one of your parameters to it.

You can also map parameters to types of sources other than input elements. For example, you can map a parameter to:

For a description of how to map a method parameter to a source, see Mapping a Method Parameter to a Source. For information on target objects, see . For information on system shared objects, see System Shared Objects.

Retrieving More or Less Data

If you add a method call that returns an object (or an array or collection of objects), the object's class type is shown as a subnode to the method. If this class contains methods that begin with the string get, the methods are shown as subnodes to the class. By default, an object returned by any of these "getter" methods is also shown as a class node, but without subnodes representing its methods.

You can, however, choose to expand such a class node. Expanding the class node adds nodes for all of the getter methods in the class and likewise adds elements corresponding to the return values of these methods to the XML output.

Conversely, you can collapse a class so that its getter methods are not displayed in the Data Source pane. You can also individually delete methods. In both of these cases, the methods are not called when the XML operation is executed. The elements of the XML output corresponding to these methods are automatically removed.

Whenever you expand or collapse a class, corresponding changes to the XML output are displayed in the XML Output Format pane as you make them. For more information on this topic, see Expanding a Class.

Trimming the Data Returned to the Client

You might find that a returned class provides some data that you don't want to include in the XML output. For example, a customer account class might have an account ID field and a corresponding getter method that is used only for internal purposes. In such a situation, you can selectively choose to exclude the element corresponding to this method from the XML output.

Excluding an element from the XML output does not affect the methods called by the XML operation or the data set returned by these methods to the XML operation. The exclusion affects only the data set returned to the client by way of the XML output document. By excluding unnecessary elements, the data passed between application containers is minimized, optimizing performance. For more information on this topic, see Excluding an Element From the XML Output.


Development Work Flow

The following steps outline the work flow for developing an XML operation.

1. Create an XML operation.

This procedure results in an XML operation that has:

2. (Optional) Edit the XML operation by performing some or all of these procedures:

3. Include the XML operation in a web service.

If you don't already have a web service, you must create one. For more information, see and .

4. Test the XML operation in the web service.

For more information on this topic, see .

5. Edit the XML operation, regenerate the web service's runtime classes, and test until satisfied.


Creating XML Operations

You can create XML operations individually or generate a group of XML operations based on an enterprise bean.

Creating an XML Operation

To create an XML operation:

1. In the Explorer, right-click the folder in which you want to create the XML operation and choose New right arrow Web Services right arrow XML Operation.

The New from Template XML Operation dialog box is displayed, as illustrated in FIGURE 4-4.

 FIGURE 4-4 New XML Operation Dialog Box

Screenshot of dialog used to specify Name, Package, and Method for a new XML operation. Buttons are Browse, Back, Next, Finish, Cancel, and Help.

2. In the Name field, type a name for the XML operation.

3. Ensure that the Package field specifies the correct location in which to create the XML operation.

4. Click the Browse button next to the Method field.

The Select Method dialog box is displayed, as illustrated in FIGURE 4-5.

 FIGURE 4-5 Select Method Dialog Box

Screenshot of Select Method dialog, showing an EJB logical node with a Business Methods subnode and several methods. Buttons are OK and Cancel.

5. Navigate to a method you want to include in the XML operation and click OK.

You can add methods from a class, a bean, an interface, or an EJB component.

You can include methods defined in an EJB's home and remote interfaces, but not methods defined only in the EJB local interface.

If you are adding an EJB method, be sure to browse to the logical EJB node (the node with the bean icon Bean icon), not the node that represents the EJB bean class or the home or remote interface. By adding the method from the logical EJB node, you provide the runtime information needed to call the method. When you create an XML operation, you include one method in it. If your XML operation requires additional methods, you can add them later. For information on adding additional methods to an XML operation, see Adding a Method to an XML Operation.

6. If the method you have selected returns an array or collection, select the class, parent class, or interface of the objects contained in it.

Click the Browse button in the New XML Operation dialog box, next to the field labeled Method Returns a Collection of This Class. A file browser opens. Use the file browser to select the class or interface.

7. Click Finish.

The XML operation is created and displayed in the Source Editor ready for editing (as shown in FIGURE 4-2). For information on editing your XML operation, see Editing an XML Operation.

Generating XML Operations From an Enterprise Bean

As an alternative to creating XML operations individually, you can generate a group of XML operations based on an enterprise bean, an EJB module, or a package containing one or more enterprise beans. Doing so generates one XML operation for each method on the home and remote interfaces of each enterprise bean. References to the generated XML operations are automatically added to your web service.

To generate XML operations from enterprise beans, you must have already created a web service. For information on creating a new web service, see .

To generate XML operations from an enterprise bean:

1. Right-click your web service and choose Generate Operations from EJB.

A file browser is displayed.

2. Browse to an enterprise bean, an EJB module, or package containing an enterprise bean and click OK.

3. Click Finish.

4. Specify the class, parent class, or interface of objects contained in any array or collection returned by methods in the enterprise beans.

If any method returns an array or collection, the Collection of What? dialog box is displayed, as illustrated in FIGURE 4-6.

 FIGURE 4-6 Collection of What? Dialog Box

Screenshot of Collection of What? dialog, displaying a field for object type. Buttons are Browse, OK, and Cancel.

The name of the method that returns the collection is indicated in the dialog box.

To specify the object type:

a. Click the Browse button.

A file browser is displayed.

b. Navigate to the class, parent class, or interface of objects contained in the array or collection and click OK.



Note - This dialog box is automatically displayed once for each method that returns an array or collection.



The XML operations are generated and references to them are added to your web service. You can now delete the XML operations you don't need and edit the others according to your requirements.


Editing an XML Operation

This section describes the ways you can edit an XML operation.

Adding a Method to an XML Operation

To add a method to an XML operation:

1. Open your XML operation in the Source Editor.

2. In the Data Source pane, right-click the Methods node and choose Add Method.

The Add Method dialog box is displayed, as illustrated in FIGURE 4-7.

 FIGURE 4-7 Add Method to XML Operation Dialog Box

Screenshot of Add Method dialog box, used to specify the Method to be added. Buttons are Browse, OK, Cancel, and Help.

3. Click Browse to select a method.

The Select Method dialog box is displayed, as illustrated in FIGURE 4-8.

 FIGURE 4-8 Select Method Dialog Box

Screenshot of Select Method dialog box, showing an EJB logical node with a Business Methods subnode and several methods. Buttons are OK and Cancel.

4. Navigate to a method you want to include in the XML operation and click OK.

You can add methods from a class, a bean, an interface, or an EJB component.

You can include methods defined in an EJB's home and remote interfaces, but not methods defined only in the EJB local interface.

If you are adding an EJB method, be sure to browse to the logical EJB node (the node with the bean icon Bean icon), not the node that represents the EJB bean class or the home or remote interface. By adding the method from the logical EJB node, you provide the runtime information needed to call the method.

5. If the method you have selected returns an array or collection, select the class, parent class, or interface of the objects contained in it.

Click the Browse button in the Add Method dialog box, next to the field labeled Method Returns a Collection of This Class. A file browser opens. Use the file browser to select the class or interface.

6. Click OK.

This action results in the following:



Note - If you later move the method to another package or edit the method's class in such a way that the signature of the method is altered, you must remove the method call from the XML operation and add it back. Altering the method signature includes changes to the name, parameters, class of the return value, and list of exceptions.



Adding an Input Document Element

To add an input document element:

1. Open your XML operation in the Source Editor.

2. In the Data Source pane, right-click the Input Document Elements node and choose Add Input Document Element.

The Add Input Document Element dialog box is displayed, as illustrated in FIGURE 4-9.

 FIGURE 4-9 Add Input Document Element Dialog Box

Screenshot of Add Input Document Element dialog. Fields are Name, Data Type, Default Value, and Permanent property. Buttons are OK, Cancel, and Help.

3. Type a name for the element in the Name field.

4. Click the Data Type combo box and select a data type for the element.

If you intend to use this input document element as a parameter for instantiating a target object, you must choose a class (such as String) as a data type. Primitives (such as int or double) will not work.

5. (Optional) Specify a default value for the element.

If you want to specify a default value for the element, type it in the Default Value field.

This value is used if the client request does not provide this element.

6. If you want this element automatically deleted when it is no longer mapped to a method parameter, deselect the Permanent checkbox.

Whenever you remap a method parameter's source, input elements that are not currently mapped to a method parameter are deleted.

7. Click OK.

The new input element is added to the Input Document Elements node in the Data Source pane. The XML Input Format pane displays the updated XML input document.

Renaming an Input Document Element

To rename an input document element:

1. Select the input document element, and open the Properties window, as illustrated in FIGURE 4-10.

2. Click the Name property.

 FIGURE 4-10 Input Document Element Properties Dialog Box

Screenshot of Input Document Element Properties dialog box, showing the Name property.

3. Type a name for the input document element and press Enter.

The input document element is now renamed. The new name is displayed both on the node inside the Input Document Elements folder and in the XML Input Format pane.

Renaming an Output Document Element

Each element in the XML output document is, by default, named after the method that returns the element's value. For example, adding a call to a method named append adds an element to the XML output document named append. Adding a call to a method named getName adds an element named Name. You can rename these elements by changing the value of the method call's TagName property.

To rename an output document element:

1. In the methods folder, select the method that returns the value for the element, select the Returns node of the method, and open the Properties window, as illustrated in FIGURE 4-11.

2. Click the Tag Name property.

 FIGURE 4-11 Output Document Element Properties Dialog Box

Screenshot of Output Document Element Properties dialog box, showing the Tag Name property.

3. Type a name for the element and press Enter.

The output document element is now renamed. The new name is displayed in the XML Output Format pane.

Giving an Input Document Element a Default Value

To specify a default value for an input document element:

1. Open the property sheet for the input document element, as illustrated in FIGURE 4-12.

2. Click the Default Value property.

 FIGURE 4-12 Input Document Element Properties (Default Value)

Screenshot of Input Document Element Properties dialog box, showing the Default Value property.

3. Type a value or, to specify a value of null, select (None).

This value is used if the client request does not provide this element.

Making an Input Document Element Permanent

The IDE automatically deletes any input document element that is not mapped to a method parameter unless the Permanent property of the input document element is enabled.

For example, if you add a method that takes a parameter to your XML operation, the IDE automatically adds an input document element and maps it to the method parameter. If you then remap the method parameter to a different source (for example, to the return value of another method), the IDE deletes the input document element because it is no longer mapped to a method parameter.

To prevent an input document element from being automatically deleted if it is not mapped, enable its Permanent property.

To enable an input document's Permanent property:

1. Open the property sheet for the input document element, as illustrated in FIGURE 4-12.

2. Click the Permanent property.

3. Select True in the combo box and press Enter.

The input document element is now permanent.

Reordering a Method or Input Document Element

You can reorder the input document elements and methods in an XML operation.

Reordering methods changes the order in which the methods are called and changes the order of the elements in the XML output document. Methods are called in the order they are listed in the Source Editor, from top to bottom.

The return value of one method in an XML operation can be used as a parameter to another method in the XML operation (see Mapping a Method Parameter to a Source for information on this topic). If you have such a dependency in your XML operation, you must ensure that the method supplying the parameter is called before the method that requires the parameter.

The ability to reorder input document elements is a development convenience. At runtime the order of input document elements has no significance to the web service.

To reorder a method or input document element:

1. Open your XML operation in the Source Editor and locate the method or input document element you want to reorder.

2. Right-click the method or input document element and choose Move Up or Move Down.

Deleting a Method or Input Document Element

You can delete input document elements and methods from an XML operation. When you delete a method, it means that the method is not called when the XML operation is executed. The method's corresponding XML output elements are also removed.

To delete a method or input document element:

1. Open your XML operation in the Source Editor and locate the method or input document element you want to delete.

2. Right-click the method or input document element and choose Delete.

Mapping a Method Parameter to a Source

To map a method parameter to a source:

1. Open your XML operation in the Source Editor and locate the method parameter you want to map.

2. Right-click the parameter and choose Change Source.

The Method Parameter Source dialog box is displayed, as illustrated in FIGURE 4-13.

 FIGURE 4-13 Method Parameter Source Dialog Box

Screenshot of Method Parameter Source dialog, showing method param. source types and values. Buttons are Add Element, Change Method, OK, Cancel, Help.[ D ]

3. Select a source type.

The following table describes the available source types.

Source Type

Description

Input Document Element

Select this radio button if you want to map the parameter to an element in the XML input document.

Previous Method

Select this radio button if you want to map the parameter to the return value of a previous method call in this XML operation.

Inline Method

Select this radio button if you want to map the parameter to the return value of an inline method call. Click Change Method to select the desired method.

Constant Value

Select this radio button if you want to map the parameter to a constant value.

System Shared Object

A web service maintains system shared objects at runtime containing data about web browser clients that access the web service. System shared objects are instantiated by the web service and populated with data obtained from the HTTP request and J2EE security mechanism.

System shared objects maintain data about the user name. The data is provided as both a String object and a java.security.Principal object.

Select this radio button if you want to map the parameter to one of these data types.

Object Reference

Select this radio button if you want to map the parameter to a target object defined in your web service. This option is available only if the parameter's type is a class (which includes String).

Selecting this radio button adds a default object reference in your web service that maps the parameter to a new object of the required class. You can reconfigure this reference to resolve to an object of your choice. For more information on this topic, see .


4. Specify the source of the parameter's value.

If the source type is set to Input Document Element, Returned by Method, Constant Value, or System Shared Object, use the enabled field to specify a source. The following table describes the action to perform depending on the specified source type.

Field Name

Action

Input Document Element

Select an input document element from the enabled combo box. The combo box lists all input document elements in the XML operation of the type required by the parameter. Selecting an input document element maps its value to the parameter.

Previous Method or Inline Method

Select a method from the enabled combo box. The combo box lists methods called before the current method that return the appropriate type for the parameter. Selecting a method maps its return value to the parameter.

Constant Value

In the enabled field, type a string indicating the value. For values of type String or char, do not type a quotation mark unless it is part of the value.

System Shared Object

Select an object from the list. The list of objects available depends on your parameter's type.

For a parameter of type java.security.Principal, the combo box lists one object, UserPrincipal.

For a parameter of type java.lang.String, the combo box lists one object, UserName


5. Click OK.

Casting a Method Return Value

To cast a method return value:

1. Open your XML operation in the Source Editor and locate the method Returns node.

2. Right-click the node and choose Properties.

One of the properties is Cast, with a default of (None). Another property is Data Type. You can change the value of Cast to any type consistent with Java rules.

3. Click OK.

Displaying and Selecting Inherited Methods

To display inherited methods:

1. Open your XML operation in the Source Editor and locate the method Returns node.

Alternatively, you can locate the Returns node in the Explorer.

2. Right-click the node and choose Expand.

The Expand window is displayed, with Getter methods automatically selected.

3. Select the Show Inherited Methods checkbox.

Inherited methods are displayed.

4. Select the desired methods and click OK.

The selected methods are displayed in the Explorer and in the Data Source pane.

Excluding an Element From the XML Output

To exclude an element from the XML output document:

1. Open your XML operation in the Source Editor.

2. Open the Methods node located in the Data Source pane.

3. Identify the node in the Data Source pane that corresponds to the element you want to exclude.

When you select a node in the Data Source pane, its corresponding element is highlighted in the XML Output Format pane. For example, FIGURE 4-14 shows a class node selected in the Data Source pane and its corresponding element highlighted in the XML Output Format pane.

 FIGURE 4-14 Source Editor: (Excluding an Output Element)

Screenshot of Source Editor, showing a selected node under Data Source Methods, and the corresponding highlighted XML Output Format tags.

4. Right-click the node and choose Exclude Tag from Output.

The corresponding element is removed from the XML Output Format pane.

Including an Element in the XML Output

By default, all return values of your XML operation's method calls are included in the XML output document. If you exclude any of the elements representing these returned values, you can add them back to the XML output document.

To include an element in the XML output document:

1. Open your XML operation in the Source Editor.

2. Open the Methods node located in the Data Source pane.

3. Identify the node that corresponds to the element you want to include.

The types of nodes you can include are those that represent an array or collection, a class, or a method that returns a primitive.

4. Right-click the node and choose Include Tag in Output.

An element corresponding to the selected node is added to the XML Output Format pane.

Expanding a Class

To expand a class:

1. Open your XML operation in the Source Editor.

2. Open the Methods node located in the Data Source pane.

3. Right-click the class and choose Expand.

All getter methods on the class are added as subnodes to the class node. Elements corresponding to the class and to the return value of the getter methods are added to the XML Output Format pane.

Collapsing a Class

To collapse a class:

1. Open your XML operation in the Source Editor.

2. Open the Methods node located in the Data Source pane.

3. Right-click the class and choose Collapse.

The nodes representing the getter methods of the class are deleted from the Data Source pane. Their corresponding elements, as well as the element corresponding to the class, are deleted from the XML Output Format pane.

System Shared Objects

A web service maintains system shared objects at runtime containing data about web browser clients that access the web service. System shared objects are instantiated by the web service and populated with data obtained from the HTTP request and J2EE security mechanism. For information about how to use a system shared object, see Mapping a Method Parameter to a Source and FIGURE 4-13.

System shared objects are limited to user name, which is obtained from the J2EE security mechanism and provided as both a String object and java.security.Principal object.

For a parameter of type java.security.Principal, the System Shared Object selection in the Method Parameter Source dialog box lists one object, UserPrincipal.

For a parameter of type java.lang.String, the System Shared Object selection lists one object, UserName.


Static Utility Methods

Data returned by a business method might require some type of processing before being displayed on a web page. For example, an EJB method might return a value of type double, but you would like to display the value in a monetary format. You can best accomplish processing of this sort by writing or reusing static utility methods rather than by adding new methods to your business components.

Organizing Static Utility Methods

For ease of use, you should organize static utility methods that you write into a small number of classes in the IDE.

Place the utility classes that are specific to the web service under development in a service-specific location, for example, in the package containing your web service. Place general purpose utility classes in a package that can be easily reused in other services and by other web service developers.

Using Static Utility Methods

To use a static utility method in an XML operation:

1. Mount the static utility class in the Explorer.

2. Open your XML operation in the Source Editor.

3. Add a call to the utility method to your XML operation.

See Adding a Method to an XML Operation for information on how to do this.

The utility method call must be positioned after the method call that returns the data to be processed. To reposition the method, right-click it and choose Move Up or Move Down.

When you add the utility method, the IDE adds an element to the XML output document corresponding to the return value of the method. This element is displayed in the XML Output Format pane.

The IDE also adds a new input document element corresponding to the method's input parameter. You can ignore this input document element; the IDE will delete it for you after you remap the method parameter source in the next step.

4. Map your utility method's input parameter to the value returned by the method call on the data component.

The method call on the data component returns the value that you want to process with the utility method. So, you must map the output from the data component to the input of your utility method.

a. Expand the utility method node and then the Parameters node.

b. Right-click the parameter and choose Change Source.

The Method Parameter Source dialog box is displayed, as illustrated in FIGURE 4-15.

c. Select Previous Method.

 FIGURE 4-15 Method Parameter Source Dialog Box

Screenshot of dialog showing method parameter source types and values. Buttons are Add Element, Change Method, OK, Cancel, and Help.[ D ]

d. In the enabled combo box, select the method that returns the data from the data component and click OK.

See FIGURE 4-13 and Mapping a Method Parameter to a Source for more information on mapping parameters.

5. Exclude the return value of the data-component method call from the XML output document, by right-clicking the method that calls the data component and choosing Exclude Tag from Output.

The element is removed from the XML Output Format pane. Your client needs the processed data, but in most cases not the raw data returned by the data component.

6. (Optional) Set the utility method's Tag Name property to a more appropriate name.

7. (Optional) Rename the output document element.

When you added the utility method to the XML operation, the IDE added a corresponding element to the XML output document. By default, this element is named after the method (for example, formatAsDollars). In most cases, some other name would be more appropriate (for example, Price). To rename the element, change the value of the utility method's Tag Name property. See Renaming an Output Document Element for information on how to do this.