use of jakarta.enterprise.inject.spi.Extension in project core by weld.
the class AfterTypeDiscoveryImpl method storeSyntheticAnnotatedType.
@Override
protected void storeSyntheticAnnotatedType(BeanDeployment deployment, AnnotatedType<?> type, String id) {
SlimAnnotatedType<?> annotatedType = transformer.getUnbackedAnnotatedType(type, getBeanManager().getId(), id);
Extension extension = getReceiver();
SlimAnnotatedTypeContext<?> annotatedTypeContext = SlimAnnotatedTypeContext.of(annotatedType, extension);
ProcessAnnotatedTypeImpl<?> event = events.fireProcessAnnotatedType(getBeanManager(), annotatedTypeContext);
if (event == null) {
deployment.getBeanDeployer().getEnvironment().addAnnotatedType(annotatedTypeContext);
store.put(annotatedType);
} else if (event.isVeto()) {
return;
} else {
annotatedType = event.getResultingAnnotatedType();
deployment.getBeanDeployer().getEnvironment().addSyntheticAnnotatedType(annotatedType, extension);
store.put(annotatedType);
}
}
use of jakarta.enterprise.inject.spi.Extension in project core by weld.
the class ContainerLifecyleObserverTest method testExtensionBuilder.
@SuppressWarnings({ "serial" })
@Test
public void testExtensionBuilder() {
ActionSequence.reset();
Extension myExtension = ContainerLifecycleObserver.extensionBuilder().add(beforeBeanDiscovery((e) -> addAction(BeforeBeanDiscovery.class.getSimpleName()))).add(afterTypeDiscovery().notify((e, b) -> {
addAction(AfterTypeDiscovery.class.getSimpleName());
e.addAnnotatedType(b.createAnnotatedType(Charlie.class), Charlie.class.getName());
})).add(afterBeanDiscovery((e) -> {
addAction(AfterBeanDiscovery.class.getSimpleName());
e.addObserverMethod().beanClass(Foo.class).observedType(Foo.class).notifyWith((ctx) -> {
});
e.addBean().beanClass(Integer.class).addType(Integer.class).addQualifier(Juicy.Literal.INSTANCE).createWith((ctx) -> Integer.valueOf(10));
})).add(afterDeploymentValidation((e) -> addAction(AfterDeploymentValidation.class.getSimpleName()))).add(beforeShutdown((e) -> addAction(BeforeShutdown.class.getSimpleName()))).build();
Extension myExtension2 = ContainerLifecycleObserver.extensionBuilder().add(processAnnotatedType().withAnnotations(RequestScoped.class).notify((e) -> e.veto())).add(processBeanAttributes().notify((e) -> addAction(ProcessBeanAttributes.class.getSimpleName()))).add(processSyntheticAnnotatedType(new TypeLiteral<ProcessSyntheticAnnotatedType<?>>() {
}.getType()).notify((e) -> addAction(ProcessSyntheticAnnotatedType.class.getSimpleName()))).add(processInjectionPoint().notify((e) -> addAction(ProcessInjectionPoint.class.getSimpleName()))).add(processProducer().notify((e) -> addAction(ProcessProducer.class.getSimpleName()))).add(processBean().notify((e) -> addAction(ProcessBean.class.getSimpleName()))).add(processManagedBean().notify((e) -> addAction(ProcessManagedBean.class.getSimpleName()))).add(processProducerField().notify((e) -> addAction(ProcessProducerField.class.getSimpleName()))).add(processProducerMethod().notify((e) -> {
// Weld SE defines some producer methods, e.g. ParametersFactory
addAction(ProcessProducerMethod.class.getSimpleName());
})).add(processBeanAttributes().notify((e) -> addAction(ProcessBeanAttributes.class.getSimpleName()))).add(processObserverMethod().notify((e) -> addAction(ProcessObserverMethod.class.getSimpleName()))).add(processObserverMethod(new TypeLiteral<ProcessObserverMethod<String, ?>>() {
}.getType()).notify((e) -> addAction(ProcessObserverMethod.class.getSimpleName() + String.class.getSimpleName()))).add(processSyntheticObserverMethod(new TypeLiteral<ProcessSyntheticObserverMethod<Foo, ?>>() {
}.getType()).notify((e) -> addAction(ProcessSyntheticObserverMethod.class.getSimpleName() + Foo.class.getSimpleName()))).add(processSyntheticBean(new TypeLiteral<ProcessSyntheticBean<Integer>>() {
}.getType()).notify((e) -> addAction(ProcessSyntheticBean.class.getSimpleName() + Integer.class.getSimpleName()))).build();
try (WeldContainer container = new Weld().disableDiscovery().beanClasses(Foo.class, Bravo.class).addExtension(myExtension).addExtension(myExtension2).initialize()) {
assertTrue(container.select(Foo.class).isUnsatisfied());
assertFalse(container.select(Bravo.class).isUnsatisfied());
Assert.assertEquals(Integer.valueOf(10), container.select(Integer.class, Juicy.Literal.INSTANCE).get());
}
ActionSequence.assertSequenceDataContainsAll(BeforeBeanDiscovery.class, AfterTypeDiscovery.class, AfterBeanDiscovery.class, AfterDeploymentValidation.class, BeforeShutdown.class);
ActionSequence.assertSequenceDataContainsAll(ProcessBeanAttributes.class, ProcessSyntheticAnnotatedType.class, ProcessInjectionPoint.class, ProcessObserverMethod.class, ProcessBeanAttributes.class, ProcessProducer.class);
ActionSequence.assertSequenceDataContainsAll(ProcessObserverMethod.class.getSimpleName() + String.class.getSimpleName(), ProcessSyntheticObserverMethod.class.getSimpleName() + Foo.class.getSimpleName(), ProcessSyntheticBean.class.getSimpleName() + Integer.class.getSimpleName());
ActionSequence.assertSequenceDataContainsAll(ProcessBean.class, ProcessManagedBean.class, ProcessProducerMethod.class, ProcessProducerField.class);
}
use of jakarta.enterprise.inject.spi.Extension in project core by weld.
the class Weld method addExtensions.
@SuppressWarnings("unchecked")
@Override
public Weld addExtensions(Class<? extends Extension>... extensionClasses) {
for (Class<? extends Extension> extensionClass : extensionClasses) {
try {
Extension extension = SecurityActions.newInstance(extensionClass);
addExtension(extension);
} catch (Exception ex) {
CommonLogger.LOG.unableToInstantiate(extensionClass, new Object[] {}, ex);
}
}
return this;
}
use of jakarta.enterprise.inject.spi.Extension in project core by weld.
the class WeldServletLifecycle method createDeployment.
/**
* Create servlet deployment.
*
* Can be overridden with custom servlet deployment. e.g. exact resources listing in restricted env like GAE
*
* @param context the servlet context
* @param bootstrap the bootstrap
* @return new servlet deployment
*/
protected CDI11Deployment createDeployment(ServletContext context, CDI11Bootstrap bootstrap) {
ImmutableSet.Builder<Metadata<Extension>> extensionsBuilder = ImmutableSet.builder();
extensionsBuilder.addAll(bootstrap.loadExtensions(WeldResourceLoader.getClassLoader()));
if (isDevModeEnabled) {
extensionsBuilder.add(new MetadataImpl<Extension>(DevelopmentMode.getProbeExtension(resourceLoader), "N/A"));
}
// Note that we only register this if we discovered at least one implementation of BuildCompatibleExtension
if (!BuildCompatibleExtensionLoader.getBuildCompatibleExtensions().isEmpty()) {
try {
extensionsBuilder.add(new MetadataImpl<Extension>(SecurityActions.newInstance(LiteExtensionTranslator.class), "synthetic:" + LiteExtensionTranslator.class.getName()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
final Iterable<Metadata<Extension>> extensions = extensionsBuilder.build();
final TypeDiscoveryConfiguration typeDiscoveryConfiguration = bootstrap.startExtensions(extensions);
final EEModuleDescriptor eeModule = new EEModuleDescriptorImpl(context.getContextPath(), ModuleType.WEB);
final BeanDiscoveryMode emptyBeansXmlDiscoveryMode = Boolean.parseBoolean(context.getInitParameter(LEGACY_EMPTY_BEANS_XML_TREATMENT)) ? BeanDiscoveryMode.ALL : BeanDiscoveryMode.ANNOTATED;
final DiscoveryStrategy strategy = DiscoveryStrategyFactory.create(resourceLoader, bootstrap, typeDiscoveryConfiguration.getKnownBeanDefiningAnnotations(), Boolean.parseBoolean(context.getInitParameter(Jandex.DISABLE_JANDEX_DISCOVERY_STRATEGY)), emptyBeansXmlDiscoveryMode);
if (Jandex.isJandexAvailable(resourceLoader)) {
try {
Class<? extends BeanArchiveHandler> handlerClass = Reflections.loadClass(resourceLoader, JANDEX_SERVLET_CONTEXT_BEAN_ARCHIVE_HANDLER);
strategy.registerHandler((SecurityActions.newConstructorInstance(handlerClass, new Class<?>[] { ServletContext.class }, context)));
} catch (Exception e) {
throw CommonLogger.LOG.unableToInstantiate(JANDEX_SERVLET_CONTEXT_BEAN_ARCHIVE_HANDLER, Arrays.toString(new Object[] { context }), e);
}
} else {
strategy.registerHandler(new ServletContextBeanArchiveHandler(context));
}
strategy.setScanner(new WebAppBeanArchiveScanner(resourceLoader, bootstrap, context, emptyBeansXmlDiscoveryMode));
Set<WeldBeanDeploymentArchive> beanDeploymentArchives = strategy.performDiscovery();
String isolation = context.getInitParameter(CONTEXT_PARAM_ARCHIVE_ISOLATION);
if (isolation == null || Boolean.valueOf(isolation)) {
CommonLogger.LOG.archiveIsolationEnabled();
} else {
CommonLogger.LOG.archiveIsolationDisabled();
Set<WeldBeanDeploymentArchive> flatDeployment = new HashSet<WeldBeanDeploymentArchive>();
flatDeployment.add(WeldBeanDeploymentArchive.merge(bootstrap, beanDeploymentArchives));
beanDeploymentArchives = flatDeployment;
}
for (BeanDeploymentArchive archive : beanDeploymentArchives) {
archive.getServices().add(EEModuleDescriptor.class, eeModule);
}
CDI11Deployment deployment = new WeldDeployment(resourceLoader, bootstrap, beanDeploymentArchives, extensions) {
@Override
protected WeldBeanDeploymentArchive createAdditionalBeanDeploymentArchive() {
WeldBeanDeploymentArchive archive = super.createAdditionalBeanDeploymentArchive();
archive.getServices().add(EEModuleDescriptor.class, eeModule);
return archive;
}
};
if (strategy.getClassFileServices() != null) {
deployment.getServices().add(ClassFileServices.class, strategy.getClassFileServices());
}
return deployment;
}
use of jakarta.enterprise.inject.spi.Extension in project helidon by oracle.
the class WebSocketCdiExtension method registerWebSockets.
private void registerWebSockets() {
try {
WebSocketApplication app = toWebSocketApplication();
// If application present call its methods
TyrusSupport.Builder builder = TyrusSupport.builder();
Optional<Class<? extends ServerApplicationConfig>> appClass = app.applicationClass();
Optional<String> contextRoot = appClass.flatMap(c -> findContextRoot(config, c));
Optional<String> namedRouting = appClass.flatMap(c -> findNamedRouting(config, c));
boolean routingNameRequired = appClass.map(c -> isNamedRoutingRequired(config, c)).orElse(false);
Routing.Builder routing;
if (appClass.isPresent()) {
Class<? extends ServerApplicationConfig> c = appClass.get();
// Attempt to instantiate via CDI
ServerApplicationConfig instance = null;
try {
instance = CDI.current().select(c).get();
} catch (UnsatisfiedResolutionException e) {
// falls through
}
// Otherwise, we create instance directly
if (instance == null) {
try {
instance = c.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException("Unable to instantiate websocket application " + c, e);
}
}
// Call methods in application class
Set<ServerEndpointConfig> endpointConfigs = instance.getEndpointConfigs(app.programmaticEndpoints());
Set<Class<?>> endpointClasses = instance.getAnnotatedEndpointClasses(app.annotatedEndpoints());
// Register classes and configs
endpointClasses.forEach(builder::register);
endpointConfigs.forEach(builder::register);
// Create routing builder
routing = serverCdiExtension.routingBuilder(namedRouting, routingNameRequired, c.getName());
} else {
// Direct registration without calling application class
app.annotatedEndpoints().forEach(builder::register);
app.programmaticEndpoints().forEach(builder::register);
app.extensions().forEach(builder::register);
// Create routing builder
routing = serverCdiExtension.serverRoutingBuilder();
}
// Finally register WebSockets in Helidon routing
String rootPath = contextRoot.orElse(DEFAULT_WEBSOCKET_PATH);
LOGGER.info("Registering websocket application at " + rootPath);
routing.register(rootPath, new TyrusSupportMp(builder.build()));
} catch (IllegalArgumentException e) {
throw new RuntimeException("Unable to load WebSocket extension", e);
}
}
Aggregations