Package org.apache.tuscany.sca.assembly

Examples of org.apache.tuscany.sca.assembly.ComponentType


     * @param resolver
     * @param impl
     */
    private void mergeComponentType(ModelResolver resolver, JavaImplementation impl) {
        // FIXME: Need to clarify how to merge
        ComponentType componentType = getComponentType(resolver, impl);
        if (componentType != null && !componentType.isUnresolved()) {
            Map<String, Reference> refMap = new HashMap<String, Reference>();
            for (Reference ref : impl.getReferences()) {
                refMap.put(ref.getName(), ref);
            }
            for (Reference reference : componentType.getReferences()) {
                refMap.put(reference.getName(), reference);
            }
            impl.getReferences().clear();
            impl.getReferences().addAll(refMap.values());

            // Try to match references by type
            Map<String, JavaElementImpl> refMembers = impl.getReferenceMembers();
            for (Reference ref : impl.getReferences()) {
                if (ref.getInterfaceContract() != null) {
                    Interface i = ref.getInterfaceContract().getInterface();
                    if (i instanceof JavaInterface) {
                        Class<?> type = ((JavaInterface)i).getJavaClass();
                        if (!refMembers.containsKey(ref.getName())) {
                            JavaElementImpl e = getMemeber(impl, ref.getName(), type);
                            if (e != null) {
                                refMembers.put(ref.getName(), e);
                            }
                        }
                    }
                }
            }

            Map<String, Service> serviceMap = new HashMap<String, Service>();
            for (Service svc : impl.getServices()) {
                serviceMap.put(svc.getName(), svc);
            }
            for (Service service : componentType.getServices()) {
                serviceMap.put(service.getName(), service);
            }
            impl.getServices().clear();
            impl.getServices().addAll(serviceMap.values());

            Map<String, Property> propMap = new HashMap<String, Property>();
            for (Property prop : impl.getProperties()) {
                propMap.put(prop.getName(), prop);
            }
            for (Property property : componentType.getProperties()) {
                propMap.put(property.getName(), property);
            }
            impl.getProperties().clear();
            impl.getProperties().addAll(propMap.values());

            if (componentType.getConstrainingType() != null) {
                impl.setConstrainingType(componentType.getConstrainingType());
            }
        }
    }
View Full Code Here


    }

    private ComponentType getComponentType(ModelResolver resolver, JavaImplementation impl) {
        String className = impl.getJavaClass().getName();
        String componentTypeURI = className.replace('.', '/') + ".componentType";
        ComponentType componentType = assemblyFactory.createComponentType();
        componentType.setUnresolved(true);
        componentType.setURI(componentTypeURI);
        componentType = resolver.resolveModel(ComponentType.class, componentType);
        if (!componentType.isUnresolved()) {
            return componentType;
        }
        return null;
    }
