1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import HelloApp.*;
  25 import org.omg.CosNaming.*;
  26 import org.omg.CosNaming.NamingContextPackage.*;
  27 import org.omg.CORBA.*;
  28 import org.omg.PortableServer.*;
  29 import org.omg.PortableServer.POA;
  30 
  31 import java.util.Properties;
  32 
  33 class HelloImpl extends HelloPOA {
  34     private ORB orb;
  35 
  36     public void setORB(ORB orb_val) {
  37         orb = orb_val;
  38     }
  39     
  40     // implement sayHello() method
  41     public String sayHello() {
  42         return "\nHello world !!\n";
  43     }
  44     
  45     // implement shutdown() method
  46     public void shutdown() {
  47         orb.shutdown(false);
  48     }
  49 }
  50 
  51 
  52 public class HelloServer {
  53 
  54     public static void main(String args[]) {
  55         try{
  56             // create and initialize the ORB
  57             ORB orb = ORB.init(args, null);
  58 
  59             // get reference to rootpoa & activate the POAManager
  60             POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
  61             rootpoa.the_POAManager().activate();
  62 
  63             // create servant and register it with the ORB
  64             HelloImpl helloImpl = new HelloImpl();
  65             helloImpl.setORB(orb);
  66 
  67             // get object reference from the servant
  68             org.omg.CORBA.Object ref = rootpoa.servant_to_reference(helloImpl);
  69             Hello href = HelloHelper.narrow(ref);
  70               
  71             // get the root naming context
  72             org.omg.CORBA.Object objRef =
  73                 orb.resolve_initial_references("NameService");
  74             // Use NamingContextExt which is part of the Interoperable
  75             // Naming Service (INS) specification.
  76             NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
  77 
  78             // bind the Object Reference in Naming
  79             String name = "Hello";
  80             NameComponent path[] = ncRef.to_name( name );
  81             ncRef.rebind(path, href);
  82 
  83             System.out.println("HelloServer ready and waiting ...");
  84 
  85             // wait for invocations from clients
  86             while (true) {
  87                 orb.run();
  88             }
  89         } catch (Exception e) {
  90             System.out.println("ERROR: " + e);
  91             e.printStackTrace(System.out);
  92         }
  93           
  94         System.out.println("HelloServer Exiting ...");
  95         
  96     }
  97 }
  98