Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2005-2015 Red Hat, Inc. Red Hat licenses this file to you 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 io.fabric8.cdi.producers;
 
 
 
 import java.util.List;
 import java.util.Set;
 
 public class FactoryMethodProducer<T, X> implements Producer<T> {
 
     private final Bean<T> bean;
     private final AnnotatedMethod<X> factoryMethod;
     private final String serviceId;
     private final String serviceProtocol;
     private final Boolean serviceExternal;
 
     public FactoryMethodProducer(Bean<T> beanAnnotatedMethod<X> factoryMethodString serviceIdString serviceProtocolBoolean serviceExternal) {
         this. = bean;
         this. = factoryMethod;
         this. = serviceId;
         this. = serviceProtocol;
         this. = serviceExternal;
     }
 
     public FactoryMethodProducer<T, X> withServiceId(String serviceId) {
         return new FactoryMethodProducer<>(serviceId);
     }
 
     @Override
     public T produce(CreationalContext<T> ctx) {
         List<Objectarguments = new ArrayList<>();
 
         for (AnnotatedParameter<X> parameter : .getParameters()) {
             Type type = parameter.getBaseType();
             ServiceName serviceName = parameter.getAnnotation(ServiceName.class);
             Configuration configuration = parameter.getAnnotation(Configuration.class);
             if (serviceName != null && String.class.equals(type)) {
                 String serviceUrl = getServiceUrl(ctx);
                 arguments.add(serviceUrl);
             } else if (serviceName != null && !String.class.equals(type)) {
                 Object serviceBean = getServiceBean(, (Class<Object>) type,  ctx);
                 arguments.add(serviceBean);
             } else if (configuration != null) {
                 Object config = getConfiguration(, (Class<Object>) typectx);
                 arguments.add(config);
             } else {
                 Object other = BeanProvider.getContextualReferences((Classtypefalse);
                 arguments.add(other);
             }
         }
         try {
             return (T) .getJavaMember().invoke(.create(ctx), arguments.toArray());
         } catch (Throwable t) {
             throw new RuntimeException(t);
         }
     }
 
     @Override
     public void dispose(T instance) {
 
     }
 
     @Override
     public Set<InjectionPointgetInjectionPoints() {
         return Collections.emptySet();
     }

    
Get Service URL from the context or create a producer.

Parameters:
serviceId
serviceProtocol
context
Returns:
    private String getServiceUrl(String serviceIdString serviceProtocolCreationalContext context) {
        try {
            return (String) BeanProvider.getContextualReference((ClassString.class, Qualifiers.create(serviceIdserviceProtocolfalse));
        } catch (IllegalStateException e) {
            //Contextual Refernece not found, let's fallback to Configuration Producer.
            Producer<Stringproducer = ServiceUrlBean.anyBean(serviceIdserviceProtocol).getProducer();
            if (producer != null) {
                return ServiceUrlBean.anyBean(serviceIdserviceProtocol).getProducer().produce(context);
            } else {
                throw new IllegalStateException("Could not find producer for service:" + serviceId + " protocol:" + serviceProtocol);
            }
        }
    }

    
Get Service Bean from the context or create a producer.

Parameters:
serviceId
serviceProtocol
context
Returns:
    private <S> S getServiceBean(String serviceIdString serviceProtocolClass<S> serviceTypeCreationalContext context) {
        try {
            return  BeanProvider.getContextualReference(serviceType, Qualifiers.create(serviceIdserviceProtocolfalse));
        } catch (IllegalStateException e) {
            Producer<S> producer = ServiceBean.anyBean(serviceIdserviceProtocolserviceType).getProducer();
            if (producer != null) {
                return (S) producer.produce(context);
            } else {
                throw new IllegalStateException("Could not find producer for service:" + serviceId + " type:" + serviceType + " protocol:" + serviceProtocol);
            }
        }
    }


    
Get Configuration from context or create a producer.

Parameters:
serviceId
type
context
<C>
Returns:
    private <C> C getConfiguration(String serviceIdClass<C> typeCreationalContext context) {
        try {
            return (C) BeanProvider.getContextualReference((Classtypenew ConfigurationQualifier(serviceId));
        } catch (IllegalStateException e) {
            //Contextual Refernece not found, let's fallback to Configuration Producer.
            return (C) ConfigurationBean.getBean(serviceIdtype).getProducer().produce(context);
        }
    }
New to GrepCode? Check out our FAQ X