Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PoratableRemoteObject

 
Vaithialingam Asai
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic