OSDN Git Service

91c50d7f8c7d9fe18345b9e73eeaefa3571bd76d
[pf3gnuchains/gcc-fork.git] / libjava / classpath / tools / gnu / classpath / tools / orbd / Main.java
1 /* NamingServicePersistent.java -- The persistent naming service.
2    Copyright (C) 2006 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 package gnu.classpath.tools.orbd;
39
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.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;
49
50 import org.omg.CosNaming.NamingContextExt;
51
52 import java.io.File;
53 import java.io.FileOutputStream;
54 import java.io.FileNotFoundException;
55 import java.io.PrintStream;
56 import java.io.UnsupportedEncodingException;
57
58 /**
59  * The server for the GNU Classpath persistent naming service. 
60  * 
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>.
64  * 
65  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
66  */
67 public class Main
68 {
69   /**
70    * The default port (900), on that the naming service starts if no
71    * -ORBInitialPort is specified in the command line.
72    */
73   public static final int PORT = 900;
74
75   private int port = PORT;
76   private String iorf;
77   private boolean cold;
78   private String directory = "";
79
80   /**
81    * Get the object key for the naming service. The default key is the string
82    * "NameService" in ASCII.
83    * 
84    * @return the byte array.
85    */
86   public static byte[] getDefaultKey()
87   {
88     try
89       { // NameService
90         return "NameService".getBytes("UTF-8");
91       }
92     catch (UnsupportedEncodingException ex)
93       {
94         throw new InternalError("UTF-8 unsupported");
95       }
96   }
97
98   private Parser initializeParser()
99   {
100     Parser parser = new ClasspathToolParser("orbd", true); //$NON-NLS-1$
101     parser.setHeader(Messages.getString("Main.Usage")); //$NON-NLS-1$
102
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$
106       {
107         public void parsed(String portArgument) throws OptionException
108         {
109           port = Integer.parseInt(portArgument);
110         }
111       });
112
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$
116       {
117         public void parsed(String fileArgument) throws OptionException
118         {
119           iorf = fileArgument;
120         }
121       });
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$
125       {
126         public void parsed(String argument) throws OptionException
127         {
128           directory = argument;
129         }
130       });
131     parser.add(new Option("restart", //$NON-NLS-1$
132                           Messages.getString("Main.Restart")) //$NON-NLS-1$
133       {
134         public void parsed(String argument) throws OptionException
135         {
136           cold = true;
137         }
138       });
139
140     return parser;
141   }
142
143   private void run(String[] args)
144   {
145     Parser parser = initializeParser();
146     parser.parse(args);
147
148     try
149       {
150         // Create and initialize the ORB
151         final OrbFunctional orb = new OrbFunctional();
152         OrbFunctional.setPort(port);
153
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();
159
160         // / TODO support more starting modes.
161         NamingContextExt namer = new Ext(
162                                          new PersistentContext(
163                                                                orb,
164                                                                dataDirectory,
165                                                                cold));
166
167         // Case with the key "NameService".
168         orb.connect(namer, "NameService".getBytes());
169
170         // Storing the IOR reference.
171         String ior = orb.object_to_string(namer);
172         IOR iorr = IOR.parse(ior);
173         if (iorf != null)
174           {
175             FileOutputStream f = new FileOutputStream(iorf);
176             PrintStream p = new PrintStream(f);
177             p.print(ior);
178             p.close();
179           }
180
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);
189
190         new Thread()
191         {
192           public void run()
193           {
194             // Wait for invocations from clients.
195             orb.run();
196           }
197         }.start();
198       }
199     catch (FileNotFoundException e)
200       {
201         throw new RuntimeException(e);
202       }
203     finally
204       {
205         // Restore the default value for allocating ports for the subsequent
206         // objects.
207         OrbFunctional.setPort(OrbFunctional.DEFAULT_INITIAL_PORT);
208       }
209   }
210
211   /**
212    * The persistent naming service entry point.
213    */
214   public static void main(String[] args)
215   {
216     Main orbdprogram = new Main();
217     try
218       {
219         orbdprogram.run(args);
220       }
221     catch (Exception e)
222       {
223         System.err.println(Messages.getString("Main.InternalError")); //$NON-NLS-1$
224         e.printStackTrace(System.err);
225         System.exit(1);
226       }
227   }
228 }