/
WeldBootstrap.java
627 lines (562 loc) · 31.6 KB
/
WeldBootstrap.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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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.jboss.weld.bootstrap;
import static org.jboss.weld.logging.Category.BOOTSTRAP;
import static org.jboss.weld.logging.Category.VERSION;
import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
import static org.jboss.weld.logging.messages.BootstrapMessage.DEPLOYMENT_ARCHIVE_NULL;
import static org.jboss.weld.logging.messages.BootstrapMessage.DEPLOYMENT_REQUIRED;
import static org.jboss.weld.logging.messages.BootstrapMessage.JTA_UNAVAILABLE;
import static org.jboss.weld.logging.messages.BootstrapMessage.MANAGER_NOT_INITIALIZED;
import static org.jboss.weld.logging.messages.BootstrapMessage.UNSPECIFIED_REQUIRED_SERVICE;
import static org.jboss.weld.logging.messages.BootstrapMessage.VALIDATING_BEANS;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.enterprise.context.spi.Context;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.CDI;
import javax.enterprise.inject.spi.Decorator;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.Interceptor;
import org.jboss.weld.Container;
import org.jboss.weld.ContainerState;
import org.jboss.weld.Weld;
import org.jboss.weld.annotated.slim.SlimAnnotatedTypeStore;
import org.jboss.weld.annotated.slim.SlimAnnotatedTypeStoreImpl;
import org.jboss.weld.bean.DecoratorImpl;
import org.jboss.weld.bean.InterceptorImpl;
import org.jboss.weld.bean.RIBean;
import org.jboss.weld.bean.builtin.BeanManagerBean;
import org.jboss.weld.bean.builtin.BeanManagerImplBean;
import org.jboss.weld.bean.builtin.ContextBean;
import org.jboss.weld.bean.proxy.util.SimpleProxyServices;
import org.jboss.weld.bootstrap.api.Bootstrap;
import org.jboss.weld.bootstrap.api.Environment;
import org.jboss.weld.bootstrap.api.Service;
import org.jboss.weld.bootstrap.api.ServiceRegistry;
import org.jboss.weld.bootstrap.api.helpers.ServiceRegistries;
import org.jboss.weld.bootstrap.api.helpers.SimpleServiceRegistry;
import org.jboss.weld.bootstrap.enablement.EnablementBuilder;
import org.jboss.weld.bootstrap.events.AfterBeanDiscoveryImpl;
import org.jboss.weld.bootstrap.events.AfterDeploymentValidationImpl;
import org.jboss.weld.bootstrap.events.AnnotationDiscovery;
import org.jboss.weld.bootstrap.events.BeforeBeanDiscoveryImpl;
import org.jboss.weld.bootstrap.events.BeforeShutdownImpl;
import org.jboss.weld.bootstrap.events.ContainerLifecycleEventPreloader;
import org.jboss.weld.bootstrap.events.ContainerLifecycleEvents;
import org.jboss.weld.bootstrap.events.ProcessModuleImpl;
import org.jboss.weld.bootstrap.events.SimpleAnnotationDiscovery;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.bootstrap.spi.BeansXml;
import org.jboss.weld.bootstrap.spi.Deployment;
import org.jboss.weld.bootstrap.spi.Metadata;
import org.jboss.weld.context.ApplicationContext;
import org.jboss.weld.context.DependentContext;
import org.jboss.weld.context.RequestContext;
import org.jboss.weld.context.SingletonContext;
import org.jboss.weld.context.bound.BoundConversationContext;
import org.jboss.weld.context.bound.BoundConversationContextImpl;
import org.jboss.weld.context.bound.BoundLiteral;
import org.jboss.weld.context.bound.BoundRequestContext;
import org.jboss.weld.context.bound.BoundRequestContextImpl;
import org.jboss.weld.context.bound.BoundSessionContext;
import org.jboss.weld.context.bound.BoundSessionContextImpl;
import org.jboss.weld.context.ejb.EjbLiteral;
import org.jboss.weld.context.ejb.EjbRequestContext;
import org.jboss.weld.context.ejb.EjbRequestContextImpl;
import org.jboss.weld.context.http.HttpConversationContext;
import org.jboss.weld.context.http.HttpConversationContextImpl;
import org.jboss.weld.context.http.HttpLiteral;
import org.jboss.weld.context.http.HttpRequestContext;
import org.jboss.weld.context.http.HttpRequestContextImpl;
import org.jboss.weld.context.http.HttpSessionContext;
import org.jboss.weld.context.http.HttpSessionContextImpl;
import org.jboss.weld.context.unbound.ApplicationContextImpl;
import org.jboss.weld.context.unbound.DependentContextImpl;
import org.jboss.weld.context.unbound.RequestContextImpl;
import org.jboss.weld.context.unbound.SingletonContextImpl;
import org.jboss.weld.context.unbound.UnboundLiteral;
import org.jboss.weld.ejb.spi.EjbServices;
import org.jboss.weld.enums.EnumService;
import org.jboss.weld.event.GlobalObserverNotifierService;
import org.jboss.weld.exceptions.IllegalArgumentException;
import org.jboss.weld.exceptions.IllegalStateException;
import org.jboss.weld.executor.ExecutorServicesFactory;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.injection.producer.InjectionTargetService;
import org.jboss.weld.logging.messages.VersionMessage;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.manager.api.ExecutorServices;
import org.jboss.weld.metadata.TypeStore;
import org.jboss.weld.metadata.cache.MetaAnnotationStore;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.resources.DefaultResourceLoader;
import org.jboss.weld.resources.MemberTransformer;
import org.jboss.weld.resources.ReflectionCache;
import org.jboss.weld.resources.ReflectionCacheFactory;
import org.jboss.weld.resources.SharedObjectCache;
import org.jboss.weld.resources.SingleThreadScheduledExecutorServiceFactory;
import org.jboss.weld.resources.WeldClassLoaderResourceLoader;
import org.jboss.weld.resources.spi.ResourceLoader;
import org.jboss.weld.resources.spi.ScheduledExecutorServiceFactory;
import org.jboss.weld.serialization.ContextualStoreImpl;
import org.jboss.weld.serialization.spi.ContextualStore;
import org.jboss.weld.serialization.spi.ProxyServices;
import org.jboss.weld.servlet.ServletApi;
import org.jboss.weld.transaction.spi.TransactionServices;
import org.jboss.weld.util.ServiceLoader;
import org.jboss.weld.util.reflection.Formats;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.instantiation.InstantiatorFactory;
import org.jboss.weld.util.reflection.instantiation.LoaderInstantiatorFactory;
import org.jboss.weld.xml.BeansXmlParser;
import org.slf4j.cal10n.LocLogger;
/**
* Common bootstrapping functionality that is run at application startup and
* detects and register beans
*
* @author Pete Muir
* @author Ales Justin
*/
public class WeldBootstrap implements Bootstrap {
private static final LocLogger log = loggerFactory().getLogger(BOOTSTRAP);
/**
* A Deployment visitor which can find the transitive closure of Bean
* Deployment Archives
*
* @author pmuir
*/
private static class DeploymentVisitor {
private final BeanManagerImpl deploymentManager;
private final Environment environment;
private final Deployment deployment;
private final Map<BeanDeploymentArchive, BeanDeployment> managerAwareBeanDeploymentArchives;
private final Collection<ContextHolder<? extends Context>> contexts;
private final EnablementBuilder enablementBuilder;
public DeploymentVisitor(BeanManagerImpl deploymentManager, Environment environment, final Deployment deployment, Collection<ContextHolder<? extends Context>> contexts, EnablementBuilder enablementBuilder) {
this.deploymentManager = deploymentManager;
this.environment = environment;
this.deployment = deployment;
this.contexts = contexts;
this.managerAwareBeanDeploymentArchives = new ConcurrentHashMap<BeanDeploymentArchive, BeanDeployment>();
this.enablementBuilder = enablementBuilder;
}
public Map<BeanDeploymentArchive, BeanDeployment> visit() {
for (BeanDeploymentArchive archvive : deployment.getBeanDeploymentArchives()) {
visit(archvive, managerAwareBeanDeploymentArchives, new HashSet<BeanDeploymentArchive>(), true);
}
return managerAwareBeanDeploymentArchives;
}
private <T extends Service> void copyService(BeanDeploymentArchive archive, Class<T> serviceClass) {
// for certain services we can fall back to deployment-level settings or defaults
ServiceRegistry registry = archive.getServices();
if (registry.contains(serviceClass) == false) {
T service = deployment.getServices().get(serviceClass);
if (service != null)
registry.add(serviceClass, service);
}
}
private BeanDeployment visit(BeanDeploymentArchive beanDeploymentArchive, Map<BeanDeploymentArchive, BeanDeployment> managerAwareBeanDeploymentArchives, Set<BeanDeploymentArchive> seenBeanDeploymentArchives, boolean validate) {
copyService(beanDeploymentArchive, ResourceLoader.class);
copyService(beanDeploymentArchive, InstantiatorFactory.class);
// Check that the required services are specified
if (validate) {
verifyServices(beanDeploymentArchive.getServices(), environment.getRequiredBeanDeploymentArchiveServices());
}
// Check the id is not null
if (beanDeploymentArchive.getId() == null) {
throw new IllegalArgumentException(DEPLOYMENT_ARCHIVE_NULL, beanDeploymentArchive);
}
BeanDeployment parent = managerAwareBeanDeploymentArchives.get(beanDeploymentArchive);
if (parent == null) {
// Create the BeanDeployment
parent = new BeanDeployment(beanDeploymentArchive, deploymentManager, deployment.getServices(), contexts, enablementBuilder);
// Attach it
managerAwareBeanDeploymentArchives.put(beanDeploymentArchive, parent);
}
seenBeanDeploymentArchives.add(beanDeploymentArchive);
for (BeanDeploymentArchive archive : beanDeploymentArchive.getBeanDeploymentArchives()) {
BeanDeployment child;
// Cut any circularties
if (!seenBeanDeploymentArchives.contains(archive)) {
child = visit(archive, managerAwareBeanDeploymentArchives, seenBeanDeploymentArchives, validate);
} else {
// already visited
child = managerAwareBeanDeploymentArchives.get(archive);
}
parent.getBeanManager().addAccessibleBeanManager(child.getBeanManager());
}
return parent;
}
}
static {
loggerFactory().getLogger(VERSION).info(VersionMessage.VERSION, Formats.version(WeldBootstrap.class.getPackage()));
}
// The Bean manager
private BeanManagerImpl deploymentManager;
private Map<BeanDeploymentArchive, BeanDeployment> beanDeployments;
private Environment environment;
private Deployment deployment;
private DeploymentVisitor deploymentVisitor;
private final BeansXmlParser beansXmlParser;
private Collection<ContextHolder<? extends Context>> contexts;
private final EnablementBuilder enablementBuilder;
public WeldBootstrap() {
this.beansXmlParser = new BeansXmlParser();
this.enablementBuilder = new EnablementBuilder();
}
public Bootstrap startContainer(Environment environment, Deployment deployment) {
synchronized (this) {
if (deployment == null) {
throw new IllegalArgumentException(DEPLOYMENT_REQUIRED);
}
final ServiceRegistry registry = deployment.getServices();
if (!registry.contains(ResourceLoader.class)) {
registry.add(ResourceLoader.class, DefaultResourceLoader.INSTANCE);
}
if (!registry.contains(InstantiatorFactory.class)) {
registry.add(InstantiatorFactory.class, new LoaderInstantiatorFactory());
}
if (!registry.contains(ScheduledExecutorServiceFactory.class)) {
registry.add(ScheduledExecutorServiceFactory.class, new SingleThreadScheduledExecutorServiceFactory());
}
if (!registry.contains(ProxyServices.class)) {
registry.add(ProxyServices.class, new SimpleProxyServices());
}
verifyServices(registry, environment.getRequiredDeploymentServices());
if (!registry.contains(TransactionServices.class)) {
log.info(JTA_UNAVAILABLE);
}
// TODO Reinstate if we can find a good way to detect.
// if (!deployment.getServices().contains(EjbServices.class))
// {
// log.info("EJB services not available. Session beans will be simple beans, CDI-style injection into non-contextual EJBs, injection of remote EJBs and injection of @EJB in simple beans will not be available");
// }
// if (!deployment.getServices().contains(JpaInjectionServices.class))
// {
// log.info("JPA services not available. Injection of @PersistenceContext will not occur. Entity beans will be discovered as simple beans.");
// }
// if
// (!deployment.getServices().contains(ResourceInjectionServices.class))
// {
// log.info("@Resource injection not available.");
// }
this.deployment = deployment;
addImplementationServices(registry);
ServiceRegistry deploymentServices = new SimpleServiceRegistry();
deploymentServices.add(ClassTransformer.class, registry.get(ClassTransformer.class));
deploymentServices.add(SlimAnnotatedTypeStore.class, registry.get(SlimAnnotatedTypeStore.class));
deploymentServices.add(MetaAnnotationStore.class, registry.get(MetaAnnotationStore.class));
deploymentServices.add(TypeStore.class, registry.get(TypeStore.class));
deploymentServices.add(ContextualStore.class, registry.get(ContextualStore.class));
deploymentServices.add(CurrentInjectionPoint.class, registry.get(CurrentInjectionPoint.class));
deploymentServices.add(GlobalObserverNotifierService.class, registry.get(GlobalObserverNotifierService.class));
deploymentServices.add(ContainerLifecycleEvents.class, registry.get(ContainerLifecycleEvents.class));
deploymentServices.add(SpecializationAndEnablementRegistry.class, registry.get(SpecializationAndEnablementRegistry.class));
deploymentServices.add(ReflectionCache.class, registry.get(ReflectionCache.class));
this.environment = environment;
this.deploymentManager = BeanManagerImpl.newRootManager("deployment", deploymentServices);
Container.initialize(deploymentManager, ServiceRegistries.unmodifiableServiceRegistry(registry));
Container.instance().setState(ContainerState.STARTING);
this.contexts = createContexts(deploymentServices);
this.deploymentVisitor = new DeploymentVisitor(deploymentManager, environment, deployment, contexts, enablementBuilder);
// Read the deployment structure, this will be the physical structure
// as caused by the presence of beans.xml
beanDeployments = deploymentVisitor.visit();
return this;
}
}
private void addImplementationServices(ServiceRegistry services) {
// Temporary workaround to provide context for building annotated class
// TODO expose AnnotatedClass on SPI and allow container to provide impl
// of this via ResourceLoader
TypeStore typeStore = new TypeStore();
services.add(TypeStore.class, typeStore);
SharedObjectCache cache = new SharedObjectCache();
services.add(SharedObjectCache.class, cache);
ReflectionCache reflectionCache = ReflectionCacheFactory.newInstance(typeStore);
services.add(ReflectionCache.class, reflectionCache);
services.add(SlimAnnotatedTypeStore.class, new SlimAnnotatedTypeStoreImpl());
ClassTransformer classTransformer = new ClassTransformer(typeStore, cache, reflectionCache);
services.add(ClassTransformer.class, classTransformer);
services.add(MemberTransformer.class, new MemberTransformer(classTransformer));
services.add(MetaAnnotationStore.class, new MetaAnnotationStore(classTransformer));
services.add(ContextualStore.class, new ContextualStoreImpl());
services.add(CurrentInjectionPoint.class, new CurrentInjectionPoint());
services.add(SpecializationAndEnablementRegistry.class, new SpecializationAndEnablementRegistry());
GlobalObserverNotifierService observerNotificationService = new GlobalObserverNotifierService(services);
services.add(GlobalObserverNotifierService.class, observerNotificationService);
BootstrapConfiguration configuration = new BootstrapConfiguration(DefaultResourceLoader.INSTANCE);
ContainerLifecycleEventPreloader preloader = null;
if (configuration.isThreadingEnabled()) {
ExecutorServices executor = ExecutorServicesFactory.create(configuration);
services.add(ExecutorServices.class, executor);
if (configuration.isConcurrentDeployerEnabled()) {
services.add(Validator.class, new ConcurrentValidator(executor));
}
if (configuration.isPreloaderEnabled()) {
preloader = new ContainerLifecycleEventPreloader(configuration, observerNotificationService.getGlobalLenientObserverNotifier());
}
}
if (!services.contains(AnnotationDiscovery.class)) {
services.add(AnnotationDiscovery.class, new SimpleAnnotationDiscovery(services.get(ReflectionCache.class)));
}
services.add(ContainerLifecycleEvents.class, new ContainerLifecycleEvents(preloader, services.get(AnnotationDiscovery.class)));
if (!services.contains(Validator.class)) {
services.add(Validator.class, new Validator());
}
}
public BeanManagerImpl getManager(BeanDeploymentArchive beanDeploymentArchive) {
synchronized (this) {
BeanDeployment beanDeployment = beanDeployments.get(beanDeploymentArchive);
if (beanDeployment != null) {
return beanDeployment.getBeanManager().getCurrent();
} else {
return null;
}
}
}
public Bootstrap startInitialization() {
synchronized (this) {
if (deploymentManager == null) {
throw new IllegalStateException(MANAGER_NOT_INITIALIZED);
}
// we need to know which BDAs are physical so that we fire ProcessModule for there archives only
Collection<BeanDeploymentArchive> physicalBeanDeploymentArchives = deployment.getBeanDeploymentArchives();
ExtensionBeanDeployer extensionBeanDeployer = new ExtensionBeanDeployer(deploymentManager, deployment, beanDeployments, contexts, enablementBuilder);
extensionBeanDeployer.addExtensions(deployment.getExtensions());
extensionBeanDeployer.deployBeans();
// Add the Deployment BeanManager Bean to the Deployment BeanManager
deploymentManager.addBean(new BeanManagerBean(deploymentManager));
deploymentManager.addBean(new BeanManagerImplBean(deploymentManager));
// Re-Read the deployment structure, this will be the physical
// structure, and will add in BDAs for any extensions outside a
// physical BDA
beanDeployments = deploymentVisitor.visit();
BeforeBeanDiscoveryImpl.fire(deploymentManager, deployment, beanDeployments, contexts, enablementBuilder);
// Re-Read the deployment structure, this will be the physical
// structure, extensions and any classes added using addAnnotatedType
// outside the physical BDA
beanDeployments = deploymentVisitor.visit();
// for each BDA transform its classes into WeldClass instances
for (Entry<BeanDeploymentArchive, BeanDeployment> entry : beanDeployments.entrySet()) {
entry.getValue().createClasses();
}
for (Entry<BeanDeploymentArchive, BeanDeployment> entry : beanDeployments.entrySet()) {
BeanDeployment beanDeployment = entry.getValue();
if (physicalBeanDeploymentArchives.contains(entry.getKey())) {
// only fire for physical BDAs
ProcessModuleImpl.fire(beanDeployment);
}
beanDeployment.createEnabled();
}
}
return this;
}
public Bootstrap deployBeans() {
synchronized (this) {
for (BeanDeployment deployment : beanDeployments.values()) {
deployment.createBeans(environment);
}
// we must use separate loops, otherwise cyclic specialization would not work
for (BeanDeployment deployment : beanDeployments.values()) {
deployment.getBeanDeployer().processClassBeanAttributes();
deployment.getBeanDeployer().createProducersAndObservers();
}
for (BeanDeployment deployment : beanDeployments.values()) {
deployment.getBeanDeployer().processProducerAttributes();
deployment.getBeanDeployer().createNewBeans();
}
for (Entry<BeanDeploymentArchive, BeanDeployment> entry : beanDeployments.entrySet()) {
entry.getValue().deploySpecialized(environment);
}
// TODO keep a list of new bdas, add them all in, and deploy beans for
// them, then merge into existing
for (Entry<BeanDeploymentArchive, BeanDeployment> entry : beanDeployments.entrySet()) {
entry.getValue().deployBeans(environment);
}
AfterBeanDiscoveryImpl.fire(deploymentManager, deployment, beanDeployments, contexts, enablementBuilder);
// Re-read the deployment structure, this will be the physical
// structure, extensions, classes, and any beans added using addBean
// outside the physical structure
beanDeployments = deploymentVisitor.visit();
for (Entry<BeanDeploymentArchive, BeanDeployment> entry : beanDeployments.entrySet()) {
entry.getValue().getBeanManager().getServices().get(InjectionTargetService.class).initialize();
entry.getValue().afterBeanDiscovery(environment);
}
Container.instance().putBeanDeployments(beanDeployments);
Container.instance().setState(ContainerState.DEPLOYED);
}
return this;
}
public Bootstrap validateBeans() {
synchronized (this) {
log.debug(VALIDATING_BEANS);
for (Entry<BeanDeploymentArchive, BeanDeployment> entry : beanDeployments.entrySet()) {
BeanManagerImpl beanManager = entry.getValue().getBeanManager();
beanManager.getBeanResolver().clear();
deployment.getServices().get(Validator.class).validateDeployment(beanManager, entry.getValue().getBeanDeployer().getEnvironment());
beanManager.getServices().get(InjectionTargetService.class).validate();
}
Container.instance().setState(ContainerState.VALIDATED);
AfterDeploymentValidationImpl.fire(deploymentManager);
}
return this;
}
public Bootstrap endInitialization() {
// TODO rebuild the manager accessibility graph if the bdas have changed
synchronized (this) {
// Register the managers so external requests can handle them
// clear the TypeSafeResolvers, so data that is only used at startup
// is not kept around using up memory
deploymentManager.getBeanResolver().clear();
deploymentManager.getAccessibleLenientObserverNotifier().clear();
deploymentManager.getGlobalStrictObserverNotifier().clear();
deploymentManager.getGlobalLenientObserverNotifier().clear();
deploymentManager.getDecoratorResolver().clear();
deploymentManager.getServices().cleanupAfterBoot();
enablementBuilder.clear();
for (Entry<BeanDeploymentArchive, BeanDeployment> entry : beanDeployments.entrySet()) {
BeanManagerImpl beanManager = entry.getValue().getBeanManager();
beanManager.getBeanResolver().clear();
beanManager.getAccessibleLenientObserverNotifier().clear();
beanManager.getDecoratorResolver().clear();
beanManager.getInterceptorMetadataReader().cleanAfterBoot();
beanManager.getServices().cleanupAfterBoot();
// clean up beans
for (Bean<?> bean : beanManager.getBeans()) {
if (bean instanceof RIBean<?>) {
RIBean<?> riBean = (RIBean<?>) bean;
riBean.cleanupAfterBoot();
}
}
// clean up decorators
for (Decorator<?> decorator : beanManager.getDecorators()) {
if (decorator instanceof DecoratorImpl<?>) {
Reflections.<DecoratorImpl<?>>cast(decorator).cleanupAfterBoot();
}
}
// clean up interceptors
for (Interceptor<?> interceptor : beanManager.getInterceptors()) {
if (interceptor instanceof InterceptorImpl<?>) {
Reflections.<InterceptorImpl<?>>cast(interceptor).cleanupAfterBoot();
}
}
}
for (BeanDeployment deployment : beanDeployments.values()) {
deployment.getBeanManager().getServices().get(EnumService.class).inject();
deployment.getBeanDeployer().cleanup();
}
Container.instance().setState(ContainerState.INITIALIZED);
return this;
}
}
protected Collection<ContextHolder<? extends Context>> createContexts(ServiceRegistry services) {
List<ContextHolder<? extends Context>> contexts = new ArrayList<ContextHolder<? extends Context>>();
/*
* Register a full set of bound and unbound contexts. Although we may not use all of
* these (e.g. if we are running in a servlet environment) they may be
* useful for an application.
*/
contexts.add(new ContextHolder<ApplicationContext>(new ApplicationContextImpl(), ApplicationContext.class, UnboundLiteral.INSTANCE));
contexts.add(new ContextHolder<SingletonContext>(new SingletonContextImpl(), SingletonContext.class, UnboundLiteral.INSTANCE));
contexts.add(new ContextHolder<BoundSessionContext>(new BoundSessionContextImpl(), BoundSessionContext.class, BoundLiteral.INSTANCE));
contexts.add(new ContextHolder<BoundConversationContext>(new BoundConversationContextImpl(), BoundConversationContext.class, BoundLiteral.INSTANCE));
contexts.add(new ContextHolder<BoundRequestContext>(new BoundRequestContextImpl(), BoundRequestContext.class, BoundLiteral.INSTANCE));
contexts.add(new ContextHolder<RequestContext>(new RequestContextImpl(), RequestContext.class, UnboundLiteral.INSTANCE));
contexts.add(new ContextHolder<DependentContext>(new DependentContextImpl(services.get(ContextualStore.class)), DependentContext.class, UnboundLiteral.INSTANCE));
if (Reflections.isClassLoadable(ServletApi.SERVLET_CONTEXT_CLASS_NAME, WeldClassLoaderResourceLoader.INSTANCE)) {
// Register the Http contexts if not in
contexts.add(new ContextHolder<HttpSessionContext>(new HttpSessionContextImpl(), HttpSessionContext.class, HttpLiteral.INSTANCE));
contexts.add(new ContextHolder<HttpConversationContext>(new HttpConversationContextImpl(), HttpConversationContext.class, HttpLiteral.INSTANCE));
contexts.add(new ContextHolder<HttpRequestContext>(new HttpRequestContextImpl(), HttpRequestContext.class, HttpLiteral.INSTANCE));
}
if (deployment.getServices().contains(EjbServices.class)) {
// Register the EJB Request context if EjbServices are available
contexts.add(new ContextHolder<EjbRequestContext>(new EjbRequestContextImpl(), EjbRequestContext.class, EjbLiteral.INSTANCE));
}
/*
* Register the contexts with the bean manager and add the beans to the
* deployment manager so that they are easily accessible (contexts are app
* scoped)
*/
for (ContextHolder<? extends Context> context : contexts) {
deploymentManager.addContext(context.getContext());
deploymentManager.addBean(ContextBean.of(context, deploymentManager));
}
return contexts;
}
public void shutdown() {
synchronized (this) {
try {
// First, the container must destroy all contexts.
deploymentManager.instance().select(ApplicationContext.class).get().invalidate();
} finally {
try {
// Next, the container must destroy dependent objects injected into enums.
if (beanDeployments != null) {
for (BeanDeployment deployment : beanDeployments.values()) {
deployment.getBeanManager().getServices().get(EnumService.class).disinject();
}
}
} finally {
try {
// Finally, the container must fire an event of type BeforeShutdown.
BeforeShutdownImpl.fire(deploymentManager, beanDeployments);
} finally {
Container.instance().setState(ContainerState.SHUTDOWN);
Container.instance().cleanup();
// remove BeanManager references
try {
CDI<?> cdi = CDI.current();
if (cdi instanceof Weld) {
((Weld) cdi).cleanup();
}
} catch (java.lang.IllegalStateException ignored) {
}
}
}
}
}
}
protected static void verifyServices(ServiceRegistry services, Set<Class<? extends Service>> requiredServices) {
for (Class<? extends Service> serviceType : requiredServices) {
if (!services.contains(serviceType)) {
throw new IllegalStateException(UNSPECIFIED_REQUIRED_SERVICE, serviceType.getName());
}
}
}
public BeansXml parse(Iterable<URL> urls) {
return parse(urls, false);
}
public BeansXml parse(Iterable<URL> urls, boolean removeDuplicates) {
return beansXmlParser.parse(urls, removeDuplicates);
}
public BeansXml parse(URL url) {
return beansXmlParser.parse(url);
}
public Iterable<Metadata<Extension>> loadExtensions(ClassLoader classLoader) {
return ServiceLoader.load(Extension.class, classLoader);
}
}