Why use PortableRemoteObject.narrow() ?

hi!
I am new to RMI-IIOP. Can somebody explain why we need to use the narrow method to type cast object refs in RMI-IIOP while in RMI, we just need to use a casting operator?
TIA,
Mani

See http://info.borland.com/devsupport/appserver/faq/45/ejb/portable_remote_object.html
--Mark Johnson

Similar Messages

  • [b]Periodic[/b] error using PortableRemoteObject.narrow

    I am trying to access a session bean on an oracle9ias(903) running on a windows 2000 server.
    Well I am actually accessing several beans, but on some of the beans the code works and on some it don't even though the code is alike. There is no execptions thrown but the LabelTypeDAOHome is null and I get a NPE when calling create on it.
    public static Context getInitialContext() throws NamingException, MissingResourceException
    Hashtable env = new Hashtable();
    String init = getResourceBundle().getString("INITIAL_CONTEXT_FACTORY");
    env.put(Context.INITIAL_CONTEXT_FACTORY,init);
    String principle = getResourceBundle().getString("SECURITY_PRINCIPAL");
    env.put(Context.SECURITY_PRINCIPAL,principle);
    String credential = getResourceBundle().getString("SECURITY_CREDENTIALS");
    env.put(Context.SECURITY_CREDENTIALS,credential);
    String url = getResourceBundle().getString("PROVIDER_URL");
    env.put(Context.PROVIDER_URL,url);
    env.put("dedicated.rmicontext","true"); // vigtig ifm web access!
    return new InitialContext(env);
    private LabelTypeDAO getLabeltypeDAO() throws NamingException, RemoteException {
    LabelTypeDAO labeltypeDAO = null;
    if (context == null) {
    context = getInitialContext();
    if (labeltypeDAOHome == null) {
    LabelTypeDAOHome labeltypeDAOHome = (LabelTypeDAOHome)PortableRemoteObject.narrow(context.lookup("LabelTypeDAO"), LabelTypeDAOHome.class);
    try {
    labeltypeDAO = labeltypeDAOHome.create();
    } catch(Exception ce) {
    throw new RemoteException("Could not create LabelTypeDAO.", ce);
    return labeltypeDAO;
    The funny part is that it doesn't happen on all the sessionbeans.
    Could it have something to do with what happens on deployment (e.g internal wrapping).
    There is no exception thrown untill I call labeltypeDAO = labeltypeDAOHome.create();
    This code is in a java class instantiated in a jsp page.

    Found it. It was my own stupid mistake. I had 2 references to the same variable name, one to an instance variable and one to a local variable and I ofcourse referenced the wrong one.
    Bad lars
    Bad lars
    Bad lars

  • Rich Client For EJB asking for  'ioser12.dll' when using PortableRemoteObject.narrow()

    Hi,
    When using narrow down the object to home interface after performing lookup(), it gives error for
    'ioser12.dll' not found. If doing 'SomeHome someHome = (SomeHome)obj;' it's working fine.
    Regards,
    Jitendra Kumar

    Remove the j2ee.jar from your classpath and narrow will work.
    The correct version is in weblogicaux.jar.
    -- Rob
    Jitendra Kumar wrote:
    >
    Hi,
    When using narrow down the object to home interface after performing lookup(), it gives error for
    'ioser12.dll' not found. If doing 'SomeHome someHome = (SomeHome)obj;' it's working fine.
    Regards,
    Jitendra Kumar

  • Confusion in PortableRemoteObject.narrow() method

    In EJB 1.0 reference of home object is obtained through external casting as HelloHome home = (HelloHome)ctx.lookup("HelloHome")
    My question is that since IIOP has not been designed for Java, but for generic languages, and this means that there are some limitations. Some languages, in fact, do not have the concept of casting.
    Still why we have to cast the Object if we use PortableRemoteObject.narrow method for obtaining the home interface reference in EJB2.0.
    HelloHome home = (HelloHome)javax.rmi.PortableRemoteObject.narrow(object, HelloHome.class)

    If the client is built using Java, you can not explicitly cast from the Naming.lookup() function. Instead, you must use lookup() to return a generic java.lang.Object, and then cast to the specific object type with the PortableRemoteObject.Narrow(objectReference, interfaceName) method.

  • Obtain remote interface using PortableRemoteObject

    Hello,
    I have sample code from "Applying Enterprinse JavaBeans" book.
    Plan plan = (Plan)PortableRemoteObject.narrow(
    it.next(), Plan.class);
    ... plan.getPlanInfo() ...
    here:
    Plan is a remote interface of the entity bean (e.g.. MyPlanBean).
    getPlanInfo() is one of the business methods in Plan.
    In this case, the remote interface is obtained by using PortableRemoteObject.narrow(...). could anyone explane the creation using the concept of life cycle of an entity bean instance? That means if any ejbCreate() methods or any callback methods are involved in the creation or not?
    Thanks.

    I am not sure if you are doing the right thing.
    The home interface represents the life-cycle methods of the component (create, destroy, find) while the remote interface represents the business method of the bean.
    Clients use the bean's home interface to obtain references to the bean's remote interface. Following is the life-cycle of
    entity bean creation:
    1. Client invokes create() on home object
    2. Home object allocates a bean from the free pool
    3. Home object passes on the create args to the ejbCreate(..)
    in the bean
    4. the bean inserts a row into the database table
    5. the bean returns the primary key of the row to the home object
    6. The container creates an EJB object for the bean and populates
    it with the primary key
    7. The home object calls the ejbPostCreate() on the bean
    8. The home object returns the remote reference to the EJB
    object back to the client
    9. Client now makes business calls to the bean instance.
    No bean instance is associated with the EJB object
    10. EJB object communicates to the container that a bean
    instance is needed
    11. The container allocates a bean instance from the pool
    and associates it with the EJB object (ejbActivate() is called)
    12. home object invokes the ejbLoad method on the bean to
    tell it to load itself(BMP) or to tell it that it has just been
    loaded (CMP)
    13. the bean uses the primary key stored in the EJB object to load the approp row's data from the database
    14. EJB object dispatches the client's business method call to the
    bean
    Hope this helps.
    Regards,
    Sun-DTS

  • ???  PortableRemoteObject.narrow()  ???  totally confusing

    Hi,
    I tried to execute the J2ee tutorial example (ConverterApplication) bean...
    I got succeed in doing that...
    But while writing the ApplicationClient code i used
    PortableRemoteObject.narrow() to get the HomeObject's reference... I know that there is some RMI-IIOP and CORBA stuff involved in that..
    But my client is a pure java client not a CORBA client ...so i tried removing this
    PortableRemoteObject.narrow() line and i directly typecated the HomeObject...
    It is also working ....
    So whats the whole purpose of writing PortableRemoteObject.narrow() for a java client ?? Becoz we know that our client is in Java and EJB is also written in java

    http://www.jguru.com/faq/view.jsp?EID=734137
    http://www.theserverside.com/discussions/thread.tss?thread_id=29200
    Richard.

  • PortableRemoteObject.narrow() method

    Hi,
    I usually use this method in case of looking for some remote home interface. But recently I found some EJB examples, where an author ommited the PortableRemoteObject.narrow method. Actually the source code was generated by Lomboz.
    This unusual procedure made me to prepare some tests. I made some EJB bean and its interfaces, after that I made a little change inside on one cpy of home interface, that this one was out of date. In the end I generated appropriate client in two ways:
    - using PortableRemoteObject.narrow method
    - unusing PortableRemoteObject.narrow method
    The results, when I used PortableRemoteObject.narrow method and when I unused it is similar, both resposed by java.lang.ClassCastException
    The small difference was, in order to use PortableRemoteObject.narrow method I got:
    at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(Unknown Source)
         at javax.rmi.PortableRemoteObject.narrow(Unknown Source)
    Ommiting this method I got only;
    java.lang.ClassCastException: $Proxy0
    Well, in both ways I observed the same final exception:
    java.lang.ClassCastException, which good explains what happend wrong. What really is better to use PortableRemoteObject.narrow method based on my above experiences?
    Thank for some suggestions
    Krzysztof

    You need to do the .narrow() method on anything that comes across RMI. The reason is that you have no guarantee that the thing coming across RMI is a Java object. It's possible to send something other than a Java object.
    If your client is using a local interface instead of a remote interface, the narrow() is not required.

  • Why PortableRemoteObject.narrow()

    Hello,
    Can any one please suggest me the difference in looking Up of the components in 2 ways..??
    1) HelloHome home = (HelloHome)ctx.lookup("HelloHome")
    2) Object ref = getInitialContext().lookup(eb/HelloBean);
    HelloHome home = (HelloHome)javax.rmi.PortableRemoteObject.narrow(ref, HelloHome.class)
    Why we need option 2 when we get a home object when we make a look up as in 1.
    why PortableRemoteObject.narrow()
    Please make ur suggestions.
    Thanks.

    Hi,
    From an article at http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/ejb-tier/ejb-tier9.html
    Type narrowing is needed because many application servers use RMI-IIOP as the communication protocol to access remote beans. However, some application servers do not use RMI-IIOP and hence allow the use of Java language typecasts as well. For portability you cannot rely on an application server allowing Java language typecasts; you should always use the PortableRemoteObject.narrow method. The overhead on this method call is usually quite small
    -Amol

  • PortableRemoteObject.narrow(...) error , why?

    when I execute
    PortableRemoteObject.narrow(objred, SBeanHome.class);
    the system throw an exception
    java.lang.ClassCastException: $Proxy78
    Please help me!
    (my application server is weblogic 6.1)

    Object obj = (MyHomeInterface) ctx.lookup("Invoice.MyHomeInterface");
         MyHomeInterface home = (MyHomeInterface) PortableRemoteObject.narrow(obj, MyHomeInterface.class);
    use this code instead.
    i hope that this will solve your problem change the code according to your classes.

  • Why use narrow?

    One difference which I observed between RMI and RMI/IIop was teh use of narrow. My question is why to use narrow rather than directly casting an object to interface extending remote.

    One reason that I know of is that with JRMP you have both the client and the server in java while in IIOP the server might be in some other language. So the simplicity of readily casting objects in JRMP disappears in IIOP. In order to overcome this hinderance the static method narrow is used.
    :)

  • PortableRemoteObject.narrow

    Why is it necessary to use and cast the result of this method call, which appears to return the same object as was passed as the first argument? In other words, why do this:
    Object myObj = ctx.lookup(MYHOME);
    MyHome myHome = (MyHome)
    PortableRemoteObject.narrow(myObj, MyHome.class);
    MyEjb myEjb = myHome.create();
    -in code that must know each EJB, when you can do this:
    //In a generic HomeFactory class
    Object myObj = ctx.lookup(MYHOME);
    Class beanHome = Class.forName(MYHOME);
    PortableRemoteObject.narrow(myObj, beanHome);
    return myObj
    //In some other class
    MyHome myHome = (MyHome) HomeFactory.getHome(MYHOME);
    MyEjb = myHome.create();
    I left out the try-catch blocks and all of that for compactness. The reason I ask is because a generic Home Factory can store EJBHome objects in a collection, testing them first and returning them without creating the actual remote reference, so it does not need to know the specific EJB, allowing the specific client code to cast the generic Object to the specific EJBHome object.
    A Sun-certified instructor once told me that it was absolutely necessary to use the Object returned from the narrow method because it performed some essential RMI activity on the object, but I have never had a problem using it the second way I describe above in the HomeFactory. Also, I have never been able to find any documentation to substantiate his claim. Any thoughts as to the necessity of using the object returned from the narrow method? It appears to be the exact same object that is passed in to the method, and likely un-changed at that.

    The narrow method is there to allow an implementation to do some extra work or return a different object if it needs to. It appears that your implementation does not need to do these things so it just returns the object that was passed. If you take your shortcut, you will limit yourself to running only on your current implementation. If you try to switch implementations or even upgrade to a later version it might break. If you follow the correct method the your code will be portable.

  • Javax.rmi.portableRemoteObject.narrow

    Hi
    Pls tell me why we narrow the instance of the object we looked up,
    before type casting it to a home referance by using javax.rmi.portableRemoteObject.narrow(Object,Home.class)

    In the early days of EJB this call was required to ensure portability due to some intracacies of how
    RMI-IIOP and CORBA ORBs are implemented. Some implementations are able to directly return
    the correctly typed object from the lookup, but in others the extra PortableRemoteObject.narrow
    call is necessary, so always using it ensures portability.
    Starting in Java EE 5, PortableRemoteObject.narrow is not needed if the code is either accessing
    a Remote 3.0 Business Interface or if the code is accessing a 2.x style Home object using
    @EJB or the new EJBContext.lookup method.
    --ken                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Java.lang.ClassCastException PortableRemoteObject.narrow(PortableRemoteObje

    Hi,
    I have a problem with a JNDI -lookup() using the CORBA namig system.
    After looking up: System.out.println( lookup("jndi-name") );
    I get an IOR: IOR:0000000000000053524d493a...............
    ok
    then I print out the name and the package of this object:
    Object o = lookup("jndi-name");
    System.out.println("name of o : " + o.getClass().getName());
    System.out.println("package of o : " + o.getClass().getPackage());
    and get:
    name of o : com.sun.corba.ee.impl.corba.CORBAObjectImpl
    package of o : package com.sun.corba.ee.impl.corba
    I expected the type of a Home-Interface, so that i could narrow the object o:
    PortableRemoteObject.narrow( o , classname);
    and the result is a:
    java.lang.ClassCastException
    at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:293)
    at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:134)........
    What's wrong

    The most likely reason is that the stubs for that home interface are not being found. Double-check your classpath to make sure they are available to the code at runtime.
    -ken

  • EjbHome = (EJBHome) PortableRemoteObject.narrow(homeObject, EJBHome.class);

    please explain the below given code , It is used in servicelocator (J2EE Pattern), basically to find the EjB Home
    ejbHome = (EJBHome) PortableRemoteObject.narrow(homeObject, EJBHome.class);

    If we plain old cast, we are assuming that the server is using the regular old RMI (which assumes that the server objects are always java objects.
    RMI/IIOP allows connecting RMI objects to non-Java objects regardless of the implementation language (for interface to any native legacy code). So, in such cases (where client and server are separated by a network), one should use the
    PortableRemoteObject.narrow(), which assumes that the objects that we are talking to are not always java objects.
    The plain old cast will work when we use local home and component interfaces as both client and server run on the same JVM.
    Rishi

  • ClassCastException with PortableRemoteObject.narrow call

    I want to call an ejb component located on a different machine and I can not get it to work. So I tried to deploy it under my version of jboss and try to invoke it. I was able to call it but when I added Context.PROVIDER_URL to my properties I get a classCastException.
    String host = "jnp://localhost:1099";
    props.setProperty(Context.PROVIDER_URL, host);
    I started getting the following exception
    2006-11-03 10:17:18,319 ERROR [STDERR] java.lang.ClassCastException
    2006-11-03 10:17:18,319 ERROR [STDERR]      at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narr
    ow(PortableRemoteObject.java:229)
    2006-11-03 10:17:18,319 ERROR [STDERR]      at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObjec
    t.java:137)
    As soon as I uncomment that props.setProperty(Context.PROVIDER_URL, host);
    line everything works fine.
    I have seen this type of exception before when I tried invoking the ejb on a different computer as well. When I inspect the object with reflection I see that it found the right interface.
    This is my code again:
    Properties props = new Properties();
    props.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.security.jndi.JndiLoginInitialContextFactory");
    props.setProperty(Context.SECURITY_PRINCIPAL, "admin");
    props.setProperty(Context.SECURITY_CREDENTIALS, "admin");
    props.put(javax.naming.Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
    String host = "jnp://localhost:1099";
    props.setProperty(Context.PROVIDER_URL, host);
    ctx = new InitialContext( props );
    Object object = ctx.lookup( "ejb/com/blah/Manager" );
    ManagerRemoteHome home = (ManagerRemoteHome)PortableRemoteObject.narrow ( object, ManagerRemoteHome.class);
    ManagerRemote manager = home.create();
    Thanks

    1099 is the RMI/JRMP Registry. You are using RMI/IIOP. Is this port number really correct?

Maybe you are looking for

  • Mac Mini - Flashing Folder Icon / No boot / Loud fans

    I recently got my Mac Mini back after 8 months of it being 'elsewhere' (someone had it, tried to gain access, failed because I have filevault). When I first got it back yesterday I turned it on, booted and then logged in, noticed no sound, and system

  • MacBook Pro doesn't go into sleep mode after connecting Thunderbolt display

    Hi, after connecting a Thunderbolt display to the 2011 MacBook Pro 13", the system doesn't stay in sleep mode. When pressing cmd.alt.eject it goes into sleep for 1 or 2 seconds and then goes back on. everything works fine otherwise and the MacBook go

  • Long currency type Output format in BSP

    Hi all. I'm looking forward to format in a BSP the output of a currency field without decimals and using the dot separator, for example: in database i have the value 1234567891011,1234 then display in the BSP 1.234.567.891.011,1234 is there any funct

  • Minute Range

    Hi all, My problem is, i have calculated time in minutes i have to show output in terms of minute range. The minute range is defined in the selection screen by the user. Eg If user has defined in selection screen the minute range as 10. Now if the ca

  • How can I change the PDAnnot object font?

    Hello All, How can I set/change the font for a PDAnnot object? Thanks for any help, Bob