1 |
779 |
jeremybenn |
/* Main.java -- RMI registry starter.
|
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 |
|
|
|
39 |
|
|
package gnu.classpath.tools.rmiregistry;
|
40 |
|
|
|
41 |
|
|
import gnu.classpath.tools.common.ClasspathToolParser;
|
42 |
|
|
import gnu.classpath.tools.getopt.FileArgumentCallback;
|
43 |
|
|
import gnu.classpath.tools.getopt.Option;
|
44 |
|
|
import gnu.classpath.tools.getopt.OptionException;
|
45 |
|
|
import gnu.classpath.tools.getopt.OptionGroup;
|
46 |
|
|
import gnu.classpath.tools.getopt.Parser;
|
47 |
|
|
import gnu.classpath.tools.rmiregistry.RegistryImpl;
|
48 |
|
|
import gnu.java.rmi.server.UnicastServerRef;
|
49 |
|
|
|
50 |
|
|
import java.io.File;
|
51 |
|
|
import java.rmi.NotBoundException;
|
52 |
|
|
import java.rmi.RemoteException;
|
53 |
|
|
import java.rmi.registry.LocateRegistry;
|
54 |
|
|
import java.rmi.registry.Registry;
|
55 |
|
|
import java.rmi.server.ObjID;
|
56 |
|
|
import java.rmi.server.RMIServerSocketFactory;
|
57 |
|
|
import java.util.Hashtable;
|
58 |
|
|
import java.util.Map;
|
59 |
|
|
|
60 |
|
|
/**
|
61 |
|
|
* The optionally persistent RMI registry implementation.
|
62 |
|
|
*
|
63 |
|
|
* @author Audrius Meskauskas (audriusa@bioinformatics.org)
|
64 |
|
|
*/
|
65 |
|
|
public class Main
|
66 |
|
|
{
|
67 |
|
|
/**
|
68 |
|
|
* The stop command.
|
69 |
|
|
*/
|
70 |
|
|
public static String STOP = "gnu.classpath.tools.rmi.registry.command.STOP";
|
71 |
|
|
|
72 |
|
|
/**
|
73 |
|
|
* If true, the registry prints registration events to console.
|
74 |
|
|
*/
|
75 |
|
|
public static boolean verbose = false;
|
76 |
|
|
|
77 |
|
|
/**
|
78 |
|
|
* Parsed parameters.
|
79 |
|
|
*/
|
80 |
|
|
private String directory = ".";
|
81 |
|
|
private boolean cold = false;
|
82 |
|
|
private boolean persistent = false;
|
83 |
|
|
private boolean stop = false;
|
84 |
|
|
private int port = Registry.REGISTRY_PORT;
|
85 |
|
|
private RMIServerSocketFactory ssf = null;
|
86 |
|
|
|
87 |
|
|
private Parser initializeParser()
|
88 |
|
|
{
|
89 |
|
|
Parser parser = new ClasspathToolParser("rmiregistry", true); //$NON-NLS-1$
|
90 |
|
|
parser.setHeader(Messages.getString("Main.Usage")); //$NON-NLS-1$
|
91 |
|
|
|
92 |
|
|
OptionGroup controlGroup
|
93 |
|
|
= new OptionGroup(Messages.getString("Main.ControlGroup")); //$NON-NLS-1$
|
94 |
|
|
controlGroup.add(new Option("restart", //$NON-NLS-1$
|
95 |
|
|
Messages.getString("Main.Restart")) //$NON-NLS-1$
|
96 |
|
|
{
|
97 |
|
|
public void parsed(String argument) throws OptionException
|
98 |
|
|
{
|
99 |
|
|
cold = true;
|
100 |
|
|
}
|
101 |
|
|
});
|
102 |
|
|
controlGroup.add(new Option("stop", //$NON-NLS-1$
|
103 |
|
|
Messages.getString("Main.Stop")) //$NON-NLS-1$
|
104 |
|
|
{
|
105 |
|
|
public void parsed(String argument) throws OptionException
|
106 |
|
|
{
|
107 |
|
|
stop = true;
|
108 |
|
|
}
|
109 |
|
|
});
|
110 |
|
|
parser.add(controlGroup);
|
111 |
|
|
|
112 |
|
|
OptionGroup persistenceGroup
|
113 |
|
|
= new OptionGroup(Messages.getString("Main.PersistenceGroup")); //$NON-NLS-1$
|
114 |
|
|
persistenceGroup.add(new Option("persistent", //$NON-NLS-1$
|
115 |
|
|
Messages.getString("Main.Persistent")) //$NON-NLS-1$
|
116 |
|
|
{
|
117 |
|
|
public void parsed(String argument) throws OptionException
|
118 |
|
|
{
|
119 |
|
|
persistent = true;
|
120 |
|
|
}
|
121 |
|
|
});
|
122 |
|
|
persistenceGroup.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(persistenceGroup);
|
132 |
|
|
|
133 |
|
|
OptionGroup debuggingGroup
|
134 |
|
|
= new OptionGroup(Messages.getString("Main.DebugGroup")); //$NON-NLS-1$
|
135 |
|
|
debuggingGroup.add(new Option("verbose", //$NON-NLS-1$
|
136 |
|
|
Messages.getString ("Main.Verbose")) //$NON-NLS-1$
|
137 |
|
|
{
|
138 |
|
|
public void parsed(String argument) throws OptionException
|
139 |
|
|
{
|
140 |
|
|
verbose = true;
|
141 |
|
|
}
|
142 |
|
|
});
|
143 |
|
|
parser.add(debuggingGroup);
|
144 |
|
|
|
145 |
|
|
return parser;
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
private void run(String[] args)
|
149 |
|
|
{
|
150 |
|
|
Parser p = initializeParser();
|
151 |
|
|
p.parse(args, new FileArgumentCallback()
|
152 |
|
|
{
|
153 |
|
|
public void notifyFile(String portArgument)
|
154 |
|
|
{
|
155 |
|
|
port = Integer.parseInt(portArgument);
|
156 |
|
|
}
|
157 |
|
|
});
|
158 |
|
|
|
159 |
|
|
if (!stop)
|
160 |
|
|
{
|
161 |
|
|
Map table;
|
162 |
|
|
if (!persistent)
|
163 |
|
|
table = new Hashtable();
|
164 |
|
|
else
|
165 |
|
|
{
|
166 |
|
|
// Start the system.
|
167 |
|
|
File dataDirectory = new File(directory);
|
168 |
|
|
if (!dataDirectory.exists())
|
169 |
|
|
dataDirectory.mkdirs();
|
170 |
|
|
table = PersistentHashTable.createInstance(
|
171 |
|
|
new File(dataDirectory, "rmiregistry.data"), cold);
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
RegistryImpl system = new RegistryImpl(table);
|
175 |
|
|
|
176 |
|
|
// We must export with the specific activation id that is only
|
177 |
|
|
// possible when going into the gnu.java.rmi
|
178 |
|
|
try
|
179 |
|
|
{
|
180 |
|
|
UnicastServerRef sref = new UnicastServerRef(
|
181 |
|
|
new ObjID(ObjID.REGISTRY_ID), port, ssf);
|
182 |
|
|
|
183 |
|
|
sref.exportObject(system);
|
184 |
|
|
System.out.println("The RMI naming service is listening at " + port);
|
185 |
|
|
}
|
186 |
|
|
catch (Exception ex)
|
187 |
|
|
{
|
188 |
|
|
System.out.println("Failed to start RMI naming service at " + port);
|
189 |
|
|
}
|
190 |
|
|
}
|
191 |
|
|
else
|
192 |
|
|
{
|
193 |
|
|
// Stop the naming service.
|
194 |
|
|
try
|
195 |
|
|
{
|
196 |
|
|
Registry r = LocateRegistry.getRegistry(port);
|
197 |
|
|
// Search for this specific line will command to stop the registry.
|
198 |
|
|
|
199 |
|
|
// Our service returns null, but any other service will thrown
|
200 |
|
|
// NotBoundException.
|
201 |
|
|
r.unbind(STOP);
|
202 |
|
|
}
|
203 |
|
|
catch (RemoteException e)
|
204 |
|
|
{
|
205 |
|
|
System.out.println("Failed to stop RMI naming service at " + port);
|
206 |
|
|
}
|
207 |
|
|
catch (NotBoundException e)
|
208 |
|
|
{
|
209 |
|
|
System.out.println("The naming service at port " + port + " is not a "
|
210 |
|
|
+ Main.class.getName());
|
211 |
|
|
}
|
212 |
|
|
}
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
/**
|
216 |
|
|
* The RMI registry implementation entry point.
|
217 |
|
|
*/
|
218 |
|
|
public static void main(String[] args)
|
219 |
|
|
{
|
220 |
|
|
Main rmiregistryprogram = new Main();
|
221 |
|
|
try
|
222 |
|
|
{
|
223 |
|
|
rmiregistryprogram.run(args);
|
224 |
|
|
}
|
225 |
|
|
catch (Exception e)
|
226 |
|
|
{
|
227 |
|
|
System.err.println(Messages.getString("Main.InternalError")); //$NON-NLS-1$
|
228 |
|
|
e.printStackTrace(System.err);
|
229 |
|
|
System.exit(1);
|
230 |
|
|
}
|
231 |
|
|
}
|
232 |
|
|
}
|