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

com.canoo.dolphin.server.javaee.CdiContainerManager Maven / Gradle / Ivy

Go to download

The Dolphin Platform is a framework that implements the presentation model pattern and provides amodern way to create enterprise applications. The Platform provides several client implementations that all canbe used in combination with a general sever API.

There is a newer version: 1.0.0.CR5
Show newest version
/*
 * Copyright 2015-2017 Canoo Engineering AG.
 *
 * 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 com.canoo.dolphin.server.javaee;

import com.canoo.dolphin.server.container.ContainerManager;
import com.canoo.dolphin.server.container.ModelInjector;
import com.canoo.dolphin.util.Assert;
import org.apache.deltaspike.core.api.provider.BeanManagerProvider;
import org.apache.deltaspike.core.util.bean.BeanBuilder;
import org.apache.deltaspike.core.util.metadata.builder.DelegatingContextualLifecycle;

import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * JavaEE / CDI based implementation of the {@link ContainerManager}
 *
 * @author Hendrik Ebbers
 */
public class CdiContainerManager implements ContainerManager {

    private Map contextMap = new HashMap<>();

    private Map beanMap = new HashMap<>();

    @Override
    public void init(ServletContext servletContext) {

    }

    @Override
    public  T createManagedController(final Class controllerClass, final ModelInjector modelInjector) {
        Assert.requireNonNull(controllerClass, "controllerClass");
        Assert.requireNonNull(modelInjector, "modelInjector");
        BeanManager bm = BeanManagerProvider.getInstance().getBeanManager();
        AnnotatedType annotatedType = bm.createAnnotatedType(controllerClass);
        final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
        final Bean bean = new BeanBuilder(bm)
                .beanClass(controllerClass)
                .name(UUID.randomUUID().toString())
                .scope(Dependent.class)
                .beanLifecycle(new DolphinPlatformContextualLifecycle(injectionTarget, modelInjector))
                .create();
        Class beanClass = bean.getBeanClass();
        CreationalContext creationalContext = bm.createCreationalContext(bean);
        T instance = (T) bm.getReference(bean, beanClass, creationalContext);
        contextMap.put(instance, creationalContext);
        beanMap.put(instance, bean);
        return instance;
    }

    @Override
    public  T createListener(Class listenerClass) {
        Assert.requireNonNull(listenerClass, "listenerClass");
        BeanManager bm = BeanManagerProvider.getInstance().getBeanManager();
        AnnotatedType annotatedType = bm.createAnnotatedType(listenerClass);
        final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
        final Bean bean = new BeanBuilder(bm)
                .beanClass(listenerClass)
                .scope(Dependent.class)
                .name(UUID.randomUUID().toString())
                .beanLifecycle(new DelegatingContextualLifecycle(injectionTarget))
                .create();
        Class beanClass = bean.getBeanClass();
        CreationalContext creationalContext = bm.createCreationalContext(bean);
        T instance = (T) bm.getReference(bean, beanClass, creationalContext);
        contextMap.put(instance, creationalContext);
        beanMap.put(instance, bean);
        return instance;
    }

    @Override
    public void destroyController(Object instance, Class controllerClass) {
        Assert.requireNonNull(instance, "instance");
        Bean bean = beanMap.remove(instance);
        CreationalContext context = contextMap.remove(instance);
        bean.destroy(instance, context);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy