Source code: gnu/classpath/tools/rmi/RMID.java
1 /* RMID.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.rmi;
39
40 import gnu.classpath.tools.HelpPrinter;
41 import gnu.classpath.tools.rmi.rmid.ActivationSystemImpl;
42 import gnu.java.rmi.activation.ActivationSystemTransient;
43 import gnu.java.rmi.server.UnicastServerRef;
44
45 import java.io.File;
46 import java.net.InetAddress;
47 import java.rmi.Remote;
48 import java.rmi.activation.ActivationSystem;
49 import java.rmi.registry.LocateRegistry;
50 import java.rmi.registry.Registry;
51 import java.rmi.server.ObjID;
52 import java.rmi.server.RMIServerSocketFactory;
53
54
55 /**
56 * The persistent RMI activation daemon.
57 *
58 * @author Audrius Meskauskas (audriusa@bioinformatics.org)
59 */
60 public class RMID
61 {
62 /**
63 * The RMI server socket factory.
64 */
65 static RMIServerSocketFactory ACTIVATION_REGISTY_SOCKET_FACTORY = null;
66
67 /**
68 * The activation registry port.
69 */
70 static int ACTIVATION_REGISTRY_PORT = ActivationSystem.SYSTEM_PORT;
71
72 /**
73 * The activation system name.
74 */
75 static String ACTIVATION_SYSTEM_NAME = "java.rmi.activation.ActivationSystem";
76
77 /**
78 * The RMI activation daemon entry point.
79 */
80 public static void main(String[] args)
81 {
82 String HelpPath = "rmi/RMID.txt";
83 HelpPrinter.checkHelpKey(args, HelpPath);
84
85 // Parse parameters:
86 boolean stop = false;
87 String folder = ".";
88 boolean cold = false;
89 boolean trans = false;
90
91 for (int i = 0; i < args.length; i++)
92 {
93 String a = args[i];
94 if (a.equals("-verbose"))
95 ActivationSystemTransient.debug = true;
96 else if (a.equals("-stop"))
97 stop = true;
98 else if (a.equals("-restart"))
99 cold = true;
100 else if (a.equals("-transient"))
101 trans = true;
102 else if (i < args.length - 1)
103 {
104 // The additional key parameter is possible.
105 if (a.equals("-port"))
106 ACTIVATION_REGISTRY_PORT = Integer.parseInt(args[++i]);
107 else if (a.equals("-folder"))
108 folder = args[++i];
109 }
110 }
111
112 try
113 {
114 if (!stop)
115 {
116 // Start the system.
117 File dataFolder = new File(folder);
118 if (!dataFolder.exists())
119 dataFolder.mkdirs();
120 ActivationSystem system;
121
122 if (trans)
123 system = ActivationSystemTransient.getInstance();
124 else
125 system = ActivationSystemImpl.getInstance(dataFolder, cold);
126
127 // We must export with the specific activation id that is only
128 // possible when going into the gnu.java.rmi.activation.
129 UnicastServerRef sref = new UnicastServerRef(
130 new ObjID(ObjID.ACTIVATOR_ID), ACTIVATION_REGISTRY_PORT,
131 ACTIVATION_REGISTY_SOCKET_FACTORY);
132 Remote systemStub = sref.exportObject(system);
133
134 // Start the naming system on the activation system port
135 // (if not already running).
136
137 Registry r;
138 try
139 {
140 // Expect the naming service running first.
141 // The local host may want to use the shared registry
142 r = LocateRegistry.getRegistry(ACTIVATION_REGISTRY_PORT);
143 r.rebind(ACTIVATION_SYSTEM_NAME, systemStub);
144 }
145 catch (Exception ex)
146 {
147 // The naming service is not running. Start it.
148 r = LocateRegistry.createRegistry(ACTIVATION_REGISTRY_PORT);
149 r.rebind(ACTIVATION_SYSTEM_NAME, systemStub);
150 }
151 String host = InetAddress.getLocalHost().getCanonicalHostName();
152 System.out.println("The RMI daemon is listening on " + host +
153 " (port "
154 + ACTIVATION_REGISTRY_PORT + ")");
155
156 }
157 else
158 {
159 // Stop the activation system.
160 Registry r;
161 try
162 {
163 System.out.print("Stopping RMI daemon at "
164 + ACTIVATION_REGISTRY_PORT+" ... ");
165 // Expect the naming service running first.
166 // The local host may want to use the shared registry
167 r = LocateRegistry.getRegistry(ACTIVATION_REGISTRY_PORT);
168 ActivationSystem asys =
169 (ActivationSystem) r.lookup(ACTIVATION_SYSTEM_NAME);
170 asys.shutdown();
171 System.out.println("OK.");
172 }
173 catch (Exception ex)
174 {
175 System.out.println("The RMI daemon seems not running at "
176 + ACTIVATION_REGISTRY_PORT);
177 if (ActivationSystemTransient.debug)
178 ex.printStackTrace();
179 }
180 }
181 }
182 catch (Exception e)
183 {
184 System.out.println("Failed to start the RMI daemon.");
185 if (ActivationSystemTransient.debug)
186 e.printStackTrace();
187 }
188 }
189 }