1 |
779 |
jeremybenn |
/* Main.java -- the RMI activation daemon.
|
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.rmid;
|
39 |
|
|
|
40 |
|
|
import gnu.classpath.tools.rmid.ActivationSystemImpl;
|
41 |
|
|
import gnu.classpath.tools.common.ClasspathToolParser;
|
42 |
|
|
import gnu.classpath.tools.getopt.Option;
|
43 |
|
|
import gnu.classpath.tools.getopt.OptionException;
|
44 |
|
|
import gnu.classpath.tools.getopt.OptionGroup;
|
45 |
|
|
import gnu.classpath.tools.getopt.Parser;
|
46 |
|
|
import gnu.java.rmi.activation.ActivationSystemTransient;
|
47 |
|
|
import gnu.java.rmi.server.UnicastServerRef;
|
48 |
|
|
|
49 |
|
|
import java.io.File;
|
50 |
|
|
import java.net.InetAddress;
|
51 |
|
|
import java.rmi.Remote;
|
52 |
|
|
import java.rmi.activation.ActivationSystem;
|
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 |
|
|
|
58 |
|
|
|
59 |
|
|
/**
|
60 |
|
|
* The persistent RMI activation daemon.
|
61 |
|
|
*
|
62 |
|
|
* @author Audrius Meskauskas (audriusa@bioinformatics.org)
|
63 |
|
|
*/
|
64 |
|
|
public class Main
|
65 |
|
|
{
|
66 |
|
|
/**
|
67 |
|
|
* The RMI server socket factory.
|
68 |
|
|
*/
|
69 |
|
|
static RMIServerSocketFactory ACTIVATION_REGISTY_SOCKET_FACTORY = null;
|
70 |
|
|
|
71 |
|
|
/**
|
72 |
|
|
* The activation registry port.
|
73 |
|
|
*/
|
74 |
|
|
static int ACTIVATION_REGISTRY_PORT = ActivationSystem.SYSTEM_PORT;
|
75 |
|
|
|
76 |
|
|
/**
|
77 |
|
|
* The activation system name.
|
78 |
|
|
*/
|
79 |
|
|
static String ACTIVATION_SYSTEM_NAME = "java.rmi.activation.ActivationSystem";
|
80 |
|
|
|
81 |
|
|
// Parse parameters:
|
82 |
|
|
private boolean stop = false;
|
83 |
|
|
private String directory = ".";
|
84 |
|
|
private boolean cold = false;
|
85 |
|
|
private boolean persistent = false;
|
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 |
|
|
|
93 |
|
|
OptionGroup controlGroup
|
94 |
|
|
= new OptionGroup(Messages.getString("Main.ControlGroup")); //$NON-NLS-1$
|
95 |
|
|
controlGroup.add(new Option("port", //$NON-NLS-1$
|
96 |
|
|
Messages.getString("Main.PortOption"), //$NON-NLS-1$
|
97 |
|
|
Messages.getString("Main.Port")) //$NON-NLS-1$
|
98 |
|
|
{
|
99 |
|
|
public void parsed(String portArgument) throws OptionException
|
100 |
|
|
{
|
101 |
|
|
ACTIVATION_REGISTRY_PORT = Integer.parseInt(portArgument);
|
102 |
|
|
}
|
103 |
|
|
});
|
104 |
|
|
controlGroup.add(new Option("restart", //$NON-NLS-1$
|
105 |
|
|
Messages.getString("Main.Restart")) //$NON-NLS-1$
|
106 |
|
|
{
|
107 |
|
|
public void parsed(String argument) throws OptionException
|
108 |
|
|
{
|
109 |
|
|
cold = true;
|
110 |
|
|
}
|
111 |
|
|
});
|
112 |
|
|
controlGroup.add(new Option("stop", //$NON-NLS-1$
|
113 |
|
|
Messages.getString("Main.Stop")) //$NON-NLS-1$
|
114 |
|
|
{
|
115 |
|
|
public void parsed(String argument) throws OptionException
|
116 |
|
|
{
|
117 |
|
|
stop = true;
|
118 |
|
|
}
|
119 |
|
|
});
|
120 |
|
|
parser.add(controlGroup);
|
121 |
|
|
|
122 |
|
|
OptionGroup persistenceGroup
|
123 |
|
|
= new OptionGroup(Messages.getString("Main.PersistenceGroup")); //$NON-NLS-1$
|
124 |
|
|
persistenceGroup.add(new Option("persistent", //$NON-NLS-1$
|
125 |
|
|
Messages.getString("Main.Persistent")) //$NON-NLS-1$
|
126 |
|
|
{
|
127 |
|
|
public void parsed(String argument) throws OptionException
|
128 |
|
|
{
|
129 |
|
|
persistent = true;
|
130 |
|
|
}
|
131 |
|
|
});
|
132 |
|
|
persistenceGroup.add(new Option("directory", //$NON-NLS-1$
|
133 |
|
|
Messages.getString("Main.Directory"), //$NON-NLS-1$
|
134 |
|
|
Messages.getString("Main.DirectoryArgument")) //$NON-NLS-1$
|
135 |
|
|
{
|
136 |
|
|
public void parsed(String argument) throws OptionException
|
137 |
|
|
{
|
138 |
|
|
directory = argument;
|
139 |
|
|
}
|
140 |
|
|
});
|
141 |
|
|
parser.add(persistenceGroup);
|
142 |
|
|
|
143 |
|
|
OptionGroup debuggingGroup
|
144 |
|
|
= new OptionGroup(Messages.getString("Main.DebugGroup")); //$NON-NLS-1$
|
145 |
|
|
debuggingGroup.add(new Option("verbose", //$NON-NLS-1$
|
146 |
|
|
Messages.getString ("Main.Verbose")) //$NON-NLS-1$
|
147 |
|
|
{
|
148 |
|
|
public void parsed(String argument) throws OptionException
|
149 |
|
|
{
|
150 |
|
|
ActivationSystemTransient.debug = true;
|
151 |
|
|
}
|
152 |
|
|
});
|
153 |
|
|
parser.add(debuggingGroup);
|
154 |
|
|
|
155 |
|
|
return parser;
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
private void run(String[] args)
|
159 |
|
|
{
|
160 |
|
|
Parser p = initializeParser();
|
161 |
|
|
p.parse(args);
|
162 |
|
|
|
163 |
|
|
try
|
164 |
|
|
{
|
165 |
|
|
if (!stop)
|
166 |
|
|
{
|
167 |
|
|
// Start the system.
|
168 |
|
|
File dataDirectory = new File(directory);
|
169 |
|
|
if (!dataDirectory.exists())
|
170 |
|
|
dataDirectory.mkdirs();
|
171 |
|
|
ActivationSystem system;
|
172 |
|
|
|
173 |
|
|
if (!persistent)
|
174 |
|
|
system = ActivationSystemTransient.getInstance();
|
175 |
|
|
else
|
176 |
|
|
system = ActivationSystemImpl.getInstance(dataDirectory, cold);
|
177 |
|
|
|
178 |
|
|
// We must export with the specific activation id that is only
|
179 |
|
|
// possible when going into the gnu.java.rmi.activation.
|
180 |
|
|
UnicastServerRef sref = new UnicastServerRef(
|
181 |
|
|
new ObjID(ObjID.ACTIVATOR_ID), ACTIVATION_REGISTRY_PORT,
|
182 |
|
|
ACTIVATION_REGISTY_SOCKET_FACTORY);
|
183 |
|
|
Remote systemStub = sref.exportObject(system);
|
184 |
|
|
|
185 |
|
|
// Start the naming system on the activation system port
|
186 |
|
|
// (if not already running).
|
187 |
|
|
|
188 |
|
|
Registry r;
|
189 |
|
|
try
|
190 |
|
|
{
|
191 |
|
|
// Expect the naming service running first.
|
192 |
|
|
// The local host may want to use the shared registry
|
193 |
|
|
r = LocateRegistry.getRegistry(ACTIVATION_REGISTRY_PORT);
|
194 |
|
|
r.rebind(ACTIVATION_SYSTEM_NAME, systemStub);
|
195 |
|
|
}
|
196 |
|
|
catch (Exception ex)
|
197 |
|
|
{
|
198 |
|
|
// The naming service is not running. Start it.
|
199 |
|
|
r = LocateRegistry.createRegistry(ACTIVATION_REGISTRY_PORT);
|
200 |
|
|
r.rebind(ACTIVATION_SYSTEM_NAME, systemStub);
|
201 |
|
|
}
|
202 |
|
|
String host = InetAddress.getLocalHost().getCanonicalHostName();
|
203 |
|
|
System.out.println("The RMI daemon is listening on " + host +
|
204 |
|
|
" (port "
|
205 |
|
|
+ ACTIVATION_REGISTRY_PORT + ")");
|
206 |
|
|
|
207 |
|
|
}
|
208 |
|
|
else
|
209 |
|
|
{
|
210 |
|
|
// Stop the activation system.
|
211 |
|
|
Registry r;
|
212 |
|
|
try
|
213 |
|
|
{
|
214 |
|
|
System.out.print("Stopping RMI daemon at "
|
215 |
|
|
+ ACTIVATION_REGISTRY_PORT+" ... ");
|
216 |
|
|
// Expect the naming service running first.
|
217 |
|
|
// The local host may want to use the shared registry
|
218 |
|
|
r = LocateRegistry.getRegistry(ACTIVATION_REGISTRY_PORT);
|
219 |
|
|
ActivationSystem asys =
|
220 |
|
|
(ActivationSystem) r.lookup(ACTIVATION_SYSTEM_NAME);
|
221 |
|
|
asys.shutdown();
|
222 |
|
|
System.out.println("OK.");
|
223 |
|
|
}
|
224 |
|
|
catch (Exception ex)
|
225 |
|
|
{
|
226 |
|
|
System.out.println("The RMI daemon seems not running at "
|
227 |
|
|
+ ACTIVATION_REGISTRY_PORT);
|
228 |
|
|
if (ActivationSystemTransient.debug)
|
229 |
|
|
ex.printStackTrace();
|
230 |
|
|
}
|
231 |
|
|
}
|
232 |
|
|
}
|
233 |
|
|
catch (Exception e)
|
234 |
|
|
{
|
235 |
|
|
System.out.println("Failed to start the RMI daemon.");
|
236 |
|
|
if (ActivationSystemTransient.debug)
|
237 |
|
|
e.printStackTrace();
|
238 |
|
|
}
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
/**
|
242 |
|
|
* The activation system entry point.
|
243 |
|
|
*/
|
244 |
|
|
public static void main(String[] args)
|
245 |
|
|
{
|
246 |
|
|
Main rmidprogram = new Main();
|
247 |
|
|
try
|
248 |
|
|
{
|
249 |
|
|
rmidprogram.run(args);
|
250 |
|
|
}
|
251 |
|
|
catch (Exception e)
|
252 |
|
|
{
|
253 |
|
|
System.err.println(Messages.getString("Main.InternalError")); //$NON-NLS-1$
|
254 |
|
|
e.printStackTrace(System.err);
|
255 |
|
|
System.exit(1);
|
256 |
|
|
}
|
257 |
|
|
}
|
258 |
|
|
}
|