Corba COS命名服务列出所有节点

时间:2013-02-14 15:21:45

标签: java namespaces corba

我有一个由4个上下文组成的名单。上下文1,2和3绑定到根节点,上下文4绑定到上下文2.我能够列出上下文1-3的名称,但我无法获得上下文4.

它只列出第一级的节点。我想如果我检查每个节点,检查它是否有子节点,然后列出它们将起作用,但我不知道该怎么做。这是我的代码。我有两种列出上下文的方法

import java.io.*;
import org.omg.CORBA.*;
import HelloNaming.*;
import org.omg.CosNaming.* ;
import org.omg.CosNaming.NamingContextPackage.*;

public class HelloNamingClient
{     

public static void main(String args[])
{
try{
    NameComponent nc[]= new NameComponent[2];


    ORB orb = ORB.init(args, null);


    org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
    NamingContext rootCtx = NamingContextHelper.narrow(objRef);



    final int batchSize = 7;
            BindingIterator b ;
            BindingListHolder bList = new BindingListHolder() ;
            BindingIteratorHolder bIterator = new BindingIteratorHolder();

            rootCtx.list(batchSize, bList, bIterator) ;
            for (int i=0; i<bList.value.length; i++) {
                 System.out.println(bList.value[i].binding_name[0].id) ;
    }


NamingContextExt ncc =
          NamingContextExtHelper.narrow(orb.resolve_initial_references(
        "NameService"));



    BindingListHolder bl = new BindingListHolder();
            BindingIteratorHolder blIt= new BindingIteratorHolder();
    ncc.list(1000, bl, blIt);

    Binding bindings[] = bl.value;

       for (int i=0; i < bindings.length; i++) {

                int lastIx = bindings[i].binding_name.length-1;


                if (bindings[i].binding_type == BindingType.ncontext) {
                  System.out.println( "Context: " +
                    bindings[i].binding_name[lastIx].id);
                } else {
                    System.out.println("Object: " +
                      bindings[i].binding_name[lastIx].id);
                }
    }



    } catch (Exception e) {
        System.out.println("ERROR : " + e) ;
        e.printStackTrace(System.out);
        }


}
}

1 个答案:

答案 0 :(得分:1)

每个上下文都是一个单独的CORBA对象,需要像根命名上下文一样单独查询。以下修改以递归方式执行此操作并打印出'[contextN ...]。objectN':

import org.omg.CORBA.*;
import org.omg.CosNaming.* ;

public class HelloNamingClient
{     
  public static void printContext( NamingContext nc, String parent )
  { 
    try {
      final int batchSize = 1000;
      BindingListHolder bList = new BindingListHolder( );
      BindingIteratorHolder bIterator = new BindingIteratorHolder( );

      nc.list( batchSize, bList, bIterator );

      for ( int i=0; i < bList.value.length; i++ ) {
        NameComponent[] name = { bList.value[i].binding_name[0] };
        if (bList.value[i].binding_type == BindingType.ncontext) {
            NamingContext context = NamingContextHelper.narrow( 
                nc.resolve( name ) );
            printContext( context, parent + name[0].id + "." );
        } else {
            System.out.println( parent + name[0].id );
        }
      }

    } catch (Exception e) {
        System.out.println("ERROR : " + e) ;
    }
  }

  public static void main(String args[])
  {
    try{
      ORB orb = ORB.init(args, null);

      org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
      NamingContext rootCtx = NamingContextHelper.narrow(objRef);

      printContext( rootCtx, "" );

    } catch (Exception e) {
      System.out.println("ERROR : " + e) ;
      e.printStackTrace(System.out);
    }
  }
}