-
Notifications
You must be signed in to change notification settings - Fork 138
/
ApplicationLoaderService.java
608 lines (517 loc) · 26.1 KB
/
ApplicationLoaderService.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation.
* Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.enterprise.v3.server;
import com.sun.enterprise.config.serverbeans.AppTenant;
import com.sun.enterprise.config.serverbeans.Application;
import com.sun.enterprise.config.serverbeans.ApplicationRef;
import com.sun.enterprise.config.serverbeans.Applications;
import com.sun.enterprise.config.serverbeans.Domain;
import com.sun.enterprise.config.serverbeans.Server;
import com.sun.enterprise.config.serverbeans.ServerTags;
import com.sun.enterprise.config.serverbeans.SystemApplications;
import com.sun.enterprise.deploy.shared.ArchiveFactory;
import com.sun.enterprise.util.io.FileUtils;
import com.sun.enterprise.v3.common.HTMLActionReporter;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Provider;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.api.ActionReport;
import org.glassfish.api.StartupRunLevel;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.api.deployment.DeployCommandParameters;
import org.glassfish.api.deployment.UndeployCommandParameters;
import org.glassfish.api.deployment.archive.ArchiveHandler;
import org.glassfish.api.deployment.archive.ReadableArchive;
import org.glassfish.api.deployment.archive.WritableArchive;
import org.glassfish.api.event.EventListener.Event;
import org.glassfish.api.event.EventTypes;
import org.glassfish.api.event.Events;
import org.glassfish.deployment.common.ApplicationConfigInfo;
import org.glassfish.deployment.common.DeploymentContextImpl;
import org.glassfish.deployment.common.DeploymentUtils;
import org.glassfish.deployment.common.InstalledLibrariesResolver;
import org.glassfish.deployment.monitor.DeploymentLifecycleStatsProvider;
import org.glassfish.external.probe.provider.PluginPoint;
import org.glassfish.external.probe.provider.StatsProviderManager;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.runlevel.RunLevel;
import org.glassfish.hk2.utilities.BuilderHelper;
import org.glassfish.internal.data.ApplicationInfo;
import org.glassfish.internal.data.ApplicationRegistry;
import org.glassfish.internal.data.ContainerRegistry;
import org.glassfish.internal.data.EngineInfo;
import org.glassfish.internal.deployment.ApplicationOrderInfo;
import org.glassfish.internal.deployment.Deployment;
import org.glassfish.internal.deployment.DeploymentOrder;
import org.glassfish.internal.deployment.DeploymentTracing;
import org.glassfish.internal.deployment.ExtendedDeploymentContext;
import org.glassfish.internal.deployment.SnifferManager;
import org.glassfish.kernel.KernelLoggerInfo;
import org.glassfish.security.services.impl.AuthenticationServiceImpl;
import org.jvnet.hk2.annotations.Optional;
import org.jvnet.hk2.annotations.Service;
/**
* This service is responsible for loading all deployed applications...
*
* @author Jerome Dochez
*/
//@Priority(8) // low priority , should be started last
@Service(name="ApplicationLoaderService")
@RunLevel( value=StartupRunLevel.VAL, mode=RunLevel.RUNLEVEL_MODE_NON_VALIDATING)
public class ApplicationLoaderService implements org.glassfish.hk2.api.PreDestroy, org.glassfish.hk2.api.PostConstruct {
//public class ApplicationLoaderService implements Startup, org.glassfish.hk2.api.PreDestroy, org.glassfish.hk2.api.PostConstruct {
final Logger logger = KernelLoggerInfo.getLogger();
// During the authentication service's PostConstruct the javax.security.auth.login.Configuration class is constructed.
// During the Configuration initialization a static variable is set to the current thread's context class loader.
// When applications are loaded via this (ApplicationLoaderService) the current thread's context class loader
// is temporarily set. When an application is loaded it will access the authentication service. If the
// authentication service gets initialized at this time then the Configuration construction will happen and its
// static variable will be set to the thread's temporarily set context class loader. Therefore by making
// a reference to the authentication service here we guarantee that it will be created before this
// (ApplicationLoaderService) service is created.
@Inject @Optional
private AuthenticationServiceImpl authenticationService;
@Inject
Deployment deployment;
@Inject
Provider<ArchiveFactory> archiveFactoryProvider;
@Inject
SnifferManager snifferManager;
@Inject
ContainerRegistry containerRegistry;
@Inject
ApplicationRegistry appRegistry;
@Inject
Events events;
@Inject
protected Applications applications;
protected SystemApplications systemApplications;
protected Domain domain;
@Inject @Named( ServerEnvironment.DEFAULT_INSTANCE_NAME)
Server server;
@Inject
ServerEnvironment env;
@Inject
ServiceLocator habitat;
private String deploymentTracingEnabled = null;
private final Map<String,Integer> appOrderInfoMap = new HashMap<>();
private int appOrder = 0;
/**
* Starts the application loader service.
*
* Look at the list of applications installed in our local repository
* Get a Deployer capable for each application found
* Invoke the deployer load() method for each application.
*/
@Override
public void postConstruct() {
assert env!=null;
try{
logger.fine("Satisfying Optional Packages dependencies...");
InstalledLibrariesResolver.initializeInstalledLibRegistry(env.getLibPath().getAbsolutePath());
}catch(Exception e){
logger.log(Level.WARNING, KernelLoggerInfo.exceptionOptionalDepend, e);
}
DeploymentLifecycleStatsProvider dlsp = new DeploymentLifecycleStatsProvider();
StatsProviderManager.register("deployment", PluginPoint.SERVER,
"deployment/lifecycle", dlsp);
deploymentTracingEnabled = System.getProperty(
"org.glassfish.deployment.trace");
domain = habitat.getService(Domain.class);
/*
* Build a map that associates an application with its
* order in domain.xml. If the deployment-order attribute
* is not used for any application, then the applications
* are loaded in the order they occur in domain.xml. Also, for
* applications with the same deployment-order attribute,
* the applications are loaded in the order they occur in domain.xml.
* Otherwise, applications are loaded according to the
* deploynment-order attribute.
*/
systemApplications = domain.getSystemApplications();
for (Application systemApp : systemApplications.getApplications()) {
appOrderInfoMap.put(systemApp.getName(), Integer.valueOf(appOrder++));
}
List<Application> standaloneAdapters =
applications.getApplicationsWithSnifferType(ServerTags.CONNECTOR, true);
for (Application standaloneAdapter : standaloneAdapters) {
appOrderInfoMap.put(standaloneAdapter.getName(), Integer.valueOf(appOrder++));
}
List<Application> allApplications = applications.getApplications();
for (Application app : allApplications) {
appOrderInfoMap.put(app.getName(), Integer.valueOf(appOrder++));
}
for (Application systemApp : systemApplications.getApplications()) {
// check to see if we need to load up this system application
if (Boolean.parseBoolean(systemApp.getDeployProperties().getProperty
(ServerTags.LOAD_SYSTEM_APP_ON_STARTUP))) {
if (deployment.isAppEnabled(systemApp) || loadAppOnDAS(systemApp.getName())) {
Integer order = appOrderInfoMap.get(systemApp.getName());
ApplicationOrderInfo info = new ApplicationOrderInfo(systemApp, order);
DeploymentOrder.addApplicationDeployment(info);
}
}
}
// load standalone resource adapters first
for (Application standaloneAdapter : standaloneAdapters) {
// load the referenced enabled applications on this instance
// and always (partially) load on DAS when application is
// referenced by non-DAS target so the application
// information is available on DAS
if (deployment.isAppEnabled(standaloneAdapter) || loadAppOnDAS(standaloneAdapter.getName())) {
DeploymentOrder.addApplicationDeployment(new ApplicationOrderInfo(standaloneAdapter, appOrderInfoMap.get(standaloneAdapter.getName()).intValue()));
}
}
// then the rest of the applications
for (Application app : allApplications) {
if (app.isStandaloneModule() &&
app.containsSnifferType(ServerTags.CONNECTOR)) {
continue;
}
// load the referenced enabled applications on this instance
// and always (partially) load on DAS when application is
// referenced by non-DAS target so the application
// information is available on DAS
if (deployment.isAppEnabled(app) || loadAppOnDAS(app.getName())) {
DeploymentOrder.addApplicationDeployment(new ApplicationOrderInfo(app, appOrderInfoMap.get(app.getName()).intValue()));
}
}
Iterator iter = DeploymentOrder.getApplicationDeployments();
while (iter.hasNext()) {
Application app = (Application)iter.next();
ApplicationRef appRef = server.getApplicationRef(app.getName());
processApplication(app, appRef);
}
// does the user want us to run a particular application
String defaultParam = env.getStartupContext().getArguments().getProperty("default");
if (defaultParam!=null) {
initializeRuntimeDependencies();
File sourceFile;
if (defaultParam.equals(".")) {
sourceFile = new File(System.getProperty("user.dir"));
} else {
sourceFile = new File(defaultParam);
}
if (sourceFile.exists()) {
sourceFile = sourceFile.getAbsoluteFile();
ReadableArchive sourceArchive=null;
try {
sourceArchive = archiveFactoryProvider.get().openArchive(sourceFile);
DeployCommandParameters parameters = new DeployCommandParameters(sourceFile);
parameters.name = sourceFile.getName();
parameters.enabled = Boolean.TRUE;
parameters.origin = DeployCommandParameters.Origin.deploy;
ActionReport report = new HTMLActionReporter();
if (!sourceFile.isDirectory()) {
// ok we need to explode the directory somwhere and remember to delete it on shutdown
final File tmpFile = File.createTempFile(sourceFile.getName(),"");
final String path = tmpFile.getAbsolutePath();
if (!tmpFile.delete()) {
logger.log(Level.WARNING, KernelLoggerInfo.cantDeleteTempFile, path);
}
File tmpDir = new File(path);
tmpDir.deleteOnExit();
events.register(event -> {
if (event.is(EventTypes.SERVER_SHUTDOWN)) {
if (tmpFile.exists()) {
FileUtils.whack(tmpFile);
}
}
});
if (tmpDir.mkdirs()) {
ArchiveHandler handler = deployment.getArchiveHandler(sourceArchive);
final String appName = handler.getDefaultApplicationName(sourceArchive);
DeploymentContextImpl dummyContext = new DeploymentContextImpl(report, logger, sourceArchive, parameters, env);
try (WritableArchive output = archiveFactoryProvider.get().createArchive(tmpDir)) {
handler.expand(sourceArchive, output, dummyContext);
}
sourceArchive = archiveFactoryProvider.get().openArchive(tmpDir);
logger.log(Level.INFO, KernelLoggerInfo.sourceNotDirectory, tmpDir.getAbsolutePath());
parameters.name = appName;
}
}
ExtendedDeploymentContext depContext = deployment.getBuilder(logger, parameters, report).source(sourceArchive).build();
ApplicationInfo appInfo = deployment.deploy(depContext);
if (appInfo==null) {
logger.log(Level.SEVERE, KernelLoggerInfo.cantFindApplicationInfo, sourceFile.getAbsolutePath());
}
} catch(RuntimeException e) {
logger.log(Level.SEVERE, KernelLoggerInfo.deployException, e);
} catch(IOException ioe) {
logger.log(Level.SEVERE, KernelLoggerInfo.deployException, ioe);
} finally {
if (sourceArchive!=null) {
try {
sourceArchive.close();
} catch (IOException ioe) {
// ignore
}
}
}
}
}
events.send(new Event<>(Deployment.ALL_APPLICATIONS_PROCESSED, null));
}
private void initializeRuntimeDependencies() {
// ApplicationLoaderService needs to be initialized after
// ManagedBeanManagerImpl. By injecting ManagedBeanManagerImpl,
// we guarantee the initialization order.
habitat.getAllServices(BuilderHelper.createNameFilter("ManagedBeanManagerImpl"));
// ApplicationLoaderService needs to be initialized after
// ResourceManager. By injecting ResourceManager, we guarantee the
// initialization order.
// See https://glassfish.dev.java.net/issues/show_bug.cgi?id=7179
habitat.getAllServices(BuilderHelper.createNameFilter("ResourceManager"));
// Application scoped resource is loaded after ResourceManager
// http://java.net/jira/browse/GLASSFISH-19161
habitat.getAllServices(BuilderHelper.createNameFilter("ApplicationScopedResourcesManager"));
}
public void processApplication(Application app, ApplicationRef appRef) {
long operationStartTime = Calendar.getInstance().getTimeInMillis();
initializeRuntimeDependencies();
String source = app.getLocation();
final String appName = app.getName();
// lifecycle modules are loaded separately
if (Boolean.parseBoolean(app.getDeployProperties().getProperty
(ServerTags.IS_LIFECYCLE))) {
return;
}
URI uri;
try {
uri = new URI(source);
} catch (URISyntaxException e) {
logger.log(Level.SEVERE, KernelLoggerInfo.cantDetermineLocation, e.getLocalizedMessage());
return;
}
File sourceFile = new File(uri);
if (sourceFile.exists()) {
try {
ReadableArchive archive = null;
try {
DeploymentTracing tracing = null;
if (deploymentTracingEnabled != null) {
tracing = new DeploymentTracing();
}
DeployCommandParameters deploymentParams =
app.getDeployParameters(appRef);
deploymentParams.target = server.getName();
deploymentParams.origin = DeployCommandParameters.Origin.load;
deploymentParams.command = DeployCommandParameters.Command.startup_server;
if (domain.isAppReferencedByPaaSTarget(appName)) {
if (server.isDas()) {
// for loading PaaS application on DAS
// we set it to the real PaaS target
deploymentParams.target = deployment.getDefaultTarget(appName, deploymentParams.origin, deploymentParams._classicstyle);
}
}
archive = archiveFactoryProvider.get().openArchive(sourceFile, deploymentParams);
ActionReport report = new HTMLActionReporter();
ExtendedDeploymentContext depContext = deployment.getBuilder(logger, deploymentParams, report).source(archive).build();
if (tracing!=null) {
depContext.addModuleMetaData(tracing);
}
depContext.getAppProps().putAll(app.getDeployProperties());
depContext.setModulePropsMap(app.getModulePropertiesMap());
new ApplicationConfigInfo(app).store(depContext.getAppProps());
deployment.deploy(deployment.getSniffersFromApp(app), depContext);
loadApplicationForTenants(app, appRef, report);
if (report.getActionExitCode().equals(ActionReport.ExitCode.SUCCESS)) {
if (tracing!=null) {
tracing.print(System.out);
}
logger.log(Level.INFO, KernelLoggerInfo.loadingApplicationTime, new Object[] {
appName, (Calendar.getInstance().getTimeInMillis() - operationStartTime)});
} else {
logger.log(Level.SEVERE, KernelLoggerInfo.deployFail, report.getMessage());
}
} finally {
if (archive!=null) {
try {
archive.close();
} catch(IOException e) {
logger.log(Level.FINE, KernelLoggerInfo.deployException, e);
}
}
}
} catch (IOException e) {
logger.log(Level.SEVERE, KernelLoggerInfo.exceptionOpenArtifact, e);
}
} else {
logger.log(Level.SEVERE, KernelLoggerInfo.notFoundInOriginalLocation, source);
}
}
@Override
public String toString() {
return "Application Loader";
}
/**
* Stopped all loaded applications
*/
@Override
public void preDestroy() {
// stop all running applications including user and system applications
// which are registered in the domain.xml
List<Application> allApplications = new ArrayList<>();
List<Application> standaloneAdapters =
applications.getApplicationsWithSnifferType(ServerTags.CONNECTOR, true);
allApplications.addAll(applications.getApplications());
allApplications.addAll(systemApplications.getApplications());
//stop applications that are not of type "standalone" connectors
for (Application app : allApplications) {
if (app.isStandaloneModule() &&
app.containsSnifferType(ServerTags.CONNECTOR)) {
continue;
}
ApplicationInfo appInfo = deployment.get(app.getName());
stopApplication(app, appInfo);
}
//stop applications that are "standalone" connectors
for (Application app : standaloneAdapters) {
ApplicationInfo appInfo = deployment.get(app.getName());
stopApplication(app, appInfo);
}
// now stop the applications which are not registered in the
// domain.xml like timer service application
Set<String> allAppNames = new HashSet<>();
allAppNames.addAll(appRegistry.getAllApplicationNames());
for (String appName : allAppNames) {
ApplicationInfo appInfo = appRegistry.get(appName);
stopApplication(null, appInfo);
}
// stop all the containers
for (EngineInfo engineInfo : containerRegistry.getContainers()) {
engineInfo.stop(logger);
}
}
private void stopApplication(Application app, ApplicationInfo appInfo) {
final ActionReport dummy = new HTMLActionReporter();
if (appInfo!=null) {
UndeployCommandParameters parameters = new UndeployCommandParameters(appInfo.getName());
parameters.origin = UndeployCommandParameters.Origin.unload;
parameters.command = UndeployCommandParameters.Command.shutdown_server;
try {
deployment.disable(parameters, app, appInfo, dummy, logger);
} catch (Exception e) {
logger.log(Level.SEVERE, KernelLoggerInfo.loadingApplicationErrorDisable, e);
}
unloadApplicationForTenants(app, dummy);
appRegistry.remove(appInfo.getName());
}
}
private void unloadApplicationForTenants(Application app, ActionReport report) {
if (app == null || app.getAppTenants() == null) {
return;
}
for (AppTenant tenant : app.getAppTenants().getAppTenant()) {
UndeployCommandParameters parameters = new UndeployCommandParameters();
parameters.name = DeploymentUtils.getInternalNameForTenant(app.getName(), tenant.getTenant());
parameters.origin = UndeployCommandParameters.Origin.unload;
parameters.target = server.getName();
ApplicationInfo appInfo = deployment.get(parameters.name);
if (appInfo == null) {
continue;
}
ActionReport subReport = report.addSubActionsReport();
try {
ExtendedDeploymentContext deploymentContext = deployment.getBuilder(KernelLoggerInfo.getLogger(), parameters, subReport).source(appInfo.getSource()).build();
deploymentContext.getAppProps().putAll(
app.getDeployProperties());
deploymentContext.getAppProps().putAll(
tenant.getDeployProperties());
deploymentContext.setModulePropsMap(
app.getModulePropertiesMap());
deploymentContext.setTenant(tenant.getTenant(), app.getName());
deployment.unload(appInfo, deploymentContext);
} catch(Throwable e) {
subReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
subReport.setMessage(e.getMessage());
subReport.setFailureCause(e);
}
appRegistry.remove(appInfo.getName());
}
}
private void loadApplicationForTenants(Application app, ApplicationRef appRef, ActionReport report) {
if (app.getAppTenants() == null) {
return;
}
for (AppTenant tenant : app.getAppTenants().getAppTenant()) {
DeployCommandParameters commandParams = app.getDeployParameters(appRef);
commandParams.contextroot = tenant.getContextRoot();
commandParams.target = server.getName();
commandParams.name = DeploymentUtils.getInternalNameForTenant(app.getName(), tenant.getTenant());
commandParams.enabled = Boolean.TRUE;
commandParams.origin = DeployCommandParameters.Origin.load;
ActionReport subReport = report.addSubActionsReport();
ReadableArchive archive = null;
try {
URI uri = new URI(app.getLocation());
File file = new File(uri);
if (file.exists()) {
archive = archiveFactoryProvider.get().openArchive(file);
ExtendedDeploymentContext deploymentContext =
deployment.getBuilder(KernelLoggerInfo.getLogger(), commandParams, subReport).source(archive).build();
deploymentContext.getAppProps().putAll(app.getDeployProperties());
deploymentContext.getAppProps().putAll(tenant.getDeployProperties());
deploymentContext.setModulePropsMap(app.getModulePropertiesMap());
deploymentContext.setTenant(tenant.getTenant(), app.getName());
deployment.deploy(deployment.getSniffersFromApp(app), deploymentContext);
} else {
logger.log(Level.SEVERE, KernelLoggerInfo.notFoundInOriginalLocation, app.getLocation());
}
} catch(Throwable e) {
subReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
subReport.setMessage(e.getMessage());
subReport.setFailureCause(e);
} finally {
try {
if (archive != null) {
archive.close();
}
} catch(IOException e) {
// ignore
}
}
}
}
private boolean loadAppOnDAS(String appName) {
if (server.isDas()) {
List<String> targets = domain.getAllReferencedTargetsForApplication(appName);
for (String target : targets) {
if (!DeploymentUtils.isDASTarget(target)) {
// if application is referenced by any non-DAS target
// we need to partially load it on DAS
return true;
}
}
}
return false;
}
}