/**
   * Implements resolving a NameComponent in this context and narrowing it to
   * CosNaming::NamingContext. It will throw appropriate exceptions if not found or not narrowable.
   *
   * @param impl an implementation of NamingContextDataStore
   * @param n a NameComponents which is the name to be found.
   * @exception org.omg.CosNaming.NamingContextPackage.NotFound The first component could not be
   *     resolved.
   * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not proceed in resolving
   *     the first component of the supplied name.
   * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA system exceptions.
   * @see resolve
   */
  protected static NamingContext resolveFirstAsContext(
      NamingContextDataStore impl, NameComponent[] n)
      throws org.omg.CosNaming.NamingContextPackage.NotFound {
    org.omg.CORBA.Object topRef = null;
    BindingTypeHolder bth = new BindingTypeHolder();
    NamingContext context = null;

    synchronized (impl) {
      // Resolve first  - must be resolveable
      topRef = impl.Resolve(n[0], bth);
      if (topRef == null) {
        // It was not bound
        throw new NotFound(NotFoundReason.missing_node, n);
      }
    }

    // Was it bound as a context?
    if (bth.value != BindingType.ncontext) {
      // It was not a context
      throw new NotFound(NotFoundReason.not_context, n);
    }

    // Narrow to a naming context
    try {
      context = NamingContextHelper.narrow(topRef);
    } catch (org.omg.CORBA.BAD_PARAM ex) {
      // It was not a context
      throw new NotFound(NotFoundReason.not_context, n);
    }

    // Hmm. must be ok
    return context;
  }
  private static void rlist(NamingContext ctx, NameComponent[] base, StringBuffer buf) {
    BindingListHolder listHolder = new BindingListHolder(new Binding[0]);
    BindingIteratorHolder iterHolder = new BindingIteratorHolder();
    ctx.list(0, listHolder, iterHolder);
    BindingHolder bindingHolder = new BindingHolder();

    if (iterHolder.value == null) return;

    NameComponent[] name = new NameComponent[base.length + 1];
    for (int i = 0; i < base.length; i++) name[i] = base[i];

    while (iterHolder.value.next_one(bindingHolder)) {
      Binding binding = bindingHolder.value;
      name[name.length - 1] = binding.binding_name[0];
      try {
        String stringName = namingService.to_string(name);
        buf.append(stringName);
      } catch (Exception e) {
        buf.append(e.getMessage());
      }

      if (binding.binding_type.value() == BindingType._ncontext) {
        // this entry is for a subcontext
        // add trailing '/' just to distinguish
        // a subcontext from a regular object
        buf.append('/');
        buf.append('\n');

        // recursively list the subcontext contents
        try {
          NamingContext subCtx = NamingContextHelper.narrow(ctx.resolve(binding.binding_name));
          rlist(subCtx, name, buf);
        } catch (Exception e) {
          buf.append(e.getMessage());
        }
      } else {
        buf.append('\n');
      }
    }
  }
  public static void main(String[] args) {
    loggedUser = args[0];
    try {
      ORB orb = ORB.init(args, null); // initialize ORB
      o = orb.resolve_initial_references("NameService");

      // get reference to Deal object
      NamingContext ncRef = NamingContextHelper.narrow(o);
      NameComponent[] nc = new NameComponent[1];
      nc[0] = new NameComponent();
      nc[0].id = "Auction";
      nc[0].kind = "";
      a = AuctionHelper.narrow(ncRef.resolve(nc));

      isr = new InputStreamReader(System.in);
      in = new BufferedReader(isr);
      int sel = 0;
      System.out.println("******* You have logged in Successfully! ********");
      while (sel == 0) {
        System.out.println("Please make a selection");
        System.out.println("\t1. Get all the listed auctions");
        System.out.println("\t2. List a new item for auction.");
        System.out.println("\t3. Lookup an auction.");
        System.out.println("\t4. Place a bid on an auction.");
        System.out.println("\t5. Logout.");
        System.out.print("Please make your selection > ");
        String f = in.readLine();
        try {
          sel = Integer.parseInt(f);
        } catch (Exception e) {
          sel = 42;
        }
        switch (sel) {
          case 1:
            allItems();
            sel = 0;
            break;
          case 2:
            newAuction();
            sel = 0;
            break;
          case 3:
            System.out.println("Stub");
            sel = 0;
            break;
          case 4:
            placeBid();
            sel = 0;
            break;
          case 5:
            a.logout(loggedUser);
            System.out.println("BYE!");
            System.exit(0);
          default:
            System.out.println("You have not made a valid selection, please try again.");
            sel = 0;
            break;
        }
      }
    } catch (Exception e) {
      System.out.println("ERROR : " + e);
      e.printStackTrace(System.out);
    }
  }
  /** @param args the command line arguments */
  public static void main(String args[]) {

    try {
      // create and initialize the ORB
      ORB orb = ORB.init(args, null);

      // get reference to rootpoa & activate the POAManager
      POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
      rootpoa.the_POAManager().activate();

      // create servant
      Log_viewerImpl log_viewerImpl = new Log_viewerImpl();
      log_viewerImpl.setORB(orb);

      LogFrame frame = new LogFrame(log_viewerImpl);
      frame.setVisible(true);

      log_viewerImpl.setFrame(frame);

      // get object reference from the servant
      org.omg.CORBA.Object ref = rootpoa.servant_to_reference(log_viewerImpl);
      Log_viewer href = Log_viewerHelper.narrow(ref);

      // read stringified Registry to file
      FileReader fr = new FileReader(IORFILE);
      BufferedReader br = new BufferedReader(fr);
      String remoteRegistryIOR = br.readLine();

      // get the romote Registry
      org.omg.CORBA.Object ncobj = orb.string_to_object(remoteRegistryIOR);
      NamingContext rootNC = NamingContextHelper.narrow(ncobj);
      frame.println("Obtained Name Service reference.");
      log_viewerImpl.serRootNC(rootNC);

      NameComponent[] name = new NameComponent[1];
      name[0] = new NameComponent("Logger", "");

      try {

        rootNC.bind(name, href);

      } catch (org.omg.CORBA.UserException ue) {
        ue.printStackTrace();
        System.exit(-1);
      }

      frame.println("Logger Remote Interface bound in Name Service");

      // wait for invocations from client
      frame.println("Logger ready and waiting ...");
      orb.run();

      frame.println("Logger Exiting ...");
      System.out.println("Logger Exiting ...");

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

      // e.printStackTrace(System.out);
    }
  }