Package org.apache.tuscany.sca.implementation.spring

Examples of org.apache.tuscany.sca.implementation.spring.SpringBeanElement


                if (!found) {
                    // REVIEW: Adding a SpringBeanElement "proxy" so that the bean id can be used at runtime to look
                    // up the bean instance from the parent context
                    implementation.setBeanForService(theService,
                                                     new SpringBeanElement(serviceElement.getTarget(), null));
                }
            } // end while

            // Next handle the references
            Iterator<SpringSCAReferenceElement> itr = references.iterator();
            while (itr.hasNext()) {
                SpringSCAReferenceElement referenceElement = itr.next();
                Class<?> interfaze = resolveClass(resolver, referenceElement.getType(), context);
                Reference theReference = createReference(interfaze, referenceElement.getName());
                // Override the older bean definition with the latest ones
                // for the duplicate definitions found.
                Reference duplicate = null;
                for (Reference reference : componentType.getReferences()) {
                    if (reference.getName().equals(theReference.getName()))
                        duplicate = reference;
                }
                if (duplicate != null)
                    componentType.getReferences().remove(duplicate);

                // add the required intents and policySets for this reference
                theReference.getRequiredIntents().addAll(referenceElement.getRequiredIntents());
                theReference.getPolicySets().addAll(referenceElement.getPolicySets());
                componentType.getReferences().add(theReference);
            } // end while

            // Next handle the properties
            Iterator<SpringSCAPropertyElement> itsp = scaproperties.iterator();
            while (itsp.hasNext()) {
                SpringSCAPropertyElement scaproperty = itsp.next();
                // Create a component type property if the SCA property element has a name
                // and a type declared...
                if (scaproperty.getType() != null && scaproperty.getName() != null) {
                    Property theProperty = assemblyFactory.createProperty();
                    theProperty.setName(scaproperty.getName());
                    // Get the Java class and then an XSD element type for the property
                    Class<?> propType = Class.forName(scaproperty.getType());
                    theProperty.setXSDType(JavaXMLMapper.getXMLType(propType));
                    // Override the older bean definition with the latest ones
                    // for the duplicate definitions found.
                    Property duplicate = null;
                    for (Property property : componentType.getProperties()) {
                        if (property.getName().equals(theProperty.getName()))
                            duplicate = property;
                    }
                    if (duplicate != null)
                        componentType.getProperties().remove(duplicate);

                    componentType.getProperties().add(theProperty);
                    // Remember the Java Class (ie the type) for this property
                    implementation.setPropertyClass(theProperty.getName(), propType);
                } // end if
            } // end while

            // Finally deal with the beans
            Iterator<SpringBeanElement> itb;
            // If there are no explicit service elements, then expose all the beans
            if (services.isEmpty()) {
                itb = beans.iterator();
                // Loop through all the beans found
                while (itb.hasNext()) {
                    SpringBeanElement beanElement = itb.next();

                    // If its not a valid bean for service, ignore it
                    if (!isValidBeanForService(beanElement)) {
                        continue;
                    }
                    try {
                        // Load the Spring bean class
                        Class<?> beanClass = resolveClass(resolver, beanElement.getClassName(), context);
                        // Introspect the bean
                        beanIntrospector = new SpringBeanIntrospector(registry, beanElement.getCustructorArgs());
                        ComponentType beanComponentType = assemblyFactory.createComponentType();
                        javaImplementation = beanIntrospector.introspectBean(beanClass, beanComponentType);
                        // Set the service name as bean name
                        for (Service componentService : beanComponentType.getServices()) {
                            componentService.setName(beanElement.getId());
                        }
                        // Get the service interface defined by this Spring Bean and add to
                        // the component type of the Spring Assembly
                        List<Service> beanServices = beanComponentType.getServices();
                        componentType.getServices().addAll(beanServices);
                        // Add these services to the Service / Bean map
                        for (Service beanService : beanServices) {
                            implementation.setBeanForService(beanService, beanElement);
                        }
                    } catch (Throwable e) {
                        // [rfeng] FIXME: Some Spring beans have constructors that take pararemters injected by Spring and
                        // Tuscany is not happy with that during the introspection
                        log.log(Level.SEVERE, e.getMessage(), e);
                    }
                } // end while
            } // end if

            // [rfeng] We only try to implicitly map Spring beans if no sca:reference or sca:property is present
            if (references.isEmpty() && scaproperties.isEmpty()) {
                itb = beans.iterator();
                while (itb.hasNext()) {
                    SpringBeanElement beanElement = itb.next();

                    // If its not a valid bean for service, ignore it
                    if (!isValidBeanForService(beanElement)) {
                        continue;
                    }
                    // Ignore if the bean has no properties and constructor arguments
                    if (beanElement.getProperties().isEmpty() && beanElement.getCustructorArgs().isEmpty())
                        continue;

                    ComponentType beanComponentType = assemblyFactory.createComponentType();

                    try {
                        Class<?> beanClass = resolveClass(resolver, beanElement.getClassName(), context);
                        // Introspect the bean
                        beanIntrospector = new SpringBeanIntrospector(registry, beanElement.getCustructorArgs());
                        javaImplementation = beanIntrospector.introspectBean(beanClass, beanComponentType);
                    } catch (Exception e) {
                        // [rfeng] FIXME: Some Spring beans have constructors that take pararemters injected by Spring and
                        // Tuscany is not happy with that during the introspection
                        log.log(Level.SEVERE, e.getMessage(), e);
                        continue;
                    }
                    Map<String, JavaElementImpl> propertyMap = javaImplementation.getPropertyMembers();
                    JavaConstructorImpl constructor = javaImplementation.getConstructor();
                    // Get the references by this Spring Bean and add the unresolved ones to
                    // the component type of the Spring Assembly
                    List<Reference> beanReferences = beanComponentType.getReferences();
                    List<Property> beanProperties = beanComponentType.getProperties();

                    Set<String> excludedNames = new HashSet<String>();
                    Iterator<SpringPropertyElement> itp = beanElement.getProperties().iterator();
                    while (itp.hasNext()) {
                        SpringPropertyElement propertyElement = itp.next();
                        // Exclude the reference that is also known as a spring property
                        excludedNames.add(propertyElement.getName());
                        for (String propertyRef : propertyElement.getRefs()) {
                            if (propertyRefUnresolved(propertyRef, beans, references, scaproperties)) {
                                // This means an unresolved reference from the spring bean...
                                for (Reference reference : beanReferences) {
                                    if (propertyElement.getName().equals(reference.getName())) {
                                        // The name of the reference in this case is the string in
                                        // the @ref attribute of the Spring property element, NOT the
                                        // name of the field in the Spring bean....
                                        reference.setName(propertyRef);
                                        // reference.setWiredByImpl(true);
                                        componentType.getReferences().add(reference);
                                        break;
                                    } // end if
                                } // end for

                                // Store the unresolved references as unresolvedBeanRef in the Spring Implementation type
                                for (Property scaproperty : beanProperties) {
                                    if (propertyElement.getName().equals(scaproperty.getName())) {
                                        // The name of the reference in this case is the string in
                                        // the @ref attribute of the Spring property element, NOT the
                                        // name of the field in the Spring bean....
                                        Class<?> interfaze =
                                            resolveClass(resolver,
                                                         (propertyMap.get(propertyElement.getName()).getType())
                                                             .getName(),
                                                         context);
                                        Reference theReference = createReference(interfaze, propertyRef);
                                        implementation.setUnresolvedBeanRef(propertyRef, theReference);
                                        break;
                                    } // end if
                                } // end for
                            } // end if
                        } // end for
                    } // end while

                    Iterator<SpringConstructorArgElement> itcr = beanElement.getCustructorArgs().iterator();
                    while (itcr.hasNext()) {
                        SpringConstructorArgElement conArgElement = itcr.next();
                        for (String constructorArgRef : conArgElement.getRefs()) {
                            if (propertyRefUnresolved(constructorArgRef, beans, references, scaproperties)) {
                                for (JavaParameterImpl parameter : constructor.getParameters()) {
View Full Code Here


    private Class<?> getBeanInterface(ModelResolver resolver,
                                      String target,
                                      List<SpringBeanElement> beans,
                                      ProcessorContext context) throws ClassNotFoundException {
        SpringBeanElement bean = null;
        for (SpringBeanElement sbe : beans) {
            if (sbe.getId().equals(target)) {
                bean = sbe;
                break;
            }
        }
        if (bean == null) {
            error(context.getMonitor(), "TargetBeanDoesNotExist", null, target);
            return null;
        }

        Class<?> beanClass = resolveClass(resolver, bean.getClassName(), context);
        Set<Class<?>> ifaces = javaIntrospectionHelper.getAllInterfaces(beanClass);
        for (Class<?> interfaze : ifaces) {
            if (interfaze.isAnnotationPresent(Remotable.class)) {
                return interfaze;
            }
View Full Code Here

        if (ref != null) {
            // Scan over the beans looking for a match
            Iterator<SpringBeanElement> itb = beans.iterator();
            while (itb.hasNext()) {
                SpringBeanElement beanElement = itb.next();
                // Does the bean name match the ref?
                if (ref.equals(beanElement.getId())) {
                    unresolved = false;
                    break;
                } // end if
            } // end while
              // Scan over the SCA reference elements looking for a match
View Full Code Here

        while (its.hasNext()) {
            SpringSCAServiceElement serviceElement = its.next();
            boolean targetBeanExists = false;
            Iterator<SpringBeanElement> itb = beans.iterator();
            while (itb.hasNext()) {
                SpringBeanElement beanElement = itb.next();
                if (serviceElement.getTarget().equals(beanElement.getId()))
                    targetBeanExists = true;
            }
            if (!targetBeanExists) {
                // REVIEW: [rfeng] The target bean can exist in the parent Spring application context which we don't know
                // until runtime
                warning(monitor, "TargetBeanDoesNotExist", beans);
            }
        } // end while

        // The value of the @name attribute of an <sca:reference/> subelement of a <beans/>
        // element MUST be unique amongst the @name attributes of the <sca:property/>
        // subelements and the <bean/> subelements of the <beans/> element.
        //                   AND
        // The @default attribute of a <sca:reference/> subelement of a <beans/> 
        // element MUST have the value of the @name attribute of one of the <bean/>
        // subelements of the <beans/> element.
        Iterator<SpringSCAReferenceElement> itr = references.iterator();
        while (itr.hasNext()) {
            SpringSCAReferenceElement referenceElement = itr.next();
            boolean defaultBeanExists = false;
            boolean isUniqueReferenceName = true;
            Iterator<SpringBeanElement> itb = beans.iterator();
            while (itb.hasNext()) {
                SpringBeanElement beanElement = itb.next();
                if (referenceElement.getDefaultBean() != null)
                    if (referenceElement.getDefaultBean().equals(beanElement.getId()))
                        defaultBeanExists = true;
                if (referenceElement.getName().equals(beanElement.getId()))
                    isUniqueReferenceName = false;
            }
            Iterator<SpringSCAPropertyElement> itp = scaproperties.iterator();
            while (itp.hasNext()) {
                SpringSCAPropertyElement propertyElement = itp.next();
                if (referenceElement.getName().equals(propertyElement.getName()))
                    isUniqueReferenceName = false;
            }
            if (!defaultBeanExists && referenceElement.getDefaultBean() != null)
                error(monitor, "DefaultBeanDoesNotExist", beans);
            if (!isUniqueReferenceName)
                error(monitor, "ScaReferenceNameNotUnique", beans);
        } // end while

        // The value of the @name attribute of an <sca:property/> subelement of a <beans/>
        // element MUST be unique amongst the @name attributes of the <sca:reference/>
        // subelements and the <bean/> subelements of the <beans/> element.     
        Iterator<SpringSCAPropertyElement> itp = scaproperties.iterator();
        while (itp.hasNext()) {
            SpringSCAPropertyElement propertyElement = itp.next();
            boolean isUniquePropertyName = true;
            Iterator<SpringBeanElement> itb = beans.iterator();
            while (itb.hasNext()) {
                SpringBeanElement beanElement = itb.next();
                if (propertyElement.getName().equals(beanElement.getId()))
                    isUniquePropertyName = false;
            }
            Iterator<SpringSCAReferenceElement> itrp = references.iterator();
            while (itrp.hasNext()) {
                SpringSCAReferenceElement referenceElement = itrp.next();
View Full Code Here

               
                if (!found) {
                    // REVIEW: Adding a SpringBeanElement "proxy" so that the bean id can be used at runtime to look
                    // up the bean instance from the parent context
                    implementation.setBeanForService(theService,
                                                     new SpringBeanElement(serviceElement.getTarget(), null));
                }
            } // end while

            // Next handle the references
            Iterator<SpringSCAReferenceElement> itr = references.iterator();
            while (itr.hasNext()) {
                SpringSCAReferenceElement referenceElement = itr.next();
                Class<?> interfaze = resolveClass(resolver, referenceElement.getType(), context);
                Reference theReference = createReference(interfaze, referenceElement.getName());
                // Override the older bean definition with the latest ones
                // for the duplicate definitions found.
                Reference duplicate = null;
                for (Reference reference : componentType.getReferences()) {
                    if (reference.getName().equals(theReference.getName()))
                        duplicate = reference;
                }
                if (duplicate != null)
                    componentType.getReferences().remove(duplicate);

                // add the required intents and policySets for this reference
                theReference.getRequiredIntents().addAll(referenceElement.getRequiredIntents());
                theReference.getPolicySets().addAll(referenceElement.getPolicySets());
                componentType.getReferences().add(theReference);
            } // end while

            // Next handle the properties
            Iterator<SpringSCAPropertyElement> itsp = scaproperties.iterator();
            while (itsp.hasNext()) {
                SpringSCAPropertyElement scaproperty = itsp.next();
                // Create a component type property if the SCA property element has a name
                // and a type declared...
                if (scaproperty.getType() != null && scaproperty.getName() != null) {
                    Property theProperty = assemblyFactory.createProperty();
                    theProperty.setName(scaproperty.getName());
                    // Get the Java class and then an XSD element type for the property
                    Class<?> propType = Class.forName(scaproperty.getType());
                    theProperty.setXSDType(JavaXMLMapper.getXMLType(propType));
                    // Override the older bean definition with the latest ones
                    // for the duplicate definitions found.
                    Property duplicate = null;
                    for (Property property : componentType.getProperties()) {
                        if (property.getName().equals(theProperty.getName()))
                            duplicate = property;
                    }
                    if (duplicate != null)
                        componentType.getProperties().remove(duplicate);

                    componentType.getProperties().add(theProperty);
                    // Remember the Java Class (ie the type) for this property
                    implementation.setPropertyClass(theProperty.getName(), propType);
                } // end if
            } // end while

            // Finally deal with the beans
            Iterator<SpringBeanElement> itb;
            // If there are no explicit service elements, then expose all the beans
            if (services.isEmpty()) {
                itb = beans.iterator();
                // Loop through all the beans found
                while (itb.hasNext()) {
                    SpringBeanElement beanElement = itb.next();

                    // If its not a valid bean for service, ignore it
                    if (!isValidBeanForService(beanElement)) {
                        continue;
                    }
                    try {
                        // Load the Spring bean class
                        Class<?> beanClass = resolveClass(resolver, beanElement.getClassName(), context);
                        // Introspect the bean
                        beanIntrospector = new SpringBeanIntrospector(registry, beanElement.getCustructorArgs());
                        ComponentType beanComponentType = assemblyFactory.createComponentType();
                        javaImplementation = beanIntrospector.introspectBean(beanClass, beanComponentType);
                        // Set the service name as bean name
                        for (Service componentService : beanComponentType.getServices()) {
                            componentService.setName(beanElement.getId());
                        }
                        // Get the service interface defined by this Spring Bean and add to
                        // the component type of the Spring Assembly
                        List<Service> beanServices = beanComponentType.getServices();
                        componentType.getServices().addAll(beanServices);
                        // Add these services to the Service / Bean map
                        for (Service beanService : beanServices) {
                            implementation.setBeanForService(beanService, beanElement);
                        }
                    } catch (Throwable e) {
                        // [rfeng] FIXME: Some Spring beans have constructors that take pararemters injected by Spring and
                        // Tuscany is not happy with that during the introspection
                        log.log(Level.SEVERE, e.getMessage(), e);
                    }
                } // end while
            } // end if

            itb = beans.iterator();
            while (itb.hasNext()) {
                SpringBeanElement beanElement = itb.next();

                // If its not a valid bean for service, ignore it
                if (!isValidBeanForService(beanElement)) {
                    continue;
                }
                // Ignore if the bean has no properties and constructor arguments
                if (beanElement.getProperties().isEmpty() && beanElement.getCustructorArgs().isEmpty())
                    continue;

                ComponentType beanComponentType = assemblyFactory.createComponentType();

                try {
                    Class<?> beanClass = resolveClass(resolver, beanElement.getClassName(), context);
                    // Introspect the bean
                    beanIntrospector = new SpringBeanIntrospector(registry, beanElement.getCustructorArgs());
                    javaImplementation = beanIntrospector.introspectBean(beanClass, beanComponentType);
                } catch (Exception e) {
                    // [rfeng] FIXME: Some Spring beans have constructors that take pararemters injected by Spring and
                    // Tuscany is not happy with that during the introspection
                    log.log(Level.SEVERE, e.getMessage(), e);
                    continue;
                }
                Map<String, JavaElementImpl> propertyMap = javaImplementation.getPropertyMembers();
                JavaConstructorImpl constructor = javaImplementation.getConstructor();
                // Get the references by this Spring Bean and add the unresolved ones to
                // the component type of the Spring Assembly
                List<Reference> beanReferences = beanComponentType.getReferences();
                List<Property> beanProperties = beanComponentType.getProperties();

                Set<String> excludedNames = new HashSet<String>();
                Iterator<SpringPropertyElement> itp = beanElement.getProperties().iterator();
                while (itp.hasNext()) {
                    SpringPropertyElement propertyElement = itp.next();
                    // Exclude the reference that is also known as a spring property
                    excludedNames.add(propertyElement.getName());
                    for (String propertyRef : propertyElement.getRefs()) {
                        if (propertyRefUnresolved(propertyRef, beans, references, scaproperties)) {
                            // This means an unresolved reference from the spring bean...
                            for (Reference reference : beanReferences) {
                                if (propertyElement.getName().equals(reference.getName())) {
                                    // The name of the reference in this case is the string in
                                    // the @ref attribute of the Spring property element, NOT the
                                    // name of the field in the Spring bean....
                                    reference.setName(propertyRef);
                                    componentType.getReferences().add(reference);
                                    break;
                                } // end if
                            } // end for

                            // Store the unresolved references as unresolvedBeanRef in the Spring Implementation type
                            for (Property scaproperty : beanProperties) {
                                if (propertyElement.getName().equals(scaproperty.getName())) {
                                    // The name of the reference in this case is the string in
                                    // the @ref attribute of the Spring property element, NOT the
                                    // name of the field in the Spring bean....
                                    Class<?> interfaze =
                                        resolveClass(resolver,
                                                     (propertyMap.get(propertyElement.getName()).getType()).getName(),
                                                     context);
                                    Reference theReference = createReference(interfaze, propertyRef);
                                    implementation.setUnresolvedBeanRef(propertyRef, theReference);
                                    break;
                                } // end if
                            } // end for
                        } // end if
                    } // end for
                } // end while

                Iterator<SpringConstructorArgElement> itcr = beanElement.getCustructorArgs().iterator();
                while (itcr.hasNext()) {
                    SpringConstructorArgElement conArgElement = itcr.next();
                    for (String constructorArgRef : conArgElement.getRefs()) {
                        if (propertyRefUnresolved(constructorArgRef, beans, references, scaproperties)) {
                            for (JavaParameterImpl parameter : constructor.getParameters()) {
View Full Code Here

        if (ref != null) {
            // Scan over the beans looking for a match
            Iterator<SpringBeanElement> itb = beans.iterator();
            while (itb.hasNext()) {
                SpringBeanElement beanElement = itb.next();
                // Does the bean name match the ref?
                if (ref.equals(beanElement.getId())) {
                    unresolved = false;
                    break;
                } // end if
            } // end while
              // Scan over the SCA reference elements looking for a match
View Full Code Here

        while (its.hasNext()) {
            SpringSCAServiceElement serviceElement = its.next();
            boolean targetBeanExists = false;
            Iterator<SpringBeanElement> itb = beans.iterator();
            while (itb.hasNext()) {
                SpringBeanElement beanElement = itb.next();
                if (serviceElement.getTarget().equals(beanElement.getId()))
                    targetBeanExists = true;
            }
            if (!targetBeanExists) {
                // REVIEW: [rfeng] The target bean can exist in the parent Spring application context which we don't know
                // until runtime
                warning(monitor, "TargetBeanDoesNotExist", beans);
            }
        } // end while

        // The value of the @name attribute of an <sca:reference/> subelement of a <beans/>
        // element MUST be unique amongst the @name attributes of the <sca:property/>
        // subelements and the <bean/> subelements of the <beans/> element.
        //                   AND
        // The @default attribute of a <sca:reference/> subelement of a <beans/> 
        // element MUST have the value of the @name attribute of one of the <bean/>
        // subelements of the <beans/> element.
        Iterator<SpringSCAReferenceElement> itr = references.iterator();
        while (itr.hasNext()) {
            SpringSCAReferenceElement referenceElement = itr.next();
            boolean defaultBeanExists = true;
            boolean isUniqueReferenceName = true;
            Iterator<SpringBeanElement> itb = beans.iterator();
            while (itb.hasNext()) {
                SpringBeanElement beanElement = itb.next();
                if (referenceElement.getDefaultBean() != null)
                    if (referenceElement.getDefaultBean().equals(beanElement.getId()))
                        defaultBeanExists = false;
                if (referenceElement.getName().equals(beanElement.getId()))
                    isUniqueReferenceName = false;
            }
            Iterator<SpringSCAPropertyElement> itp = scaproperties.iterator();
            while (itp.hasNext()) {
                SpringSCAPropertyElement propertyElement = itp.next();
                if (referenceElement.getName().equals(propertyElement.getName()))
                    isUniqueReferenceName = false;
            }
            if (!defaultBeanExists)
                error(monitor, "DefaultBeanDoesNotExist", beans);
            if (!isUniqueReferenceName)
                error(monitor, "ScaReferenceNameNotUnique", beans);
        } // end while

        // The value of the @name attribute of an <sca:property/> subelement of a <beans/>
        // element MUST be unique amongst the @name attributes of the <sca:reference/>
        // subelements and the <bean/> subelements of the <beans/> element.     
        Iterator<SpringSCAPropertyElement> itp = scaproperties.iterator();
        while (itp.hasNext()) {
            SpringSCAPropertyElement propertyElement = itp.next();
            boolean isUniquePropertyName = true;
            Iterator<SpringBeanElement> itb = beans.iterator();
            while (itb.hasNext()) {
                SpringBeanElement beanElement = itb.next();
                if (propertyElement.getName().equals(beanElement.getId()))
                    isUniquePropertyName = false;
            }
            Iterator<SpringSCAReferenceElement> itrp = references.iterator();
            while (itrp.hasNext()) {
                SpringSCAReferenceElement referenceElement = itrp.next();
View Full Code Here

    public synchronized List<SpringBeanElement> getBeanElements() {
        if (beanElements == null) {
            beanElements = new ArrayList<SpringBeanElement>();
            for (String name : getBeanDefinitionNames()) {
                BeanDefinition def = getBeanDefinition(name);
                SpringBeanElement beanElement = new SpringBeanElement(name, def.getBeanClassName());
                beanElements.add(beanElement);
                beanElement.setAbstractBean(def.isAbstract());
                beanElement.setFactoryBeanAttribute(def.getFactoryBeanName() != null);
                beanElement.setFactoryMethodAttribute(def.getFactoryMethodName() != null);
                beanElement.setParentAttribute(def.getParentName() != null);
                beanElement.setInnerBean(beanElement.getId() == null);

                ConstructorArgumentValues args = def.getConstructorArgumentValues();
                for (Map.Entry<Integer, ValueHolder> e: args.getIndexedArgumentValues().entrySet()) {
                    ValueHolder holder = e.getValue();
                    SpringConstructorArgElement arg = new SpringConstructorArgElement(holder.getType());
                    arg.setIndex(e.getKey());
                    beanElement.addCustructorArgs(arg);
                }

                MutablePropertyValues values = def.getPropertyValues();
                for (PropertyValue p : values.getPropertyValueList()) {
                    SpringPropertyElement propertyElement = new SpringPropertyElement(p.getName());
                    Object value = p.getValue();
                    configurePropertyElement(propertyElement, value);
                    beanElement.getProperties().add(propertyElement);
                }
            }
        }
        return beanElements;
    }
View Full Code Here

                if (!found) {
                    // REVIEW: Adding a SpringBeanElement "proxy" so that the bean id can be used at runtime to look
                    // up the bean instance from the parent context
                    implementation.setBeanForService(theService,
                                                     new SpringBeanElement(serviceElement.getTarget(), null));
                }
            } // end while

            // Next handle the references
            Iterator<SpringSCAReferenceElement> itr = references.iterator();
            while (itr.hasNext()) {
                SpringSCAReferenceElement referenceElement = itr.next();
                Class<?> interfaze = resolveClass(resolver, referenceElement.getType(), context);
                Reference theReference = createReference(interfaze, referenceElement.getName());
                // Override the older bean definition with the latest ones
                // for the duplicate definitions found.
                Reference duplicate = null;
                for (Reference reference : componentType.getReferences()) {
                    if (reference.getName().equals(theReference.getName()))
                        duplicate = reference;
                }
                if (duplicate != null)
                    componentType.getReferences().remove(duplicate);

                // add the required intents and policySets for this reference
                theReference.getRequiredIntents().addAll(referenceElement.getRequiredIntents());
                theReference.getPolicySets().addAll(referenceElement.getPolicySets());
                componentType.getReferences().add(theReference);
            } // end while

            // Next handle the properties
            Iterator<SpringSCAPropertyElement> itsp = scaproperties.iterator();
            while (itsp.hasNext()) {
                SpringSCAPropertyElement scaproperty = itsp.next();
                // Create a component type property if the SCA property element has a name
                // and a type declared...
                if (scaproperty.getType() != null && scaproperty.getName() != null) {
                    Property theProperty = assemblyFactory.createProperty();
                    theProperty.setName(scaproperty.getName());
                    // Get the Java class and then an XSD element type for the property
                    Class<?> propType = Class.forName(scaproperty.getType());
                    theProperty.setXSDType(JavaXMLMapper.getXMLType(propType));
                    // Override the older bean definition with the latest ones
                    // for the duplicate definitions found.
                    Property duplicate = null;
                    for (Property property : componentType.getProperties()) {
                        if (property.getName().equals(theProperty.getName()))
                            duplicate = property;
                    }
                    if (duplicate != null)
                        componentType.getProperties().remove(duplicate);

                    componentType.getProperties().add(theProperty);
                    // Remember the Java Class (ie the type) for this property
                    implementation.setPropertyClass(theProperty.getName(), propType);
                } // end if
            } // end while

            // Finally deal with the beans
            Iterator<SpringBeanElement> itb;
            // If there are no explicit service elements, then expose all the beans
            if (services.isEmpty()) {
                itb = beans.iterator();
                // Loop through all the beans found
                while (itb.hasNext()) {
                    SpringBeanElement beanElement = itb.next();

                    // If its not a valid bean for service, ignore it
                    if (!isValidBeanForService(beanElement)) {
                        continue;
                    }
                    try {
                        // Load the Spring bean class
                        Class<?> beanClass = resolveClass(resolver, beanElement.getClassName(), context);
                        // Introspect the bean
                        beanIntrospector = new SpringBeanIntrospector(registry, beanElement.getCustructorArgs());
                        ComponentType beanComponentType = assemblyFactory.createComponentType();
                        javaImplementation = beanIntrospector.introspectBean(beanClass, beanComponentType);
                        // Set the service name as bean name
                        for (Service componentService : beanComponentType.getServices()) {
                            componentService.setName(beanElement.getId());
                        }
                        // Get the service interface defined by this Spring Bean and add to
                        // the component type of the Spring Assembly
                        List<Service> beanServices = beanComponentType.getServices();
                        componentType.getServices().addAll(beanServices);
                        // Add these services to the Service / Bean map
                        for (Service beanService : beanServices) {
                            implementation.setBeanForService(beanService, beanElement);
                        }
                    } catch (Throwable e) {
                        // [rfeng] FIXME: Some Spring beans have constructors that take pararemters injected by Spring and
                        // Tuscany is not happy with that during the introspection
                        log.log(Level.SEVERE, e.getMessage(), e);
                    }
                } // end while
            } // end if

            // [rfeng] We only try to implicitly map Spring beans if no sca:reference or sca:property is present
            if (references.isEmpty() && scaproperties.isEmpty()) {
                itb = beans.iterator();
                while (itb.hasNext()) {
                    SpringBeanElement beanElement = itb.next();

                    // If its not a valid bean for service, ignore it
                    if (!isValidBeanForService(beanElement)) {
                        continue;
                    }
                    // Ignore if the bean has no properties and constructor arguments
                    if (beanElement.getProperties().isEmpty() && beanElement.getCustructorArgs().isEmpty())
                        continue;

                    ComponentType beanComponentType = assemblyFactory.createComponentType();

                    try {
                        Class<?> beanClass = resolveClass(resolver, beanElement.getClassName(), context);
                        // Introspect the bean
                        beanIntrospector = new SpringBeanIntrospector(registry, beanElement.getCustructorArgs());
                        javaImplementation = beanIntrospector.introspectBean(beanClass, beanComponentType);
                    } catch (Exception e) {
                        // [rfeng] FIXME: Some Spring beans have constructors that take pararemters injected by Spring and
                        // Tuscany is not happy with that during the introspection
                        log.log(Level.SEVERE, e.getMessage(), e);
                        continue;
                    }
                    Map<String, JavaElementImpl> propertyMap = javaImplementation.getPropertyMembers();
                    JavaConstructorImpl constructor = javaImplementation.getConstructor();
                    // Get the references by this Spring Bean and add the unresolved ones to
                    // the component type of the Spring Assembly
                    List<Reference> beanReferences = beanComponentType.getReferences();
                    List<Property> beanProperties = beanComponentType.getProperties();

                    Set<String> excludedNames = new HashSet<String>();
                    Iterator<SpringPropertyElement> itp = beanElement.getProperties().iterator();
                    while (itp.hasNext()) {
                        SpringPropertyElement propertyElement = itp.next();
                        // Exclude the reference that is also known as a spring property
                        excludedNames.add(propertyElement.getName());
                        for (String propertyRef : propertyElement.getRefs()) {
                            if (propertyRefUnresolved(propertyRef, beans, references, scaproperties)) {
                                // This means an unresolved reference from the spring bean...
                                for (Reference reference : beanReferences) {
                                    if (propertyElement.getName().equals(reference.getName())) {
                                        // The name of the reference in this case is the string in
                                        // the @ref attribute of the Spring property element, NOT the
                                        // name of the field in the Spring bean....
                                        reference.setName(propertyRef);
                                        // reference.setWiredByImpl(true);
                                        componentType.getReferences().add(reference);
                                        break;
                                    } // end if
                                } // end for

                                // Store the unresolved references as unresolvedBeanRef in the Spring Implementation type
                                for (Property scaproperty : beanProperties) {
                                    if (propertyElement.getName().equals(scaproperty.getName())) {
                                        // The name of the reference in this case is the string in
                                        // the @ref attribute of the Spring property element, NOT the
                                        // name of the field in the Spring bean....
                                        Class<?> interfaze =
                                            resolveClass(resolver,
                                                         (propertyMap.get(propertyElement.getName()).getType())
                                                             .getName(),
                                                         context);
                                        Reference theReference = createReference(interfaze, propertyRef);
                                        implementation.setUnresolvedBeanRef(propertyRef, theReference);
                                        break;
                                    } // end if
                                } // end for
                            } // end if
                        } // end for
                    } // end while

                    Iterator<SpringConstructorArgElement> itcr = beanElement.getCustructorArgs().iterator();
                    while (itcr.hasNext()) {
                        SpringConstructorArgElement conArgElement = itcr.next();
                        for (String constructorArgRef : conArgElement.getRefs()) {
                            if (propertyRefUnresolved(constructorArgRef, beans, references, scaproperties)) {
                                for (JavaParameterImpl parameter : constructor.getParameters()) {
View Full Code Here

    private Class<?> getBeanInterface(ModelResolver resolver,
                                      String target,
                                      List<SpringBeanElement> beans,
                                      ProcessorContext context) throws ClassNotFoundException {
        SpringBeanElement bean = null;
        for (SpringBeanElement sbe : beans) {
            if (sbe.getId().equals(target)) {
                bean = sbe;
                break;
            }
        }
        if (bean == null) {
            error(context.getMonitor(), "TargetBeanDoesNotExist", null, target);
            return null;
        }

        Class<?> beanClass = resolveClass(resolver, bean.getClassName(), context);
        List<Class<?>> ifaces = javaIntrospectionHelper.getAllInterfaces(beanClass);
        for (Class<?> interfaze : ifaces) {
            if (interfaze.isAnnotationPresent(Remotable.class)) {
                return interfaze;
            }
View Full Code Here

TOP

Related Classes of org.apache.tuscany.sca.implementation.spring.SpringBeanElement

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.