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.contribution.processor.impl;
 
 import java.io.File;
 import java.net.URI;
 import java.net.URL;
 import java.util.List;
 
URLArtifactProcessor that handles contribution files and the artifacts they contain and returns a contribution model.

Version:
$Rev: 983165 $ $Date: 2010-08-07 03:43:54 +0100 (Sat, 07 Aug 2010) $
 
     // Marks pre-resolve phase completed
     private boolean preResolved = false;
 
     public ContributionContentProcessor(ExtensionPointRegistry extensionPointsStAXArtifactProcessor<ObjectextensionProcessor) {
         this. = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class);
         this. = extensionPoints.getExtensionPoint(ModelResolverExtensionPoint.class);
         URLArtifactProcessorExtensionPoint artifactProcessors = extensionPoints.getExtensionPoint(URLArtifactProcessorExtensionPoint.class);
         this. = new ExtensibleURLArtifactProcessor(artifactProcessors);
         this. = extensionProcessor;
         this. = extensionPoints.getExtensionPoint(ContributionScannerExtensionPoint.class);
     }
 
     public String getArtifactType() {
         return ".contribution/content";
     }
 
     public Class<ContributiongetModelType() {
         return Contribution.class;
     }
 
     private File toFile(URL url) {
         if("file".equalsIgnoreCase(url.getProtocol())) {
             try {
                 return new File(url.toURI());
             } catch(URISyntaxException e) {
                 return new File(url.getPath());
             } catch(IllegalArgumentException e) {
                // Hack for file:./a.txt or file:../a/c.wsdl
                return new File(url.getPath());
            }
        }
        return null;
    }
    public Contribution read(URL parentURLURI contributionURIURL contributionURLProcessorContext contextthrows ContributionReadException {
        
        // Create contribution model
        Contribution contribution = .createContribution();
        contribution.setURI(contributionURI.toString());
        contribution.setLocation(contributionURL.toString());
        ModelResolver modelResolver = new ExtensibleModelResolver(contribution);
        contribution.setModelResolver(modelResolver);
        contribution.setUnresolved(true);
        
        Monitor monitor = context.getMonitor();
        monitor.pushContext("Contribution: " + contribution.getURI());
        Contribution old = context.setContribution(contribution);
        try {
            // Create a contribution scanner
            ContributionScanner scanner = .getContributionScanner(contributionURL.getProtocol());
            if (scanner == null) {
                File file = toFile(contributionURL);
                if (file != null && file.isDirectory()) {
                    scanner = new DirectoryContributionScanner();
                } else {
                    scanner = new JarContributionScanner();
                }
            }
    
            // Scan the contribution and list the artifacts contained in it
            boolean contributionMetadata = false;
            List<Artifactartifacts = scanner.scan(contribution);
            for (Artifact artifact : artifacts) {
                // Add the deployed artifact model to the contribution
                modelResolver.addModel(artifactcontext);
                
                monitor.pushContext("Artifact: " + artifact.getURI());
    
                Artifact oldArtifact = context.setArtifact(artifact);
                try {
                    // Read each artifact
                    URL artifactLocationURL = null;
                    try {
                        artifactLocationURL = new URL(artifact.getLocation());
                    } catch(MalformedURLException e) {
                        //ignore
                    }
                    
                    Object model = .read(contributionURL, URI.create(artifact.getURI()), artifactLocationURLcontext);
                    if (model != null) {
                        artifact.setModel(model);
        
                        // Add the loaded model to the model resolver
                        modelResolver.addModel(modelcontext);
        
                        // Merge contribution metadata into the contribution model
                        if (model instanceof ContributionMetadata) {
                            contributionMetadata = true;
                            ContributionMetadata c = (ContributionMetadata)model;
                            contribution.getImports().addAll(c.getImports());
                            contribution.getExports().addAll(c.getExports());
                            contribution.getDeployables().addAll(c.getDeployables());
                            contribution.getExtensions().addAll(c.getExtensions());
                            contribution.getAttributeExtensions().addAll(c.getAttributeExtensions());
                        }
                    }
                } finally {
                    monitor.popContext();
                    context.setArtifact(oldArtifact);
                }                    
            }
            
            List<ArtifactcontributionArtifacts = contribution.getArtifacts();
            contributionArtifacts.addAll(artifacts);
    
            // If no sca-contribution.xml file was provided then just consider
            // all composites in the contribution as deployables
            if (!contributionMetadata) {
                for (Artifact artifactartifacts) {
                    if (artifact.getModel() instanceof Composite) {
                        contribution.getDeployables().add((Composite)artifact.getModel());
                    }
                }
    
                // Add default contribution import and export
                DefaultImport defaultImport = .createDefaultImport();
                defaultImport.setModelResolver(new DefaultModelResolver());
                contribution.getImports().add(defaultImport);
                DefaultExport defaultExport = .createDefaultExport();
                contribution.getExports().add(defaultExport);
            } else {
                if (contribution.getDeployables().size() > 0) {
                    // Update the deployable Composite objects with the correct Composite object for the artifact
                    for (Artifact a : contribution.getArtifacts()) {
                        if (a.getModel() instanceof Composite) {
                            for (ListIterator<Compositelit = contribution.getDeployables().listIterator(); lit.hasNext();) {
                                if (lit.next().getName().equals(((Composite)a.getModel()).getName())) {
                                    lit.set((Composite)a.getModel());
                                }
                            }
                        }
                    }
                }
            }
        } finally {
            monitor.popContext();
            context.setContribution(old);
        }
        
        return contribution;
    }

    
A pre-resolution step, which is required for Contributions to handle the resolution of imports and exports so that at resolve time, imports can be followed to exports and anything exported that is required can be resolved on demand without the need to have already resolved the whole of the Contribution containing the export

Parameters:
contribution - the Contribution
resolver - the Resolver to use
Throws:
org.apache.tuscany.sca.contribution.processor.ContributionResolveException
    public void preResolve(Contribution contributionModelResolver resolverProcessorContext contextthrows ContributionResolveException {
        // Resolve the contribution model itself
        ModelResolver contributionResolver = contribution.getModelResolver();
        contribution.setUnresolved(false);
        contributionResolver.addModel(contributioncontext);
        // Resolve Exports
        resolveExports(contributioncontributionResolvercontext);
        // Resolve Imports
        resolveImports(contributioncontributionResolvercontext);
         = true;
    } // end method preResolve
    public void resolve(Contribution contributionModelResolver resolverProcessorContext contextthrows ContributionResolveException {
        Monitor monitor = context.getMonitor();
        Contribution old = context.setContribution(contribution);
    	try {
    		monitor.pushContext("Contribution: " + contribution.getURI());
    		
	    	if( ! ) preResolvecontributionresolvercontext);
	    	ModelResolver contributionResolver = contribution.getModelResolver();
	        // Validate Java Exports: [JCI100007] A Java package that is specified on an export 
	    	// element MUST be contained within the contribution containing the export element.
	    	for (Export exportcontribution.getExports()) {
	    		if (export instanceof JavaExport) {
	    			boolean available = false;
	    			String packageName = ((JavaExport)export).getPackage();
		    		for (Artifact artifact : contribution.getArtifacts()) {
		    			if (packageName.equals(artifact.getURI().replace("/"".")))
		    					available = true;
			    	}
		    		if (! available)
		    			throw new ContributionResolveException("[JCI100007] A Java package "packageName +" that is specified on an export " +
		    					"element MUST be contained within the contribution containing the export element.");
	    		}
	    	}	    	
	    	
	    	// Resolve all artifact models
	        for (Artifact artifact : contribution.getArtifacts()) {
	            Object model = artifact.getModel();
	            if (model != null) {
	                Artifact oldArtifact = context.setArtifact(artifact);
	                try {
	                   .resolve(modelcontributionResolvercontext);
	                } catch (Throwable e) {
	                    throw new ContributionResolveException(e);
	                } finally {
	                    context.setArtifact(oldArtifact);
	                }
	            }
	        }
            // Resolve deployable composites
            List<Compositedeployables = contribution.getDeployables();
            Artifact oldArtifact = context.setArtifact(contribution);
            try {
                for (int i = 0, n = deployables.size(); i < ni++) {
                    Composite deployable = deployables.get(i);
                    Composite resolved =
                        (Composite)contributionResolver.resolveModel(Composite.classdeployablecontext);
                    if (resolved != deployable) {
                        deployables.set(iresolved);
                    }
                } // end for
            } finally {
                context.setArtifact(oldArtifact);
            }
    	} finally {
    		monitor.popContext();
    		context.setContribution(old);
    	} // end try
    } // end method resolve

    
Resolves the Exports of the contribution

Parameters:
contribution
resolver
    private void resolveExports(Contribution contributionModelResolver resolverProcessorContext contextthrows ContributionResolveException {
    	for (Export exportcontribution.getExports()) {
            if (export instanceof DefaultExport) {
                // Initialize the default export's resolver
                export.setModelResolver(resolver);
            } else {
                .resolve(exportresolvercontext);
            } // end if
        } // end for
    } // end method resolveExports

    
Resolves the Imports of the contribution

Parameters:
contribution
resolver
    private void resolveImports(Contribution contributionModelResolver resolverProcessorContext contextthrows ContributionResolveException {
        for (Import import_contribution.getImports()) {
            .resolve(import_resolvercontext);
        } // end for
    } // end method resolveImports
// end class ContributionContentProcessor
New to GrepCode? Check out our FAQ X