![]() |
![]() |
|
|
Using Application Views by Writing Custom Code
If you are a developer, you may want to modify an application view by writing custom code. You can use most application view features by using its Web-based GUI, but there are some application view features you can use only by custom coding.
This section contains information on the following subjects:
Scenario 1: Connecting Using Specific Credentials
If necessary, you can invoke methods on an application view that let you set the security level before invoking services on the application view.
Use the new ApplicationView methods setConnectionSpec() and getConnectionSpec() to set the credentials for an EIS. Both methods use a ConnectionSpec object. To instantiate a ConnectionSpec object, you can use the ConnectionRequestInfoMap class provided by the BEA WebLogic Integration Adapter Development Kit (ADK), or you can implement your own class. If you implement your own class, you must include the interfaces ConnectionSpec, ConnectionRequestInfo, Map, and Serializable.
Implementing ConnectionSpec
Before you can use setConnectionSpec() or getConnectionSpec(), you must instantiate a ConnectionSpec object. Use the ConnectionRequestInfoMap class provided by the ADK, or derive your own class.
To implement ConnectionSpec:
Calling setConnectionSpec() and getConnectionSpec()
After you implement the ConnectionSpec class and instantiate a ConnectionSpec object, you can use it in conjunction with the following two new ApplicationView methods:
Listing 4-1 Complete Code for setConnectionSpec()
/**
* Sets the connectionSpec for connections made to the EIS. After the
* ConnectionSpec is set it will be used to make connections to the
* EIS when invoking a service. To clear the connection spec, and use
* the default connection parameters, call this method using null.
*
* @params connectionCriteria connection criteria for the EIS.
*/
public void setConnectionSpec(ConnectionSpec connectionCriteria)
{
m_connCriteria = connectionCriteria;
}
Listing 4-2 Complete Code for getConnectionSpec()
/**
* Returns the ConnectionSpec set by setConnectionSpec. If no
* ConnectionSpec has been set null is returned.
*
* @returns ConnectionSpec
*/
public ConnectionSpec getConnectionSpec()
{
return m_connCriteria;
}
Using the ConnectionSpec
To set the ConnectionSpec, pass it a properly initialized ConnectionSpec object. To clear the ConnectionSpec, pass it a ConnectionSpec object with a null value.
Listing 4-3 shows a specific example for using ConnectionSpec.
Listing 4-3 An Example That Uses ConnectionSpec
Properties props = new Properties();
ApplicationView applicationView = new ApplicationView(getInitialContext(props),"appViewTestSend");
ConnectionRequestInfoMap map = new ConnectionRequestInfoMap();
// map properties here
map.put("PropertyOne","valueOne");
map.put("PropertyTwo","valueTwo");
.
.
.
//set new connection spec
applicationView.setConnectionSpec(map);
IDocumentDefinition requestDocumentDef = applicationView.getRequestDocumentDefinition("serviceName");
SOMSchema requestSchema = requestDocumentDef.getDocumentSchema();
DefaultDocumentOptions options = new DefaultDocumentOptions();
options.setForceMinOccurs(1);
options.setRootName("ROOTNAME");
options.setTargetDocument(DocumentFactory.createDocument());
IDocument requestDocument = requestSchema.createDefaultDocument(options);
requestDocument.setStringInFirst("//ROOT/ElementOne","value");
requestDocument.setStringInFirst("//ROOT/ElementTwo","value");
.
.
.
// the service invocation will use the connection spec set to connect to the EIS
IDocument result = applicationView.invokeService("serviceName", requestDocument);
System.out.println(result.toXML());
Scenario 2: Custom Coding a Business Process
Although the primary way to use application views in business processes is to use business process management (BPM), an alternate way is to write custom Java code to represent the business process. If you are a developer who uses the custom coding method, this section uses a simple example to demonstrate how to custom code a business process.
For a thorough comparison of the two ways to use application views, see Deciding Which of the Two Methods to Use.
About this Scenario
In the simple example used throughout this section, the following business logic is implemented:
An enterprise has a customer relationship management (CRM) system and an order processing (OP) system. You want a business process that coordinates the synchronization of customer information between these two systems. That means that whenever a customer is created on the CRM system, it should trigger the creation of a corresponding customer record on the OP system. The attached Java class SyncCustomerInformation implements this business logic.
This example does not cover everything you can do using custom code. It only demonstrates the basic steps you take when you implement your own organization's business processes.
Your role is to use this example code as a template for custom coding your own business processes.
This scenario uses a concrete example class called SyncCustomerInformation to explain how to write custom code. In general, you must do the following two steps to create custom code that uses an application view in a business process:
Before You Begin
The following prerequisites must be met before you write custom code to implement a business process:
In addition, this particular scenario assumes the following prerequisites are already complete:
Note: Your organization will have its own folders and application views.
Note: For your organization, get this information from the system administrator.
Creating the SyncCustomerInformation Class
When writing custom code, a Java class must exist to represent each application required for the business process. Create the necessary Java classes if they do not exist already. This example calls for one application class called SyncCustomerInformation. Of course, your own code will use different variable names. To create the SyncCustomerInformation Java class:
Note: For your own projects, use the SyncCustomerInformation code as a template or guide. The SyncCustomerInformation example code is thoroughly commented.
When a New Customer event is received, the onEvent method of the listener is called.
The onEvent method should then call a method to respond to the event. In this example, the onEvent method provides the event object that contains the data associated with the event. The method is called handleNewCustomer.
The following things happen:
When you are finished, a new Java class exists called SyncCustomerInformation. This class implements the Sync Customer Information business logic. This SyncCustomerInformation class uses the application integration API to get events from the CRM system and to invoke services on the OP system.
Example Code for SyncCustomerInformation
The following code listing is the full source code for the SyncCustomerInformation Java class. It implements the business logic for the scenario described earlier in this chapter. Use this example code as a guide for writing your own custom code to implement your enterprise's business processes.
Listing 4-4 Full Class Source Code for SyncCustomerInformation
import java.util.Hashtable;
import javax.naming.*;
import java.rmi.RemoteException;
import com.bea.wlai.client.*;
import com.bea.wlai.common.*;
import com.bea.document.*;
/**
* This class implements the business logic for the 'Sync Customer Information'
* business process. It uses the WLAI API to listen to events from the CRM
* system, and to invoke services on the OP system. It assumes that there
* are two ApplicationViews defined and deployed in the 'EastCoast'
* namespace. The application views and their required events and services
* are shown below.
*
* CustomerManagement
* events (NewCustomer)
* services (none)
*
* OrderProcessing
* events (none)
* services (CreateCustomer)
*/
public class SyncCustomerInformation
implements EventListener
{
/**
* Main method to start this application. No args are required.
*/
public static void
main(String[] args)
{
// Check that we have the information needed to connect to the server.
if (args.length != 3)
{
System.out.println("Usage: SyncCustomerInformation ");
System.out.println(" <server url> <user id> <password>");
return;
}
try
{
// Create an instance of SyncCustomerInformation to work with
SyncCustomerInformation syncCustInfo =
new SyncCustomerInformation(args[0], args[1], args[2]);
// Get a connection to WLAI
InitialContext initialContext = syncCustInfo.getInitialContext();
// Get a reference to an instance of the 'EastCoast.CustomerManagement'
// Application View
ApplicationView custMgmt =
new ApplicationView(initialContext, "EastCoast.CustomerManagement");
// Add the listener for 'New Customer' events. In this case we have
// our application class implement EventListener so it can listen for
// events directly.
custMgmt.addEventListener("NewCustomer", syncCustInfo);
// Process up to 10 events and then quit.
syncCustInfo.setMaxEventCount(10);
syncCustInfo.processEvents();
}
catch (Exception e)
{
e.printStackTrace();
}
return;
}
/**
* EventListener method to respond to 'New Customer' events
*/
public void
onEvent(IEvent newCustomerEvent)
{
try
{
// Print the contents of the incoming 'New Customer' event.
System.out.println("Handling new customer: ");
System.out.println(newCustomerEvent.toXML());
// Handle it
IDocument response = handleNewCustomer(newCustomerEvent.getPayload());
// Print the response
System.out.println("Response: ");
System.out.println(response.toXML());
// If we have processed all the events we want to, quit.
m_eventCount++;
if (m_eventCount >= m_maxEventCount)
{
quit();
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Quitting...");
quit();
}
}
/**
* Handles any 'New Customer' event by invoking the 'Create Customer'
* service on the 'Order Processing' ApplicationView. The response
* document from the service is returned as the return value of this
* method.
*/
public IDocument
handleNewCustomer(IDocument newCustomerData)
throws Exception
{
// Get an instance of the 'OrderProcessing' ApplicationView.
if (m_orderProc == null)
{
m_orderProc =
![]() |
![]() |
![]() |
|
Copyright © 2001 BEA Systems, Inc. All rights reserved.
|