package com.wiley.compbooks.brose.chapter9.tie;

import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.CosNaming.*;
import com.wiley.compbooks.brose.chapter9.HelloWorld.*;

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

            //init basic object adapter
            POA poa = POAHelper.narrow( orb.resolve_initial_references("RootPOA"));
    
            // create an implementation object
            GoodDayImpl goodDayImpl = new GoodDayImpl( args[0] );

            // create a Tie object
            GoodDayPOATie goodDayPseudoImpl = new GoodDayPOATie( goodDayImpl );

            // export the object reference
			org.omg.CORBA.Object obj = poa.servant_to_reference( goodDayPseudoImpl );
            System.out.println( orb.object_to_string( obj )  );

			try 
			{
				NamingContextExt nc =
					NamingContextExtHelper.narrow( orb.resolve_initial_references("NameService"));

				nc.bind( nc.to_name("any.example"), obj );
			}
			catch( Exception e )
			{
				System.err.println("Error contacting NameServer: " + e.getMessage());
			}

            // wait for requests
			poa.the_POAManager().activate();
            orb.run();
        }
        catch(UserException u) {
            System.err.println(u);
        }
        catch(SystemException e) {
            System.err.println(e);
        }
    }
}
