1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.server;
|
12
|
|
13
|
import static eu.etaxonomy.cdm.server.AssumedMemoryRequirements.KB;
|
14
|
import static eu.etaxonomy.cdm.server.CommandOptions.DATASOURCES_FILE;
|
15
|
import static eu.etaxonomy.cdm.server.CommandOptions.HELP;
|
16
|
import static eu.etaxonomy.cdm.server.CommandOptions.HTTP_PORT;
|
17
|
import static eu.etaxonomy.cdm.server.CommandOptions.JMX;
|
18
|
import static eu.etaxonomy.cdm.server.CommandOptions.LOG_DIR;
|
19
|
import static eu.etaxonomy.cdm.server.CommandOptions.WEBAPP;
|
20
|
import static eu.etaxonomy.cdm.server.CommandOptions.WEBAPP_CLASSPATH;
|
21
|
import static eu.etaxonomy.cdm.server.CommandOptions.WIN32SERVICE;
|
22
|
|
23
|
import java.io.BufferedReader;
|
24
|
import java.io.File;
|
25
|
import java.io.FileNotFoundException;
|
26
|
import java.io.FileOutputStream;
|
27
|
import java.io.IOException;
|
28
|
import java.io.InputStream;
|
29
|
import java.io.InputStreamReader;
|
30
|
import java.io.OutputStream;
|
31
|
import java.lang.management.ManagementFactory;
|
32
|
import java.net.URL;
|
33
|
import java.util.List;
|
34
|
import java.util.Properties;
|
35
|
|
36
|
import org.apache.commons.cli.CommandLine;
|
37
|
import org.apache.commons.cli.CommandLineParser;
|
38
|
import org.apache.commons.cli.GnuParser;
|
39
|
import org.apache.commons.cli.HelpFormatter;
|
40
|
import org.apache.commons.cli.ParseException;
|
41
|
import org.apache.commons.io.FileUtils;
|
42
|
import org.apache.log4j.Logger;
|
43
|
import org.apache.log4j.PatternLayout;
|
44
|
import org.apache.log4j.RollingFileAppender;
|
45
|
import org.eclipse.jetty.jmx.MBeanContainer;
|
46
|
import org.eclipse.jetty.security.HashLoginService;
|
47
|
import org.eclipse.jetty.server.Handler;
|
48
|
import org.eclipse.jetty.server.Server;
|
49
|
import org.eclipse.jetty.server.handler.ContextHandler;
|
50
|
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
|
51
|
import org.eclipse.jetty.util.log.Log;
|
52
|
import org.eclipse.jetty.webapp.WebAppClassLoader;
|
53
|
import org.eclipse.jetty.webapp.WebAppContext;
|
54
|
|
55
|
import eu.etaxonomy.cdm.server.instance.CdmInstance;
|
56
|
import eu.etaxonomy.cdm.server.instance.Configuration;
|
57
|
import eu.etaxonomy.cdm.server.instance.InstanceManager;
|
58
|
import eu.etaxonomy.cdm.server.instance.SharedAttributes;
|
59
|
import eu.etaxonomy.cdm.server.instance.Status;
|
60
|
import eu.etaxonomy.cdm.server.win32service.Win32Service;
|
61
|
|
62
|
|
63
|
/**
|
64
|
* A bootstrap class for starting Jetty Runner using an embedded war.
|
65
|
*
|
66
|
* Recommended start options for the java virtual machine:
|
67
|
* <pre>
|
68
|
* -Xmx1024M
|
69
|
*
|
70
|
* -XX:PermSize=128m
|
71
|
* -XX:MaxPermSize=192m
|
72
|
*
|
73
|
* -XX:+UseConcMarkSweepGC
|
74
|
* -XX:+CMSClassUnloadingEnabled
|
75
|
* -XX:+CMSPermGenSweepingEnabled
|
76
|
* </pre>
|
77
|
*
|
78
|
* @version $Revision$
|
79
|
*/
|
80
|
public final class Bootloader {
|
81
|
/**
|
82
|
*
|
83
|
*/
|
84
|
private static final String VERSION_PROPERTIES_FILE = "version.properties";
|
85
|
|
86
|
//private static final String DEFAULT_WARFILE = "target/";
|
87
|
|
88
|
|
89
|
|
90
|
private static final Logger logger = Logger.getLogger(Bootloader.class);
|
91
|
|
92
|
private static final String DATASOURCE_BEANDEF_FILE = "datasources.xml";
|
93
|
private static final String REALM_PROPERTIES_FILE = "cdm-server-realm.properties";
|
94
|
|
95
|
private static final String USERHOME_CDM_LIBRARY_PATH = System.getProperty("user.home")+File.separator+".cdmLibrary"+File.separator;
|
96
|
private static final String TMP_PATH = USERHOME_CDM_LIBRARY_PATH + "server" + File.separator;
|
97
|
private static final String LOG_PATH = USERHOME_CDM_LIBRARY_PATH + "log" + File.separator;
|
98
|
|
99
|
private static final String APPLICATION_NAME = "CDM Server";
|
100
|
private static final String WAR_POSTFIX = ".war";
|
101
|
|
102
|
private static final String CDMLIB_REMOTE_WEBAPP = "cdmlib-remote-webapp";
|
103
|
private static final String CDMLIB_REMOTE_WEBAPP_VERSION = "cdmlib-remote-webapp.version";
|
104
|
|
105
|
private static final String DEFAULT_WEBAPP_WAR_NAME = "default-webapp";
|
106
|
private static final File DEFAULT_WEBAPP_TEMP_FOLDER = new File(TMP_PATH + DEFAULT_WEBAPP_WAR_NAME);
|
107
|
private static final File CDM_WEBAPP_TEMP_FOLDER = new File(TMP_PATH + CDMLIB_REMOTE_WEBAPP);
|
108
|
|
109
|
private final InstanceManager instanceManager = new InstanceManager(new File(USERHOME_CDM_LIBRARY_PATH, DATASOURCE_BEANDEF_FILE));
|
110
|
|
111
|
public List<CdmInstance> getCdmInstances() {
|
112
|
return instanceManager.getInstances();
|
113
|
}
|
114
|
|
115
|
public InstanceManager getInstanceManager(){
|
116
|
return instanceManager;
|
117
|
}
|
118
|
|
119
|
private File cdmRemoteWebAppFile = null;
|
120
|
private File defaultWebAppFile = null;
|
121
|
|
122
|
private String logPath = null;
|
123
|
|
124
|
private String webAppClassPath = null;
|
125
|
|
126
|
private Server server = null;
|
127
|
private final ContextHandlerCollection contexts = new ContextHandlerCollection();
|
128
|
|
129
|
private CommandLine cmdLine;
|
130
|
|
131
|
/* thread save singleton implementation */
|
132
|
|
133
|
private static Bootloader bootloader = new Bootloader();
|
134
|
|
135
|
private Bootloader() {}
|
136
|
|
137
|
/**
|
138
|
* @return the thread save singleton instance of the Bootloader
|
139
|
*/
|
140
|
public synchronized static Bootloader getBootloader(){
|
141
|
return bootloader;
|
142
|
}
|
143
|
|
144
|
/* end of singleton implementation */
|
145
|
|
146
|
|
147
|
public int writeStreamTo(final InputStream input, final OutputStream output, int bufferSize) throws IOException {
|
148
|
int available = Math.min(input.available(), 256 * KB);
|
149
|
byte[] buffer = new byte[Math.max(bufferSize, available)];
|
150
|
int answer = 0;
|
151
|
int count = input.read(buffer);
|
152
|
while (count >= 0) {
|
153
|
output.write(buffer, 0, count);
|
154
|
answer += count;
|
155
|
count = input.read(buffer);
|
156
|
}
|
157
|
return answer;
|
158
|
}
|
159
|
|
160
|
|
161
|
|
162
|
public void parseCommandOptions(String[] args) throws ParseException {
|
163
|
CommandLineParser parser = new GnuParser();
|
164
|
cmdLine = parser.parse( CommandOptions.getOptions(), args );
|
165
|
|
166
|
// print the help message
|
167
|
if(cmdLine.hasOption(HELP.getOpt())){
|
168
|
HelpFormatter formatter = new HelpFormatter();
|
169
|
formatter.setWidth(200);
|
170
|
formatter.printHelp( "java .. ", CommandOptions.getOptions() );
|
171
|
System.exit(0);
|
172
|
}
|
173
|
}
|
174
|
|
175
|
|
176
|
private File extractWar(String warName) throws IOException, FileNotFoundException {
|
177
|
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
178
|
String warFileName = warName + WAR_POSTFIX;
|
179
|
|
180
|
// 1. find in classpath
|
181
|
URL resource = classLoader.getResource(warFileName);
|
182
|
if (resource == null) {
|
183
|
logger.error("Could not find the " + warFileName + " on classpath!");
|
184
|
|
185
|
File pomxml = new File("pom.xml");
|
186
|
if(pomxml.exists()){
|
187
|
// 2. try finding in target folder of maven project
|
188
|
File warFile = new File("target" + File.separator + warFileName);
|
189
|
logger.debug("looging for war file at " + warFile.getAbsolutePath());
|
190
|
if (warFile.canRead()) {
|
191
|
resource = warFile.toURI().toURL();
|
192
|
} else {
|
193
|
logger.error("Also could not find the " + warFileName + " in maven project, try excuting 'mvn install'");
|
194
|
}
|
195
|
}
|
196
|
}
|
197
|
|
198
|
if (resource == null) {
|
199
|
// no way finding the war file :-(
|
200
|
System.exit(1);
|
201
|
}
|
202
|
|
203
|
|
204
|
File warFile = new File(TMP_PATH, warName + "-" + WAR_POSTFIX);
|
205
|
logger.info("Extracting " + warFileName + " to " + warFile + " ...");
|
206
|
|
207
|
writeStreamTo(resource.openStream(), new FileOutputStream(warFile), 8 * KB);
|
208
|
|
209
|
logger.info("Extracted " + warFileName);
|
210
|
return warFile;
|
211
|
}
|
212
|
|
213
|
|
214
|
/**
|
215
|
* MAIN METHOD
|
216
|
*
|
217
|
* @param args
|
218
|
* @throws Exception
|
219
|
*/
|
220
|
public static void main(String[] args) throws Exception {
|
221
|
|
222
|
Bootloader bootloader = Bootloader.getBootloader();
|
223
|
|
224
|
bootloader.parseCommandOptions(args);
|
225
|
|
226
|
bootloader.startServer();
|
227
|
}
|
228
|
|
229
|
|
230
|
|
231
|
public void startServer() throws IOException,
|
232
|
FileNotFoundException, Exception, InterruptedException {
|
233
|
|
234
|
|
235
|
if(cmdLine.hasOption(LOG_DIR.getOpt())){
|
236
|
logPath = cmdLine.getOptionValue(LOG_DIR.getOpt());
|
237
|
} else {
|
238
|
logPath = LOG_PATH;
|
239
|
}
|
240
|
|
241
|
|
242
|
//assure LOG_PATH exists
|
243
|
File logPathFile = new File(logPath);
|
244
|
if(!logPathFile.exists()){
|
245
|
FileUtils.forceMkdir(new File(logPath));
|
246
|
}
|
247
|
|
248
|
//append logger
|
249
|
configureFileLogger();
|
250
|
|
251
|
logger.info("Starting "+APPLICATION_NAME);
|
252
|
logger.info("Using " + System.getProperty("user.home") + " as home directory. Can be specified by -Duser.home=<FOLDER>");
|
253
|
|
254
|
//assure TMP_PATH exists and clean it up
|
255
|
File tempDir = new File(TMP_PATH);
|
256
|
if(!tempDir.exists() && !tempDir.mkdirs()){
|
257
|
logger.error("Error creating temporary directory for webapplications " + tempDir.getAbsolutePath());
|
258
|
System.exit(-1);
|
259
|
} else {
|
260
|
if(FileUtils.deleteQuietly(tempDir)){
|
261
|
tempDir.mkdirs();
|
262
|
logger.info("Old webapplications successfully cleared");
|
263
|
}
|
264
|
}
|
265
|
tempDir = null;
|
266
|
|
267
|
|
268
|
// WEBAPP options
|
269
|
// prepare web application files to run either from war files (production mode)
|
270
|
// or from source (development mode)
|
271
|
if(cmdLine.hasOption(WEBAPP.getOpt())){
|
272
|
|
273
|
cdmRemoteWebAppFile = new File(cmdLine.getOptionValue(WEBAPP.getOpt()));
|
274
|
if(cdmRemoteWebAppFile.isDirectory()){
|
275
|
logger.info("using user defined web application folder: " + cdmRemoteWebAppFile.getAbsolutePath());
|
276
|
} else {
|
277
|
logger.info("using user defined warfile: " + cdmRemoteWebAppFile.getAbsolutePath());
|
278
|
}
|
279
|
|
280
|
// load the default-web-application from source if running in development mode mode
|
281
|
if(isRunningFromCdmRemoteWebAppSource()){
|
282
|
//TODO check if all local paths are valid !!!!
|
283
|
defaultWebAppFile = new File("./src/main/webapp");
|
284
|
} else {
|
285
|
defaultWebAppFile = extractWar(DEFAULT_WEBAPP_WAR_NAME);
|
286
|
}
|
287
|
|
288
|
if(isRunningFromCdmRemoteWebAppSource()){
|
289
|
if(cmdLine.hasOption(WEBAPP_CLASSPATH.getOpt())){
|
290
|
String classPathOption = cmdLine.getOptionValue(WEBAPP_CLASSPATH.getOpt());
|
291
|
normalizeClasspath(classPathOption);
|
292
|
// webAppClassPath = classPathOption;
|
293
|
}
|
294
|
}
|
295
|
} else {
|
296
|
// read version number
|
297
|
String version = readCdmRemoteVersion();
|
298
|
cdmRemoteWebAppFile = extractWar(CDMLIB_REMOTE_WEBAPP + "-" + version);
|
299
|
defaultWebAppFile = extractWar(DEFAULT_WEBAPP_WAR_NAME);
|
300
|
}
|
301
|
|
302
|
|
303
|
// HTTP Port
|
304
|
int httpPort = 8080;
|
305
|
if(cmdLine.hasOption(HTTP_PORT.getOpt())){
|
306
|
try {
|
307
|
httpPort = Integer.parseInt(cmdLine.getOptionValue(HTTP_PORT.getOpt()));
|
308
|
logger.info(HTTP_PORT.getOpt()+" set to "+cmdLine.getOptionValue(HTTP_PORT.getOpt()));
|
309
|
} catch (NumberFormatException e) {
|
310
|
logger.error("Supplied portnumber is not an integer");
|
311
|
System.exit(-1);
|
312
|
}
|
313
|
}
|
314
|
|
315
|
if(cmdLine.hasOption(DATASOURCES_FILE.getOpt())){
|
316
|
logger.error(DATASOURCES_FILE.getOpt() + " NOT JET IMPLEMENTED!!!");
|
317
|
}
|
318
|
|
319
|
verifySystemResources();
|
320
|
|
321
|
// load the configured instances for the first time
|
322
|
instanceManager.reLoadInstanceConfigurations();
|
323
|
|
324
|
server = new Server(httpPort);
|
325
|
server.addLifeCycleListener(instanceManager);
|
326
|
|
327
|
// JMX support
|
328
|
if(cmdLine.hasOption(JMX.getOpt())){
|
329
|
logger.info("adding JMX support ...");
|
330
|
MBeanContainer mBeanContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
|
331
|
server.getContainer().addEventListener(mBeanContainer);
|
332
|
mBeanContainer.addBean(Log.getLog());
|
333
|
mBeanContainer.start();
|
334
|
}
|
335
|
|
336
|
if(cmdLine.hasOption(WIN32SERVICE.getOpt())){
|
337
|
Win32Service win32Service = new Win32Service();
|
338
|
win32Service.setServer(server);
|
339
|
server.setStopAtShutdown(true);
|
340
|
server.addBean(win32Service);
|
341
|
}
|
342
|
|
343
|
// add default servlet context
|
344
|
logger.info("preparing default WebAppContext");
|
345
|
WebAppContext defaultWebappContext = new WebAppContext();
|
346
|
|
347
|
setWebApp(defaultWebappContext, defaultWebAppFile);
|
348
|
defaultWebappContext.setContextPath("/");
|
349
|
defaultWebappContext.setTempDirectory(DEFAULT_WEBAPP_TEMP_FOLDER);
|
350
|
|
351
|
// configure security context
|
352
|
// see for reference * http://docs.codehaus.org/display/JETTY/Realms
|
353
|
// * http://wiki.eclipse.org/Jetty/Starting/Porting_to_Jetty_7
|
354
|
HashLoginService loginService = new HashLoginService();
|
355
|
loginService.setConfig(USERHOME_CDM_LIBRARY_PATH + REALM_PROPERTIES_FILE);
|
356
|
defaultWebappContext.getSecurityHandler().setLoginService(loginService);
|
357
|
|
358
|
// Important:
|
359
|
// the defaultWebappContext MUST USE the super classloader
|
360
|
// otherwise the status page (index.jsp) might not work
|
361
|
defaultWebappContext.setClassLoader(this.getClass().getClassLoader());
|
362
|
contexts.addHandler(defaultWebappContext);
|
363
|
|
364
|
logger.info("setting contexts ...");
|
365
|
server.setHandler(contexts);
|
366
|
logger.info("starting jetty ...");
|
367
|
// try {
|
368
|
|
369
|
server.start();
|
370
|
|
371
|
// } catch(org.springframework.beans.BeansException e){
|
372
|
// Throwable rootCause = null;
|
373
|
// while(e.getCause() != null){
|
374
|
// rootCause = e.getCause();
|
375
|
// }
|
376
|
// if(rootCause != null && rootCause.getClass().getSimpleName().equals("InvalidCdmVersionException")){
|
377
|
//
|
378
|
// logger.error("rootCause ----------->" + rootCause.getMessage());
|
379
|
//// for(CdmInstanceProperties props : configAndStatus){
|
380
|
//// if(props.getDataSourceName())
|
381
|
//// }
|
382
|
// }
|
383
|
// }
|
384
|
|
385
|
if(cmdLine.hasOption(WIN32SERVICE.getOpt())){
|
386
|
logger.info("jetty has started as win32 service");
|
387
|
} else {
|
388
|
server.join();
|
389
|
logger.info(APPLICATION_NAME+" stopped.");
|
390
|
System.exit(0);
|
391
|
}
|
392
|
}
|
393
|
|
394
|
/**
|
395
|
* @param classpath
|
396
|
*/
|
397
|
private void normalizeClasspath(String classpath) {
|
398
|
StringBuilder classPathBuilder = new StringBuilder((int) (classpath.length() * 1.2));
|
399
|
String[] cpEntries = classpath.split("[\\:]");
|
400
|
for(String cpEntry : cpEntries){
|
401
|
classPathBuilder.append(',');
|
402
|
// if(cpEntry.endsWith(".jar")){
|
403
|
// classPathBuilder.append("jar:");
|
404
|
// }
|
405
|
classPathBuilder.append(cpEntry);
|
406
|
}
|
407
|
webAppClassPath = classPathBuilder.toString();
|
408
|
}
|
409
|
|
410
|
public String readCdmRemoteVersion() throws IOException {
|
411
|
String version = "cdmlib version unreadable";
|
412
|
InputStream versionInStream = Bootloader.class.getClassLoader().getResourceAsStream(VERSION_PROPERTIES_FILE);
|
413
|
if (versionInStream != null){
|
414
|
Properties versionProperties = new Properties();
|
415
|
versionProperties.load(versionInStream);
|
416
|
version = versionProperties.getProperty(CDMLIB_REMOTE_WEBAPP_VERSION, version);
|
417
|
}
|
418
|
return version;
|
419
|
}
|
420
|
|
421
|
|
422
|
|
423
|
private void verifySystemResources() {
|
424
|
|
425
|
OsChecker osChecker = new OsChecker();
|
426
|
if(osChecker.isLinux()){
|
427
|
try {
|
428
|
Process p = Runtime.getRuntime().exec(new String[] { "bash", "-c", "ulimit -n" });
|
429
|
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
430
|
String line;
|
431
|
StringBuilder response = new StringBuilder();
|
432
|
while ((line = in.readLine()) != null) {
|
433
|
response.append(line);
|
434
|
}
|
435
|
logger.info("OS Limit (Linux): maximum number of open files: " + response);
|
436
|
} catch (IOException e) {
|
437
|
// TODO Auto-generated catch block
|
438
|
e.printStackTrace();
|
439
|
}
|
440
|
} else {
|
441
|
logger.info("verifySystemResources only implemented for linux");
|
442
|
}
|
443
|
}
|
444
|
|
445
|
|
446
|
/**
|
447
|
* Configures and adds a {@link RollingFileAppender} to the root logger
|
448
|
*
|
449
|
* The log files of the cdm-remote instances are configured by the
|
450
|
* {@link eu.etaxonomy.cdm.remote.config.LoggingConfigurer}
|
451
|
*/
|
452
|
private void configureFileLogger() {
|
453
|
|
454
|
PatternLayout layout = new PatternLayout("%d %p [%c] - %m%n");
|
455
|
try {
|
456
|
String logFile = logPath + File.separator + "cdmserver.log";
|
457
|
RollingFileAppender appender = new RollingFileAppender(layout, logFile);
|
458
|
appender.setMaxBackupIndex(3);
|
459
|
appender.setMaxFileSize("2MB");
|
460
|
Logger.getRootLogger().addAppender(appender);
|
461
|
logger.info("logging to :" + logFile);
|
462
|
} catch (IOException e) {
|
463
|
logger.error("Creating RollingFileAppender failed:", e);
|
464
|
}
|
465
|
}
|
466
|
|
467
|
|
468
|
/**
|
469
|
* Adds a new WebAppContext to the contexts of the running jetty instance.
|
470
|
* <ol>
|
471
|
* <li>Initialize WebAppContext:
|
472
|
* <ol>
|
473
|
* <li>set context path</li>
|
474
|
* <li>set tmp directory</li>
|
475
|
* <li>bind JndiDataSource</li>
|
476
|
* <li>set web app context attributes</li>
|
477
|
* <li>create and setup individual classloader for the instance</li>
|
478
|
* </ol>
|
479
|
* </li>
|
480
|
* <li>
|
481
|
* finally add the new webappcontext to the contexts of the jetty instance</li>
|
482
|
* </ol>
|
483
|
*
|
484
|
* @param instance
|
485
|
* @return the instance given as parameter of null in case the instance has
|
486
|
* {@link Status.#disabled} or if it is already added.
|
487
|
* @throws IOException
|
488
|
*/
|
489
|
public WebAppContext addCdmInstanceContext(CdmInstance instance) throws IOException {
|
490
|
|
491
|
Configuration conf = instance.getConfiguration();
|
492
|
if(!instance.isEnabled()){
|
493
|
logger.info(conf.getInstanceName() + " is disabled, possibly due to JVM memory limitations");
|
494
|
return null;
|
495
|
}
|
496
|
if(getContextFor(conf) != null){
|
497
|
logger.info(conf.getInstanceName() + " is alreaddy added to the contexts - skipping");
|
498
|
return null;
|
499
|
}
|
500
|
|
501
|
instance.setStatus(Status.initializing);
|
502
|
logger.info("preparing WebAppContext for '"+ conf.getInstanceName() + "'");
|
503
|
WebAppContext cdmWebappContext = new WebAppContext();
|
504
|
|
505
|
cdmWebappContext.setContextPath(constructContextPath(conf));
|
506
|
cdmWebappContext.setTempDirectory(CDM_WEBAPP_TEMP_FOLDER);
|
507
|
|
508
|
if(!instance.bindJndiDataSource()){
|
509
|
// a problem with the datasource occurred skip this webapp
|
510
|
cdmWebappContext = null;
|
511
|
logger.error("a problem with the datasource occurred -> skipping /" + conf.getInstanceName());
|
512
|
instance.setStatus(Status.error);
|
513
|
return cdmWebappContext;
|
514
|
}
|
515
|
|
516
|
cdmWebappContext.setAttribute(SharedAttributes.ATTRIBUTE_DATASOURCE_NAME, conf.getInstanceName());
|
517
|
cdmWebappContext.setAttribute(SharedAttributes.ATTRIBUTE_JDBC_JNDI_NAME, conf.getJdbcJndiName());
|
518
|
setWebApp(cdmWebappContext, getCdmRemoteWebAppFile());
|
519
|
|
520
|
cdmWebappContext.setAttribute(SharedAttributes.ATTRIBUTE_CDM_LOGFILE,
|
521
|
logPath + File.separator + "cdm-"
|
522
|
+ conf.getInstanceName() + ".log");
|
523
|
|
524
|
if( getCdmRemoteWebAppFile().isDirectory() && isRunningFromCdmRemoteWebAppSource()){
|
525
|
|
526
|
/*
|
527
|
* when running the webapp from {projectpath} src/main/webapp we
|
528
|
* must assure that each web application is using it's own
|
529
|
* classloader thus we tell the WebAppClassLoader where the
|
530
|
* dependencies of the webapplication can be found. Otherwise
|
531
|
* the system classloader would load these resources.
|
532
|
*/
|
533
|
WebAppClassLoader classLoader = new WebAppClassLoader(cdmWebappContext);
|
534
|
// String classPath = System.getProperty("java.class.path");
|
535
|
// logger.info("Running cdmlib-remote-webapp from source folder:, thus adding system property 'java.class.path=" + classPath +"' to WebAppClassLoader");
|
536
|
// classLoader.addClassPath(classPath);
|
537
|
if(webAppClassPath != null){
|
538
|
logger.info("Running cdmlib-remote-webapp from source folder: Adding class path supplied by option '-" + WEBAPP_CLASSPATH.getOpt() +" =" + webAppClassPath +"' to WebAppClassLoader");
|
539
|
classLoader.addClassPath(webAppClassPath);
|
540
|
} else {
|
541
|
throw new RuntimeException("Classpath cdmlib-remote-webapp for missing while running cdmlib-remote-webapp from source folder. Please supplied cdm-server option '-" + WEBAPP_CLASSPATH.getOpt() +"");
|
542
|
}
|
543
|
cdmWebappContext.setClassLoader(classLoader);
|
544
|
}
|
545
|
|
546
|
contexts.addHandler(cdmWebappContext);
|
547
|
instance.setWebAppContext(cdmWebappContext);
|
548
|
cdmWebappContext.addLifeCycleListener(instance);
|
549
|
instance.setStatus(Status.stopped);
|
550
|
|
551
|
return cdmWebappContext;
|
552
|
}
|
553
|
|
554
|
/**
|
555
|
* @param conf
|
556
|
* @return
|
557
|
*/
|
558
|
private String constructContextPath(Configuration conf) {
|
559
|
return "/" + conf.getInstanceName();
|
560
|
}
|
561
|
|
562
|
/**
|
563
|
* Removes the given instance from the contexts. If the instance is running
|
564
|
* at the time of calling this method it will be stopped first.
|
565
|
* The JndiDataSource and the webapplicationContext will be released and removed.
|
566
|
*
|
567
|
* @param instance the instance to be removed
|
568
|
*
|
569
|
* @throws Exception in case stopping the instance fails
|
570
|
*/
|
571
|
public void removeCdmInstanceContext(CdmInstance instance) throws Exception {
|
572
|
|
573
|
if(instance.getWebAppContext() != null){
|
574
|
if(instance.getWebAppContext().isRunning()){
|
575
|
try {
|
576
|
instance.getWebAppContext().stop();
|
577
|
} catch (Exception e) {
|
578
|
instance.getProblems().add("Error while stopping instance: " + e.getMessage());
|
579
|
throw e;
|
580
|
}
|
581
|
}
|
582
|
contexts.removeHandler(instance.getWebAppContext());
|
583
|
instance.releaseWebAppContext();
|
584
|
} else {
|
585
|
// maybe something went wrong before, try to find the potentially lost
|
586
|
// contexts directly in the server
|
587
|
ContextHandler handler = getContextFor(instance.getConfiguration());
|
588
|
if(handler != null){
|
589
|
contexts.removeHandler(handler);
|
590
|
}
|
591
|
}
|
592
|
instance.unbindJndiDataSource();
|
593
|
}
|
594
|
|
595
|
/**
|
596
|
* Sets the webapp specified by the <code>webApplicationResource</code> to
|
597
|
* the given <code>context</code>.
|
598
|
*
|
599
|
* @param context
|
600
|
* @param webApplicationResource the resource can either be a directory containing
|
601
|
* a Java web application or *.war file.
|
602
|
*
|
603
|
*/
|
604
|
private void setWebApp(WebAppContext context, File webApplicationResource) {
|
605
|
if(webApplicationResource.isDirectory()){
|
606
|
context.setResourceBase(webApplicationResource.getAbsolutePath());
|
607
|
logger.debug("setting directory " + webApplicationResource.getAbsolutePath() + " as webapplication");
|
608
|
} else {
|
609
|
context.setWar(webApplicationResource.getAbsolutePath());
|
610
|
logger.debug("setting war file " + webApplicationResource.getAbsolutePath() + " as webapplication");
|
611
|
}
|
612
|
}
|
613
|
|
614
|
/**
|
615
|
* @return
|
616
|
*/
|
617
|
private boolean isRunningFromCdmRemoteWebAppSource() {
|
618
|
String webappPathNormalized = cdmRemoteWebAppFile.getAbsolutePath().replace('\\', '/');
|
619
|
return webappPathNormalized.endsWith("src/main/webapp") || webappPathNormalized.endsWith("cdmlib-remote-webapp/target/cdmserver");
|
620
|
}
|
621
|
|
622
|
|
623
|
public Server getServer() {
|
624
|
return server;
|
625
|
}
|
626
|
|
627
|
public ContextHandler getContextFor(Configuration conf) {
|
628
|
return getContextFor(constructContextPath(conf));
|
629
|
}
|
630
|
|
631
|
public ContextHandler getContextFor(String contextPath) {
|
632
|
for( Handler handler : contexts.getHandlers()){
|
633
|
if(handler instanceof ContextHandler){
|
634
|
if(((ContextHandler)handler).getContextPath().equals(contextPath)){
|
635
|
return (ContextHandler)handler;
|
636
|
}
|
637
|
}
|
638
|
}
|
639
|
return null;
|
640
|
}
|
641
|
|
642
|
public ContextHandlerCollection getContexts() {
|
643
|
return contexts;
|
644
|
}
|
645
|
|
646
|
/**
|
647
|
* @return a File object pointing to the location of the cdmlib-remote-webapp
|
648
|
*/
|
649
|
public File getCdmRemoteWebAppFile(){
|
650
|
if(cdmRemoteWebAppFile == null){
|
651
|
throw new RuntimeException("Invalid order of action. Server not yet started. The startServer() method must be called first. ");
|
652
|
}
|
653
|
return cdmRemoteWebAppFile;
|
654
|
}
|
655
|
}
|