How Do I: Create a Script for Use with a Web Service?

You can create ECMAScript functions to customize WebLogic Server's translation between XML messages your service receives or sends and your Java code. You use the script by placing a reference to it in your JWS file. At run time, WebLogic Server uses the script for translation. Scripts you create are stored in files with a JSX extension.

The following steps describe what to do at a high level, and offer links to more detailed information.

1. Open the JSX file that will contain your new script function, or create a new file.

2. Write a function to translate incoming XML for use by your Java code, or write a function that will translate your Java data into outgoing XML.

3. Connect the script function to your Java code in a JWS through an XML map.

The following procedures offer step-by-step instructions on the basics of writing an ECMAScript function for translating XML and Java.

To Create a New JSX File

1. Locate the folder in your project where you will store the script (JSX) file.

Where you put the file depends on how your project is structured. For example, you might put it in the same folder as a JWS file that will be using it. You could also create a separate, or subordinate, "scripts" folder where you store all JSX files for this project.

2. Right-click the folder, then click New File.

3. In the Create New File dialog, click JavaScript.

4. In the File name box, enter the name of your script file.

Note that the File extension box contains "jsx" by default for script files.

5. Click OK.

The new script file should open in Source View. The Project Tree should list the file you have just created.

To Create ECMAScript for Translating to XML from Java

1. Open the JSX file to which you will be adding a new function.

Note: If you are working with a JSX file you have just created, there should already be a placeholder for this function.

2. If needed, use the import statement to import Java classes for use in your ECMAScript code.

This statement is similar to the import directive in Java. You will need to import any Java classes that will be used by your script. For more information about import, see Importing Java Classes to ECMAScript with the import Statement.

    import mypackage.MyOuterClass.MyClass;

3. Declare a function for translating to XML.

The function declaration can have any name you like, but it:

- Must end with the phrase "ToXML".

- Must have a parameter representing each part of the Java declaration (in the JWS file) that this script will use for translating to XML.

For example, the following illustration describes a function declaration designed to receive two Java parameters:

You will refer to your function from an XML map in a JWS file. This illustration, for example, could receive two parameters from a callback to a client or method on a ServiceControl.

For more detail on connecting script functions to a JWS file, see How Do I: Use Script from a Web Service?

4. Add ECMAScript code that will use the data represented by the parameters to form XML.

Use the extended version of ECMAScript to make creating and handling XML easier. For example, you might:

- Declare an XML variable. For more information, see Creating and Using XML Variables.

- Assign data from your parameters to the elements and attributes of the XML contained in the XML variable.

5. At the end of the function, return the XML variable your code has created.

The XML you return will be added to the message sent by WebLogic Server. For example, a simple script that creates XML and adds two values to it might look like the following:

    /* Create an XML variable. */
    var orderMessage = <order/>;
    /* Add two child elements, inserting values from parameters received by the script. */
    orderMessage.appendChild(<item_name>{orderInfo.name}</item_name>);
    orderMessage.appendChild(<customer_name>{customerInfo.name}</customer_name>):
    /* Return the new XML */
    return orderMessage;

After you have created your script, you use it by connecting it to your JWS file through an XML map. For more information, see How Do I: Use Script from a Web Service?

To Create ECMAScript for Translating from XML to Java

1. Open the JSX file to which you will be adding a new function.

Note: If you are working with a JSX file you have just created, there should already be a placeholder for this function.

2. If needed, use the import statement to import Java classes for use in your ECMAScript code.

This statement is similar to the import directive in Java. You will need to import any Java classes that will be used by your script. For more information about import, see Importing Java Classes to ECMAScript with the import Statement.

    import mypackage.MyOuterClass.MyClass;

3. Declare a function for translating from XML to Java.

The function declaration can have any name you like, but it:

- Must end with the phrase "FromXML".

- Must have a parameter representing the incoming XML that this script will translate to Java.

For example, the following illustration describes a function declaration designed to receive the XML:

You will refer to your function from an XML map in a JWS file. For more detail on connecting script functions to a JWS file, see How Do I: Use Script from a Web Service? For more information on how this kind of function is referenced from an XML map, see Using Script Functions From XML Maps.

4. Add ECMAScript code that will use the incoming XML represented by the function's parameter to create the data needed by the JWS file.

Use the extended version of ECMAScript to make handling XML easier. For example, you might:

- Declare variables to hold the data that will be needed by your JWS code. These will be returned by your function.

- Access the xml parameter using the . operator, as described in Accessing Element Children With the . Operator. Or see information on related operators in Summary of ECMAScript Language Extensions.

- Assign data from the xml variable to variables that will be returned by the function.

5. At the end of the function, return an array containing the variables needed by your Java code.

The variables you return will be passed to your Java declaration. For example, a simple script that accesses XML and extracts two values from it might look like the following:

    /* Create variables you will later return to your Java declaration. */
    var orderInfo = xml.order.order_info;
    var customerInfo = xml.order.customer_info;
    /* Return an array containing the variables. */
    return [orderInfo, customerInfo];

After you have created your script, you use it by connecting it to your JWS file through an XML map. For more information, see How Do I Use Script from a Web Service?

Related Topics

How Do I: Use Script from a Web Service?

Summary of ECMAScript Language Extensions

Creating and Using XML Variables