Advertisement
simeonvarbanov

Untitled

Nov 11th, 2012
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.util.Properties;
  5.  
  6. import org.omg.CORBA.ORB;
  7. import org.omg.CosNaming.Binding;
  8. import org.omg.CosNaming.BindingHolder;
  9. import org.omg.CosNaming.BindingIteratorHolder;
  10. import org.omg.CosNaming.BindingListHolder;
  11. import org.omg.CosNaming.BindingType;
  12. import org.omg.CosNaming.NameComponent;
  13. import org.omg.CosNaming.NamingContextExt;
  14. import org.omg.CosNaming.NamingContextExtHelper;
  15.  
  16. public class Client {
  17.     private String[] theArgs;
  18.     private ORB theOrb;
  19.     private File f;
  20.     private BufferedReader br;
  21.  
  22.     private Properties props;
  23.     protected NamingContextExt theRoot;
  24.  
  25.     public Client(String args[]) {
  26.         theArgs = args;
  27.         try {
  28.             if (args.length == 0) {
  29.                 // initialize the ORB.
  30.                 theOrb = ORB.init(args, null);
  31.                 // init buffer for reading object references from file:
  32.                 f = new File("NS_Ref");
  33.                 br = new BufferedReader(new FileReader(f));
  34.             } else {
  35.                 props = new Properties();
  36.                 props.put("org.omg.CORBA.ORBInitialHost", args[0]);
  37.                 props.put("org.omg.CORBA.ORBInitialPort", args[1]);
  38.  
  39.                 // initialize the ORB.
  40.                 theOrb = ORB.init(args, props);
  41.  
  42.                 // init root of name service:
  43.  
  44.                 theRoot = NamingContextExtHelper.narrow(theOrb
  45.                         .resolve_initial_references("NameService"));
  46.             }
  47.         } catch (Exception ex) {
  48.             System.err.println(ex);
  49.         }
  50.     }
  51.  
  52.     public org.omg.CORBA.Object getObjectReference(String objName) {
  53.         org.omg.CORBA.Object obj = null;
  54.         try {
  55.             if (objName == null) {
  56.                 // get object reference from file
  57.                 obj = theOrb.string_to_object(br.readLine());
  58.             } else {
  59.                 obj = theRoot.resolve(theRoot.to_name(objName));
  60.             }
  61.         } catch (Exception ex) {
  62.             System.err.println(ex);
  63.         }
  64.         return obj;
  65.     }
  66.  
  67.     protected void closeBuffer() {
  68.         try {
  69.             br.close();
  70.         } catch (Exception ex) {
  71.             System.err.println(ex);
  72.         }
  73.     }
  74.  
  75.     public void list(NamingContextExt nCxt, String indent)
  76.     {
  77.         try {
  78.             BindingListHolder bListHolder = new BindingListHolder(
  79.                     new Binding[0]);
  80.             BindingIteratorHolder bItrHolder = new BindingIteratorHolder();
  81.  
  82.             nCxt.list(0, bListHolder, bItrHolder);
  83.  
  84.             BindingHolder bHolder = new BindingHolder();
  85.  
  86.             if (bItrHolder.value == null)
  87.                 return;
  88.  
  89.             while (bItrHolder.value.next_one(bHolder)) {
  90.                 String stringName = theRoot
  91.                         .to_string(bHolder.value.binding_name);
  92.  
  93.                 System.out.print(indent + stringName);
  94.                 if (bHolder.value.binding_type.value() == BindingType._ncontext) {
  95.                     String _indent = indent + "\t";
  96.                     System.out.println("/");
  97.  
  98.                     NameComponent[] name = nCxt.to_name(stringName);
  99.                     NamingContextExt sub_context = NamingContextExtHelper
  100.                             .narrow(nCxt.resolve(name));
  101.                     list(sub_context, _indent);
  102.                 } else
  103.                     System.out.println();
  104.             }
  105.         } catch (Exception e) {
  106.             e.printStackTrace();
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement