Search in sources :

Example 1 with ManagedBeanComponentDescription

use of org.jboss.as.ee.managedbean.component.ManagedBeanComponentDescription in project wildfly by wildfly.

the class ManagedBeanAnnotationProcessor method deploy.

/**
 * Check the deployment annotation index for all classes with the @ManagedBean annotation.  For each class with the
 * annotation, collect all the required information to create a managed bean instance, and attach it to the context.
 *
 * @param phaseContext the deployment unit context
 * @throws DeploymentUnitProcessingException
 */
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final EEResourceReferenceProcessorRegistry registry = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.RESOURCE_REFERENCE_PROCESSOR_REGISTRY);
    final EEModuleDescription moduleDescription = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
    final CompositeIndex compositeIndex = deploymentUnit.getAttachment(Attachments.COMPOSITE_ANNOTATION_INDEX);
    final PropertyReplacer replacer = EJBAnnotationPropertyReplacement.propertyReplacer(deploymentUnit);
    if (compositeIndex == null) {
        return;
    }
    final List<AnnotationInstance> instances = compositeIndex.getAnnotations(MANAGED_BEAN_ANNOTATION_NAME);
    if (instances == null || instances.isEmpty()) {
        return;
    }
    for (AnnotationInstance instance : instances) {
        AnnotationTarget target = instance.target();
        if (!(target instanceof ClassInfo)) {
            throw EeLogger.ROOT_LOGGER.classOnlyAnnotation("@ManagedBean", target);
        }
        final ClassInfo classInfo = (ClassInfo) target;
        // skip if it's not a valid managed bean class
        if (!assertManagedBeanClassValidity(classInfo)) {
            continue;
        }
        final String beanClassName = classInfo.name().toString();
        // Get the managed bean name from the annotation
        final AnnotationValue nameValue = instance.value();
        final String beanName = (nameValue == null || nameValue.asString().isEmpty()) ? beanClassName : replacer.replaceProperties(nameValue.asString());
        final ManagedBeanComponentDescription componentDescription = new ManagedBeanComponentDescription(beanName, beanClassName, moduleDescription, deploymentUnit.getServiceName());
        // Add the view
        ViewDescription viewDescription = new ViewDescription(componentDescription, beanClassName);
        viewDescription.getConfigurators().addFirst(new ViewConfigurator() {

            public void configure(final DeploymentPhaseContext context, final ComponentConfiguration componentConfiguration, final ViewDescription description, final ViewConfiguration configuration) throws DeploymentUnitProcessingException {
                // Add MB association interceptors
                configuration.addClientPostConstructInterceptor(ManagedBeanCreateInterceptor.FACTORY, InterceptorOrder.ClientPostConstruct.INSTANCE_CREATE);
                final ClassLoader classLoader = componentConfiguration.getModuleClassLoader();
                configuration.addViewInterceptor(AccessCheckingInterceptor.getFactory(), InterceptorOrder.View.CHECKING_INTERCEPTOR);
                configuration.addViewInterceptor(new ImmediateInterceptorFactory(new ContextClassLoaderInterceptor(classLoader)), InterceptorOrder.View.TCCL_INTERCEPTOR);
            }
        });
        viewDescription.getBindingNames().addAll(Arrays.asList("java:module/" + beanName, "java:app/" + moduleDescription.getModuleName() + "/" + beanName));
        componentDescription.getViews().add(viewDescription);
        moduleDescription.addComponent(componentDescription);
        // register an EEResourceReferenceProcessor which can process @Resource references to this managed bean.
        registry.registerResourceReferenceProcessor(new ManagedBeanResourceReferenceProcessor(beanClassName));
    }
}
Also used : AnnotationTarget(org.jboss.jandex.AnnotationTarget) ViewConfigurator(org.jboss.as.ee.component.ViewConfigurator) DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) ManagedBeanComponentDescription(org.jboss.as.ee.managedbean.component.ManagedBeanComponentDescription) ViewDescription(org.jboss.as.ee.component.ViewDescription) CompositeIndex(org.jboss.as.server.deployment.annotation.CompositeIndex) ContextClassLoaderInterceptor(org.jboss.invocation.ContextClassLoaderInterceptor) ManagedBeanResourceReferenceProcessor(org.jboss.as.ee.managedbean.component.ManagedBeanResourceReferenceProcessor) DeploymentPhaseContext(org.jboss.as.server.deployment.DeploymentPhaseContext) EEResourceReferenceProcessorRegistry(org.jboss.as.ee.component.deployers.EEResourceReferenceProcessorRegistry) ComponentConfiguration(org.jboss.as.ee.component.ComponentConfiguration) ViewConfiguration(org.jboss.as.ee.component.ViewConfiguration) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) ImmediateInterceptorFactory(org.jboss.invocation.ImmediateInterceptorFactory) AnnotationValue(org.jboss.jandex.AnnotationValue) PropertyReplacer(org.jboss.metadata.property.PropertyReplacer) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ClassInfo(org.jboss.jandex.ClassInfo)

