Merge with trank @ 137446
[official-gcc.git] / libjava / classpath / tools / gnu / classpath / tools / orbd / Main.java
blob15f3612cc9a6ab1ecbeb7aedaba64a92b059bc6e
1 /* NamingServicePersistent.java -- The persistent naming service.
2 Copyright (C) 2006, 2008 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)
9 any later version.
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
19 02110-1301 USA.
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.
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;
41 import gnu.CORBA.IOR;
42 import gnu.CORBA.NamingService.Ext;
43 import gnu.classpath.tools.common.ClasspathToolParser;
44 import gnu.classpath.tools.getopt.Option;
45 import gnu.classpath.tools.getopt.OptionException;
46 import gnu.classpath.tools.getopt.Parser;
48 import org.omg.CosNaming.NamingContextExt;
50 import java.io.File;
51 import java.io.FileOutputStream;
52 import java.io.FileNotFoundException;
53 import java.io.PrintStream;
54 import java.io.UnsupportedEncodingException;
56 /**
57 * The server for the GNU Classpath persistent naming service.
59 * GNU Classpath currently works with this naming service and is also
60 * interoperable with the Sun Microsystems naming services from releases 1.3 and
61 * 1.4, both transient <i>tnameserv</i> and persistent <i>orbd</i>.
63 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
65 public class Main
67 /**
68 * The default port (900), on that the naming service starts if no
69 * -ORBInitialPort is specified in the command line.
71 public static final int PORT = 900;
73 private int port = PORT;
74 private String iorf;
75 private boolean cold;
76 private String directory = "";
78 /**
79 * Get the object key for the naming service. The default key is the string
80 * "NameService" in ASCII.
82 * @return the byte array.
84 public static byte[] getDefaultKey()
86 try
87 { // NameService
88 return "NameService".getBytes("UTF-8");
90 catch (UnsupportedEncodingException ex)
92 throw new InternalError("UTF-8 unsupported");
96 private Parser initializeParser()
98 Parser parser = new ClasspathToolParser("orbd", true); //$NON-NLS-1$
99 parser.setHeader(Messages.getString("Main.Usage")); //$NON-NLS-1$
101 parser.add(new Option("ORBInitialPort", //$NON-NLS-1$
102 Messages.getString("Main.ORBInitialPort"), //$NON-NLS-1$
103 Messages.getString("Main.Port")) //$NON-NLS-1$
105 public void parsed(String portArgument) throws OptionException
107 port = Integer.parseInt(portArgument);
111 parser.add(new Option("ior", //$NON-NLS-1$
112 Messages.getString("Main.IOR"), //$NON-NLS-1$
113 Messages.getString("Main.IORFile")) //$NON-NLS-1$
115 public void parsed(String fileArgument) throws OptionException
117 iorf = fileArgument;
120 parser.add(new Option("directory", //$NON-NLS-1$
121 Messages.getString("Main.Directory"), //$NON-NLS-1$
122 Messages.getString("Main.DirectoryArgument")) //$NON-NLS-1$
124 public void parsed(String argument) throws OptionException
126 directory = argument;
129 parser.add(new Option("restart", //$NON-NLS-1$
130 Messages.getString("Main.Restart")) //$NON-NLS-1$
132 public void parsed(String argument) throws OptionException
134 cold = true;
138 return parser;
141 private void run(String[] args)
143 Parser parser = initializeParser();
144 parser.parse(args);
148 // Create and initialize the ORB
149 final OrbFunctional orb = new OrbFunctional();
150 OrbFunctional.setPort(port);
152 // Create the servant and register it with the ORB
153 File dataDirectory = new File(directory);
154 System.out.println("Persistent data stored at "
155 + dataDirectory.getAbsolutePath());
156 dataDirectory.mkdirs();
158 // / TODO support more starting modes.
159 NamingContextExt namer = new Ext(
160 new PersistentContext(
161 orb,
162 dataDirectory,
163 cold));
165 // Case with the key "NameService".
166 orb.connect(namer, "NameService".getBytes());
168 // Storing the IOR reference.
169 String ior = orb.object_to_string(namer);
170 IOR iorr = IOR.parse(ior);
171 if (iorf != null)
173 FileOutputStream f = new FileOutputStream(iorf);
174 PrintStream p = new PrintStream(f);
175 p.print(ior);
176 p.close();
179 System.out.println("GNU Classpath persistent naming service "
180 + "started at " + iorr.Internet.host + ":"
181 + iorr.Internet.port + " key 'NameService'.\n\n"
182 + "Copyright (C) 2008 Free Software Foundation\n"
183 + "This tool comes with ABSOLUTELY NO WARRANTY. "
184 + "This is free software, and you are\nwelcome to "
185 + "redistribute it under conditions, defined in "
186 + "GNU Classpath license.\n\n" + ior);
188 new Thread()
190 public void run()
192 // Wait for invocations from clients.
193 orb.run();
195 }.start();
197 catch (FileNotFoundException e)
199 throw new RuntimeException(e);
201 finally
203 // Restore the default value for allocating ports for the subsequent
204 // objects.
205 OrbFunctional.setPort(OrbFunctional.DEFAULT_INITIAL_PORT);
210 * The persistent naming service entry point.
212 public static void main(String[] args)
214 Main orbdprogram = new Main();
217 orbdprogram.run(args);
219 catch (Exception e)
221 System.err.println(Messages.getString("Main.InternalError")); //$NON-NLS-1$
222 e.printStackTrace(System.err);
223 System.exit(1);