home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / TRIAL / JBUILDER / JVISBRKR.Z / Find.java < prev    next >
Encoding:
Java Source  |  1998-05-08  |  6.2 KB  |  168 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20. package visibroker.samples.locate;
  21.  
  22.  
  23. public class Find {
  24.  
  25.   private static void UsageHelper()
  26.   {
  27.     System.out.println("Unable to locate LocationService\n" +
  28.        "   -Make sure the locserv process is running\n" +
  29.        "   -Make sure you set the client's System Property\n" +
  30.        "    ORBservices=\"com.visigenic.vbroker.ObjLocation\"");
  31.   }
  32.  
  33.   public static void main(String[] args)
  34.   {
  35.     try {
  36.       // Initialize the ORB.
  37.       org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
  38.       // Initialize the BOA.
  39.       org.omg.CORBA.BOA boa = orb.BOA_init();
  40.       com.visigenic.vbroker.ObjLocation.Agent agent = null;
  41.  
  42.       try {
  43.     org.omg.CORBA.Object obj = 
  44.       orb.resolve_initial_references("LocationService");
  45.     if ((obj == null) ||
  46.         ((agent=com.visigenic.vbroker.ObjLocation.AgentHelper.narrow(obj)) == null)) {
  47.       UsageHelper();
  48.       System.exit(1);
  49.     }
  50.       } catch (org.omg.CORBA.ORBPackage.InvalidName e) {
  51.     System.out.println("Not able to resolve references " +
  52.                "for LocationService");
  53.     UsageHelper();
  54.     System.exit(1);
  55.       } catch (Exception e) {
  56.     System.out.println("Caught exception: " + e);
  57.     UsageHelper();
  58.     System.exit(1);
  59.       }
  60.  
  61.       boolean done=false;
  62.       java.io.DataInputStream in = new java.io.DataInputStream(System.in);
  63.  
  64.       while (! done) {
  65.         System.out.print("-> ");
  66.     System.out.flush();
  67.     /* This code is needed for some VMs, but not others!!!*/
  68.     if (System.getProperty("VM_THREAD_BUG") != null) {
  69.       while(in.available() == 0) {
  70.         try {
  71.            Thread.currentThread().sleep(100);
  72.             }
  73.         catch(InterruptedException e) {
  74.         }
  75.       }
  76.     }
  77.  
  78.     String line = in.readLine();
  79.     if(line.startsWith("agents")) {
  80.           java.lang.String[] agentList = agent.all_agent_locations();
  81.           System.out.println("Located " + agentList.length + " agents");
  82.           for (int i=0; i < agentList.length; i++) {
  83.             System.out.println("\t" + "Agent #" + (i+1) + ":  " +
  84.                                agentList[i]);
  85.           }
  86.     } else if(line.startsWith("rep")) {
  87.           java.lang.String[] repIds = agent.all_repository_ids();
  88.           System.out.println("Located " + repIds.length 
  89.                  + " repository Ids");
  90.           for (int i=0; i < repIds.length; i++) {
  91.             System.out.println("\t" + "Repository Id #" + (i+1) + ":  " +
  92.                                repIds[i]);
  93.           }
  94.     } else if(line.startsWith("objects ")) {
  95.         String names = 
  96.                 line.substring("objects ".length(), line.length());
  97.             PrintObjects(names,agent,orb);
  98.     } else if(line.startsWith("quit")) {
  99.           done = true;
  100.         } else {
  101.             System.out.println("Commands: agents\n" +
  102.                                "          repository_ids\n" +
  103.                                "          objects         <rep Id>\n" +
  104.                                "          objects         <rep Id> <obj name>\n" +
  105.                                "          quit\n");
  106.         }
  107.       }
  108.     } catch (com.visigenic.vbroker.ObjLocation.Fail err) {
  109.       System.out.println("Location call failed with reason " +
  110.              err.reason);
  111.     } catch (java.lang.Exception err) {
  112.       System.out.println("Caught error " + err);
  113.       err.printStackTrace();
  114.     }
  115.   }
  116.   
  117.   public static void PrintObjects(String names,
  118.                   com.visigenic.vbroker.ObjLocation.Agent agent,
  119.                   org.omg.CORBA.ORB orb) 
  120.     throws com.visigenic.vbroker.ObjLocation.Fail
  121.   {
  122.     int space_pos = names.indexOf(' ');
  123.     String repository_id;
  124.     String object_name;
  125.  
  126.     if (space_pos == -1) {
  127.       repository_id = names;
  128.       object_name = null;
  129.     } else {
  130.       repository_id = names.substring(0,names.indexOf(' '));
  131.       object_name = names.substring(names.indexOf(' ')+1);
  132.     }
  133.  
  134.     org.omg.CORBA.Object[] objects;
  135.     com.visigenic.vbroker.ObjLocation.Desc[] descriptors;
  136.     
  137.     if (object_name == null) {
  138.       objects = agent.all_instances(repository_id);
  139.       descriptors = agent.all_instances_descs(repository_id);
  140.     } else {
  141.       objects = agent.all_replica(repository_id,object_name);
  142.       descriptors = agent.all_replica_descs(repository_id,object_name);
  143.     }
  144.     
  145.     System.out.println("Returned " + objects.length + " objects");
  146.     for (int i=0; i<objects.length; i++) {
  147.       System.out.println("\n\nObject #" + (i+1) + ":");
  148.       System.out.println("==================");
  149.       System.out.println("\tRep ID: " + objects[i]._repository_id());
  150.       System.out.println("\tInstance:" + objects[i]._object_name());
  151.       System.out.println("\tIOR: " + orb.object_to_string(objects[i]));
  152.       System.out.println();
  153.       System.out.println("Descriptor #" + (i+1));
  154.       System.out.println("=====================================");
  155.       System.out.println("IIOP:          " + 
  156.              descriptors[i].iiop_locator.iiop_version.major +
  157.              " " + 
  158.              descriptors[i].iiop_locator.iiop_version.minor);
  159.       System.out.println("Host:          " + descriptors[i].iiop_locator.host);
  160.       System.out.println("Port:          " + descriptors[i].iiop_locator.port);
  161.       System.out.println("Agent Host:    " + descriptors[i].agent_hostname);
  162.       System.out.println("Repository Id: " + descriptors[i].repository_id);
  163.       System.out.println("Instance:      " + descriptors[i].instance_name);
  164.       System.out.println("Activable:     " + descriptors[i].activable);
  165.     }
  166.   }
  167. }
  168.