Example 2 with ManagedBeanComponentDescription

use of org.jboss.as.ee.managedbean.component.ManagedBeanComponentDescription in project wildfly by wildfly.

the class EjbComponentIntegrator method integrate.

@Override
public boolean integrate(ServiceName beanManagerServiceName, ComponentConfiguration configuration, ComponentDescription description, ServiceBuilder<?> weldComponentServiceBuilder, Supplier<ServiceName> bindingServiceNameSupplier, DefaultInterceptorIntegrationAction integrationAction, ComponentInterceptorSupport interceptorSupport) {
    if (description instanceof EJBComponentDescription) {
        ServiceName bindingServiceName = bindingServiceNameSupplier.get();
        integrationAction.perform(bindingServiceName);
        if (description.isPassivationApplicable()) {
            configuration.addPrePassivateInterceptor(factory(InterceptionType.PRE_PASSIVATE, weldComponentServiceBuilder, bindingServiceName, interceptorSupport), InterceptorOrder.ComponentPassivation.CDI_INTERCEPTORS);
            configuration.addPostActivateInterceptor(factory(InterceptionType.POST_ACTIVATE, weldComponentServiceBuilder, bindingServiceName, interceptorSupport), InterceptorOrder.ComponentPassivation.CDI_INTERCEPTORS);
        }
        if (description instanceof StatefulComponentDescription) {
            // add a context key for weld interceptor replication
            configuration.getInterceptorContextKeys().add(SerializedCdiInterceptorsKey.class);
        }
        return true;
    } else if (description instanceof ManagedBeanComponentDescription) {
        integrationAction.perform(bindingServiceNameSupplier.get());
        return true;
    }
    return false;
}
Also used : ManagedBeanComponentDescription(org.jboss.as.ee.managedbean.component.ManagedBeanComponentDescription) ServiceName(org.jboss.msc.service.ServiceName) StatefulComponentDescription(org.jboss.as.ejb3.component.stateful.StatefulComponentDescription) EJBComponentDescription(org.jboss.as.ejb3.component.EJBComponentDescription)

Example 3 with ManagedBeanComponentDescription

use of org.jboss.as.ee.managedbean.component.ManagedBeanComponentDescription in project wildfly by wildfly.

the class JaxrsComponentDeployer method deploy.

