| 1 |
/* |
| 2 |
* junixsocket |
| 3 |
* |
| 4 |
* Copyright 2009-2022 Christian Kohlschütter |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
package org.newsclub.net.unix.rmi; |
| 19 |
|
| 20 |
import java.io.File; |
| 21 |
import java.io.IOException; |
| 22 |
import java.rmi.AlreadyBoundException; |
| 23 |
import java.rmi.RemoteException; |
| 24 |
|
| 25 |
import com.kohlschutter.annotations.compiletime.SuppressFBWarnings; |
| 26 |
import com.kohlschutter.util.SystemPropertyUtil; |
| 27 |
|
| 28 |
/** |
| 29 |
* A simple RMI Registry that is launched as forked Java VM from unit tests such as |
| 30 |
* {@link RemoteRegistryTest}. |
| 31 |
* |
| 32 |
* Important: The server will terminate ca. 10 seconds after starting. |
| 33 |
* |
| 34 |
* @author Christian Kohlschütter |
| 35 |
*/ |
| 36 |
public class TestRegistryServer { |
| 37 |
public static void main(String[] args) throws AlreadyBoundException, IOException, |
| 38 |
InterruptedException { |
| 39 |
AFUNIXNaming naming; |
| 40 |
String socketDirStr = System.getProperty("rmitest.junixsocket.socket-dir", ""); |
| 41 |
if (socketDirStr.isEmpty()) { |
| 42 |
naming = AFUNIXNaming.getInstance(); |
| 43 |
} else { |
| 44 |
File socketDir = new File(socketDirStr); |
| 45 |
if (!socketDir.mkdirs() && !socketDir.exists()) { |
| 46 |
throw new IOException("Could not create socketDir: " + socketDir); |
| 47 |
} |
| 48 |
naming = AFUNIXNaming.getInstance(socketDir); |
| 49 |
} |
| 50 |
|
| 51 |
naming.setRemoteShutdownAllowed(SystemPropertyUtil.getBooleanSystemProperty( |
| 52 |
"rmitest.junixsocket.remote-shutdown.allowed", true)); |
| 53 |
|
| 54 |
File socketDir = naming.getRegistrySocketDir(); |
| 55 |
System.out.println(socketDir); |
| 56 |
|
| 57 |
if (SystemPropertyUtil.getBooleanSystemProperty("rmitest.junixsocket.create-registry", true)) { |
| 58 |
naming.createRegistry(); |
| 59 |
} |
| 60 |
|
| 61 |
if (SystemPropertyUtil.getBooleanSystemProperty("rmitest.junixsocket.export-hello", true)) { |
| 62 |
int delay = SystemPropertyUtil.getIntSystemProperty("rmitest.junixsocket.export-hello.delay", |
| 63 |
0); |
| 64 |
if (delay > 0) { |
| 65 |
System.out.println("Delaying Hello export by " + delay + " ms"); |
| 66 |
try { |
| 67 |
Thread.sleep(delay); |
| 68 |
} catch (InterruptedException e) { |
| 69 |
// ignore |
| 70 |
} |
| 71 |
} |
| 72 |
naming.exportAndBind("hello", new HelloImpl()); |
| 73 |
} |
| 74 |
|
| 75 |
System.out.println("Server is ready"); |
| 76 |
|
| 77 |
int shutdownAfter = SystemPropertyUtil.getIntSystemProperty( |
| 78 |
"rmitest.junixsocket.shutdown-after.secs", -1); |
| 79 |
if (shutdownAfter >= 0) { |
| 80 |
Thread t = new Thread() { |
| 81 |
@SuppressFBWarnings({"DM_EXIT"}) |
| 82 |
@Override |
| 83 |
public void run() { |
| 84 |
try { |
| 85 |
Thread.sleep(shutdownAfter * 1000L); |
| 86 |
} catch (InterruptedException e) { |
| 87 |
// ignored |
| 88 |
} |
| 89 |
try { |
| 90 |
naming.shutdownRegistry(); |
| 91 |
} catch (RemoteException e) { |
| 92 |
e.printStackTrace(); |
| 93 |
} |
| 94 |
|
| 95 |
System.exit(0); // NOPMD |
| 96 |
} |
| 97 |
}; |
| 98 |
t.setDaemon(true); |
| 99 |
t.start(); |
| 100 |
} |
| 101 |
} |
| 102 |
} |