package examples.wtc.atmi.convsimp;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ejb.CreateException;
import weblogic.ejb.GenericSessionBean;
import weblogic.wtc.gwt.TuxedoConnection;
import weblogic.wtc.gwt.TuxedoConnectionFactory;
import weblogic.wtc.jatmi.*;
import weblogic.ejbgen.*;
/**
* ToupperBean is a stateful SessionBean. This EJBean illustrates:
* <ul>
* <li> The use of the Tuxedo Oatmial connector
* <li> The use of Application-defined exceptions
* </ul>
*
* @author Copyright (c) 1998-@COPYRIGHT_CURRENTYEAR by BEA Systems, Inc. All Rights Reserved.
*/
@FileGeneration(remoteClass = Constants.Bool.TRUE,
localHome = Constants.Bool.FALSE,
remoteHome = Constants.Bool.TRUE,
remoteClassName = "TuxedoConversation",
remoteHomeName = "TuxedoConversationHome",
localClass = Constants.Bool.FALSE)
@JarSettings(ejbClientJar = "wtc_tux_conversation_client.jar")
@JndiName(remote = "tuxedo.services.TuxedoConversationHome")
@Session(idleTimeoutSeconds = "600",
maxBeansInCache = "100",
transTimeoutSeconds = "0",
type = Session.SessionType.STATEFUL,
defaultTransaction = Constants.TransactionAttribute.SUPPORTS,
ejbName = "TuxedoConversation")
public class TuxedoConversationBean extends GenericSessionBean {
static final boolean VERBOSE = true;
/**
* This is the classic TOUPPER simpapp method, which takes the string
* argument, and converts it to all upper case. This implementation
* will get the TuxedoConnectionFactory from JNDI, and use it to get
* a Tuxedo object, which can then be used to do the actual tpcall. Of
* course, we must first convert the java String object to the TypedBuffer
* TypedString object.
*
* @param datasize
* @param iterations
*/
@RemoteMethod()
public void conversation(int datasize, int iterations) throws TPException {
Context ctx;
TuxedoConnectionFactory tcf;
TuxedoConnection myTux; // For now we get it via NEW until the Factory works
TypedCArray myData = null;
TypedCArray returnData = null;
Reply myRtn = null;
int status;
int flags;
int lcv;
Conversation myConversation;
int num_sends;
int outstanding;
log("conversation called, datasize:" + datasize + " iterations: "
+ iterations);
if (iterations <= 0) {
return;
}
try {
ctx = new InitialContext();
tcf =
(TuxedoConnectionFactory) ctx.lookup("tuxedo.services.TuxedoConnection");
} catch (NamingException ne) {
// Could not get the tuxedo object, throw TPENOENT
throw new TPException(TPException.TPENOENT,
"Could not get TuxedoConnectionFactory : " + ne);
}
myTux = tcf.getTuxedoConnection();
if (datasize > 0) {
myData = new TypedCArray(datasize);
for (lcv = 0; lcv < datasize; lcv++) {
myData.carray[lcv] = (byte) (lcv & 0xff);
}
}
num_sends = iterations;
if (iterations <= 1) {
flags = ApplicationToMonitorInterface.TPRECVONLY;
} else {
flags = ApplicationToMonitorInterface.TPSENDONLY;
}
log("About to start convesation");
try {
myConversation = myTux.tpconnect("CTOUPPER", myData, flags);
num_sends--;
} catch (TPException te) {
log("tpconnect threw TPException " + te);
throw te;
} catch (Exception ee) {
log("tpconnect threw exception: " + ee);
throw new TPException(TPException.TPESYSTEM, "Exception: " + ee);
}
log("tpconnect successfull!");
while (num_sends > 0) {
if (num_sends <= 1) {
flags = ApplicationToMonitorInterface.TPRECVONLY;
} else {
flags = 0;
}
try {
myConversation.tpsend(myData, flags);
} catch (TPException te) {
log("tpsend threw TPException " + te);
throw te;
} catch (Exception ee) {
log("tpsend threw exception: " + ee);
throw new TPException(TPException.TPESYSTEM, "Exception: " + ee);
}
num_sends--;
}
log("first send successful!");
outstanding = iterations;
while (outstanding > 0) {
try {
myRtn = myConversation.tprecv(0);
if (outstanding <= 1) {
log("tprecv did not get a SENDONLY when expected");
throw new TPException(TPException.TPESYSTEM,
"Did not get expected SENDONLY event");
}
} catch (TPReplyException tre) {
switch (tre.gettperrno()) {
case TPException.TPEEVENT:
switch (tre.getrevent()) {
case TPException.TPEV_SENDONLY:
if (outstanding > 1) {
log("tprecv got an SENDONLY before its time: " + tre);
throw new TPException(TPException.TPESYSTEM,
"Unexpected SENDONLY event: " + tre);
}
myRtn = tre.getExceptionReply();
break;
default:
// Anything else in this recv loop is an error
log("tprecv got an unexpected event from the server: " + tre);
throw new TPException(TPException.TPESYSTEM,
"Unexpected tprecv event: " + tre);
}
break;
default:
log("tprecv got an unexpected error from the server: " + tre);
throw new TPException(TPException.TPESYSTEM,
"Unexpected tprecv exception: " + tre);
}
} catch (TPException te) {
log("tprecv(1) threw TPException " + te);
throw te;
} catch (Exception ee) {
log("tprecv(1) threw exception: " + ee);
throw new TPException(TPException.TPESYSTEM, "Exception: " + ee);
}
returnData = (TypedCArray) myRtn.getReplyBuffer();
/**
* returnData.sendSize may not equal myData.sendSize do to return buffer padding
*/
for (lcv = 0; lcv < myData.sendSize; lcv++) {
if (returnData.carray[lcv] != myData.carray[lcv]) {
log("Invalid return data:" + lcv + "/" + returnData.carray[lcv]
+ "/" + myData.carray[lcv]);
throw new TPException(TPException.TPESYSTEM, "Invalid return data");
}
}
outstanding--;
}
log("first receive successful!");
num_sends = iterations;
while (num_sends > 0) {
if (num_sends <= 1) {
flags = ApplicationToMonitorInterface.TPRECVONLY;
} else {
flags = 0;
}
try {
myConversation.tpsend(myData, flags);
} catch (TPException te) {
log("tpsend(2) threw TPException " + te);
throw te;
} catch (Exception ee) {
log("tpsend(2) threw exception: " + ee);
throw new TPException(TPException.TPESYSTEM, "Exception: " + ee);
}
num_sends--;
}
log("second send successful!");
outstanding = iterations;
while (outstanding > 0) {
try {
myRtn = myConversation.tprecv(0);
if (outstanding <= 1) {
log("tprecv(2) did not get a SVCSUCC when expected");
throw new TPException(TPException.TPESYSTEM,
"Did not get expected SVCSUCC event");
}
} catch (TPReplyException tre) {
switch (tre.gettperrno()) {
case TPException.TPEEVENT:
switch (tre.getrevent()) {
case TPException.TPEV_SVCSUCC:
if (outstanding > 1) {
log("tprecv got an SVCSUCC before its time: " + tre);