/**
 * Copyright (C) 2014 Antonin Stefanutti (antonin.stefanutti@gmail.com)
 *
 * 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.apache.camel.cdi;

import javax.enterprise.context.ApplicationScoped;
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.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.enterprise.inject.spi.PassivationCapable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

final class CdiCamelContextBean implements Bean<CdiCamelContext>, PassivationCapable {

    private final Set<Type> types;

    private final InjectionTarget<CdiCamelContext> target;

    CdiCamelContextBean(BeanManager manager) {
        AnnotatedType<CdiCamelContext> annotatedType = manager.createAnnotatedType(CdiCamelContext.class);
        this.types = Collections.unmodifiableSet(annotatedType.getTypeClosure());
        this.target = manager.createInjectionTarget(annotatedType);
    }

    @Override
    public Class<? extends Annotation> getScope() {
        return ApplicationScoped.class;
    }

    @Override
    public Set<Annotation> getQualifiers() {
        return Collections.unmodifiableSet(new HashSet<>(Arrays.asList(DefaultLiteral.INSTANCE, AnyLiteral.INSTANCE)));
    }

    @Override
    public CdiCamelContext create(CreationalContext<CdiCamelContext> creational) {
        CdiCamelContext context = target.produce(creational);
        target.inject(context, creational);
        target.postConstruct(context);
        creational.push(context);
        return context;
    }

    @Override
    public void destroy(CdiCamelContext instance, CreationalContext<CdiCamelContext> creational) {
        target.preDestroy(instance);
        target.dispose(instance);
        creational.release();
    }

    @Override
    public Class<CdiCamelContext> getBeanClass() {
        return CdiCamelContext.class;
    }

    @Override
    public Set<InjectionPoint> getInjectionPoints() {
        return Collections.emptySet();
    }

    @Override
    public String getName() {
        return "camel-cdi";
    }

    @Override
    public String toString() {
        return "Default CdiCamelContext Bean";
    }

    @Override
    public Set<Class<? extends Annotation>> getStereotypes() {
        return Collections.emptySet();
    }

    @Override
    public Set<Type> getTypes() {
        return types;
    }

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

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

    @Override
    public String getId() {
        return getClass().getName();
    }
}

@@ -666,16 +666,60 @@
 l.cdi;%0A%0A
+import org.apache.camel.util.ObjectHelper;%0A%0A
 import j
@@ -1415,67 +1415,253 @@
 es;%0A
-%0A    private final InjectionTarget%3CCdiCamelContext%3E target;
+    private final String name;%0A    private final String camelContextName;%0A%0A    private final InjectionTarget%3CCdiCamelContext%3E target;%0A%0A    public CdiCamelContextBean(BeanManager beanManager) %7B%0A        this(beanManager, %22CamelContext%22, %22%22);%0A    %7D
 %0A%0A  
@@ -1697,24 +1697,62 @@
 ager manager
+, String name, String camelContextName
 ) %7B%0A        
@@ -1996,24 +1996,100 @@
 tatedType);%0A
+        this.name = name;%0A        this.camelContextName = camelContextName;%0A
     %7D%0A%0A    @
@@ -2533,32 +2533,146 @@
 ce(creational);%0A
+        if (ObjectHelper.isNotEmpty(camelContextName)) %7B%0A            context.setName(camelContextName);%0A        %7D%0A
         target.i
@@ -3311,19 +3311,12 @@
 urn 
-%22c
+n
 ame
-l-cdi%22
 ;%0A  

Add fields name, camelContextName