[an error occurred while processing this directive]
> developer > web app development
Calling xCommerce Services from Java
by Frank Hamilton, Lead Consulting Engineer, Novell
Date Created: 2001-06-06 08:32:00.000
  Introduction
  Requirements
  Deploy your xCommerce Service
  Compile the Program
  Test
introduction

The purpose of this document is to describe the process of calling an xCommerce service from a Java application on a SilverStream application server.  In the example that follows, we will be using version 3.71 of the S3 application server and the xCommerce tutorial sample project.  Hopefully, the example shown here should provide a point of reference should you wish to call a service on WebSphere or WebLogic.

requirements

We will assume that a few simple prerequisites have been met:

.        xCommerce Designer must be installed on your local machine (e.g., d:\xCommerce\Designer).  

.        The xCommerce TutorialEnd Project must be installed on your local machine (e.g., d:\xCommerce\Samples\TutorialEnd). The Tutorial will be automatically installed as part of the standard xCommerce installation.

.        A MS Access ODBC database driver must be installed on your local machine.  

.        An ODBC DSN must be created called "XCTutorial" and it must point at the database "TutorialProject.mdb" in the \xCommerce\Samples\TutorialFiles directory.  (This DSN should have been created for you automatically during the standard xCommerce Install.)

.        You need access to a SilverStream Application Server, running on the network or locally.

.        The server must have xCommerce Server for SilverStream installed and running.

.        You will need a JDK 1.2.2 or higher to compile the sample Java code.

Deploy your xCommerce Service

.         Make sure your SilverStream server is running and the xCommerce Server has been loaded.  On the SilverStream server console, during startup, you will see the  message "xCommerce Server Started, Version: x.x  Build:xxx" if the xCommerce server is running.  During the server startup, it may also be worth noting the port that the JBroker ORB is listening on (the default being 54890), since we will need this information later.

.         Launch xCommerce Designer.

.         Choose File...Open Project and navigate to the \xCommerce\Samples\TutorialEnd project that was installed during initial installation. 

.         Choose File...Deploy to start the deployment wizard.

o        Service Description

*         In this example, we are deploying to a SilverStream application server.

*         You can specify any directory as your temporary staging directory; just make sure it is outside the root of the project      directory.

*         Choose a jar name that is appropriate for your application and make note of the name (for the example: tutorial.jar).

*         Choose a deployment context that is appropriate for your application (for the example: com.xcommerce.tutorial).

o        Servlet-Based Triggers

*         This is an optional step. Complete if your service is going to use servlet-based triggers.

o        EJB-Based Triggers

*         This is the critical step to enable your Java application to call your xCommerce service.

*         In the first pulldown menu, choose the service that you would like to call

*         Provide a unique JNDI name that will be used to identify the service.  The name must be unique on the server, or you will get an org.omg.CosNaming.NamingContextPackage.AlreadyBound exception.   The JNDI name is also case sensitive.

*         Choose a transaction attribute that is appropriate for your service. For the example, we choose "Not Supported".

o        The next three dialogs are optional and are not used in this example

o        The final dialog describes where to deploy the service.

*         Specify the server and port of your SilverStream application server.  We will use the same information in our Java application, so it will definitely be worth noting.

*         The server database name that will hold your deployed objects

*         If your server is secure, specify an administrator name and password to deploy your service.

*         In the example, we will choose "Yes" to automatically upload the project.

o        Hit "Finish" to deploy your service

 

Create a Java program to call the service. 

For this example, we've created a small program in a file called CallXCEJB.java.

The file contains the following code:



// Core imports for calling xCommerce:

import com.sssw.b2b.xs.ejb.*;                  	// xcs-all.jar

import com.sssw.b2b.xs.sssw.GXSEJBAccessor;  	// xcs-all.jar

import com.sssw.b2b.xs.ejb.IGXSEJBServiceHome; 	// xcs-all.jar

import com.sssw.rt.util.*;                     	// SilverRuntime.zip

import javax.ejb.*;                            	// ejb.jar



public class CallXCEJB {



// Change the following constants to reflect your environment.  The

// JNDI name was defined when you deployed your service.



static final String sHOST = "fhamilton";

static final int iPORT = 80;

static final String sJNDINAME = "ProductLookupJNDI";

static final int iRMIPORT = 54890;



public static void main(String[] args) {



// This will hold the return value from the xCommerce service



String sReturn = "";



// Build an XML Document for the Service.  Notice the escape sequence.



StringBuffer xmlDoc = new StringBuffer();

xmlDoc.append("<?xml version =\"1.0\" encoding=\"UTF-8\"?>");

xmlDoc.append("<ROOT>");

xmlDoc.append("<SKU>LOR8437</SKU>");

xmlDoc.append("</ROOT>");



try {



// Note: The following  method is overloaded and can accept two additional

// Arguments: username, password. The version we are using

//	 has the following signature:

//

// public static java.lang.Object getHomeBean(java.lang.String className,

	//					java.lang.String jndiName,

	// 					java.lang.String ejbHostname,

	//    			        	int port,

	//					int rmiPort)

	//					throws java.lang.Exception

//

// But there is also:

//

// public static java.lang.Object getHomeBean(java.lang.String className,

	//					java.lang.String jndiName,

	//					java.lang.String ejbHostname,

	//					int port,

	//					int rmiPort,

	//					java.lang.String uname,

	//					java.lang.String password

	//					throws java.lang.Exception



IGXSEJBServiceHome srvcHome = (IGXSEJBServiceHome)

GXSEJBAccessor.getHomeBean("com.sssw.b2b.xs.ejb.IGXSEJBServiceHome",

                         sJNDINAME,

                         sHOST,

                         iPORT,

                         iRMIPORT);



IGXSEJBServiceComponent ejbSrvc = srvcHome.create();

sReturn = ejbSrvc.execute(xmlDoc.toString());

System.out.println(sReturn);



}

catch (Throwable tx) {

System.out.println(tx.getMessage());

tx.printStackTrace();

}

}



}   

 

compile the program
You need to modify your classpath variable, or supply a classpath to the javac compiler so the SilverRuntime.zip, ejb.jar and the xCommerce xcs-all.jar can be found.  

.        Example:

javac -classpath "c:\SilverStream371\lib\SilverRuntime.zip; c:\SilverStream371\lib\ejb.jar; c:\SilverStream371\xCommerce\lib\xcs-all.jar"; CallXCEJB.java

test

.        In order to run the application, you will need the EJB stub jar that was created by the deployment process.  On your SilverStream server, you can find this under "EJB JARs & Media", in the Jars folder; it will be named "EJBStub-yourjarname.jar".  In the deployment above, this was the jar we created in the first step, so our stub jar is called "EJBStub-tutorial.jar".  You need to right-click on this jar, choose "Save to Disk," and place it in a directory that is readily accessible to your java application.

.        You will need to modify your classpath environment variable, or supply a classpath using the -cp switch, so the SilverRuntime.zip, ejb.jar, xcs-all.jar and the EJBstub-tutorial.jar can be found.

.        Example: 

java -cp ".;c:\SilverStream371\lib\SilverRuntime.zip; c:\SilverStream371\lib\ejb.jar; c:\SilverStream371\xCommerce\lib\xcs-all.jar;.\EJBStub-tutorial.jar" CallXCEJB

If you would like to see additional information on xCommerce Deployment Framework please reference the xCommerce Server for SilverStream User Guide, Chapter 5 Using the Deployment Framework.