/
Entry.java
259 lines (229 loc) · 8.54 KB
/
Entry.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright 2008 Glencoe Software, Inc. All rights reserved.
* Use is subject to license terms supplied in LICENSE.txt
*/
package ome.services.blitz;
import java.util.concurrent.locks.ReentrantLock;
import ome.system.OmeroContext;
import ome.util.messages.ShutdownMessage;
import ome.util.messages.UserSignalMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.Signal;
import sun.misc.SignalHandler;
/**
* OMERO.blitz startup code. Replaces the standard <code>Main</code> class as
* the main application entry point. Uses Sun-specific APIs to handle signals.
*/
public class Entry {
private final static Logger log = LoggerFactory.getLogger(Entry.class);
/**
* Return code status. Initially -1. On successful start, 0. > 1 on
* exception.
*/
int status = -1;
/**
* Name of the {@link OmeroContext} to
*/
final String name;
/**
* Prevents multiple calls to {@link #shutdown()}
*/
final ReentrantLock lock = new ReentrantLock();
/**
* Context for the OMERO application
*/
volatile OmeroContext ctx = null;
/**
* {@link Ice.Communicator} which will be waited on.
*/
volatile Ice.Communicator ic = null;
private static void waitOnStartup() {
int ms = 10000; // 10 seconds by default
try {
String prop = System.getenv("OMERO_STARTUP_WAIT");
ms = Integer.valueOf(prop);
} catch (Exception e) {
log.debug(e.toString()); // slf4j migration: toString()
}
try {
log.info(String.format("Waiting %s ms on startup", ms));
Thread.sleep(ms);
} catch (InterruptedException e) {
log.debug(e.toString()); // slf4j migration: toString()
}
}
/**
* Entry point to the server. The first argument on the command line will be
* used as the name for the {@link OmeroContext}. Other options include:
*
* -s Check status (all args passed to {@link Ice.Util#initialize(String[])}
*
*/
public static void main(final String[] args) {
String name = "OMERO.blitz";
if (args != null && args.length > 0) {
if ("-s".equals(args[0])) {
try {
new Status(args).run();
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
System.exit(0);
}
// Now we find the first non-"--Ice.Config" argument and
// pass that to main(). The last --Ice.Config value will be
// seen by the Ice.Communicator. The last non-Ice.Config argument
// will be used as a beanRefContext lookup.
for (String string : args) {
if (string.startsWith("--Ice.Config")) {
System.setProperty("ICE_CONFIG", string.substring(13));
} else {
name = string;
waitOnStartup();
}
}
}
final Entry instance = new Entry(name);
SignalHandler handler = new SignalHandler() {
public void handle(Signal sig) {
log.info(sig.getName() + ": Shutdown requested.");
instance.lock.lock();
try {
instance.status = sig.getNumber();
instance.shutdown(true);
} finally {
// As with the try/finally block around shutdown in the
// start method, execution should never reach this point.
// But just in case, future code changes should introduce
// an exception (and to make findbugs happy) we'll add the
// try/finally
instance.lock.unlock();
}
}
};
registerSignal(handler, "INT");
registerSignal(handler, "TERM");
registerSignal(handler, "BREAK");
// ticket:#4210
class UserSignalHandler implements SignalHandler {
int signal;
UserSignalHandler(int signal) {
this.signal = signal;
}
public void handle(Signal arg0) {
try {
UserSignalMessage msg = new UserSignalMessage(this, signal);
instance.ctx.publishMessage(msg);
} catch (Throwable e) {
log.error("Error on user signal " + signal, e);
}
}
};
registerSignal(new UserSignalHandler(1), "USR1");
registerSignal(new UserSignalHandler(2), "USR2");
instance.start();
}
private static void registerSignal(SignalHandler handler, String sig) {
try {
Signal.handle(new Signal(sig), handler);
} catch (IllegalArgumentException iae) {
// Ok. BREAK will not exist on non-Windows systems, for example.
}
}
/**
* Stores name of the {@link OmeroContext} which is to be used by this
* instance.
*/
public Entry(String name) {
this.name = name;
}
/**
* Obtains the {@link #name named} {@link OmeroContext}, creating it if
* necessary, and then delegates to
* {@link Ice.Communicator#waitForShutdown()} until either it is externally
* shutdown, or until a signal is caught.
*/
public void start() {
try {
log.info("Creating " + name + ". Please wait...");
// TEMPORARY WORKAROUND
String ICE_CONFIG = System.getProperty("ICE_CONFIG");
// Parse out any omero.* properties from ICE_CONFIG
// and set them in the System
Ice.InitializationData id = new Ice.InitializationData();
id.properties = Ice.Util.createProperties();
if (ICE_CONFIG != null) {
id.properties.load(ICE_CONFIG);
for (String k : id.properties.getPropertiesForPrefix("omero").keySet()) {
System.setProperty(k, id.properties.getProperty(k));
}
}
ctx = OmeroContext.getInstance(name);
if (ctx.containsBean("Ice.Communicator")) {
ic = (Ice.Communicator) ctx.getBean("Ice.Communicator");
} else {
// TODO This should be adapted to work for any process
// that doesn't need to add servants. Here "Indexer" could
// be replaced by omero.name or similar.
ic = Ice.Util.initialize(id);
String adapterName = ctx.getBean("adapterName", String.class);
Ice.ObjectAdapter oa = ic.createObjectAdapter(adapterName);
oa.activate();
}
log.info(name + " now accepting connections.");
ic.waitForShutdown();
status = 0;
} catch (Ice.LocalException e) {
log.error("Error on startup.", e);
status = 1;
} catch (Exception e) {
log.error("Error on startup.", e);
status = 2;
}
System.out.flush();
System.err.flush();
lock.lock();
try {
shutdown(true);
} finally {
// This will never be called since System.exit is called in
// shutdown where no exception can be thrown, but just in case
// the code paths are ever changed and the exit doesn't get called
// we'll unlock so other threads won't hang the server.
lock.unlock();
}
}
public int status() {
return status;
}
/**
* Calls {@link OmeroContext#closeAll()} to recursively close all
* OMERO.blitz resources in the reverse order that they were created.
*
* Throws no exceptions.
*
* If true is passed for callSystemExit, then {@link System#exit(int)} will
* be called with the current status.
*/
public void shutdown(final boolean callSystemExit) {
// Finally shutdown the whole context
if (ctx != null) {
try {
ctx.publishMessage(new ShutdownMessage(this));
log.info("Calling close on context " + name);
OmeroContext forClose = ctx;
ctx = null;
forClose.closeAll();
log.info("Finished shutdown.");
} catch (Throwable t) {
log.error("Error shutting down " + name, t);
status = 3;
}
}
if (callSystemExit) {
System.exit(status);
}
}
}