View Full Code Here

    public void tearDown() throws Exception {
    }

    public void testReadWriteComponentType() throws Exception {
        InputStream is = getClass().getResourceAsStream("/CalculatorServiceImpl.componentType");
        ComponentType componentType = staxProcessor.read(is, ComponentType.class);
        assertNotNull(componentType);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        staxProcessor.write(componentType, bos);
        assertEquals("<?xml version='1.0' encoding='UTF-8'?><componentType xmlns=\"http://www.osoa.org/xmlns/sca/1.0\" xmlns:ns1=\"http://www.osoa.org/xmlns/sca/1.0\"><service name=\"CalculatorService\"><binding.sca /></service><reference name=\"addService\"><binding.sca /></reference></componentType>",
                     bos.toString());
View Full Code Here

            String ctURI = bundleName.endsWith(".jar") || bundleName.endsWith(".JAR")?
                    bundleName.substring(0, bundleName.lastIndexOf(".")) : bundleName;
            ctURI = ctURI.replaceAll("\\.", "/");
            ctURI = ctURI + ".componentType";

            ComponentType componentType = assemblyFactory.createComponentType();
            componentType.setURI(ctURI);
            componentType.setUnresolved(true);
            componentType = resolver.resolveModel(ComponentType.class, componentType);
            if (componentType.isUnresolved()) {

                throw new ContributionResolveException("missing .componentType side file " + ctURI);
            }
           
            List<Service> services = componentType.getServices();
            for (Service service : services) {
                Interface interfaze = service.getInterfaceContract().getInterface();
                if (interfaze instanceof JavaInterface) {
                    JavaInterface javaInterface = (JavaInterface)interfaze;
                    if (javaInterface.getJavaClass() == null) {
                       
                        javaInterface.setJavaClass(getJavaClass(resolver, javaInterface.getName()));
                    }
                    Class<?> callback = null;
                    if (service.getInterfaceContract().getCallbackInterface() instanceof JavaInterface) {
                        JavaInterface callbackInterface = (JavaInterface)service.getInterfaceContract().getCallbackInterface();
                        if (callbackInterface.getJavaClass() == null) {
                            callbackInterface.setJavaClass(getJavaClass(resolver, callbackInterface.getName()));
                        }
                        callback = callbackInterface.getJavaClass();
                    }
                   
                    Service serv = createService(service, javaInterface.getJavaClass(), callback);
                    impl.getServices().add(serv);
                }
            }
           
            List<Reference> references = componentType.getReferences();
            for (Reference reference : references) {
                Interface interfaze = reference.getInterfaceContract().getInterface();
                if (interfaze instanceof JavaInterface) {
                    JavaInterface javaInterface = (JavaInterface)interfaze;
                    if (javaInterface.getJavaClass() == null) {
                        javaInterface.setJavaClass(getJavaClass(resolver, javaInterface.getName()));
                    }
                    Reference ref = createReference(reference, javaInterface.getJavaClass());
                    impl.getReferences().add(ref);
                }
                else
                    impl.getReferences().add(reference);
            }
           
            List<Property> properties = componentType.getProperties();
            for (Property property : properties) {
                impl.getProperties().add(property);
            }
            impl.setConstrainingType(componentType.getConstrainingType());
          
           
        } catch (Exception e) {
            throw new ContributionResolveException(e);
        }
View Full Code Here

            XMLStreamReader reader = inputFactory.createXMLStreamReader(url.toString(), urlStream);
            ValidatingXMLInputFactory.setMonitor(reader, monitor);
            reader.nextTag();
           
            // Reader the componentType model
            ComponentType componentType = (ComponentType)extensionProcessor.read(reader, context);
            if (componentType != null) {
                componentType.setURI(uri.toString());
            }

            // For debugging purposes, write it back to XML
