0

I want to build an object hierarchy over the CORBA namespace and I tried this tutorial

http://docs.oracle.com/javase/7/docs/technotes/guides/idl/jidlNaming.html

I successfully compiled NameClient.java, started orbd on localhost then ran the test. It failed with the following exception:

java.lang.ClassCastException: org.omg.CosNaming._NamingContextStub cannot be cast to org.omg.CosNaming.NamingContextExt
        at NameClient.main(NameClient.java:24)

The source code for the NameClient class:

import java.util.Properties;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;

public class NameClient
{
   public static void main(String args[])
   {
      try {
        Properties props = new Properties();
        props.put("org.omg.CORBA.ORBInitialPort", "1050");
        props.put("org.omg.CORBA.ORBInitialHost", "localhost");
        ORB orb = ORB.init(args, props);
        NamingContextExt ctx =
          NamingContextExtHelper.narrow(orb.resolve_initial_references(
            "NameService"));
        org.omg.CORBA.Object objref = ctx;

        NameComponent name1[] = ctx.to_name("plans");
        ctx.rebind(name1, objref);
        System.out.println("plans rebind successful!");

        NameComponent name2[] = ctx.to_name("Personal");
        NamingContextExt ctx2 = (NamingContextExt)ctx.bind_new_context(name2);
        System.out.println("New naming context, Personal, added!");

        NameComponent name3[] = ctx.to_name("schedule");
        ctx2.rebind(name3, objref);
        System.out.println("schedule rebind successful!");

        NameComponent name4[] = ctx.to_name("calendar");
        ctx2.rebind(name4, objref);
        System.out.println("calendar rebind successful!");


    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
  }  
}

Isn't this example suppose to work ? Any idea how to get passed this error ?

Thank you for helping.

4

1 回答 1

0

我上次使用 corba 是几年前的事了。我记得这个想法是远离裸 Java “cast” 运算符,推荐的转换 CORBA 对象的方法是(是?)使用目标类型的“Helper”方法将 name2 缩小到 ctx2。我建议

    NameComponent name2[] = ctx.to_name("Personal");
    NamingContextExt ctx2 = NamingContextExtHelper.narrow(name2);

    not

    NamingContextExt ctx2 = (NamingContextExt)ctx.bind_new_context(name2);
于 2014-03-31T01:21:27.403 回答