Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF 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 org.apache.tuscany.sca.node.impl;
 
 import static java.lang.System.currentTimeMillis;
 import static org.apache.tuscany.sca.common.java.io.IOHelper.createURI;
 import static org.apache.tuscany.sca.common.java.io.IOHelper.openStream;
 
 import java.io.File;
 import java.io.Reader;
 import java.net.URI;
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
 
 
This class provides a node factory that can create multiple nodes that share the same extension point registry
 
 public class NodeFactoryImpl extends NodeFactory {
     protected static final Logger logger = Logger.getLogger(NodeImpl.class.getName());
 
     protected boolean inited;
     protected Map<ObjectNodenodes = new ConcurrentHashMap<ObjectNode>();
 
     protected Deployer deployer;
     protected ExtensionPointRegistry registry;
     protected ProxyFactory proxyFactory;
     protected MonitorFactory monitorFactory;
    
    
Automatically destroy the factory when last node is stopped. Subclasses can set this flag.
    protected boolean autoDestroy = true;
    boolean quietLogging;
    @Override
    public Node createNode(NodeConfiguration configuration) {
        if (configuration.getURI() == null) {
            // Make sure a unique node URI is created for the same node factory
            configuration.setURI(generateNodeURI());
        }
        return new NodeImpl(thisconfiguration);
    }
    protected Node removeNode(NodeConfiguration configuration) {
        Node node = .remove(getNodeKey(configuration));
        if ( && .isEmpty()) {
            destroy();
        }
        return node;
    }
    @SuppressWarnings("unchecked")
    @Override
    public Node createNode(List<?> contributions) {
        NodeConfigurationFactory nodeConfigurationFactory = new DefaultNodeConfigurationFactory();
        return new NodeImpl(thisnodeConfigurationFactory.createNodeConfiguration(), new ArrayList<Contribution>((List<Contribution>)contributions));
    }
    protected void addNode(NodeConfiguration configurationNode node) {
        .put(getNodeKey(configuration), node);
    }
    @Override
    public NodeConfiguration loadConfiguration(InputStream xmlURL base) {
        try {
            init();
            InputStreamReader reader = new InputStreamReader(xml"UTF-8");
            ProcessorContext context = .createProcessorContext();
            NodeConfiguration config = .loadXMLDocument(readercontext.getMonitor());
            if (base != null && config != null) {
                // Resolve the contribution location against the node.xml
                for (ContributionConfiguration c : config.getContributions()) {
                    String location = c.getLocation();
                    if (location != null) {
                        URL url = new URL(baselocation);
                        url = IOHelper.normalize(url);
                        c.setLocation(url.toString());
                    }
                }
            }
            return config;
        } catch (Throwable e) {
            throw new ServiceRuntimeException(e);
        }
    }
    
    public Map<ObjectNodegetNodes() {
        return ;
    }
    public List<NodegetNodesInDomain(String domainName) {
        List<NodedomainNodes = new ArrayList<Node>();
        if ( != null) {
            for (Node n : .values()) {
                if (domainName.equals(((NodeImpl)n).getConfiguration().getDomainURI())) {
                    domainNodes.add(n);
                }
            }
        }
        return domainNodes;
    }
    protected Object getNodeKey(NodeConfiguration configuration) {
        return new NodeKey(configuration);
    }
    public synchronized void destroy() {
        if () {
            for (Node node : .values()) {
                node.stop();
            }
            .clear();
            .stop();
            .stop();
            super.destroy();
             = false;
        }
    }

    
Analyze problems reported by the artifact processors and builders.

    private void analyzeProblems(Monitor monitorthrows Throwable {
        try {
            for (Problem problem : monitor.getProblems()) {
                if ((problem.getSeverity() == .)) {
                    if (problem.getCause() != null) {
                        throw problem.getCause();
                    } else {
                        throw new ServiceRuntimeException(problem.toString());
                    }
                }
            }
        } finally {
            // FIXME: Clear problems so that the monitor is clean again
            monitor.reset();
        }
    }
    private boolean attachDeploymentComposite(Contribution contributionReader xmlString locationboolean attachedProcessorContext context)
        // Read the composite model
        Composite composite = .loadXMLDocument(xmlcontext.getMonitor());
        // Replace the deployable composites with the deployment composites
        // Clear the deployable composites if it's the first deployment composite
        .attachDeploymentComposite(contributioncompositeattached);
        if (!attached) {
            attached = true;
        } 
        return attached;
    }
        if ( == null) {
            // Create extension point registry
             = createExtensionPointRegistry();
            .start();
        }        
        return ;
    }
    public synchronized void init() {
        if () {
            return;
        }
        long start = currentTimeMillis();
        getExtensionPointRegistry();
        
         = utilities.getUtility(MonitorFactory.class);
        utilities.getUtility(RuntimeProperties.class).setProperties();
        
        // Use the runtime-enabled assembly factory
        FactoryExtensionPoint modelFactories = .getExtensionPoint(FactoryExtensionPoint.class);
        AssemblyFactory assemblyFactory = new RuntimeAssemblyFactory();
        modelFactories.addFactory(assemblyFactory);
        // Load the Deployer
         = utilities.getUtility(Deployer.class);
        // Enable schema validation only of the logger level is FINE or higher
        // Initialize the Tuscany module activators
        // The module activators will be started
        // Initialize runtime
        // Get proxy factory
         = ExtensibleProxyFactory.getInstance();
        utilities.getUtility(WorkScheduler.class);
         = true;
        if (.isLoggable(.)) {
            long end = currentTimeMillis();
            .fine("The tuscany runtime started in " + (end - start) + " ms.");
        }
    }
    
    public void start() {
        init();
        DomainRegistryFactory domainRegistryFactory = ExtensibleDomainRegistryFactory.getInstance();
        domainRegistryFactory.getEndpointRegistry(.getProperty("reguri"), .getProperty("defaultDomainName"));
    }
        return new DefaultExtensionPointRegistry();
    }
    
    protected boolean isSchemaValidationEnabled() {
        String enabled = getSystemProperty(ValidationSchemaExtensionPoint.class.getName() + ".enabled");
        if (enabled == null) {
            enabled = "true";
        }
        boolean debug = .isLoggable(.);
        return "true".equals(enabled) || debug;
    }    
    protected Composite configureNode(NodeConfiguration configurationList<ContributioncontributionsProcessorContext context)
        throws Throwable {
        if (contributions == null) {
            // Load contributions
            contributions = loadContributions(configurationcontext);
        }
        
        Monitor monitor = context.getMonitor();
        Map<QNameList<String>> bindingBaseURIs = new HashMap<QNameList<String>>();
        for (BindingConfiguration config : configuration.getBindings()) {
            bindingBaseURIs.put(config.getBindingType(), config.getBaseURIs());
        }
        List<ContributionallContributions = getAllContributions(configuration.getDomainURI());
        
        Composite domainComposite = .build(contributionsallContributionsbindingBaseURIsmonitor);
        analyzeProblems(monitor);
        
        // postBuildEndpointReferenceMatching(domainComposite);
        
        return domainComposite;
    }
    
    private List<ContributiongetAllContributions(String domainName) {
        List<Contributioncontributions = new ArrayList<Contribution>();
        for (NodeFactory f : getNodeFactories()) {
            for (Node node : ((NodeFactoryImpl)f).getNodesInDomain(domainName)) {
                contributions.addAll(((NodeImpl)node).getContributions());            
            }
        }
        return contributions;
    }   
    protected List<ContributionloadContributions(NodeConfiguration configurationProcessorContext contextthrows Throwable {
        List<Contributioncontributions = new ArrayList<Contribution>();
        // Load the specified contributions
        for (ContributionConfiguration contrib : configuration.getContributions()) {
            URI contributionURI = createURI(contrib.getURI());
            URI uri = createURI(contrib.getLocation());
            if (uri.getScheme() == null) {
                uri = new File(contrib.getLocation()).toURI();
            }
            URL contributionURL = uri.toURL();
            // Load the contribution
            .log(. : ."Loading contribution: " + contributionURL);
            Contribution contribution = .loadContribution(contributionURIcontributionURLcontext.getMonitor());
            contributions.add(contribution);
            boolean attached = false;
            for (DeploymentComposite dc : contrib.getDeploymentComposites()) {
                if (dc.getContent() != null) {
                    Reader xml = new StringReader(dc.getContent());
                    attached = attachDeploymentComposite(contributionxmlnullattachedcontext);
                } else if (dc.getLocation() != null) {
                    URI dcURI = createURI(dc.getLocation());
                    if (!dcURI.isAbsolute()) {
                        Composite composite = null;
                        // The location is pointing to an artifact within the contribution
                        for (Artifact a : contribution.getArtifacts()) {
                            if (dcURI.toString().equals(a.getURI())) {
                                composite = (Composite)a.getModel();
                                if (!attached) {
                                    contribution.getDeployables().clear();
                                    attached = true;
                                }
                                contribution.getDeployables().add(composite);
                                break;
                            }
                        }
                        if (composite == null) {
                            // Not found
                            throw new ServiceRuntimeException("Deployment composite " + dcURI
                                + " cannot be found within contribution "
                                + contribution.getLocation());
                        }
                    } else {
                        URL url = dcURI.toURL();
                        InputStream is = openStream(url);
                        Reader xml = new InputStreamReader(is"UTF-8");
                        attached = attachDeploymentComposite(contributionxmlurl.toString(), attachedcontext);
                    }
                }
            }
            analyzeProblems(context.getMonitor());
        }
        return contributions;
    }
    protected static String getSystemProperty(final String name) {
        return AccessController.doPrivileged(new PrivilegedAction<String>() {
            public String run() {
                return System.getProperty(name);
            }
        });
    }
    protected static class NodeKey {
        private String domainURI;
        private String nodeURI;
        public NodeKey(NodeConfiguration configuration) {
            this. = configuration.getDomainURI();
            this. = configuration.getURI();
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + (( == null) ? 0 : .hashCode());
            result = prime * result + (( == null) ? 0 : .hashCode());
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            NodeKey other = (NodeKey)obj;
            if ( == null) {
                if (other.domainURI != null)
                    return false;
            } else if (!.equals(other.domainURI))
                return false;
            if ( == null) {
                if (other.nodeURI != null)
                    return false;
            } else if (!.equals(other.nodeURI))
                return false;
            return true;
        }
        public String toString() {
            StringBuffer buf = new StringBuffer();
            if ( != null) {
                buf.append("{").append().append("}");
            }
            if ( != null) {
                buf.append();
            }
            return buf.toString();
        }
    }
    @Override
    public void configure(Map<StringMap<StringString>> attributes) {
        for (Map.Entry<StringMap<StringString>> e : attributes.entrySet()) {
            discovery.setAttribute(e.getKey(), e.getValue());
        }
        for (Object o : .keySet()) {
            String p = (Stringo;
            if (p.indexOf('.') > -1) {
                String serviceType = p.substring(0, p.lastIndexOf('.'));
                String attribute = p.substring(p.lastIndexOf('.')+1);
                discovery.setAttribute(serviceTypeattribute.getProperty(p));
            }
        }
        super.configure(attributes);
    }
    
    
Added to allow the node access to the deployer in order to get to the systemContribution and hence set up the CompositeContext so that the runtime epr matching algorithm can get at the binding types

Returns:
    public Deployer getDeployer() {
        return ;
    }
    @Override
    public void setAutoDestroy(boolean b) {
         = b;
    }
    
New to GrepCode? Check out our FAQ X