All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.weld.module.ejb.SessionBeanProxyInstantiator Maven / Gradle / Ivy

There is a newer version: 6.0.0.Beta4
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2012, 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.module.ejb;

import java.lang.reflect.Constructor;

import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.context.spi.CreationalContext;

import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.bean.SessionBean;
import org.jboss.weld.bean.proxy.EnterpriseTargetBeanInstance;
import org.jboss.weld.bean.proxy.ProxyFactory;
import org.jboss.weld.injection.producer.Instantiator;
import org.jboss.weld.logging.BeanLogger;
import org.jboss.weld.manager.BeanManagerImpl;

/**
 * Instantiator implementation that instantiates a proxy for a session bean.
 *
 * @author Jozef Hartinger
 */
class SessionBeanProxyInstantiator implements Instantiator {

    private final Class proxyClass;
    private final SessionBeanImpl bean;

    SessionBeanProxyInstantiator(EnhancedAnnotatedType type, SessionBeanImpl bean) {
        this.bean = bean;
        this.proxyClass = new EnterpriseProxyFactory(type.getJavaClass(), bean).getProxyClass();
    }

    @Override
    public T newInstance(CreationalContext ctx, BeanManagerImpl manager) {
        try {
            T instance = proxyClass.getDeclaredConstructor().newInstance();
            if (!bean.getScope().equals(Dependent.class)) {
                ctx.push(instance);
            }
            ProxyFactory.setBeanInstance(bean.getBeanManager().getContextId(), instance, createEnterpriseTargetBeanInstance(),
                    bean);
            return instance;
        } catch (Exception e) {
            throw BeanLogger.LOG.sessionBeanProxyInstantiationFailed(bean, proxyClass, e);
        }
    }

    protected EnterpriseTargetBeanInstance createEnterpriseTargetBeanInstance() {
        if (bean.getEjbDescriptor().isStateless() || bean.getEjbDescriptor().isSingleton()) {
            return new InjectionPointPropagatingEnterpriseTargetBeanInstance(bean.getBeanClass(),
                    new EnterpriseBeanProxyMethodHandler(bean), bean.getBeanManager());
        } else {
            return new EnterpriseTargetBeanInstance(bean.getBeanClass(), new EnterpriseBeanProxyMethodHandler(bean));
        }
    }

    @Override
    public boolean hasInterceptorSupport() {
        return false;
    }

    @Override
    public boolean hasDecoratorSupport() {
        return false;
    }

    public SessionBean getBean() {
        return bean;
    }

    @Override
    public Constructor getConstructor() {
        return null; // not relevant
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy