OSDN Git Service

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