@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final Module module = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.MODULE);
    if (module == null) {
        return;
    }
    final ResteasyDeploymentData resteasy = deploymentUnit.getAttachment(JaxrsAttachments.RESTEASY_DEPLOYMENT_DATA);
    if (resteasy == null) {
        return;
    }
    // Set up the context for managed threads
    phaseContext.getDeploymentUnit().addToAttachmentList(Attachments.ADDITIONAL_FACTORIES, ResteasyContextHandleFactory.INSTANCE);
    // right now I only support resources
    if (!resteasy.isScanResources())
        return;
    final EEModuleDescription moduleDescription = deploymentUnit.getAttachment(Attachments.EE_MODULE_DESCRIPTION);
    if (moduleDescription == null) {
        return;
    }
    final ClassLoader loader = module.getClassLoader();
    final CapabilityServiceSupport support = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.CAPABILITY_SERVICE_SUPPORT);
    boolean partOfWeldDeployment = false;
    if (support.hasCapability(WELD_CAPABILITY_NAME)) {
        partOfWeldDeployment = support.getOptionalCapabilityRuntimeAPI(WELD_CAPABILITY_NAME, WeldCapability.class).get().isPartOfWeldDeployment(deploymentUnit);
    }
    for (final ComponentDescription component : moduleDescription.getComponentDescriptions()) {
        Class<?> componentClass = null;
        try {
            componentClass = loader.loadClass(component.getComponentClassName());
        } catch (ClassNotFoundException e) {
            throw new DeploymentUnitProcessingException(e);
        }
        if (!GetRestful.isRootResource(componentClass))
            continue;
        if (isInstanceOf(component, SESSION_BEAN_DESCRIPTION_CLASS_NAME)) {
            if (isInstanceOf(component, STATEFUL_SESSION_BEAN_DESCRIPTION_CLASS_NAME)) {
                // using SFSB's as Jakarta RESTful Web Services endpoints is not recommended, but if people really want to do it they can
                JAXRS_LOGGER.debugf("Stateful session bean %s is being used as a Jakarta RESTful Web Services endpoint, this is not recommended", component.getComponentName());
                if (partOfWeldDeployment) {
                    // if possible just let CDI handle the integration
                    continue;
                }
            }
            Class<?>[] jaxrsType = GetRestful.getSubResourceClasses(componentClass);
            final String jndiName;
            if (component.getViews().size() == 1) {
                // only 1 view, just use the simple JNDI name
                jndiName = "java:app/" + moduleDescription.getModuleName() + "/" + component.getComponentName();
            } else {
                boolean found = false;
                String foundType = null;
                for (final ViewDescription view : component.getViews()) {
                    for (Class<?> subResource : jaxrsType) {
                        if (view.getViewClassName().equals(subResource.getName())) {
                            foundType = subResource.getName();
                            found = true;
                            break;
                        }
                    }
                    if (found) {
                        break;
                    }
                }
                if (!found) {
                    throw JAXRS_LOGGER.typeNameNotAnEjbView(Arrays.asList(jaxrsType), component.getComponentName());
                }
                jndiName = "java:app/" + moduleDescription.getModuleName() + "/" + component.getComponentName() + "!" + foundType;
            }
            JAXRS_LOGGER.debugf("Found Jakarta RESTful Web Services Managed Bean: %s local jndi jaxRsTypeName: %s", component.getComponentClassName(), jndiName);
            StringBuilder buf = new StringBuilder();
            buf.append(jndiName).append(";").append(component.getComponentClassName()).append(";").append("true");
            resteasy.getScannedJndiComponentResources().add(buf.toString());
            // make sure its removed from list
            resteasy.getScannedResourceClasses().remove(component.getComponentClassName());
        } else if (component instanceof ManagedBeanComponentDescription) {
            String jndiName = "java:app/" + moduleDescription.getModuleName() + "/" + component.getComponentName();
            JAXRS_LOGGER.debugf("Found Jakarta RESTful Web Services Managed Bean: %s local jndi name: %s", component.getComponentClassName(), jndiName);
            StringBuilder buf = new StringBuilder();
            buf.append(jndiName).append(";").append(component.getComponentClassName()).append(";").append("true");
            resteasy.getScannedJndiComponentResources().add(buf.toString());
            // make sure its removed from list
            resteasy.getScannedResourceClasses().remove(component.getComponentClassName());
        }
    }
}
Also used : DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) ManagedBeanComponentDescription(org.jboss.as.ee.managedbean.component.ManagedBeanComponentDescription) ComponentDescription(org.jboss.as.ee.component.ComponentDescription) ManagedBeanComponentDescription(org.jboss.as.ee.managedbean.component.ManagedBeanComponentDescription) ViewDescription(org.jboss.as.ee.component.ViewDescription) WeldCapability(org.jboss.as.weld.WeldCapability) CapabilityServiceSupport(org.jboss.as.controller.capability.CapabilityServiceSupport) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit)

Aggregations

ManagedBeanComponentDescription (org.jboss.as.ee.managedbean.component.ManagedBeanComponentDescription)3 EEModuleDescription (org.jboss.as.ee.component.EEModuleDescription)2 ViewDescription (org.jboss.as.ee.component.ViewDescription)2 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)2 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)2 CapabilityServiceSupport (org.jboss.as.controller.capability.CapabilityServiceSupport)1 ComponentConfiguration (org.jboss.as.ee.component.ComponentConfiguration)1 ComponentDescription (org.jboss.as.ee.component.ComponentDescription)1 ViewConfiguration (org.jboss.as.ee.component.ViewConfiguration)1 ViewConfigurator (org.jboss.as.ee.component.ViewConfigurator)1 EEResourceReferenceProcessorRegistry (org.jboss.as.ee.component.deployers.EEResourceReferenceProcessorRegistry)1 ManagedBeanResourceReferenceProcessor (org.jboss.as.ee.managedbean.component.ManagedBeanResourceReferenceProcessor)1 EJBComponentDescription (org.jboss.as.ejb3.component.EJBComponentDescription)1 StatefulComponentDescription (org.jboss.as.ejb3.component.stateful.StatefulComponentDescription)1 DeploymentPhaseContext (org.jboss.as.server.deployment.DeploymentPhaseContext)1 CompositeIndex (org.jboss.as.server.deployment.annotation.CompositeIndex)1 WeldCapability (org.jboss.as.weld.WeldCapability)1 ContextClassLoaderInterceptor (org.jboss.invocation.ContextClassLoaderInterceptor)1 ImmediateInterceptorFactory (org.jboss.invocation.ImmediateInterceptorFactory)1 AnnotationInstance (org.jboss.jandex.AnnotationInstance)1