Source for gnu.CORBA.NamingService.NamingServiceTransient

   1: /* NamingServiceTransient.java --
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.CORBA.NamingService;
  40: 
  41: import gnu.CORBA.OrbFunctional;
  42: import gnu.CORBA.IOR;
  43: 
  44: import org.omg.CosNaming.NamingContextExt;
  45: 
  46: import java.io.FileOutputStream;
  47: import java.io.PrintStream;
  48: import java.io.UnsupportedEncodingException;
  49: 
  50: /**
  51:  * The server for the gnu classpath naming service. This is an executable class
  52:  * that must be started to launch the GNU Classpath CORBA transient naming
  53:  * service.
  54:  *
  55:  * GNU Classpath currently works with this naming service and is also
  56:  * interoperable with the Sun Microsystems naming services from releases 1.3 and
  57:  * 1.4, both transient <i>tnameserv</i> and persistent <i>orbd</i>.
  58:  *
  59:  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  60:  */
  61: public class NamingServiceTransient
  62: {
  63:   /**
  64:    * The default port (900), on that the naming service starts if no
  65:    * -ORBInitialPort is specified in the command line.
  66:    */
  67:   public static final int PORT = 900;
  68: 
  69:   /**
  70:    * Get the object key for the naming service. The default key is the string
  71:    * "NameService" in ASCII.
  72:    *
  73:    * @return the byte array.
  74:    */
  75:   public static byte[] getDefaultKey()
  76:   {
  77:     try
  78:       { // NameService
  79:         return "NameService".getBytes("UTF-8");
  80:       }
  81:     catch (UnsupportedEncodingException ex)
  82:       {
  83:         throw new InternalError("UTF-8 unsupported");
  84:       }
  85:   }
  86: 
  87:   /**
  88:    * Start the naming service on the current host at the given port.
  89:    *
  90:    * @param portArgument the port on which the service will be
  91:    * started, or -1 to use the default port, 900
  92:    * @param fileArgument if non-null, store the IOR string of this
  93:    * naming service in a file by this name
  94:    */
  95:   public static void start(int portArgument, String fileArgument)
  96:   {
  97:     int port = PORT;
  98: 
  99:     if (portArgument > -1)
 100:       port = portArgument;
 101: 
 102:     String iorf = fileArgument;
 103:     try
 104:       {
 105:         // Create and initialize the ORB
 106:         final OrbFunctional orb = new OrbFunctional();
 107: 
 108:         OrbFunctional.setPort(port);
 109: 
 110:         // Create the servant and register it with the ORB
 111:         NamingContextExt namer = new Ext(new TransientContext());
 112: 
 113:         // Case with the key "NameService".
 114:         orb.connect(namer, "NameService".getBytes());
 115: 
 116:         // Storing the IOR reference.
 117:         String ior = orb.object_to_string(namer);
 118:         IOR iorr = IOR.parse(ior);
 119:         if (iorf != null)
 120:           {
 121:             FileOutputStream f = new FileOutputStream(iorf);
 122:             PrintStream p = new PrintStream(f);
 123:             p.print(ior);
 124:             p.close();
 125:           }
 126: 
 127:         new Thread()
 128:         {
 129:           public void run()
 130:           {
 131:             // Wait for invocations from clients.
 132:             orb.run();
 133:           }
 134:         }.start();
 135:       }
 136:     catch (Exception e)
 137:       {
 138:         System.err.println("ERROR: " + e);
 139:         e.printStackTrace(System.err);
 140:       }
 141: 
 142:     // Restore the default value for allocating ports for the subsequent
 143:     // objects.
 144:     OrbFunctional.setPort(OrbFunctional.DEFAULT_INITIAL_PORT);
 145:   }
 146: }