1 /* NamingServicePersistent.java -- The persistent naming service.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
38 package gnu.classpath.tools.orbd;
40 import gnu.CORBA.OrbFunctional;
42 import gnu.CORBA.NamingService.Ext;
43 import gnu.classpath.tools.common.ClasspathToolParser;
44 import gnu.classpath.tools.getopt.FileArgumentCallback;
45 import gnu.classpath.tools.getopt.Option;
46 import gnu.classpath.tools.getopt.OptionException;
47 import gnu.classpath.tools.getopt.OptionGroup;
48 import gnu.classpath.tools.getopt.Parser;
50 import org.omg.CosNaming.NamingContextExt;
53 import java.io.FileOutputStream;
54 import java.io.FileNotFoundException;
55 import java.io.PrintStream;
56 import java.io.UnsupportedEncodingException;
59 * The server for the GNU Classpath persistent naming service.
61 * GNU Classpath currently works with this naming service and is also
62 * interoperable with the Sun Microsystems naming services from releases 1.3 and
63 * 1.4, both transient <i>tnameserv</i> and persistent <i>orbd</i>.
65 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
70 * The default port (900), on that the naming service starts if no
71 * -ORBInitialPort is specified in the command line.
73 public static final int PORT = 900;
75 private int port = PORT;
78 private String directory = "";
81 * Get the object key for the naming service. The default key is the string
82 * "NameService" in ASCII.
84 * @return the byte array.
86 public static byte[] getDefaultKey()
90 return "NameService".getBytes("UTF-8");
92 catch (UnsupportedEncodingException ex)
94 throw new InternalError("UTF-8 unsupported");
98 private Parser initializeParser()
100 Parser parser = new ClasspathToolParser("orbd", true); //$NON-NLS-1$
101 parser.setHeader(Messages.getString("Main.Usage")); //$NON-NLS-1$
103 parser.add(new Option("ORBInitialPort", //$NON-NLS-1$
104 Messages.getString("Main.ORBInitialPort"), //$NON-NLS-1$
105 Messages.getString("Main.Port")) //$NON-NLS-1$
107 public void parsed(String portArgument) throws OptionException
109 port = Integer.parseInt(portArgument);
113 parser.add(new Option("ior", //$NON-NLS-1$
114 Messages.getString("Main.IOR"), //$NON-NLS-1$
115 Messages.getString("Main.IORFile")) //$NON-NLS-1$
117 public void parsed(String fileArgument) throws OptionException
122 parser.add(new Option("directory", //$NON-NLS-1$
123 Messages.getString("Main.Directory"), //$NON-NLS-1$
124 Messages.getString("Main.DirectoryArgument")) //$NON-NLS-1$
126 public void parsed(String argument) throws OptionException
128 directory = argument;
131 parser.add(new Option("restart", //$NON-NLS-1$
132 Messages.getString("Main.Restart")) //$NON-NLS-1$
134 public void parsed(String argument) throws OptionException
143 private void run(String[] args)
145 Parser parser = initializeParser();
150 // Create and initialize the ORB
151 final OrbFunctional orb = new OrbFunctional();
152 OrbFunctional.setPort(port);
154 // Create the servant and register it with the ORB
155 File dataDirectory = new File(directory);
156 System.out.println("Persistent data stored at "
157 + dataDirectory.getAbsolutePath());
158 dataDirectory.mkdirs();
160 // / TODO support more starting modes.
161 NamingContextExt namer = new Ext(
162 new PersistentContext(
167 // Case with the key "NameService".
168 orb.connect(namer, "NameService".getBytes());
170 // Storing the IOR reference.
171 String ior = orb.object_to_string(namer);
172 IOR iorr = IOR.parse(ior);
175 FileOutputStream f = new FileOutputStream(iorf);
176 PrintStream p = new PrintStream(f);
181 System.out.println("GNU Classpath persistent naming service "
182 + "started at " + iorr.Internet.host + ":"
183 + iorr.Internet.port + " key 'NameService'.\n\n"
184 + "Copyright (C) 2006 Free Software Foundation\n"
185 + "This tool comes with ABSOLUTELY NO WARRANTY. "
186 + "This is free software, and you are\nwelcome to "
187 + "redistribute it under conditions, defined in "
188 + "GNU Classpath license.\n\n" + ior);
194 // Wait for invocations from clients.
199 catch (FileNotFoundException e)
201 throw new RuntimeException(e);
205 // Restore the default value for allocating ports for the subsequent
207 OrbFunctional.setPort(OrbFunctional.DEFAULT_INITIAL_PORT);
212 * The persistent naming service entry point.
214 public static void main(String[] args)
216 Main orbdprogram = new Main();
219 orbdprogram.run(args);
223 System.err.println(Messages.getString("Main.InternalError")); //$NON-NLS-1$
224 e.printStackTrace(System.err);