/*
  server.java

  Server for simple client/server CORBA application with file-based IORs.

  To run this example, prepare your environment as follows:

c:\ set ORB=c:\JOB-4.1.0\lib
c:\ set PATH=c:\orbacus\bin
c:\ set CLASSPATH=.;Hallo;%ORB%\OB.jar
c:\ jidl --package Hallo Hallo.idl
c:\ javac Hallo\*.java
c:\ java Hallo.server

  Author: Ingo Kloeckl
 */

package Hallo;

public class server {
  public static void main(String args[]){
    java.util.Properties props = System.getProperties();
    props.put("org.omg.CORBA.ORBClass", "com.ooc.CORBA.ORB");
    props.put("org.omg.CORBA.ORBSingletonClass", "com.ooc.CORBA.ORBSingleton");
    org.omg.CORBA.ORB oORB = null;

    try {
      oORB = org.omg.CORBA.ORB.init(args, props);
    } catch (org.omg.CORBA.SystemException e){
      System.err.println("Error initializing ORB");
      System.exit(1);
    }
    
    org.omg.PortableServer.POA rootPOA = null;
    org.omg.PortableServer.POAManager manager = null;
    try {
      org.omg.CORBA.Object obj = oORB.resolve_initial_references("RootPOA");
      if (obj==null){
	System.err.println("Invalid reference to RootPOA");
	System.exit(1);
      }
      rootPOA = org.omg.PortableServer.POAHelper.narrow(obj);
      if (rootPOA==null){
	System.err.println("Reference is not a RootPOA");
	System.exit(1);
      }
      manager = rootPOA.the_POAManager();
    } catch (org.omg.CORBA.ORBPackage.InvalidName e){
      System.err.println("Invalid name passed: "+e);
      System.exit(1);
    } catch (org.omg.CORBA.SystemException e){
      System.err.println("System exception: "+e);
      System.exit(1);
    }
      
    try {
      Hallo_impl halloImpl = new Hallo_impl(oORB, "Ingos server in Java");
      Hallo hallo = halloImpl._this(oORB);
      try {
	String ref = oORB.object_to_string(hallo);
	String refFile = "HAL.ref";
	java.io.PrintWriter out = new java.io.PrintWriter(
							  new java.io.FileOutputStream(refFile));
	out.println(ref);
	out.close();
      } catch (java.io.IOException e){
	System.err.println("Error writing IOR: "+e);
	System.exit(1);
      }
    } catch (org.omg.CORBA.SystemException e){
      System.err.println("Error activating servant: "+e);
      System.exit(1);
    }

    // now run POA, ORB and implementation
    try {
      manager.activate();
      oORB.run();      
    } catch (org.omg.PortableServer.POAManagerPackage.AdapterInactive e){
      System.err.println("POA inactive!");
    } catch (org.omg.CORBA.SystemException e){
      System.err.println("Error running ORB event loop: " + e);
    }
    
    if (oORB != null){
      try {
	oORB.destroy();
      } catch (org.omg.CORBA.SystemException e){
	System.err.println("System exception: " + e);
      }
    }
    
  }

}

