Search in sources :

Example 1 with ConstructionException

use of org.apache.xbean.recipe.ConstructionException in project tomee by apache.

the class Assembler method doCreateResource.

private Object doCreateResource(final Collection<ServiceInfo> infos, final ResourceInfo serviceInfo) throws OpenEJBException {
    // do it early otherwise we can loose it
    final String skipPropertiesFallback = (String) serviceInfo.properties.remove("SkipPropertiesFallback");
    final ObjectRecipe serviceRecipe = createRecipe(infos, serviceInfo);
    final boolean properties = PropertiesFactory.class.getName().equals(serviceInfo.className);
    if ("false".equalsIgnoreCase(serviceInfo.properties.getProperty("SkipImplicitAttributes", "false")) && !properties) {
        serviceRecipe.setProperty("transactionManager", transactionManager);
        serviceRecipe.setProperty("ServiceId", serviceInfo.id);
    }
    serviceInfo.properties.remove("SkipImplicitAttributes");
    // if custom instance allow to skip properties fallback to avoid to set unexpectedly it - connectionProps of DBs
    final AtomicReference<Properties> injectedProperties = new AtomicReference<>();
    if (!"true".equalsIgnoreCase(skipPropertiesFallback)) {
        serviceRecipe.setProperty("properties", new UnsetPropertiesRecipe() {

            @Override
            protected Object internalCreate(final Type expectedType, final boolean lazyRefAllowed) throws ConstructionException {
                final Map<String, Object> original = serviceRecipe.getUnsetProperties();
                final Properties properties = new SuperProperties() {

                    @Override
                    public Object remove(final Object key) {
                        // avoid to log them then
                        original.remove(key);
                        return super.remove(key);
                    }
                }.caseInsensitive(// keep our nice case insensitive feature
                true);
                for (final Map.Entry<String, Object> entry : original.entrySet()) {
                    properties.put(entry.getKey(), entry.getValue());
                }
                injectedProperties.set(properties);
                return properties;
            }
        });
    } else {
        // this is not the best fallback we have but since it is super limited it is acceptable
        final Map<String, Object> unsetProperties = serviceRecipe.getUnsetProperties();
        injectedProperties.set(new Properties() {

            @Override
            public String getProperty(final String key) {
                final Object obj = unsetProperties.get(key);
                return String.class.isInstance(obj) ? String.valueOf(obj) : null;
            }

            @Override
            public Set<String> stringPropertyNames() {
                return unsetProperties.keySet();
            }

            @Override
            public Set<Object> keySet() {
                // noinspection unchecked
                return Set.class.cast(unsetProperties.keySet());
            }

            @Override
            public synchronized boolean containsKey(final Object key) {
                return getProperty(String.valueOf(key)) != null;
            }
        });
    }
    if (serviceInfo.types.contains("DataSource") || serviceInfo.types.contains(DataSource.class.getName())) {
        final Properties props = PropertyPlaceHolderHelper.simpleHolds(serviceInfo.properties);
        if (serviceInfo.properties.containsKey("Definition")) {
            final Object encoding = serviceInfo.properties.remove("DefinitionEncoding");
            try {
                // we catch classcast etc..., if it fails it is not important
                final InputStream is = new ByteArrayInputStream(serviceInfo.properties.getProperty("Definition").getBytes(encoding != null ? encoding.toString() : "ISO-8859-1"));
                final Properties p = new SuperProperties();
                IO.readProperties(is, p);
                for (final Entry<Object, Object> entry : p.entrySet()) {
                    final String key = entry.getKey().toString();
                    if (!props.containsKey(key) && !(key.equalsIgnoreCase("url") && props.containsKey("JdbcUrl"))) {
                        // with @DataSource we can get both, see org.apache.openejb.config.ConvertDataSourceDefinitions.rawDefinition()
                        props.put(key, entry.getValue());
                    }
                }
            } catch (final Exception e) {
            // ignored
            }
        }
        serviceRecipe.setProperty("Definition", PropertiesHelper.propertiesToString(props));
    }
    // else: any other kind of resource relying on it? shouldnt be
    replaceResourceAdapterProperty(serviceRecipe);
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    boolean customLoader = false;
    try {
        if (serviceInfo.classpath != null && serviceInfo.classpath.length > 0) {
            final URL[] urls = new URL[serviceInfo.classpath.length];
            for (int i = 0; i < serviceInfo.classpath.length; i++) {
                urls[i] = serviceInfo.classpath[i].toURL();
            }
            loader = new URLClassLoaderFirst(urls, loader);
            customLoader = true;
            serviceRecipe.setProperty("OpenEJBResourceClasspath", "true");
        }
    } catch (final MalformedURLException e) {
        throw new OpenEJBException("Unable to create a classloader for " + serviceInfo.id, e);
    }
    if (!customLoader && serviceInfo.classpathAPI != null) {
        throw new IllegalArgumentException("custom-api provided but not classpath used for " + serviceInfo.id);
    }
    Object service = serviceRecipe.create(loader);
    if (customLoader) {
        final Collection<Class<?>> apis;
        if (serviceInfo.classpathAPI == null) {
            apis = new ArrayList<>(Arrays.asList(service.getClass().getInterfaces()));
        } else {
            final String[] split = serviceInfo.classpathAPI.split(" *, *");
            apis = new ArrayList<>(split.length);
            final ClassLoader apiLoader = Thread.currentThread().getContextClassLoader();
            for (final String fqn : split) {
                try {
                    apis.add(apiLoader.loadClass(fqn));
                } catch (final ClassNotFoundException e) {
                    throw new IllegalArgumentException(fqn + " not usable as API for " + serviceInfo.id, e);
                }
            }
        }
        if (apis.size() - (apis.contains(Serializable.class) ? 1 : 0) - (apis.contains(Externalizable.class) ? 1 : 0) > 0) {
            service = Proxy.newProxyInstance(loader, apis.toArray(new Class<?>[apis.size()]), new ClassLoaderAwareHandler(null, service, loader));
        }
    // else proxy would be useless
    }
    serviceInfo.unsetProperties = injectedProperties.get();
    // Java Connector spec ResourceAdapters and ManagedConnectionFactories need special activation
    if (service instanceof ResourceAdapter) {
        final ResourceAdapter resourceAdapter = (ResourceAdapter) service;
        // Create a thead pool for work manager
        final int threadPoolSize = getIntProperty(serviceInfo.properties, "threadPoolSize", 30);
        final Executor threadPool;
        if (threadPoolSize <= 0) {
            logger.warning("Thread pool for '" + serviceInfo.id + "' is (unbounded), consider setting a size using: " + serviceInfo.id + ".QueueSize=[size]");
            threadPool = Executors.newCachedThreadPool(new DaemonThreadFactory(serviceInfo.id + "-worker-"));
        } else {
            threadPool = new ExecutorBuilder().size(threadPoolSize).prefix(serviceInfo.id).threadFactory(new DaemonThreadFactory(serviceInfo.id + "-worker-")).build(new Options(serviceInfo.properties, SystemInstance.get().getOptions()));
            logger.info("Thread pool size for '" + serviceInfo.id + "' is (" + threadPoolSize + ")");
        }
        // WorkManager: the resource adapter can use this to dispatch messages or perform tasks
        final WorkManager workManager;
        if (GeronimoTransactionManager.class.isInstance(transactionManager)) {
            final GeronimoTransactionManager geronimoTransactionManager = (GeronimoTransactionManager) transactionManager;
            final TransactionContextHandler txWorkContextHandler = new TransactionContextHandler(geronimoTransactionManager);
            // use id as default realm name if realm is not specified in service properties
            final String securityRealmName = getStringProperty(serviceInfo.properties, "realm", serviceInfo.id);
            final SecurityContextHandler securityContextHandler = new SecurityContextHandler(securityRealmName);
            final HintsContextHandler hintsContextHandler = new HintsContextHandler();
            final Collection<WorkContextHandler> workContextHandlers = new ArrayList<>();
            workContextHandlers.add(txWorkContextHandler);
            workContextHandlers.add(securityContextHandler);
            workContextHandlers.add(hintsContextHandler);
            workManager = new GeronimoWorkManager(threadPool, threadPool, threadPool, workContextHandlers);
        } else {
            workManager = new SimpleWorkManager(threadPool);
        }
        // BootstrapContext: wraps the WorkMananger and XATerminator
        final BootstrapContext bootstrapContext;
        if (transactionManager instanceof GeronimoTransactionManager) {
            bootstrapContext = new GeronimoBootstrapContext(GeronimoWorkManager.class.cast(workManager), (GeronimoTransactionManager) transactionManager, (GeronimoTransactionManager) transactionManager);
        } else if (transactionManager instanceof XATerminator) {
            bootstrapContext = new SimpleBootstrapContext(workManager, (XATerminator) transactionManager);
        } else {
            bootstrapContext = new SimpleBootstrapContext(workManager);
        }
        // start the resource adapter
        try {
            logger.debug("createResource.startingResourceAdapter", serviceInfo.id, service.getClass().getName());
            resourceAdapter.start(bootstrapContext);
        } catch (final ResourceAdapterInternalException e) {
            throw new OpenEJBException(e);
        }
        final Map<String, Object> unset = serviceRecipe.getUnsetProperties();
        unset.remove("threadPoolSize");
        logUnusedProperties(unset, serviceInfo);
        registerAsMBean(serviceInfo.id, "ResourceAdapter", resourceAdapter);
        service = new ResourceAdapterReference(resourceAdapter, threadPool, OPENEJB_RESOURCE_JNDI_PREFIX + serviceInfo.id);
    } else if (service instanceof ManagedConnectionFactory) {
        final ManagedConnectionFactory managedConnectionFactory = (ManagedConnectionFactory) service;
        // connection manager is constructed via a recipe so we automatically expose all cmf properties
        final ObjectRecipe connectionManagerRecipe = new ObjectRecipe(GeronimoConnectionManagerFactory.class, "create");
        connectionManagerRecipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
        connectionManagerRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
        connectionManagerRecipe.setAllProperties(serviceInfo.properties);
        connectionManagerRecipe.setProperty("name", serviceInfo.id);
        connectionManagerRecipe.setProperty("mcf", managedConnectionFactory);
        // standard properties
        connectionManagerRecipe.setProperty("transactionManager", transactionManager);
        ClassLoader classLoader = loader;
        if (classLoader == null) {
            classLoader = getClass().getClassLoader();
        }
        if (classLoader == null) {
            classLoader = ClassLoader.getSystemClassLoader();
        }
        connectionManagerRecipe.setProperty("classLoader", classLoader);
        logger.getChildLogger("service").info("createResource.createConnectionManager", serviceInfo.id, service.getClass().getName());
        // create the connection manager
        final ConnectionManager connectionManager = (ConnectionManager) connectionManagerRecipe.create();
        String txSupport = "xa";
        try {
            txSupport = (String) connectionManagerRecipe.getProperty("transactionSupport");
        } catch (Exception e) {
        // ignore
        }
        if (txSupport == null || txSupport.trim().length() == 0) {
            txSupport = "xa";
        }
        if (connectionManager == null) {
            throw new OpenEJBRuntimeException(messages.format("assembler.invalidConnectionManager", serviceInfo.id));
        }
        final Map<String, Object> unsetA = serviceRecipe.getUnsetProperties();
        final Map<String, Object> unsetB = connectionManagerRecipe.getUnsetProperties();
        final Map<String, Object> unset = new HashMap<>();
        for (final Entry<String, Object> entry : unsetA.entrySet()) {
            if (unsetB.containsKey(entry.getKey())) {
                unset.put(entry.getKey(), entry.getValue());
            }
        }
        // service becomes a ConnectorReference which merges connection manager and mcf
        service = new ConnectorReference(connectionManager, managedConnectionFactory);
        // init cm if needed
        final Object eagerInit = unset.remove("eagerInit");
        if (eagerInit != null && eagerInit instanceof String && "true".equalsIgnoreCase((String) eagerInit) && connectionManager instanceof AbstractConnectionManager) {
            try {
                ((AbstractConnectionManager) connectionManager).doStart();
                try {
                    final Object cf = managedConnectionFactory.createConnectionFactory(connectionManager);
                    if (cf instanceof ConnectionFactory) {
                        final Connection connection = ((ConnectionFactory) cf).getConnection();
                        connection.getMetaData();
                        connection.close();
                    }
                } catch (final Exception e) {
                // no-op: just to force eager init of pool
                }
            } catch (final Exception e) {
                logger.warning("Can't start connection manager", e);
            }
        }
        logUnusedProperties(unset, serviceInfo);
    } else if (service instanceof DataSource) {
        ClassLoader classLoader = loader;
        if (classLoader == null) {
            classLoader = getClass().getClassLoader();
        }
        final ImportSql importer = new ImportSql(classLoader, serviceInfo.id, (DataSource) service);
        if (importer.hasSomethingToImport()) {
            importer.doImport();
        }
        final ObjectRecipe recipe = DataSourceFactory.forgetRecipe(service, serviceRecipe);
        if (recipe != serviceRecipe || !serviceInfo.properties.containsKey("XaDataSource")) {
            logUnusedProperties(recipe, serviceInfo);
        }
        // else logged on xadatasource itself
        final Properties prop = serviceInfo.properties;
        String url = prop.getProperty("JdbcUrl", prop.getProperty("url"));
        if (url == null) {
            url = prop.getProperty("jdbcUrl");
        }
        if (url == null) {
            logger.debug("Unable to find url for " + serviceInfo.id + " will not monitor it");
        } else {
            final String host = extractHost(url);
            if (host != null) {
                remoteResourceMonitor.addHost(host);
                remoteResourceMonitor.registerIfNot();
            }
        }
    } else if (!Properties.class.isInstance(service)) {
        if (serviceInfo.unsetProperties == null || isTemplatizedResource(serviceInfo)) {
            logUnusedProperties(serviceRecipe, serviceInfo);
        }
        // else wait post construct
        registerAsMBean(serviceInfo.id, "Resource", service);
    }
    final ResourceCreated event = new ResourceCreated(service, serviceInfo.id);
    SystemInstance.get().fireEvent(event);
    return event.getReplacement() == null ? service : event.getReplacement();
}
Also used : URLClassLoaderFirst(org.apache.openejb.util.classloader.URLClassLoaderFirst) OpenEJBException(org.apache.openejb.OpenEJBException) MalformedURLException(java.net.MalformedURLException) Serializable(java.io.Serializable) GeronimoBootstrapContext(org.apache.geronimo.connector.GeronimoBootstrapContext) TransactionContextHandler(org.apache.geronimo.connector.work.TransactionContextHandler) ArrayList(java.util.ArrayList) GeronimoWorkManager(org.apache.geronimo.connector.work.GeronimoWorkManager) SuperProperties(org.apache.openejb.util.SuperProperties) ExecutorBuilder(org.apache.openejb.util.ExecutorBuilder) AbstractConnectionManager(org.apache.geronimo.connector.outbound.AbstractConnectionManager) ConnectionManager(javax.resource.spi.ConnectionManager) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) SimpleWorkManager(org.apache.openejb.core.transaction.SimpleWorkManager) WorkManager(javax.resource.spi.work.WorkManager) GeronimoWorkManager(org.apache.geronimo.connector.work.GeronimoWorkManager) ResourceAdapter(javax.resource.spi.ResourceAdapter) SecurityContextHandler(org.apache.openejb.core.security.SecurityContextHandler) UnsetPropertiesRecipe(org.apache.xbean.recipe.UnsetPropertiesRecipe) GeronimoConnectionManagerFactory(org.apache.openejb.resource.GeronimoConnectionManagerFactory) DataSource(javax.sql.DataSource) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) HintsContextHandler(org.apache.geronimo.connector.work.HintsContextHandler) SimpleBootstrapContext(org.apache.openejb.core.transaction.SimpleBootstrapContext) ByteArrayInputStream(java.io.ByteArrayInputStream) PropertiesFactory(org.apache.openejb.resource.PropertiesFactory) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) ConnectorReference(org.apache.openejb.core.ConnectorReference) Options(org.apache.openejb.loader.Options) XATerminator(javax.resource.spi.XATerminator) HashSet(java.util.HashSet) Set(java.util.Set) UrlSet(org.apache.xbean.finder.UrlSet) ClassLoaderAwareHandler(org.apache.openejb.util.classloader.ClassLoaderAwareHandler) DaemonThreadFactory(org.apache.openejb.util.DaemonThreadFactory) SimpleWorkManager(org.apache.openejb.core.transaction.SimpleWorkManager) SimpleBootstrapContext(org.apache.openejb.core.transaction.SimpleBootstrapContext) GeronimoBootstrapContext(org.apache.geronimo.connector.GeronimoBootstrapContext) BootstrapContext(javax.resource.spi.BootstrapContext) AbstractConnectionManager(org.apache.geronimo.connector.outbound.AbstractConnectionManager) SuperProperties(org.apache.openejb.util.SuperProperties) Properties(java.util.Properties) URL(java.net.URL) Entry(java.util.Map.Entry) ManagedConnectionFactory(javax.resource.spi.ManagedConnectionFactory) ConnectionFactory(javax.resource.cci.ConnectionFactory) Executor(java.util.concurrent.Executor) GeronimoTransactionManager(org.apache.geronimo.transaction.manager.GeronimoTransactionManager) ResourceCreated(org.apache.openejb.assembler.classic.event.ResourceCreated) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(javax.resource.cci.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResourceAdapterInternalException(javax.resource.spi.ResourceAdapterInternalException) InvalidObjectException(java.io.InvalidObjectException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ObjectStreamException(java.io.ObjectStreamException) ResourceAdapterInternalException(javax.resource.spi.ResourceAdapterInternalException) URISyntaxException(java.net.URISyntaxException) UndeployException(org.apache.openejb.UndeployException) DefinitionException(javax.enterprise.inject.spi.DefinitionException) ConstructionException(org.apache.xbean.recipe.ConstructionException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ValidationException(javax.validation.ValidationException) MalformedObjectNameException(javax.management.MalformedObjectNameException) DuplicateDeploymentIdException(org.apache.openejb.DuplicateDeploymentIdException) TimeoutException(java.util.concurrent.TimeoutException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) DeploymentException(javax.enterprise.inject.spi.DeploymentException) NoSuchApplicationException(org.apache.openejb.NoSuchApplicationException) MalformedURLException(java.net.MalformedURLException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) BeanType(org.apache.openejb.BeanType) Type(java.lang.reflect.Type) TransactionType(org.apache.openejb.core.transaction.TransactionType) ManagedConnectionFactory(javax.resource.spi.ManagedConnectionFactory) ConstructionException(org.apache.xbean.recipe.ConstructionException) WorkContextHandler(org.apache.geronimo.connector.work.WorkContextHandler)

Example 2 with ConstructionException

use of org.apache.xbean.recipe.ConstructionException in project tomee by apache.

the class BeanContext method newInstance.

@SuppressWarnings("unchecked")
public InstanceContext newInstance() throws Exception {
    final boolean dynamicallyImplemented = isDynamicallyImplemented();
    final WebBeansContext webBeansContext = getWebBeansContext();
    if (dynamicallyImplemented) {
        if (!InvocationHandler.class.isAssignableFrom(getProxyClass())) {
            throw new OpenEJBException("proxy class can only be InvocationHandler");
        }
    }
    final ThreadContext callContext = new ThreadContext(this, null, Operation.INJECTION);
    final ThreadContext oldContext = ThreadContext.enter(callContext);
    try {
        final Context ctx = getJndiEnc();
        final Class beanClass = getBeanClass();
        final CurrentCreationalContext<Object> currentCreationalContext = get(CurrentCreationalContext.class);
        CreationalContext<Object> creationalContext = currentCreationalContext != null ? currentCreationalContext.get() : null;
        final CdiEjbBean cdiEjbBean = get(CdiEjbBean.class);
        if (!CreationalContextImpl.class.isInstance(creationalContext) && webBeansContext != null) {
            if (creationalContext == null) {
                creationalContext = webBeansContext.getCreationalContextFactory().getCreationalContext(cdiEjbBean);
            } else {
                creationalContext = webBeansContext.getCreationalContextFactory().wrappedCreationalContext(creationalContext, cdiEjbBean);
            }
        }
        final Object rootInstance;
        if (cdiEjbBean != null && !dynamicallyImplemented && CdiEjbBean.EjbInjectionTargetImpl.class.isInstance(cdiEjbBean.getInjectionTarget())) {
            rootInstance = CdiEjbBean.EjbInjectionTargetImpl.class.cast(cdiEjbBean.getInjectionTarget()).createNewPojo(creationalContext);
        } else {
            // not a cdi bean
            rootInstance = getManagedClass().newInstance();
        }
        // Create bean instance
        Object beanInstance;
        final InjectionProcessor injectionProcessor;
        if (!dynamicallyImplemented) {
            injectionProcessor = new InjectionProcessor(rootInstance, getInjections(), InjectionProcessor.unwrap(ctx));
            beanInstance = injectionProcessor.createInstance();
            inject(beanInstance, creationalContext);
        } else {
            // update target
            final List<Injection> newInjections = new ArrayList<>();
            for (final Injection injection : getInjections()) {
                if (beanClass.equals(injection.getTarget())) {
                    final Injection updated = new Injection(injection.getJndiName(), injection.getName(), proxyClass);
                    newInjections.add(updated);
                } else {
                    newInjections.add(injection);
                }
            }
            injections.clear();
            injections.addAll(newInjections);
            injectionProcessor = new InjectionProcessor(rootInstance, injections, InjectionProcessor.unwrap(ctx));
            final InvocationHandler handler = (InvocationHandler) injectionProcessor.createInstance();
            beanInstance = DynamicProxyImplFactory.newProxy(this, handler);
            inject(handler, creationalContext);
        }
        // Create interceptors
        final Map<String, Object> interceptorInstances = new LinkedHashMap<>();
        // Add the stats interceptor instance and other already created interceptor instances
        for (final InterceptorInstance interceptorInstance : this.getUserAndSystemInterceptors()) {
            final Class clazz = interceptorInstance.getData().getInterceptorClass();
            interceptorInstances.put(clazz.getName(), interceptorInstance.getInterceptor());
        }
        final Collection<DependentCreationalContext<?>> createdDependents = getDependents(creationalContext);
        for (final InterceptorData interceptorData : this.getInstanceScopedInterceptors()) {
            if (interceptorData.getInterceptorClass().equals(beanClass)) {
                continue;
            }
            final Class clazz = interceptorData.getInterceptorClass();
            final Object iInstance;
            if (webBeansContext != null) {
                Object preInstantiated = null;
                if (createdDependents != null) {
                    for (final DependentCreationalContext<?> dcc : createdDependents) {
                        if (clazz.isInstance(dcc.getInstance())) {
                            // is that enough? do we have more to match?
                            preInstantiated = dcc.getInstance();
                            break;
                        }
                    }
                }
                if (preInstantiated != null) {
                    iInstance = preInstantiated;
                } else {
                    ConstructorInjectionBean interceptorConstructor = interceptorData.get(ConstructorInjectionBean.class);
                    if (interceptorConstructor == null) {
                        synchronized (this) {
                            interceptorConstructor = interceptorData.get(ConstructorInjectionBean.class);
                            if (interceptorConstructor == null) {
                                interceptorConstructor = new ConstructorInjectionBean(webBeansContext, clazz, webBeansContext.getAnnotatedElementFactory().newAnnotatedType(clazz));
                                interceptorData.set(ConstructorInjectionBean.class, interceptorConstructor);
                            }
                        }
                    }
                    CreationalContextImpl cc = (CreationalContextImpl) creationalContext;
                    Object oldDelegate = cc.putDelegate(beanInstance);
                    Bean<?> oldBean = cc.putBean(cdiEjbBean);
                    Contextual<?> oldContextual = cc.putContextual(interceptorData.getCdiInterceptorBean() != null ? interceptorData.getCdiInterceptorBean() : // otherwise BeanMetaData is broken
                    interceptorConstructor);
                    try {
                        iInstance = interceptorConstructor.create(creationalContext);
                    } finally {
                        cc.putBean(oldBean);
                        cc.putContextual(oldContextual);
                        cc.putDelegate(oldDelegate);
                    }
                }
            } else {
                iInstance = clazz.newInstance();
            }
            final InjectionProcessor interceptorInjector = new InjectionProcessor(iInstance, this.getInjections(), InjectionProcessor.unwrap(ctx));
            try {
                final Object interceptorInstance = interceptorInjector.createInstance();
                if (webBeansContext != null) {
                    try {
                        OWBInjector.inject(webBeansContext.getBeanManagerImpl(), interceptorInstance, creationalContext);
                    } catch (final Throwable t) {
                    // TODO handle this differently
                    // this is temporary till the injector can be rewritten
                    }
                }
                interceptorInstances.put(clazz.getName(), interceptorInstance);
            } catch (final ConstructionException e) {
                throw new Exception("Failed to create interceptor: " + clazz.getName(), e);
            }
        }
        interceptorInstances.put(beanClass.getName(), beanInstance);
        // Invoke post construct method
        callContext.setCurrentOperation(Operation.POST_CONSTRUCT);
        final List<InterceptorData> callbackInterceptors = this.getCallbackInterceptors();
        final InterceptorStack postConstruct = new InterceptorStack(beanInstance, null, Operation.POST_CONSTRUCT, callbackInterceptors, interceptorInstances);
        // Transaction Demarcation for Singleton PostConstruct method
        TransactionType transactionType;
        if (componentType == BeanType.SINGLETON || componentType == BeanType.STATEFUL) {
            final Set<Method> callbacks = callbackInterceptors.get(callbackInterceptors.size() - 1).getPostConstruct();
            if (callbacks.isEmpty()) {
                transactionType = TransactionType.RequiresNew;
            } else {
                // TODO: we should take the last one I think
                transactionType = getTransactionType(callbacks.iterator().next());
                if (transactionType == TransactionType.Required) {
                    transactionType = TransactionType.RequiresNew;
                }
            }
        } else {
            transactionType = isBeanManagedTransaction() ? TransactionType.BeanManaged : TransactionType.NotSupported;
        }
        final TransactionPolicy transactionPolicy = EjbTransactionUtil.createTransactionPolicy(transactionType, callContext);
        try {
            // Call the chain
            if (cdiEjbBean != null) {
                // call it, it has no postconstruct but extensions can add stuff here, TODO: see if it should be called before or after effective postconstruct
                cdiEjbBean.getInjectionTarget().postConstruct(beanInstance);
            }
            postConstruct.invoke();
        } catch (final Throwable e) {
            // RollBack Transaction
            EjbTransactionUtil.handleSystemException(transactionPolicy, e, callContext);
        } finally {
            EjbTransactionUtil.afterInvoke(transactionPolicy, callContext);
        }
        // handle cdi decorators
        if (cdiEjbBean != null) {
            final Class<?> proxyClass = Class.class.cast(Reflections.get(cdiEjbBean.getInjectionTarget(), "proxyClass"));
            if (proxyClass != null) {
                // means interception
                final InterceptorResolutionService.BeanInterceptorInfo interceptorInfo = cdiEjbBean.getBeanContext().get(InterceptorResolutionService.BeanInterceptorInfo.class);
                if (interceptorInfo.getDecorators() != null && !interceptorInfo.getDecorators().isEmpty()) {
                    final InterceptorDecoratorProxyFactory pf = webBeansContext.getInterceptorDecoratorProxyFactory();
                    // decorators
                    final Object instance = beanInstance;
                    final List<Decorator<?>> decorators = interceptorInfo.getDecorators();
                    final Map<Decorator<?>, Object> instances = new HashMap<>();
                    for (int i = decorators.size(); i > 0; i--) {
                        final Decorator<?> decorator = decorators.get(i - 1);
                        CreationalContextImpl cc = (CreationalContextImpl) creationalContext;
                        Object oldDelegate = cc.putDelegate(beanInstance);
                        Bean<?> oldBean = cc.putBean(cdiEjbBean);
                        // otherwise BeanMetaData is broken
                        Contextual<?> oldContextual = cc.putContextual(decorator);
                        Object decoratorInstance = null;
                        try {
                            decoratorInstance = decorator.create(CreationalContext.class.cast(creationalContext));
                        } finally {
                            cc.putBean(oldBean);
                            cc.putContextual(oldContextual);
                            cc.putDelegate(oldDelegate);
                        }
                        instances.put(decorator, decoratorInstance);
                        beanInstance = pf.createProxyInstance(proxyClass, instance, new DecoratorHandler(interceptorInfo, decorators, instances, i - 1, instance, cdiEjbBean.getId()));
                    }
                }
            }
        }
        return new InstanceContext(this, beanInstance, interceptorInstances, creationalContext);
    } finally {
        ThreadContext.exit(oldContext);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) CreationalContextImpl(org.apache.webbeans.context.creational.CreationalContextImpl) LinkedHashMap(java.util.LinkedHashMap) InstanceContext(org.apache.openejb.core.InstanceContext) InterceptorInstance(org.apache.openejb.core.interceptor.InterceptorInstance) Method(java.lang.reflect.Method) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) TimedObject(javax.ejb.TimedObject) EJBLocalObject(javax.ejb.EJBLocalObject) EJBObject(javax.ejb.EJBObject) TransactionType(org.apache.openejb.core.transaction.TransactionType) CdiEjbBean(org.apache.openejb.cdi.CdiEjbBean) DecoratorHandler(org.apache.webbeans.intercept.DecoratorHandler) WebBeansContext(org.apache.webbeans.config.WebBeansContext) DependentCreationalContext(org.apache.webbeans.context.creational.DependentCreationalContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) CurrentCreationalContext(org.apache.openejb.cdi.CurrentCreationalContext) CreationalContext(javax.enterprise.context.spi.CreationalContext) Context(javax.naming.Context) DependentCreationalContext(org.apache.webbeans.context.creational.DependentCreationalContext) InstanceContext(org.apache.openejb.core.InstanceContext) ThreadContext(org.apache.openejb.core.ThreadContext) InterceptorResolutionService(org.apache.webbeans.intercept.InterceptorResolutionService) ConstructorInjectionBean(org.apache.openejb.cdi.ConstructorInjectionBean) ThreadContext(org.apache.openejb.core.ThreadContext) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) InvocationHandler(java.lang.reflect.InvocationHandler) ApplicationException(javax.ejb.ApplicationException) ConstructionException(org.apache.xbean.recipe.ConstructionException) Decorator(javax.enterprise.inject.spi.Decorator) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) ConstructionException(org.apache.xbean.recipe.ConstructionException) InterceptorDecoratorProxyFactory(org.apache.webbeans.proxy.InterceptorDecoratorProxyFactory)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 TransactionType (org.apache.openejb.core.transaction.TransactionType)2 ConstructionException (org.apache.xbean.recipe.ConstructionException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InvalidObjectException (java.io.InvalidObjectException)1 ObjectStreamException (java.io.ObjectStreamException)1 Serializable (java.io.Serializable)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1