-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMain.java
More file actions
388 lines (330 loc) · 14.6 KB
/
Copy pathMain.java
File metadata and controls
388 lines (330 loc) · 14.6 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* Elemental
* Copyright (C) 2024, Evolved Binary Ltd
*
* admin@evolvedbinary.com
* https://www.evolvedbinary.com | https://www.elemental.xyz
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
* The original license header is included below.
*
* =====================================================================
*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
* info@exist-db.org
* http://www.exist-db.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* NOTE: Parts of this file contain code from 'Mort Bay Consulting Pty. Ltd.'.
* The original license header is included below.
*
* =====================================================================
*
* Copyright 2002-2005 Mort Bay Consulting Pty. Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.exist.start;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* This is an adopted version of the corresponding classes shipped
* with Jetty.
*
* @author Jan Hlavaty (hlavac@code.cz)
* @author Wolfgang Meier (meier@ifs.tu-darmstadt.de)
* @version $Revision$
* TODO:
* - finish possible jetty.home locations
* - better handling of errors (i.e. when jetty.home cannot be autodetected...)
* - include entries from lib _when needed_
*/
public class Main {
public static final String STANDARD_ENABLED_JETTY_CONFIGS = "standard.enabled-jetty-configs";
public static final String STANDALONE_ENABLED_JETTY_CONFIGS = "standalone.enabled-jetty-configs";
public static final String PROP_LOG4J_DISABLEJMX = "log4j2.disableJmx";
private static final int ERROR_CODE_GENERAL = 1;
private static final int ERROR_CODE_NO_JETTY_CONFIG = 7;
static final int ERROR_CODE_INCOMPATIBLE_JAVA_DETECTED = 13;
public static final String CONFIG_DIR_NAME = "etc";
private static final String PROP_EXIST_START_DEBUG = "exist.start.debug";
public static final String PROP_EXIST_JETTY_CONFIG = "exist.jetty.config";
public static final String PROP_EXIST_HOME = "exist.home";
public static final String PROP_JETTY_HOME = "jetty.home";
private static final String PROP_LOG4J_CONFIGURATION_FILE = "log4j.configurationFile";
private static final String PROP_JUL_MANAGER = "java.util.logging.manager";
private static final String PROP_JAVA_TEMP_DIR = "java.io.tmpdir";
public static final String ENV_EXIST_JETTY_CONFIG = "EXIST_JETTY_CONFIG";
public static final String ENV_EXIST_HOME = "EXIST_HOME";
public static final String ENV_JETTY_HOME = "JETTY_HOME";
private static Main exist;
private String _mode = "jetty";
private boolean _debug = Boolean.getBoolean(PROP_EXIST_START_DEBUG);
public static void main(final String[] args) {
try {
getMain().run(args);
} catch (final Exception e) {
e.printStackTrace();
System.exit(ERROR_CODE_GENERAL);
}
}
/**
* Singleton Factory Method
*
* @return instance of Main class.
*/
public static Main getMain() {
if (exist == null) {
exist = new Main();
}
return exist;
}
public String getMode() {
return this._mode;
}
private Main() {
}
public Main(final String mode) {
this._mode = mode;
}
private static Path getDirectory(final String name) {
try {
if (name != null) {
final Path dir = Paths.get(name).normalize().toAbsolutePath();
if (Files.isDirectory(dir)) {
return dir;
}
}
} catch (final InvalidPathException e) {
// NOP
}
return null;
}
private static void invokeMain(final ClassLoader classloader, final String classname, final String[] args)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException, ClassNotFoundException {
final Class<?> invoked_class = classloader.loadClass(classname);
final Class<?>[] method_param_types = new Class[1];
method_param_types[0] = args.getClass();
final Method main = invoked_class.getDeclaredMethod("main", method_param_types);
final Object[] method_params = new Object[1];
method_params[0] = args;
main.invoke(null, method_params);
}
public void run(final String[] args) {
try {
runEx(args);
} catch (final StartException e) {
if (e.getMessage() != null && !e.getMessage().isEmpty()) {
System.err.println(e.getMessage());
}
System.exit(e.getErrorCode());
}
}
public void runEx(String[] args) throws StartException {
// Check if the OpenJDK version can corrupt the database
CompatibleJavaVersionCheck.checkForCompatibleJavaVersion();
final String _classname;
if (args.length > 0) {
if ("client".equals(args[0])) {
_classname = "org.exist.client.InteractiveClient";
_mode = "client";
} else if ("backup".equals(args[0])) {
_classname = "org.exist.backup.Main";
_mode = "backup";
} else if ("jetty".equals(args[0]) || "standalone".equals(args[0])) {
_classname = "org.exist.jetty.JettyStart";
_mode = args[0];
} else if ("launch".equals(args[0])) {
_classname = "org.exist.launcher.LauncherWrapper";
_mode = "jetty";
} else if ("launcher".equals(args[0])) {
_classname = "org.exist.launcher.LauncherWrapper";
_mode = "other";
} else if ("shutdown".equals(args[0])) {
_classname = "org.exist.jetty.ServerShutdown";
_mode = "other";
} else {
_classname = args[0];
_mode = "other";
}
final String[] nargs = new String[args.length - 1];
if (args.length > 1) {
System.arraycopy(args, 1, nargs, 0, args.length - 1);
}
args = nargs;
} else {
_classname = "org.exist.launcher.LauncherWrapper";
_mode = "other";
}
if (_debug) {
System.err.println("mode=" + _mode);
}
// try and figure out exist home dir
final Optional<Path> existHomeDir = getFromSysPropOrEnv(PROP_EXIST_HOME, ENV_EXIST_HOME).map(Paths::get);
// try to find Jetty
if ("jetty".equals(_mode) || "standalone".equals(_mode)) {
final Optional<Path> jettyHomeDir = getFromSysPropOrEnv(PROP_JETTY_HOME, ENV_JETTY_HOME).map(Paths::get);
Optional<Path> existJettyConfigFile = getFromSysPropOrEnv(PROP_EXIST_JETTY_CONFIG, ENV_EXIST_JETTY_CONFIG).map(Paths::get);
if (!existJettyConfigFile.isPresent()) {
final String config;
if ("jetty".equals(_mode)) {
config = STANDARD_ENABLED_JETTY_CONFIGS;
} else {
config = STANDALONE_ENABLED_JETTY_CONFIGS;
}
if (jettyHomeDir.isPresent() && Files.exists(jettyHomeDir.get().resolve(CONFIG_DIR_NAME))) {
existJettyConfigFile = jettyHomeDir.map(f -> f.resolve(CONFIG_DIR_NAME).resolve(config));
}
if (existHomeDir.isPresent() && Files.exists(existHomeDir.get().resolve(CONFIG_DIR_NAME))) {
existJettyConfigFile = existHomeDir.map(f -> f.resolve(CONFIG_DIR_NAME).resolve(config));
}
if (!existJettyConfigFile.isPresent()) {
System.err.println("ERROR: jetty config file could not be found! Make sure to set exist.jetty.config or EXIST_JETTY_CONFIG.");
System.err.flush();
throw new StartException(ERROR_CODE_NO_JETTY_CONFIG);
}
}
final String[] jettyStartArgs = new String[1 + args.length];
jettyStartArgs[0] = existJettyConfigFile.get().toAbsolutePath().toString();
System.arraycopy(args, 0, jettyStartArgs, 1, args.length);
args = jettyStartArgs;
}
// find log4j2.xml
Optional<Path> log4jConfigurationFile = Optional.ofNullable(System.getProperty(PROP_LOG4J_CONFIGURATION_FILE)).map(Paths::get);
if (!log4jConfigurationFile.isPresent()) {
if (existHomeDir.isPresent() && Files.exists(existHomeDir.get().resolve(CONFIG_DIR_NAME))) {
log4jConfigurationFile = existHomeDir.map(f -> f.resolve(CONFIG_DIR_NAME).resolve("log4j2.xml"));
}
}
// (fix for windows machine)Always normalise to a file: URI so Log4j2 can resolve it on all platforms (including Windows)
if (log4jConfigurationFile.isPresent() && Files.isReadable(log4jConfigurationFile.get())) {
System.setProperty(PROP_LOG4J_CONFIGURATION_FILE, log4jConfigurationFile.get().toAbsolutePath().toUri().toString());
}
if (log4jConfigurationFile.isPresent()) {
//redirect JUL to log4j2 unless otherwise specified
System.setProperty(PROP_JUL_MANAGER, Optional.ofNullable(System.getProperty(PROP_JUL_MANAGER)).orElse("org.apache.logging.log4j.jul.LogManager"));
}
// Enable JXM support log4j since v2.24.0 [2024]
System.setProperty(PROP_LOG4J_DISABLEJMX, "false");
// clean up tempdir for Jetty...
try {
final Path tmpdir = Paths.get(System.getProperty(PROP_JAVA_TEMP_DIR)).toAbsolutePath();
if (Files.isDirectory(tmpdir)) {
System.setProperty(PROP_JAVA_TEMP_DIR, tmpdir.toString());
}
} catch (final InvalidPathException e) {
// ignore
}
if (_debug) {
System.err.println(PROP_JAVA_TEMP_DIR + "=" + System.getProperty(PROP_JAVA_TEMP_DIR));
}
// setup classloader
final Classpath _classpath = new Classpath();
final EXistClassLoader cl = _classpath.getClassLoader(null);
Thread.currentThread().setContextClassLoader(cl);
// Invoke main class using new classloader.
try {
invokeMain(cl, _classname, args);
} catch (final Exception e) {
e.printStackTrace();
throw new StartException(ERROR_CODE_GENERAL);
}
}
private Optional<String> getFromSysPropOrEnv(final String sysPropName, final String envVarName) {
Optional<String> value = Optional.ofNullable(System.getProperty(sysPropName));
if (!value.isPresent()) {
value = Optional.ofNullable(System.getenv().get(envVarName));
// if we managed to detect from environment, store it in a system property
value.ifPresent(s -> System.setProperty(sysPropName, s));
}
if (_debug && value.isPresent()) {
System.err.println(sysPropName + "=" + System.getProperty(sysPropName));
}
return value;
}
public void shutdown() {
// only used in test suite
try {
shutdownEx();
} catch (final StopException e) {
e.printStackTrace();
}
}
public void shutdownEx() throws StopException {
// only used in test suite
try {
final Class<?> brokerPool = Class.forName("org.exist.storage.BrokerPools");
final Method stopAll = brokerPool.getDeclaredMethod("stopAll", boolean.class);
stopAll.setAccessible(true);
stopAll.invoke(null, false);
} catch (final ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new StopException(e.getMessage(), e);
}
}
/**
* Copied from {@link org.exist.util.FileUtils#list(Path, Predicate)}
* as org.exist.start is compiled into a separate Jar and doesn't have
* the rest of eXist available on the classpath
*/
static List<Path> list(final Path directory, final Predicate<Path> filter) throws IOException {
try(final Stream<Path> entries = Files.list(directory).filter(filter)) {
return entries.collect(Collectors.toList());
}
}
/**
* Copied from {@link org.exist.util.FileUtils#fileName(Path)}
* as org.exist.start is compiled into a separate Jar and doesn't have
* the rest of eXist available on the classpath
*/
static String fileName(final Path path) {
return path.getFileName().toString();
}
}