/** * CORBA Training The Repertory Application 1999-2003 * *@author Fabienne.Boyer@imag.fr , Didier.Donsez@imag.fr */ package corbatraining; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import org.omg.PortableServer.*; import org.omg.PortableServer.POA; import java.util.*; import java.io.*; import java.lang.reflect.*; /** * The generic server * *@author Fabienne Boyer, Didier Donsez *@created 3 juin 2003 */ public class GenericServer { /** * Description of the Method * *@param args Description of the Parameter */ public static void main(String args[]) { try { InputStream is; // load the ORB properties Properties orbproperties = new Properties(); try { is = new FileInputStream("orb.properties"); orbproperties.load(is); is.close(); } catch (IOException ioe) { System.err.println("No orb.properties file found"); } // load the description of objects to deploy Properties config = new Properties(); try { is = new FileInputStream("config.properties"); config.load(is); is.close(); } catch (IOException ioe) { System.err.println("Could not load config.properties"); throw ioe; } // create and initialize the ORB ORB orb = ORB.init(args, orbproperties); // get reference to rootpoa & activate the POAManager POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA")); rootpoa.the_POAManager().activate(); // get and build the POAs list (rootpoa is default) String poaIdsList = config.getProperty("poas"); // future usage Map poas = new HashMap(); poas.put("rootpoa", rootpoa); // TO DO // get the list of servants to create String servantIdsList = config.getProperty("servants"); if (servantIdsList == null) { System.out.println("No servant to create : Existing ..."); return; } ClassLoader classLoader = this.getClass().getClassLoader(); StringTokenizer stlist = new StringTokenizer(servantIdsList, ";"); while (stlist.hasMoreTokens()) { String id = stlist.nextToken(); System.out.print("Create " + id); // get the creation parameters String className = config.getProperty(id + ".class"); String bindingName = config.getProperty(id + ".nsbind"); String iorFileName = config.getProperty(id + ".iorfile"); String poaId = config.getProperty(id + ".poa"); String codeBase = config.getProperty(id + ".codebase"); // future usage if (poaId == null) { poaId = "rootpoa"; } POA poa = (POA) poas.get(poaId); if(poa==null) { System.out.println(" ERROR: POA " + poaId + " is unknown"); continue; } try { // create a servant org.omg.CORBA.Object servant = createServant( //Class.forName(className), classLoader.loadClass(className), null, // no argument constructor orb, poa ); // rebind a servant to the name service if (bindingName != null) { rebind(servant, orb, bindingName); } // get the ior from the servant String ior = orb.object_to_string(servant); // save the ior string in a file if (iorFileName != null) { PrintWriter ps = new PrintWriter( new FileOutputStream(new File(iorFileName))); ps.print(ior); ps.close(); } // print the ior System.out.print(ior); System.out.println(" OK"); } catch (Exception e) { System.out.println(" ERROR: " + e); e.printStackTrace(System.out); } } String delayprop=config.getProperty("shutdown.delay"); int delay; if(delayprop!=null){ delay=Integer.parseInt(delayprop); } else { delay=5; // shutdown 5 seconds after exit() } Thread hook=new ShutdownHook(delay); // run this thread when VM shutdowns: when System.exit(int) was invoked, when the user types ^C Runtime.getRuntime().addShutdownHook(hook); System.out.println("GenericServer ready and waiting ..."); // wait for invocations from clients orb.run(); } catch (Exception e) { System.err.println("ERROR: " + e); e.printStackTrace(System.out); } System.out.println("GenericServer Exiting ..."); } /** * Create a servant * *@param clazz the class to instanciate *@param initparams the parameters for the constructor *@param orb the ORB parameter to set in the new * instance *@param poa the POA parameter to set in the new * instance *@return the CORBA object *@exception org.omg.CORBA.UserException Description of the Exception *@exception InstantiationException the exception thrown in the * constructor *@exception IllegalAccessException the exception thrown in the * constructor, setPOA and setORB invocations *@exception InvocationTargetException wrap the exception thrown in setPOA * and setORB invocations */ static org.omg.CORBA.Object createServant(Class clazz, java.lang.Object[] initparams, ORB orb, POA poa) throws org.omg.CORBA.UserException, InstantiationException, IllegalAccessException, InvocationTargetException { // create servant java.lang.Object instance; if (initparams == null) { instance = clazz.newInstance(); } else { // TO DO instance = null; } // set the ORB field if it exists try { Method method = clazz.getDeclaredMethod( "setORB", new Class[]{ORB.class} ); method.invoke(instance, new java.lang.Object[]{orb}); } catch (NoSuchMethodException e) { // do nothing } catch (IllegalArgumentException e) { // do nothing } // set the POA field if it exists try { Method method = clazz.getDeclaredMethod( "setPOA", new Class[]{POA.class} ); method.invoke(instance, new java.lang.Object[]{poa}); } catch (NoSuchMethodException e) { // do nothing } catch (IllegalArgumentException e) { // do nothing } // get object reference from the servant org.omg.CORBA.Object servant = poa.servant_to_reference((org.omg.PortableServer.Servant) instance); // XXX ref = XXXHelper.narrow(oref); return servant; } /** * Create and register a servant then bind it in the NameService if required * *@param obj the object to bind *@param orb Description of the Parameter *@param path Description of the Parameter *@exception org.omg.CORBA.UserException Description of the Exception */ static void rebind(org.omg.CORBA.Object obj, ORB orb, String path) throws org.omg.CORBA.UserException { NamingContextExt nc = getNamingContextExt(orb); NameComponent ncpath[] = nc.to_name(path); nc.rebind(ncpath, obj); } /** * Get the root naming context * *@param orb the ORB parameter to set in the new * instance *@return the root naming context *@exception org.omg.CORBA.UserException Description of the Exception */ static NamingContextExt getNamingContextExt(ORB orb) throws org.omg.CORBA.UserException { if (nc == null) { org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); // Use NamingContextExt which is part of the Interoperable // Naming Service (INS) specification. nc = NamingContextExtHelper.narrow(objRef); } return nc; } private static NamingContextExt nc = null; } class ShutdownHook extends Thread { private int delayInSec; private ORB orb; public ShutdownHook(int delay, ORB obr){ this.delayInSec=delayInSec; this.orb=orb; } public void run(){ System.out.println("Shutdown the server in " + delayInSec + " sec."); while((delayInSec--)>0){ try { Thread.sleep(1000); } catch (java.lang.InterruptedException e) { // do nothing } System.out.print('.'); System.out.flush(); } if(orb!=null) orb.shutdown(false); } }