//            if (componentType != null) {
//                try {
View Full Code Here

        //
        staxHelper = StAXHelper.getInstance(extensionPoints);
    }
   
    public ComponentType read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException {
        ComponentType componentType = null;
        Service service = null;
        Reference reference = null;
        Contract contract = null;
        Property property = null;
        Callback callback = null;
        QName name = null;
       
        try {
            // Read the componentType document
            while (reader.hasNext()) {
                int event = reader.getEventType();
                switch (event) {
                    case START_ELEMENT:
                        name = reader.getName();
   
                        if (Constants.COMPONENT_TYPE_QNAME.equals(name)) {
   
                            // Read a <componentType>
                            componentType = assemblyFactory.createComponentType();
   
                        } else if (Constants.SERVICE_QNAME.equals(name)) {
   
                            // Read a <service>
                            service = assemblyFactory.createService();
                            contract = service;
                            service.setName(getString(reader, Constants.NAME));
                            componentType.getServices().add(service);
                            policyProcessor.readPolicies(service, reader);
   
                        } else if (Constants.REFERENCE_QNAME.equals(name)) {
   
                            // Read a <reference>
                            reference = assemblyFactory.createReference();
                            contract = reference;
                            reference.setName(getString(reader, Constants.NAME));
                            reference.setWiredByImpl(getBoolean(reader, Constants.WIRED_BY_IMPL));
                            readMultiplicity(reference, reader);
                            readTargets(reference, reader);
                            componentType.getReferences().add(reference);
                            policyProcessor.readPolicies(reference, reader);
   
                        } else if (Constants.PROPERTY_QNAME.equals(name)) {
   
                            // Read a <property>
                            property = assemblyFactory.createProperty();
                            readAbstractProperty(property, reader, context);
                            policyProcessor.readPolicies(property, reader);
                           
                            // Read the property value
                            Document value = readPropertyValue(property.getXSDElement(), property.getXSDType(), property.isMany(), reader, context);
                            property.setValue(value);
                           
                            componentType.getProperties().add(property);
                           
                        } else if (Constants.IMPLEMENTATION_QNAME.equals(name)) {
                           
                            // Read an <implementation> element
                            policyProcessor.readPolicies(componentType, reader);
View Full Code Here

                int d = uri.lastIndexOf('.');
                if (d != -1) {
                    uri = uri.substring(0, d) + ".componentType";

                    // Resolve the component type
                    ComponentType componentType = assemblyFactory.createComponentType();
                    componentType.setURI(uri);
                    componentType.setUnresolved(true);

                    componentType = resolver.resolveModel(ComponentType.class, componentType, context);
                    if (componentType != null && !componentType.isUnresolved()) {

                        // We found a component type, merge it into the implementation model
                        implementation.getServices().addAll(componentType.getServices());
                        implementation.getReferences().addAll(componentType.getReferences());
                        implementation.getProperties().addAll(componentType.getProperties());

                        if (implementation instanceof PolicySubject &&
                                componentType instanceof PolicySubject ) {
                            PolicySubject policiedImpl = (PolicySubject)implementation;
                            PolicySubject policiedCompType = (PolicySubject)componentType;
View Full Code Here

     *
     */
    public void load(SpringImplementation implementation, ModelResolver resolver, ProcessorContext context)
        throws ContributionReadException {
        //System.out.println("Spring TypeLoader - load method start");
        ComponentType componentType = implementation.getComponentType();
        /* Check that there is a component type object already set  */
        if (componentType == null) {
            throw new ContributionReadException("SpringXMLLoader load: implementation has no ComponentType object");
        }
        if (componentType.isUnresolved()) {
            /* Fetch the location of the application-context file from the implementation */
            loadFromXML(implementation, resolver, context);
            if (!componentType.isUnresolved())
                implementation.setUnresolved(false);
        } // end if
          //System.out.println("Spring TypeLoader - load method complete");
    } // end method load
View Full Code Here

         * 6. Each bean property which is not a reference and which is not pointing
         *    at another bean in the application context becomes a property in the component type
         */

        JavaImplementation javaImplementation = null;
        ComponentType componentType = implementation.getComponentType();

        try {
            // Deal with the services first....
            Iterator<SpringSCAServiceElement> its = services.iterator();
            while (its.hasNext()) {
                SpringSCAServiceElement serviceElement = its.next();

                Class<?> interfaze;
                if (serviceElement.getType() != null) {
                    interfaze = resolveClass(resolver, serviceElement.getType(), context);
                } else {
                    interfaze = getBeanInterface(resolver, serviceElement.getTarget(), beans, context);
                }

                Service theService = createService(interfaze, serviceElement.getName());
                // Spring allows duplication of bean definitions in multiple context scenario,
                // in such cases, the latest bean definition overrides the older ones, hence
                // we will remove any older definition and use the latest.
                Service duplicate = null;
                for (Service service : componentType.getServices()) {
                    if (service.getName().equals(theService.getName()))
                        duplicate = service;
                }
                if (duplicate != null)
                    componentType.getServices().remove(duplicate);

                componentType.getServices().add(theService);
                // Add this service to the Service / Bean map
                String beanName = serviceElement.getTarget();
                boolean found = false;
                for (SpringBeanElement beanElement : beans) {
                    if (beanName.equals(beanElement.getId())) {
                        if (isValidBeanForService(beanElement)) {
                            // add the required intents and policySets for the service
                            theService.getRequiredIntents().addAll(serviceElement.getRequiredIntents());
                            theService.getPolicySets().addAll(serviceElement.getPolicySets());
                            implementation.setBeanForService(theService, beanElement);
                            found = true;
                            break;
                        }
                    }
                } // end for

                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();
View Full Code Here

    @Test
    public void testReadComponentType() throws Exception {
        InputStream is = getClass().getResourceAsStream("CalculatorImpl.componentType");
        XMLStreamReader reader = inputFactory.createXMLStreamReader(is);
        ComponentType componentType = (ComponentType)staxProcessor.read(reader);
        assertNotNull(componentType);
    }
View Full Code Here

TOP

Related Classes of org.apache.tuscany.sca.assembly.ComponentType

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.