*
The moose likes EJB and other Java EE Technologies and the fly likes PoratableRemoteObject Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "PoratableRemoteObject" Watch "PoratableRemoteObject" New topic
Author

PoratableRemoteObject

Vaithialingam Asai
Greenhorn

Joined: Mar 07, 2004
Posts: 10
We can get the Home object by looking up the JNDI name. But why do we narrow it using PortableRemoteObject? I have read that JNDI might not return the server's implementation of your home interface. Why so? What narrowing does?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Hello Asai,
May I ask you to take a look at our naming policy which (among other things) requires a display name to have both first and last name, and then proceed to edit your display name accordingly.
Thanks!


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
Asai,
This is a good one. Only today, I had discussed this question in another forum. So make that small change to ur name and I'll give U the answer
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
The Object that is returned from the JNDI.lookup() method is not guaranteed to implement the home interface. This is because the underlying protocol is RMI-IIOP. IIOP is primarily for CORBA,which supports different languages.
Not all languages have the concept of casting or implementing an interface.
I remember having read somewhere that it is possible to have Cobol/C++ clients for EJBs.
So it is PortableRemoteObject.narrow()'s responsibilty to ensure that an Object implementing the home interface is returned to the Java client.
I have also enclosed some snippets from the specs.
EJB2.0 Spec p:67, sec 6.10, chap 6. Client view of a session

6.10 Type narrowing
A client program that is intended to be interoperable with all compliant EJB Container implementations must use the javax.rmi.PortableRemoteObject.narrow(...) method to perform type-narrowing of the client-side representations of the remote home and remote interfaces.
Note: Programs using the cast operator for narrowing the remote and remote home interfaces are likely to fail if the Container implementation uses RMI-IIOP as the underlying communication transport.

EJB1.1 spec, Appendix C, Rev history, sec: C3, p:295
Changes since Release 0.95
Use the javax.rmi.PortableRemoteObject.narrow(...) method to perform the narrow operations after a JNDI lookup in the code samples used in the specification. While some JNDI providers may return from the lookup(... method the exact stub for the home interface making it possible to for the client application to use a Java cast, other providers may return a wider type that requires an explicit narrow to the home interface type. The javax.rmi.PortableRemoteObject.narrow(...) method is the standard Java RMI way to perform the explicit narrow operation.
Ajoy Sharma
Greenhorn

Joined: Feb 24, 2004
Posts: 18
Caught an unexpected exception!
java.lang.ClassCastException
at com.sun.corba.se.internal.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:293)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:134)
at ConverterClient.main(ConverterClient.java:69)
Code or Clenit:
Client Code is given below:
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.util.Properties;
import javax.ejb.CreateException;
// NOT NECESSARY import Converter;
// NOT NECESSARY import ConverterHome;
public class ConverterClient {
public static void main(String[] args) {
try {
Properties env = new Properties();
env.put("java.naming.factory.initial","com.sun.jndi.cosnaming.CNCtxFactory");
env.put("java.naming.provider.url", "iiop://localhost:3700");
Context initial = new InitialContext(env);
ConverterHome home =(ConverterHome)PortableRemoteObject.narrow(initial.lookup("MyConverter"),ConverterHome.class);

// Create converter session bean
Converter currencyConverter = home.create();
// Calculate from dollars to yen
double amount = currencyConverter.dollarToYen(100.00);
System.out.println("100 Dollars is " + String.valueOf(amount) + " Yen");
// Calculate from yen to dollars
amount = currencyConverter.yenToEuro(100.00);
System.out.println("100 Yen is " + String.valueOf(amount) + " Dollars");
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
}

thnsk waiting for ur response.
Anand
Ajoy Sharma
Greenhorn

Joined: Feb 24, 2004
Posts: 18
Iam facing this problem .Could some help me to resolve this problem.
Iam struggling from last four days.
plzzzzzzzzzzz help me.
Caught an unexpected exception!
java.lang.ClassCastException
at com.sun.corba.se.internal.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:293)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:134)
at ConverterClient.main(ConverterClient.java:69)
Code or Clenit:
Client Code is given below:
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.util.Properties;
import javax.ejb.CreateException;
// NOT NECESSARY import Converter;
// NOT NECESSARY import ConverterHome;
public class ConverterClient {
public static void main(String[] args) {
try {
Properties env = new Properties();
env.put("java.naming.factory.initial","com.sun.jndi.cosnaming.CNCtxFactory");
env.put("java.naming.provider.url", "iiop://localhost:3700");
Context initial = new InitialContext(env);
ConverterHome home =(ConverterHome)PortableRemoteObject.narrow(initial.lookup("MyConverter"),ConverterHome.class);

// Create converter session bean
Converter currencyConverter = home.create();
// Calculate from dollars to yen
double amount = currencyConverter.dollarToYen(100.00);
System.out.println("100 Dollars is " + String.valueOf(amount) + " Yen");
// Calculate from yen to dollars
amount = currencyConverter.yenToEuro(100.00);
System.out.println("100 Yen is " + String.valueOf(amount) + " Dollars");
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
}

thnsk waiting for ur response.
Anand
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: PoratableRemoteObject
 
Similar Threads
Caching references to RemoteInterface
why we need home interface in EJB 2.0
Why dont we need to narrow the EJBObject stub reference
Why do we need to serialize the home?
why do we need JNDI