private void configureNode()

in modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/NodeImpl.java [493:635]


    private void configureNode(ConfiguredNodeImplementation configuration, String compositeContent) throws Exception {

        // Find if any contribution JARs already available locally on the classpath
        Map<String, URL> localContributions = localContributions();

        // Load the specified contributions
        ContributionService contributionService = runtime.getContributionService();
        contributions = new ArrayList<Contribution>();
        for (Contribution contribution : configuration.getContributions()) {
            URI uri = createURI(contribution.getLocation());
            if (uri.getScheme() == null) {
                uri = new File(contribution.getLocation()).toURI();
            }
            URL contributionURL = uri.toURL();

            // Extract contribution file name
            String file = contributionURL.getPath();
            int i = file.lastIndexOf('/');
            if (i != -1 && i < file.length() - 1) {
                file = file.substring(i + 1);

                // If we find the local contribution file on the classpath, use it in
                // place of the original contribution URL
                URL localContributionURL = localContributions.get(file);
                if (localContributionURL != null) {
                    contributionURL = localContributionURL;
                }
            }

            // Load the contribution
            logger.log(Level.INFO, "Loading contribution: " + contributionURL);
            contributions.add(contributionService.contribute(contribution.getURI(), contributionURL, false));
            analyzeProblems();
        }

        composite = configuration.getComposite();

        // Read the composite content after the contributions have been loaded, so that the
        // policySet definitions provided by the contributions are available (TUSCANY-3569)
        if (composite == null && compositeContent != null) {
            logger.log(Level.INFO, "Loading composite: " + configurationName);
            CompositeDocumentProcessor compositeDocProcessor =
                (CompositeDocumentProcessor)documentProcessors.getProcessor(Composite.class);
            composite =
                compositeDocProcessor.read(URI.create(configurationName), new ByteArrayInputStream(compositeContent
                    .getBytes("UTF-8")));
            analyzeProblems();
            configuration.setComposite(composite);
        }

        if(composite != null && composite.isUnresolved()) {
            ContributionFactory contributionFactory = modelFactories.getFactory(ContributionFactory.class);
            Artifact compositeFile = contributionFactory.createArtifact();
            compositeFile.setUnresolved(true);
            compositeFile.setURI(composite.getURI());
            for (Contribution c : contributions) {
                ModelResolver resolver = c.getModelResolver();
                Artifact resolved = resolver.resolveModel(Artifact.class, compositeFile);
                if (resolved != null && resolved.isUnresolved() == false) {
                    composite = (Composite) resolved.getModel();
                    break;
                }
            }
        }

        // FIXME: This is a hack to get a list of deployable composites. By design, the deployment composite should
        // has been configured
        if (composite == null) {
            List<Composite> deployables = new ArrayList<Composite>();
            for (Contribution c : contributions) {
                deployables.addAll(c.getDeployables());
            }
            aggregate(deployables);
            configuration.setComposite(composite);
        }

        Contribution contribution = null;
        if (composite.getName() == null) {
            // Load the specified composite
            URL compositeURL;

            URI uri = createURI(configuration.getComposite().getURI());
            if (uri.getScheme() == null) {

                // If the composite URI is a relative URI, try to resolve it within the contributions
                contribution = contribution(contributions, uri.toString());
                if (contribution == null) {
                    throw new IllegalArgumentException("Composite is not found in contributions: " + uri);
                }
                compositeURL = new URL(location(contribution, uri.toString()));

            } else {

                // If the composite URI is an absolute URI, use it as is
                compositeURL = uri.toURL();
            }

            URLArtifactProcessor<Composite> compositeDocProcessor = documentProcessors.getProcessor(Composite.class);
            // Read the composite
            logger.log(Level.INFO, "Loading composite: " + compositeURL);
            // InputStream is = compositeURL.openStream();
            // XMLStreamReader reader = inputFactory.createXMLStreamReader(is);
            try {
                composite = compositeDocProcessor.read(null, uri, compositeURL);
            } catch (ContributionReadException e) {
                // ignore - errors will be detected by analyzeProblems() call below
            }
            // reader.close();

            analyzeProblems();

        }
        // And resolve the composite within the scope of the last contribution
        if (contribution == null && contributions.size() != 0) {
            contribution = contributions.get(contributions.size() - 1);
        }

        // Resolve the given composite within the scope of the selected contribution
        if (contribution != null) {
            StAXArtifactProcessor<Composite> compositeProcessor = artifactProcessors.getProcessor(Composite.class);
            compositeProcessor.resolve(composite, contribution.getModelResolver());
            analyzeProblems();
        }
        // Create a top level composite to host our composite
        // This is temporary to make the activator happy
        AssemblyFactory assemblyFactory = runtime.getAssemblyFactory();
        Composite tempComposite = assemblyFactory.createComposite();
        tempComposite.setName(new QName("http://tuscany.apache.org/xmlns/sca/1.0", "temp"));
        tempComposite.setURI("http://tuscany.apache.org/xmlns/sca/1.0");

        // Include the node composite in the top-level composite 
        tempComposite.getIncludes().add(composite);

        // set the top level composite on the composite activator as 
        // logic in callable reference resolution relies on this being 
        // available
        compositeActivator.setDomainComposite(tempComposite);

        // Build the composite
        runtime.buildComposite(composite);

        analyzeProblems();
    }