Stateless SessionBean — JNDI — CORBA object
GuessGame.idl
interface GuessGame {
short guess( in short number );
};
GuessGameImpl.java
public class GuessGameImpl implements GuessGameOperations {
private int answer;
public GuessGameImpl( int max ) {
answer = new java.util.Random().nextInt( max ) + 1;
System.out.println( "GuessGameImpl created" );
}
public short guess( short number ) {
short returnValue;
if (number == answer) {
returnValue = 0;
System.out.println( number + " is right on" );
} else if (number < answer) {
returnValue = -1;
System.out.println( number + " is too low" );
} else {
returnValue = 1;
System.out.println( number + " is too high" );
}
return returnValue;
} }
GuessGameBean.java
import java.rmi.RemoteException;
import javax.ejb.*;
interface RIGuessGame extends EJBObject {
short guess( short num ) throws RemoteException;
}
interface HIGuessGame extends EJBHome {
RIGuessGame create() throws RemoteException, CreateException;
}
public class GuessGameBean implements SessionBean {
private SessionContext context;
private GuessGame gg;
public short guess( short num ) {
short ans = gg.guess( num );
System.out.println( "GuessGameBean.guess() - input is " + num + ", answer is " + ans );
return ans;
}
public void ejbCreate() {
try {
javax.naming.InitialContext ic = new javax.naming.InitialContext();
// --- CORBA using JavaIDL ---
org.omg.CORBA.Object obj = (org.omg.CORBA.Object) ic.lookup( "GuessGameCorbaServant" );
gg = GuessGameHelper.narrow( obj );
// --- RMI-IIOP ---
// java.lang.Object obj = ic.lookup( "GuessGameCorbaServant" );
// gg = (GuessGame) javax.rmi.PortableRemoteObject.narrow( obj, GuessGame.class );
System.out.println( "GuessGameBean.ejbCreate() - CORBA DO found - " + gg );
} catch (Exception ex) { ex.printStackTrace();
} }
public void setSessionContext( SessionContext ctx ) {
System.out.println( "GuessGameBean.setSessionContext()" );
context = ctx;
}
public void ejbRemove() { System.out.println( "ejbRemove()" ); }
public void ejbActivate() { System.out.println( "ejbActivate()" ); }
public void ejbPassivate() { System.out.println( "ejbPassivate()" ); }
}
Server (bind to CORBA Naming Service or JNDI)
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
import org.omg.CosNaming.NameComponent;
import javax.naming.InitialContext;
public class Server {
public static void main( String[] args ) {
try {
// Create and initialize the ORB
ORB orb = ORB.init( args, null );
// Create the DO and the servant. Register the servant with the ORB.
GuessGameImpl gg = new GuessGameImpl( 100 );
GuessGame_Tie servant = new GuessGame_Tie( gg );
orb.connect( servant );
if (args.length == 2) {
// Get a reference to CORBA's Naming Service root context
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
// Bind the DO in CORBA's Naming Service
NameComponent nc = new NameComponent( "GuessGame", "" );
NameComponent path[] = { nc };
ncRef.rebind( path, servant );
System.out.println( "CORBA DO bound in CORBA Naming Service - " + servant );
} else {
// Get a reference to J2EE's JNDI root context
java.util.Properties props = new java.util.Properties();
props.put("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
props.put("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
InitialContext ic = new InitialContext( props );
// Bind the DO in J2EE's JNDI
ic.rebind( "GuessGameCorbaServant", servant );
System.out.println( "CORBA DO bound in JNDI - " + servant );
}
// Wait for requests from clients
java.lang.Object sync = new java.lang.Object();
synchronized( sync ) { sync.wait(); }
} catch (Exception ex) {
System.err.println( "ERROR: " + ex );
ex.printStackTrace();
} } }
Client (lookup in CORBA Naming Service or JNDI)
import java.io.*;
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
import org.omg.CosNaming.NameComponent;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class Client {
public static void main( String[] args ) throws IOException {
GuessGame ggCORBA = null;
RIGuessGame ggEJB = null;
try {
if (args.length == 2) {
System.out.println( "Client finding CORBA Distributed Object" );
ORB orb = ORB.init( args, null );
// Get a reference to the Naming Service root context
org.omg.CORBA.Object obj = orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow( obj );
// Find the DO in the Naming Service
NameComponent nc = new NameComponent( "GuessGame", "" );
NameComponent path[] = { nc };
ggCORBA = GuessGameHelper.narrow( ncRef.resolve( path ) );
} else {
System.out.println( "Client finding EJB Stateless SessionBean" );
InitialContext ic = new InitialContext();
java.lang.Object obj = ic.lookup( "GuessGameHome" );
HIGuessGame home = (HIGuessGame) PortableRemoteObject.narrow( obj, HIGuessGame.class );
ggEJB = home.create();
}
if (ggCORBA != null) System.out.println( "Server found - " + ggCORBA );
else System.out.println( "Server found - " + ggEJB.getClass().getName() );
short number, response=0, min=1, max=100;
BufferedReader in = new BufferedReader(
new InputStreamReader( System.in ) );
while (true) {
System.out.print( "Enter guess (" + min + "-" + max + "): " );
number = (short) Integer.parseInt( in.readLine() );
if (ggCORBA != null) response = ggCORBA.guess( number );
else response = ggEJB.guess( number );
if (response == 0) break;
if (response < 0) if (min < number) min = number;
if (response > 0) if (max > number) max = number;
System.out.println( " too " + ((response < 0) ? "low" : "high") );
}
System.out.println( "bells!! sirens!! confetti!!" );
} catch (Exception ex) { ex.printStackTrace(); }
} }
Files to deploy for EJB
stateless SessionBean files
HIGuessGame.class
RIGuessGame.class
GuessGameBean.class
client-side CORBA files
GuessGame.class
_GuessGameStub.class
GuessGame_Tie.class
GuessGameOperations.class
GuessGameHelper.class
CORBA only output
D:\java\corba\guess> idlj -fallTIE GuessGame.idl
D:\java\corba\guess> javac *.java
D:\> tnameserv -ORBInitialPort 1050
Initial Naming Context:
IOR:000000000000002849444c3a6f6d672e6f72672f436f734e616d696e672f4e616d696e67436f
6e746578743a312e3000000000010000000000000054000101000000000b31302e302e342e323334
00000530000000000018afabcaff000000022966a559000000080000000000000000000000010000
0001000000140000000000010020000000000001010000000000
TransientNameServer: setting port for initial object references to: 1050
Ready.
D:\java\corba\guess> java Server -ORBInitialPort 1050
GuessGameImpl created
CORBA DO bound in CORBA Naming Service - GuessGame_Tie:IOR:000000000000001249444
c3a477565737347616d653a312e30000000000000010000000000000054000101000000000b31302
e302e342e32333400000538000000000018afabcaff000000022966ba2a000000080000000000000
0000000000100000001000000140000000000010020000000000001010000000000
33 is too low
66 is too high
49 is too high
41 is too high
37 is too low
39 is too low
40 is right on
D:\java\corba\guess> java Client -ORBInitialPort 1050
Client finding CORBA Distributed Object
Server found - _GuessGameStub:IOR:000000000000001249444c3a477565737347616d653a31
2e30000000000000010000000000000054000101000000000b31302e302e342e3233340000053800
0000000018afabcaff000000022966ba2a0000000800000000000000000000000100000001000000
140000000000010020000000000001010000000000
Enter guess (1-100): 33
too low
Enter guess (33-100): 66
too high
Enter guess (33-66): 49
too high
Enter guess (33-49): 41
too high
Enter guess (33-41): 37
too low
Enter guess (37-41): 39
too low
Enter guess (39-41): 40
bells!! sirens!! confetti!!
EJB to CORBA output
D:\> j2ee -verbose
J2EE server Listen Port: = 1101
Naming service started: :1100
.....
Web service started: 9191
Web service started: 8000
Web service started: 7000
.....
J2EE server startup complete.
.....
Application Guess deployed.
GuessGameBean.setSessionContext()
GuessGameBean.ejbCreate() - CORBA DO found - _GuessGameStub:IOR:0000000000000012
49444c3a477565737347616d653a312e30000000000000010000000000000054000101000000000b
31302e302e342e32333400000526000000000018afabcafe00000002296683b30000000800000001
000000000000000100000001000000140000000000010020000000000001010000000000
GuessGameBean.guess() - input is 33, answer is 1
GuessGameBean.guess() - input is 16, answer is 1
GuessGameBean.guess() - input is 8, answer is 1
GuessGameBean.guess() - input is 4, answer is -1
GuessGameBean.guess() - input is 6, answer is 0
D:\java\corba\guess> java Server
GuessGameImpl created
CORBA DO bound in JNDI - GuessGame_Tie:IOR:000000000000001249444c3a4775657373476
16d653a312e30000000000000010000000000000054000101000000000b31302e302e342e3233340
0000526000000000018afabcafe00000002296683b30000000800000001000000000000000100000
001000000140000000000010020000000000001010000000000
33 is too high
16 is too high
8 is too high
4 is too low
6 is right on
D:\java\corba\guess> java Client
Client finding EJB Stateless SessionBean
Server found - _RIGuessGame_Stub
Enter guess (1-100): 33
too high
Enter guess (1-33): 16
too high
Enter guess (1-16): 8
too high
Enter guess (1-8): 4
too low
Enter guess (4-8): 6
bells!! sirens!! confetti!!