// ============================================================
// = CORBA Sample											  =
// ============================================================

//
// Tutorial, STEP 2
//
public class Client
{
	public static void main( String args[] )
	{		
		// 1.
		// Initialize the ORB
		org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

		// 2.
		// Resolve the NamingService 
		org.omg.CORBA.Object obj = null;
		org.omg.CosNaming.NamingContext naming = null;
		try
		{			
			obj = orb.resolve_initial_references("NamingService");
			naming = org.omg.CosNaming.NamingContextHelper.narrow(obj);
		}
		catch ( org.omg.CORBA.ORBPackage.InvalidName name )
		{
			System.out.println("Enable to resolve NamingService");
			System.exit(0);
		}
		
		// 3.
		// Build Calculator path 
		org.omg.CosNaming.NameComponent [] name = new org.omg.CosNaming.NameComponent[1];
		name[0] = new org.omg.CosNaming.NameComponent();
		name[0].id = "Calculator";
		name[0].kind = "Example";

		// 4. 
		// Resolve Calculator reference from NamingService
		try
		{
			obj = naming.resolve(name);
		}
		catch ( org.omg.CosNaming.NamingContextPackage.NotFound ex )			
		{
			System.out.println("Object not found");
			System.exit(0);
		}
		catch ( org.omg.CosNaming.NamingContextPackage.CannotProceed ex )
		{
			System.out.println("Cannot proceed");
			System.exit(0);
		}
		catch ( org.omg.CosNaming.NamingContextPackage.InvalidName ex )
		{
			System.out.println("Invalid name");
			System.exit(0);
		}
						
		// 3.
		// Narrow the object reference
		Calculator calc = CalculatorHelper.narrow(obj);		

		// 4.
		// Use the Calculator object
		try
		{
			System.out.println("5 + 3 = " + calc.add(5,3) );

			System.out.println("5 / 0 = " + calc.div(5,0) );
		}
		catch ( DivisionByZero ex )
		{
			System.out.println("A division by zero has been intercepted");
			System.out.println("The division was "+ex.op1+" / "+ex.op2);
		}
		catch ( org.omg.CORBA.SystemException ex )
		{
			System.out.println("A CORBA System exception has been intercepted");
			System.out.println(ex.getMessage());
		}

	}
}
