/
WeldStartup.java
659 lines (564 loc) · 30.3 KB
/
WeldStartup.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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, 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.config.ConfigurationKey.ROLLING_UPGRADES_ID_DELIMITER;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.Initialized;
import javax.enterprise.context.NormalScope;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.context.spi.Context;
import javax.enterprise.inject.Model;
import javax.enterprise.inject.Stereotype;
import javax.enterprise.inject.spi.Bean;
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.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.ProtectionDomainCache;
import org.jboss.weld.bean.proxy.ProxyInstantiator;
import org.jboss.weld.bean.proxy.util.SimpleProxyServices;
import org.jboss.weld.bootstrap.api.Environment;
import org.jboss.weld.bootstrap.api.Environments;
import org.jboss.weld.bootstrap.api.Service;
import org.jboss.weld.bootstrap.api.ServiceRegistry;
import org.jboss.weld.bootstrap.api.TypeDiscoveryConfiguration;
import org.jboss.weld.bootstrap.api.helpers.ServiceRegistries;
import org.jboss.weld.bootstrap.api.helpers.SimpleServiceRegistry;
import org.jboss.weld.bootstrap.enablement.GlobalEnablementBuilder;
import org.jboss.weld.bootstrap.events.AfterBeanDiscoveryImpl;
import org.jboss.weld.bootstrap.events.AfterDeploymentValidationImpl;
import org.jboss.weld.bootstrap.events.AfterTypeDiscoveryImpl;
import org.jboss.weld.bootstrap.events.BeforeBeanDiscoveryImpl;
import org.jboss.weld.bootstrap.events.ContainerLifecycleEventPreloader;
import org.jboss.weld.bootstrap.events.ContainerLifecycleEvents;
import org.jboss.weld.bootstrap.events.RequiredAnnotationDiscovery;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.bootstrap.spi.CDI11Deployment;
import org.jboss.weld.bootstrap.spi.Deployment;
import org.jboss.weld.bootstrap.spi.Metadata;
import org.jboss.weld.bootstrap.spi.helpers.MetadataImpl;
import org.jboss.weld.config.ConfigurationKey;
import org.jboss.weld.config.WeldConfiguration;
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.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.event.ContextEvent;
import org.jboss.weld.event.CurrentEventMetadata;
import org.jboss.weld.event.DefaultObserverNotifierFactory;
import org.jboss.weld.event.GlobalObserverNotifierService;
import org.jboss.weld.executor.ExecutorServicesFactory;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.injection.ResourceInjectionFactory;
import org.jboss.weld.injection.producer.InjectionTargetService;
import org.jboss.weld.logging.BootstrapLogger;
import org.jboss.weld.logging.VersionLogger;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.manager.BeanManagerLookupService;
import org.jboss.weld.manager.api.ExecutorServices;
import org.jboss.weld.metadata.TypeStore;
import org.jboss.weld.metadata.cache.MetaAnnotationStore;
import org.jboss.weld.module.ObserverNotifierFactory;
import org.jboss.weld.module.WeldModules;
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.spi.ClassFileServices;
import org.jboss.weld.resources.spi.ResourceLoader;
import org.jboss.weld.security.NoopSecurityServices;
import org.jboss.weld.security.spi.SecurityServices;
import org.jboss.weld.serialization.BeanIdentifierIndex;
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.spi.HttpContextActivationFilter;
import org.jboss.weld.servlet.spi.helpers.AcceptingHttpContextActivationFilter;
import org.jboss.weld.transaction.spi.TransactionServices;
import org.jboss.weld.util.Permissions;
import org.jboss.weld.util.collections.ImmutableSet;
import org.jboss.weld.util.collections.Iterables;
import org.jboss.weld.util.reflection.Formats;
import org.jboss.weld.util.reflection.Reflections;
/**
* Common bootstrapping functionality that is run at application startup and
* detects and register beans
*
* @author Pete Muir
* @author Ales Justin
* @author Marko Luksa
*/
public class WeldStartup {
static {
VersionLogger.LOG.version(Formats.version(null));
}
private BeanManagerImpl deploymentManager;
private BeanDeploymentArchiveMapping bdaMapping;
private Collection<ContextHolder<? extends Context>> contexts;
private List<Metadata<? extends Extension>> extensions;
private Environment environment;
private Deployment deployment;
private DeploymentVisitor deploymentVisitor;
private final ServiceRegistry initialServices = new SimpleServiceRegistry();
private String contextId;
public WeldStartup() {
}
public WeldRuntime startContainer(String contextId, Environment environment, Deployment deployment) {
if (deployment == null) {
throw BootstrapLogger.LOG.deploymentRequired();
}
checkApiVersion();
final ServiceRegistry registry = deployment.getServices();
// initiate part of registry in order to allow access to WeldConfiguration
new AdditionalServiceLoader(deployment).loadAdditionalServices(registry);
// Resource Loader has to be loaded prior to WeldConfiguration
if (!registry.contains(ResourceLoader.class)) {
registry.add(ResourceLoader.class, DefaultResourceLoader.INSTANCE);
}
WeldConfiguration configuration = new WeldConfiguration(registry, deployment);
registry.add(WeldConfiguration.class, configuration);
String finalContextId = BeanDeployments.getFinalId(contextId,
registry.get(WeldConfiguration.class).getStringProperty(ROLLING_UPGRADES_ID_DELIMITER));
this.contextId = finalContextId;
this.deployment = deployment;
this.environment = environment;
if (this.extensions == null) {
setExtensions(deployment.getExtensions());
}
// Add extension to register built-in components
this.extensions.add(MetadataImpl.from(new WeldExtension()));
// Add Weld extensions
String vetoTypeRegex = configuration.getStringProperty(ConfigurationKey.VETO_TYPES_WITHOUT_BEAN_DEFINING_ANNOTATION);
if (!vetoTypeRegex.isEmpty()) {
this.extensions.add(MetadataImpl.from(new WeldVetoExtension(vetoTypeRegex)));
}
// Finish the rest of registry init, setupInitialServices() requires already changed finalContextId
setupInitialServices();
registry.addAll(initialServices.entrySet());
if (!registry.contains(ProxyServices.class)) {
registry.add(ProxyServices.class, new SimpleProxyServices());
}
if (!registry.contains(SecurityServices.class)) {
registry.add(SecurityServices.class, NoopSecurityServices.INSTANCE);
}
addImplementationServices(registry);
verifyServices(registry, environment.getRequiredDeploymentServices(), contextId);
if (!registry.contains(TransactionServices.class)) {
BootstrapLogger.LOG.jtaUnavailable();
}
this.deploymentManager = BeanManagerImpl.newRootManager(finalContextId, "deployment", registry);
Container.initialize(finalContextId, deploymentManager, ServiceRegistries.unmodifiableServiceRegistry(deployment.getServices()));
getContainer().setState(ContainerState.STARTING);
this.contexts = createContexts(registry);
this.bdaMapping = new BeanDeploymentArchiveMapping();
this.deploymentVisitor = new DeploymentVisitor(deploymentManager, environment, deployment, contexts, bdaMapping);
if (deployment instanceof CDI11Deployment) {
registry.add(BeanManagerLookupService.class, new BeanManagerLookupService((CDI11Deployment) deployment, bdaMapping.getBdaToBeanManagerMap()));
} else {
BootstrapLogger.LOG.legacyDeploymentMetadataProvided();
}
// Read the deployment structure, bdaMapping will be the physical structure
// as caused by the presence of beans.xml
deploymentVisitor.visit();
return new WeldRuntime(finalContextId, deploymentManager, bdaMapping.getBdaToBeanManagerMap());
}
private void checkApiVersion() {
if (Bean.class.getInterfaces().length == 1) {
// this means Bean only extends Contextual - since CDI 1.1 Bean also extends BeanAttributes
// CDI 1.0 API is detected on classpath - since that would result in obscure exception later, we
// throw an appropriate exception right now
throw BootstrapLogger.LOG.cdiApiVersionMismatch();
}
}
private void setupInitialServices() {
if (initialServices.contains(TypeStore.class)) {
return;
}
// instantiate initial services which we need for this phase
TypeStore store = new TypeStore();
SharedObjectCache cache = new SharedObjectCache();
ReflectionCache reflectionCache = ReflectionCacheFactory.newInstance(store);
ClassTransformer classTransformer = new ClassTransformer(store, cache, reflectionCache, contextId);
initialServices.add(TypeStore.class, store);
initialServices.add(SharedObjectCache.class, cache);
initialServices.add(ReflectionCache.class, reflectionCache);
initialServices.add(ClassTransformer.class, classTransformer);
}
private void addImplementationServices(ServiceRegistry services) {
final WeldModules modules = new WeldModules();
services.add(WeldModules.class, modules);
final WeldConfiguration configuration = services.get(WeldConfiguration.class);
services.add(SlimAnnotatedTypeStore.class, new SlimAnnotatedTypeStoreImpl());
if (services.get(ClassTransformer.class) == null) {
throw new IllegalStateException(ClassTransformer.class.getSimpleName() + " not installed.");
}
services.add(MemberTransformer.class, new MemberTransformer(services.get(ClassTransformer.class)));
services.add(MetaAnnotationStore.class, new MetaAnnotationStore(services.get(ClassTransformer.class)));
BeanIdentifierIndex beanIdentifierIndex = null;
if (configuration.getBooleanProperty(ConfigurationKey.BEAN_IDENTIFIER_INDEX_OPTIMIZATION)) {
beanIdentifierIndex = new BeanIdentifierIndex();
services.add(BeanIdentifierIndex.class, beanIdentifierIndex);
}
services.add(ContextualStore.class, new ContextualStoreImpl(contextId, beanIdentifierIndex));
services.add(CurrentInjectionPoint.class, new CurrentInjectionPoint());
services.add(CurrentEventMetadata.class, new CurrentEventMetadata());
services.add(SpecializationAndEnablementRegistry.class, new SpecializationAndEnablementRegistry());
services.add(MissingDependenciesRegistry.class, new MissingDependenciesRegistry());
/*
* Setup ExecutorServices
*/
ExecutorServices executor = services.get(ExecutorServices.class);
if (executor == null) {
executor = ExecutorServicesFactory.create(DefaultResourceLoader.INSTANCE, configuration);
if (executor != null) {
services.add(ExecutorServices.class, executor);
}
}
services.add(RequiredAnnotationDiscovery.class, new RequiredAnnotationDiscovery(services.get(ReflectionCache.class)));
services.add(GlobalEnablementBuilder.class, new GlobalEnablementBuilder());
if (!services.contains(HttpContextActivationFilter.class)) {
services.add(HttpContextActivationFilter.class, AcceptingHttpContextActivationFilter.INSTANCE);
}
services.add(ProtectionDomainCache.class, new ProtectionDomainCache());
services.add(ProxyInstantiator.class, ProxyInstantiator.Factory.create(configuration));
services.add(ObserverNotifierFactory.class, DefaultObserverNotifierFactory.INSTANCE);
services.add(ResourceInjectionFactory.class, new ResourceInjectionFactory());
modules.postServiceRegistration(contextId, services);
/*
* Setup Validator
*/
if (configuration.getBooleanProperty(ConfigurationKey.CONCURRENT_DEPLOYMENT) && services.contains(ExecutorServices.class)) {
services.add(Validator.class, new ConcurrentValidator(modules.getPluggableValidators(), executor));
} else {
services.add(Validator.class, new Validator(modules.getPluggableValidators()));
}
GlobalObserverNotifierService observerNotificationService = new GlobalObserverNotifierService(services, contextId);
services.add(GlobalObserverNotifierService.class, observerNotificationService);
/*
* Preloader for container lifecycle events
*/
ContainerLifecycleEventPreloader preloader = null;
int preloaderThreadPoolSize = configuration.getIntegerProperty(ConfigurationKey.PRELOADER_THREAD_POOL_SIZE);
if (preloaderThreadPoolSize > 0 && Permissions.hasPermission(Permissions.MODIFY_THREAD_GROUP)) {
preloader = new ContainerLifecycleEventPreloader(preloaderThreadPoolSize, observerNotificationService.getGlobalLenientObserverNotifier());
}
services.add(ContainerLifecycleEvents.class, new ContainerLifecycleEvents(preloader, services.get(RequiredAnnotationDiscovery.class)));
if (isEEModulesAwareEnvironment()) {
services.add(BeanDeploymentModules.class, new BeanDeploymentModules(contextId, services));
}
}
// needs to be resolved once extension beans are deployed
private void installFastProcessAnnotatedTypeResolver(ServiceRegistry services) {
ClassFileServices classFileServices = services.get(ClassFileServices.class);
if (classFileServices != null) {
final GlobalObserverNotifierService observers = services.get(GlobalObserverNotifierService.class);
try {
final FastProcessAnnotatedTypeResolver resolver = new FastProcessAnnotatedTypeResolver(observers.getAllObserverMethods());
services.add(FastProcessAnnotatedTypeResolver.class, resolver);
} catch (UnsupportedObserverMethodException e) {
BootstrapLogger.LOG.notUsingFastResolver(e.getObserver());
return;
}
}
}
public void startInitialization() {
if (deploymentManager == null) {
throw BootstrapLogger.LOG.managerNotInitialized();
}
Set<BeanDeployment> physicalBeanDeploymentArchives = new HashSet<BeanDeployment>(getBeanDeployments());
ExtensionBeanDeployer extensionBeanDeployer = new ExtensionBeanDeployer(deploymentManager, deployment, bdaMapping, contexts);
extensionBeanDeployer.addExtensions(extensions);
extensionBeanDeployer.deployBeans();
installFastProcessAnnotatedTypeResolver(deploymentManager.getServices());
// Add the Deployment BeanManager Bean to the Deployment BeanManager
deploymentManager.addBean(new BeanManagerBean(deploymentManager));
deploymentManager.addBean(new BeanManagerImplBean(deploymentManager));
// Re-Read the deployment structure, bdaMapping will be the physical
// structure, and will add in BDAs for any extensions outside a
// physical BDA
deploymentVisitor.visit();
BeforeBeanDiscoveryImpl.fire(deploymentManager, deployment, bdaMapping, contexts);
// for each physical BDA transform its classes into AnnotatedType instances
for (BeanDeployment beanDeployment : physicalBeanDeploymentArchives) {
beanDeployment.createClasses();
}
// Re-Read the deployment structure, bdaMapping will be the physical
// structure, extensions and any classes added using addAnnotatedType
// outside the physical BDA
deploymentVisitor.visit();
for (BeanDeployment beanDeployment : getBeanDeployments()) {
beanDeployment.createTypes();
}
AfterTypeDiscoveryImpl.fire(deploymentManager, deployment, bdaMapping, contexts);
for (BeanDeployment beanDeployment : getBeanDeployments()) {
beanDeployment.createEnablement();
}
}
public void deployBeans() {
for (BeanDeployment deployment : getBeanDeployments()) {
deployment.createBeans(environment);
}
// we must use separate loops, otherwise cyclic specialization would not work
for (BeanDeployment deployment : getBeanDeployments()) {
deployment.getBeanDeployer().processClassBeanAttributes();
deployment.getBeanDeployer().createProducersAndObservers();
}
for (BeanDeployment deployment : getBeanDeployments()) {
deployment.getBeanDeployer().processProducerAttributes();
deployment.getBeanDeployer().createNewBeans();
}
for (BeanDeployment beanDeployment : getBeanDeployments()) {
beanDeployment.deploySpecialized(environment);
}
// TODO keep a list of new bdas, add them all in, and deploy beans for them, then merge into existing
for (BeanDeployment beanDeployment : getBeanDeployments()) {
beanDeployment.deployBeans(environment);
}
getContainer().setState(ContainerState.DISCOVERED);
// Flush caches for BeanManager.getBeans() to be usable in ABD (WELD-1729)
flushCaches();
AfterBeanDiscoveryImpl.fire(deploymentManager, deployment, bdaMapping, contexts);
// Extensions may have registered beans / observers. We need to flush caches.
flushCaches();
// If needed, recreate enablement once again - extensions may have registered interceptors, decorators and alternatives
if(deployment.getServices().getRequired(GlobalEnablementBuilder.class).isDirty()) {
for (BeanDeployment beanDeployment : getBeanDeployments()) {
beanDeployment.createEnablement();
}
}
// Re-read the deployment structure, bdaMapping will be the physical
// structure, extensions, classes, and any beans added using addBean
// outside the physical structure
deploymentVisitor.visit();
for (BeanDeployment beanDeployment : getBeanDeployments()) {
beanDeployment.getBeanManager().getServices().get(InjectionTargetService.class).initialize();
beanDeployment.afterBeanDiscovery(environment);
}
getContainer().putBeanDeployments(bdaMapping);
getContainer().setState(ContainerState.DEPLOYED);
}
public void validateBeans() {
BootstrapLogger.LOG.validatingBeans();
for (BeanDeployment beanDeployment : getBeanDeployments()) {
BeanManagerImpl beanManager = beanDeployment.getBeanManager();
beanManager.getBeanResolver().clear();
deployment.getServices().get(Validator.class).validateDeployment(beanManager, beanDeployment);
beanManager.getServices().get(InjectionTargetService.class).validate();
}
getContainer().setState(ContainerState.VALIDATED);
AfterDeploymentValidationImpl.fire(deploymentManager);
}
public void endInitialization() {
final BeanIdentifierIndex index = deploymentManager.getServices().get(BeanIdentifierIndex.class);
if (index != null) {
// Build a special index of bean identifiers
index.build(getBeansForBeanIdentifierIndex());
}
// TODO rebuild the manager accessibility graph if the bdas have changed
// 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
flushCaches();
deploymentManager.getServices().cleanupAfterBoot();
deploymentManager.cleanupAfterBoot();
for (BeanDeployment beanDeployment : getBeanDeployments()) {
BeanManagerImpl beanManager = beanDeployment.getBeanManager();
beanManager.getInterceptorMetadataReader().cleanAfterBoot();
beanManager.getServices().cleanupAfterBoot();
beanManager.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 beanDeployment : getBeanDeployments()) {
beanDeployment.getBeanDeployer().cleanup();
}
// feed BeanDeploymentModule registry
final BeanDeploymentModules modules = deploymentManager.getServices().get(BeanDeploymentModules.class);
if (modules != null) {
modules.processBeanDeployments(getBeanDeployments());
BootstrapLogger.LOG.debugv("EE modules: {0}", modules);
}
getContainer().setState(ContainerState.INITIALIZED);
if (modules != null) {
// fire @Initialized(ApplicationScoped.class) for non-web modules
// web modules are handled by HttpContextLifecycle
for (BeanDeploymentModule module : modules) {
if (!module.isWebModule()) {
module.fireEvent(Object.class, ContextEvent.APPLICATION_INITIALIZED, Initialized.Literal.APPLICATION);
}
}
}
}
private void flushCaches() {
deploymentManager.getBeanResolver().clear();
deploymentManager.getAccessibleLenientObserverNotifier().clear();
deploymentManager.getGlobalStrictObserverNotifier().clear();
deploymentManager.getGlobalLenientObserverNotifier().clear();
deploymentManager.getDecoratorResolver().clear();
deploymentManager.getInterceptorResolver().clear();
deploymentManager.getNameBasedResolver().clear();
for (BeanDeployment beanDeployment : getBeanDeployments()) {
BeanManagerImpl beanManager = beanDeployment.getBeanManager();
beanManager.getBeanResolver().clear();
beanManager.getAccessibleLenientObserverNotifier().clear();
beanManager.getDecoratorResolver().clear();
beanManager.getInterceptorResolver().clear();
beanManager.getNameBasedResolver().clear();
}
}
private Collection<BeanDeployment> getBeanDeployments() {
return bdaMapping.getBeanDeployments();
}
private Container getContainer() {
return Container.instance(contextId);
}
protected Collection<ContextHolder<? extends Context>> createContexts(ServiceRegistry services) {
List<ContextHolder<? extends Context>> contexts = new ArrayList<ContextHolder<? extends Context>>();
BeanIdentifierIndex beanIdentifierIndex = services.get(BeanIdentifierIndex.class);
/*
* 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(contextId), ApplicationContext.class, UnboundLiteral.INSTANCE));
contexts.add(new ContextHolder<SingletonContext>(new SingletonContextImpl(contextId), SingletonContext.class, UnboundLiteral.INSTANCE));
contexts.add(new ContextHolder<BoundSessionContext>(new BoundSessionContextImpl(contextId, beanIdentifierIndex), BoundSessionContext.class, BoundLiteral.INSTANCE));
contexts.add(new ContextHolder<BoundConversationContext>(new BoundConversationContextImpl(contextId, services), BoundConversationContext.class, BoundLiteral.INSTANCE));
contexts.add(new ContextHolder<BoundRequestContext>(new BoundRequestContextImpl(contextId), BoundRequestContext.class, BoundLiteral.INSTANCE));
contexts.add(new ContextHolder<RequestContext>(new RequestContextImpl(contextId), RequestContext.class, UnboundLiteral.INSTANCE));
contexts.add(new ContextHolder<DependentContext>(new DependentContextImpl(services.get(ContextualStore.class)), DependentContext.class, UnboundLiteral.INSTANCE));
services.get(WeldModules.class).postContextRegistration(contextId, services, contexts);
/*
* 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;
}
protected static void verifyServices(ServiceRegistry services, Set<Class<? extends Service>> requiredServices, Object target) {
for (Class<? extends Service> serviceType : requiredServices) {
if (!services.contains(serviceType)) {
throw BootstrapLogger.LOG.unspecifiedRequiredService(serviceType.getName(), target);
}
}
}
public TypeDiscoveryConfiguration startExtensions(Iterable<Metadata<Extension>> extensions) {
setExtensions(extensions);
// TODO: we should fire BeforeBeanDiscovery to allow extensions to register additional scopes
final Set<Class<? extends Annotation>> beanDefiningAnnotations = ImmutableSet.of(
// built-in scopes
Dependent.class, RequestScoped.class, ConversationScoped.class, SessionScoped.class, ApplicationScoped.class,
javax.interceptor.Interceptor.class, javax.decorator.Decorator.class,
// built-in stereotype
Model.class,
// meta-annotations
NormalScope.class, Stereotype.class);
return new TypeDiscoveryConfigurationImpl(beanDefiningAnnotations);
}
public BeanManagerImpl getManager(BeanDeploymentArchive beanDeploymentArchive) {
BeanDeployment beanDeployment = bdaMapping.getBeanDeployment(beanDeploymentArchive);
return beanDeployment == null ? null : beanDeployment.getBeanManager().getCurrent();
}
/**
* Right now we only index all session and conversation scoped beans.
*
* @return the set of beans the index should be built from
*/
private Set<Bean<?>> getBeansForBeanIdentifierIndex() {
Set<Bean<?>> beans = new HashSet<Bean<?>>();
for (BeanDeployment beanDeployment : getBeanDeployments()) {
for (Bean<?> bean : beanDeployment.getBeanManager().getBeans()) {
if (bean.getScope().equals(SessionScoped.class) || bean.getScope().equals(ConversationScoped.class)) {
beans.add(bean);
}
}
}
return beans;
}
private void setExtensions(Iterable<Metadata<Extension>> extensions) {
this.extensions = new ArrayList<Metadata<? extends Extension>>();
Iterables.addAll(this.extensions, extensions);
}
BeanManagerImpl getDeploymentManager() {
return deploymentManager;
}
BeanDeploymentArchiveMapping getBdaMapping() {
return bdaMapping;
}
Collection<ContextHolder<? extends Context>> getContexts() {
return contexts;
}
Deployment getDeployment() {
return deployment;
}
private boolean isEEModulesAwareEnvironment() {
return !Environments.SE.equals(environment);
}
}