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;
 
 
 import java.util.List;
 import java.util.Set;
 
 public class Fabric8Extension implements Extension {
 
     private static final Set<FactoryMethodContextfactories = new LinkedHashSet<>();
 
     public void afterDiscovery(final @Observes AfterBeanDiscovery event) {
         event.addBean(new KubernetesFactoryBean());
         event.addBean(new KubernetesClientBean());
 
         //We need to process factories in reverse order so that we make feasible forwarding for service id etc.
         List<FactoryMethodContextreverseFactories = new ArrayList<>(FactoryMethodContext.sort());
         Collections.reverse(reverseFactories);
 
         for (final FactoryMethodContext factoryMethodContext : reverseFactories) {
             ServiceBean.doWith(factoryMethodContext.getReturnType(), new ServiceBean.Callback() {
                 @Override
                 public ServiceBean apply(ServiceBean bean) {
                     String serviceId = bean.getServiceName();
                     String serviceProtocol = bean.getServiceProtocol();
                     Boolean serviceExternal = bean.getServiceExternal();
 
                     //Ensure that there is a factory String -> sourceType before adding producer.
                     if (!String.class.equals(factoryMethodContext.getSourceType())) {
                         ServiceBean.getBean(serviceIdserviceProtocolnullserviceExternal, (Class<Object>) factoryMethodContext.getSourceType());
                     }
                     return bean.withProducer(new FactoryMethodProducer(factoryMethodContext.getBean(), factoryMethodContext.getFactoryMethod(), serviceIdserviceProtocolserviceExternal));
                 }
             });
         }
 
         for (ServiceUrlBean bean : ServiceUrlBean.getBeans()) {
             event.addBean(bean);
         }
 
         for (ServiceUrlCollectionBean bean : ServiceUrlCollectionBean.getBeans()) {
             event.addBean(bean);
         }
         
         for (ServiceBean bean : ServiceBean.getBeans()) {
             if (bean.getProducer() != null) {
                 event.addBean(bean);
             }
         }
         for (ConfigurationBean b : ConfigurationBean.getBeans()) {
             event.addBean(b);
         }
     }
 
    public <T, X> void onInjectionPoint(@Observes ProcessInjectionPoint<T, X> event) {
        final InjectionPoint injectionPoint = event.getInjectionPoint();
        if (isServiceInjectionPoint(injectionPoint)) {
            Annotated annotated = injectionPoint.getAnnotated();
            Alias alias = annotated.getAnnotation(Alias.class);
            ServiceName name = annotated.getAnnotation(ServiceName.class);
            Protocol protocol = annotated.getAnnotation(Protocol.class);
            External external = annotated.getAnnotation(External.class);
            
            Boolean serviceEndpoint = annotated.getAnnotation(Endpoint.class) != null;
            String serviceName = name.value();
            String serviceProtocol = protocol != null ? protocol.value() : "tcp";
            String serviceAlias = alias != null ? alias.value() : null;
            Boolean serviceExternal = external != null && external.value();
            
            Type type = annotated.getBaseType();
            if (type.equals(String.class)) {
                ServiceUrlBean.getBean(serviceNameserviceProtocolserviceAliasserviceExternal);
            } else if (serviceEndpoint && isGenericOf(typeList.classString.class)) {
                ServiceUrlCollectionBean.getBean(serviceNameserviceProtocolserviceAliasserviceEndpoint.);
            } else if (serviceEndpoint && isGenericOf(typeList.classnull)) {
                //TODO: Integrate with Factories(?)
            } else if (serviceEndpoint && isGenericOf(typeSet.classString.class)) {
                ServiceUrlCollectionBean.getBean(serviceNameserviceProtocolserviceAliasserviceEndpoint.);
            } else if (serviceEndpoint && isGenericOf(typeSet.classnull)) {
                //TODO: Integrate with Factories(?)
            } else {
                ServiceBean.getBean(serviceNameserviceProtocolserviceAliasserviceExternal, (Classtype);
            }
            if (protocol == null) {
                setDefaultProtocol(event);
            }
            if (external == null) {
                setExternalFalse(event);
            }
        } else if (isConfigurationInjectionPoint(injectionPoint)) {
            Annotated annotated = injectionPoint.getAnnotated();
            Configuration configuration = annotated.getAnnotation(Configuration.class);
            Type type = injectionPoint.getType();
            String configurationId = configuration.value();
            ConfigurationBean.getBean(configurationId, (Classtype);
        }
    }
    public <X> void onManagedBean(final @Observes ProcessManagedBean<X> event) {
        for (final AnnotatedMethod<?> method : event.getAnnotatedBeanClass().getMethods()) {
            final Factory factory = method.getAnnotation(Factory.class);
            if (factory != null) {
                final Type sourceType = getSourceType(method);
                final Type returnType = method.getJavaMember().getReturnType();
                .add(new FactoryMethodContext(event.getBean(), (ClasssourceType, (ClassreturnTypemethod));
            }
        }
    }
    private static <T> Type getSourceType(AnnotatedMethod<T> method) {
        for (AnnotatedParameter<T> parameter : method.getParameters()) {
            if (parameter.isAnnotationPresent(ServiceName.class)) {
                return parameter.getBaseType();
            }
        }
        return String.class;
    }

    
Checks if the InjectionPoint is annotated with the

Parameters:
injectionPoint The injection point.
Returns:
Configuration:
qualifier.
    private static boolean isConfigurationInjectionPoint(InjectionPoint injectionPoint) {
        Set<Annotationqualifiers = injectionPoint.getQualifiers();
        for (Annotation annotation : qualifiers) {
            if (annotation.annotationType().isAssignableFrom(Configuration.class)) {
                return true;
            }
        }
        return false;
    }
    
    
    private <T, X> void setDefaultProtocol(ProcessInjectionPoint<T, X> event) {
        //if protocol is not specified decorate injection point with "default" protocol.
        event.setInjectionPoint(new DelegatingInjectionPoint(event.getInjectionPoint()) {
            @Override
            public Set<AnnotationgetQualifiers() {
                Set<Annotationqualifiers = new LinkedHashSet<>(super.getQualifiers());
                qualifiers.add(new ProtocolQualifier("tcp"));
                return Collections.unmodifiableSet(qualifiers);
            }
        });
    }
    private <T, X> void setExternalFalse(ProcessInjectionPoint<T, X> event) {
        //if protocol is not specified decorate injection point with "default" protocol.
        event.setInjectionPoint(new DelegatingInjectionPoint(event.getInjectionPoint()) {
            @Override
            public Set<AnnotationgetQualifiers() {
                Set<Annotationqualifiers = new LinkedHashSet<>(super.getQualifiers());
                qualifiers.add(new ExternalQualifier(false));
                return Collections.unmodifiableSet(qualifiers);
            }
        });
    }
    private static boolean isGenericOf(Type typeType rawType argument) {
        if (type instanceof ParameterizedType) {
            ParameterizedType p = (ParameterizedTypetype;
            return p.getRawType() != null
                    && p.getRawType().equals(raw)
                    && p.getActualTypeArguments().length == 1
                    && (p.getActualTypeArguments()[0].equals(argument) || argument == null);
        } else if (type instanceof Class) {
            return argument == null && type.equals(raw);
        } else {
            return false;
        }
    }
    

    
Checks if the InjectionPoint is annotated with the

Parameters:
injectionPoint The injection point.
Returns:
Service:
qualifier.
    public static boolean isServiceInjectionPoint(InjectionPoint injectionPoint) {
        Set<Annotationqualifiers = injectionPoint.getQualifiers();
        for (Annotation annotation : qualifiers) {
            if (annotation.annotationType().isAssignableFrom(ServiceName.class)) {
                return true;
            }
        }
        return false;
    }
New to GrepCode? Check out our FAQ X