package com.wiley.compbooks.brose.chapter10.office;

import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import org.omg.PortableServer.*;
import java.io.*;

public class SecretaryServer 
{
    private static org.omg.CORBA.ORB orb;
    private static String filename;
    private static DigitalSecretaryImpl secretaryImpl = null;

    /**
     * Serialize state and shutdown.
     */
    
    public static void shutdown()
    {
        try
        {
            File f = new File(filename);
            FileOutputStream fout = 
                new FileOutputStream(f);
            ObjectOutputStream out = 
                new ObjectOutputStream(fout);
			
            /* save state */
            out.writeObject((DigitalSecretaryImpl)secretaryImpl);
        }
        catch( IOException io )
        {
            io.printStackTrace();
            System.err.println("Error opening output file "  + filename );
        }	
        orb.shutdown(false);
    }

    /** Main */

    public static void main( String args[] )  
    {
        if( args.length == 0 )
        {
            System.err.println("usage: java SecretaryServer <username> [ <e-mail address> ]");
            System.exit(1);
        }

        String name = args[0];	
        filename = "." + name + "_pss";
        String mailAddress = "<unknown>";

        if( args.length == 2 )
            mailAddress = args[1];

        try
        {
            File f = new File( filename );
            if( f.exists() )
            {
                FileInputStream f_in = new FileInputStream(f);
				
                if( f_in.available() > 0 )
                {
                    ObjectInputStream in = new ObjectInputStream(f_in);
                    secretaryImpl = (DigitalSecretaryImpl)in.readObject();
                    in.close();
                }
                f_in.close();
            }
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }

        try
        {
            /* intialize the ORB and Root POA */
			
            orb = org.omg.CORBA.ORB.init(args, null);
            org.omg.PortableServer.POA rootPOA = 
                org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA"));

            /* retrieve Naming Service reference  */

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

            if( nc == null )
            {
                System.err.println("No Nameserver! Exiting...");
                System.exit( 1 );
            }

            /* init the secretary implementation */
	    
            if( secretaryImpl == null )
            {
                secretaryImpl = new DigitalSecretaryImpl( name, "", orb );
            }

            /* create a user defined poa */
            org.omg.CORBA.Policy [] policies = new org.omg.CORBA.Policy[3];

            policies[0] = rootPOA.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID);
            policies[1] = rootPOA.create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
            policies[2] = rootPOA.create_request_processing_policy(
                                                                   RequestProcessingPolicyValue.USE_ACTIVE_OBJECT_MAP_ONLY);

            POA secretaryPOA = 
                rootPOA.create_POA("SecretaryPOA", rootPOA.the_POAManager(), policies);

            secretaryPOA.activate_object_with_id(name.getBytes(), secretaryImpl );
            secretaryPOA.the_POAManager().activate();

            for (int i=0; i<policies.length; i++) 
                policies[i].destroy();			
       
            /* export the reference  */

            nc.rebind( nc.to_name( name + ".secretary" ) , 
                       secretaryPOA.servant_to_reference(secretaryImpl) );

            secretaryImpl.init( secretaryPOA, nc, orb );

            SecretaryGUI gui = new SecretaryGUI( secretaryImpl );

            orb.run();
            System.exit(0);
        } 
        catch( Exception e )
        {
            e.printStackTrace();
            System.exit(1);
        } 
    }
}













