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.implementation.bpel.xml;
 
 import java.net.URI;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
A Model Resolver for BPEL process models.

Version:
$Rev: 967109 $ $Date: 2010-07-23 15:30:46 +0100 (Fri, 23 Jul 2010) $
 
 public class BPELDocumentModelResolver implements ModelResolver {
 	
     private WSDLFactory wsdlFactory;
     private Contribution contribution;
     
     public BPELDocumentModelResolver(Contribution contributionFactoryExtensionPoint modelFactories) {
     	this. = modelFactories.getFactory(WSDLFactory.class);
         this. = contribution;
     }
 
     public void addModel(Object resolvedProcessorContext context) {
         BPELProcessDefinition process = (BPELProcessDefinition)resolved;
         .put(process.getName(), process);
     }
     
     public Object removeModel(Object resolvedProcessorContext context) {
         return .remove(((BPELProcessDefinition)resolved).getName());
     }
     
     public <T> T resolveModel(Class<T> modelClass, T unresolvedProcessorContext context) {    	
     	BPELProcessDefinition resolved = null;
     	QName qname = ((BPELProcessDefinition)unresolved).getName();
     	
     	// Lookup a definition for the given namespace, from imports
     	List<Stringlocations = new ArrayList<String>();
         // Collection of namespace imports with location
         Map<StringNamespaceImportlocationMap = new HashMap<StringNamespaceImport>();
         for (Import import_ : this..getImports()) {
             if (import_ instanceof NamespaceImport) {
                 NamespaceImport namespaceImport = (NamespaceImport)import_;
                 if (namespaceImport.getNamespace().equals(qname.getNamespaceURI())) {
                     if (namespaceImport.getLocation() == null) {
 	                    // Delegate the resolution to the import resolver
 	                    resolved = namespaceImport.getModelResolver().resolveModel(BPELProcessDefinition.class, (BPELProcessDefinition)unresolvedcontext);
 	                    if (!resolved.isUnresolved()) {
 	                        return modelClass.cast(resolved);
 	                    }
                     } else {
                     	// We might have multiple imports for the same namespace,
                		// need to search them in lexical order.
                		locations.add(namespaceImport.getLocation());
                    }
                }
            }
        }
        // Search namespace imports with locations in lexical order
        Collections.sort(locations);
        for (String location : locations) {
        	NamespaceImport namespaceImport = (NamespaceImport)locationMap.get(location);
        	// Delegate the resolution to the namespace import resolver
            resolved = namespaceImport.getModelResolver().resolveModel(BPELProcessDefinition.class, (BPELProcessDefinition)unresolvedcontext);
            if (!resolved.isUnresolved()) {
                return modelClass.cast(resolved);
            }
        }
        
        
        // Not found, Lookup a definition for the given namespace, within contribution       
        resolved = (BPELProcessDefinition.get(qname);
        
        if(resolved.isUnresolved()) {
        	try {
        		resolve(resolvedcontext);
        	} catch(Exception e) {
        		//FIXME
        	}
        }
        
        if (resolved != null) {
            return modelClass.cast(resolved);
        }        
        
        return (T)unresolved;
    }
    
    public void resolve(BPELProcessDefinition unresolvedProcessorContext contextthrows ContributionResolveException {
        // FIXME - serious resolving needs to happen here
    	
    	// Step 1 is to resolve the WSDL files referenced from this BPEL process
    	// - one complexity here is that the WSDL definitions hold BPEL extension elements for
    	// the partnerLinkType declarations - and these must be used in later steps
    	//
    	// Step 2 is to take all the partnerLink definitions and establish the PortType being
    	// used, by tracing through the related partnerLinkType declarations - the PortType is
    	// effectively a definition of the interface used by the partnerLink.
    	// - another consideration here is that each partnerLink can involve 2 interfaces, one
    	// for the forward calls to the process, the other for calls from the process - depending
    	// on whether the partnerLink is a reference or a service, one of these interfaces is a
    	// callback interface.
    	List<BPELImportElementtheImports = unresolved.getImports();
    	Set<DefinitionwsdlDefinitions = getImportedWSDLDefinitionstheImports.getModelResolver(), context );
    	
    	// Fetch the sets of partner links, port types and interfaces
    	List<BPELPartnerLinkTypeElementthePLinkTypes = getPartnerLinkTypeswsdlDefinitionscontext.getMonitor() );
    	Collection<PortTypethePortTypes = getAllPortTypestheImportstheInterfaces.getModelResolver(), context );
    	
    	// Store the Port Types and the Interfaces for later calculation of the component type...
    	unresolved.getPortTypes().addAll(thePortTypes);
    	unresolved.getInterfaces().addAll(theInterfaces);
    	
    	// Now, for each partnerLink in the BPEL process, find the related partnerLinkType element 
        List<BPELPartnerLinkElementthePartnerLinks = unresolved.getPartnerLinks();
        for (BPELPartnerLinkElement thePartnerLink : thePartnerLinks) {
            QName partnerLinkType = thePartnerLink.getPartnerLinkType();
            BPELPartnerLinkTypeElement pLinkType = findPartnerLinkType(partnerLinkTypethePLinkTypes);
            if (pLinkType == null) {
                error(context.getMonitor(), "PartnerLinkNoMatchingType"thePartnerLinkthePartnerLink.getName());
            } else {
                thePartnerLink.setPartnerLinkType(pLinkType);
            }
        } // end for
        
        unresolved.setUnresolved(false);
    	
    } // end resolve    
    
    
Get all the WSDL definitions referenced through the import statements of the BPEL process

Parameters:
theImports - a list of the import statements
Returns:
- a Set containing all the referenced WSDL definitions
    private Set<DefinitiongetImportedWSDLDefinitionsList<BPELImportElementtheImportsModelResolver resolverProcessorContext context ) {
    	Set<DefinitionwsdlDefinitions = null;
    	for (BPELImportElement theImport : theImports) {
            if (theImport.getImportType().equals(.)) {
            	// If the Import is a WSDL import, resolve the WSDL
            	WSDLDefinition theWSDL = resolveWSDLDefinitiontheImport.getLocation(), 
            			                                        theImport.getNamespace(), resolvercontext );
                iftheWSDL != null ) {
	            	theImport.setWSDLDefinitiontheWSDL );
	            	
	                // Find all the WSDL definitions matching the imported namespace
	            	ifwsdlDefinitions == null ) {
	            		wsdlDefinitions = new HashSet<Definition>();
	            	} // end if 
	            	
	                wsdlDefinitions.add(theWSDL.getDefinition());
                } // end if
            } // end if
        } // end for
        
        return wsdlDefinitions;
    } // end getImportedWSDLDefinitions
    
    
Resolve a reference to a WSDL, given by a namespace and a location

Parameters:
wsdlLocation - a string containing the WSDL location
wsdlNamespace - a string containing the WSDL namespace
resolver - a model resolver
context
Returns:
- a WSDLDefinition object for the referenced WSDL, or null if the WSDL cannot be resolved
    private WSDLDefinition resolveWSDLDefinitionString wsdlLocationString wsdlNamespaceModelResolver resolverProcessorContext context ) {
        
        // Resolve the WSDL definition
        WSDLDefinition proxy = .createWSDLDefinition();
        proxy.setUnresolved(true);
        proxy.setNamespace(wsdlNamespace);
        if (wsdlLocation != null) {
            proxy.setLocation(URI.create(wsdlLocation));
        }
        WSDLDefinition resolved = resolver.resolveModel(WSDLDefinition.classproxycontext);
        if (resolved != null && !resolved.isUnresolved()) {
        	return resolved;
        } else {
            error(context.getMonitor(), "CannotResolveWSDLReference"resolverwsdlLocationwsdlNamespace);
            return null;
        } // end if
    } // end resolveWSDLDefinition
    

    
Retrieve all the Partner Link types defined in the imported WSDL files

Parameters:
wsdlDefinitions - the set of imported WSDL definitions
Returns:
- a List of PartnerLinkType elements
    @SuppressWarnings("unchecked")
    private List<BPELPartnerLinkTypeElementgetPartnerLinkTypesSet<DefinitionwsdlDefinitionsMonitor monitor ) throws ContributionResolveException {
    	
        // The BPEL partnerLinkType elements are extension elements within the WSDL definitions
        for (Definition wsdlDefinitionwsdlDefinitions) {
            for (ExtensibilityElement theElement : (List<ExtensibilityElement>)wsdlDefinition.getExtensibilityElements()) {
                QName elementType = theElement.getElementType();
                if (elementType.equals(.) || elementType.equals(.)) {
                    BPELPartnerLinkTypeExt pLinkExt = (BPELPartnerLinkTypeExt)theElement;
                    
                    // Fetch the name of the partnerLinkType
                    QName qName = new QName(wsdlDefinition.getTargetNamespace(), pLinkExt.getName());
                    BPELPartnerLinkTypeElement pLinkElement = new BPELPartnerLinkTypeElement(qName);
                    // The partnerLinkType must have one and may have 2 role child elements
                    int count = 0;
                    for (int i = 0; i < 2; i++) {
                        ifcount > 1 ) break;
                    	if (pLinkExt.getRoleName(i) == nullcontinue;
                        PortType pType = wsdlDefinition.getPortType(pLinkExt.getRolePortType(i));
                        if (count == 0) {
                            pLinkElement.setRole1(pLinkExt.getRoleName(i), pLinkExt.getRolePortType(i), pType);
                        } else {
                            pLinkElement.setRole2(pLinkExt.getRoleName(i), pLinkExt.getRolePortType(i), pType);
                        } // end if
                        count++;
                    } // end for
                    if (count == 0) {
                        error(monitor"PartnerLinkTypeNoRoles"theElementpLinkElement.getName());
                        throw new ContributionResolveException("partnerLinkType " + pLinkElement.getName() + " has no Roles defined");
                    } else
                        thePLinks.add(pLinkElement);
                } // end if
            } // end for
        } // end for
        return thePLinks;
    } // end getPartnerLinkTypes
    

    
Finds a partnerLinkType definition within the WSDLs imported by the BPEL process.

Parameters:
partnerLinkTypeName - the name of the partnerLinkType
theImports a list of the WSDL import declarations
Returns:
a BPELPartnerLinkTypeElement for the partnerLinkType or null if it cannot be found
    private BPELPartnerLinkTypeElement findPartnerLinkTypeQName partnerLinkTypeNameList<BPELPartnerLinkTypeElementthePLinkTypes) {
    	// We must find the partner link type element from amongst the imported WSDLs
    	for ( BPELPartnerLinkTypeElement thePLinkType : thePLinkTypes ){
    		ifthePLinkType.getName().equals(partnerLinkTypeName) ) return thePLinkType;
     	} // end for
    	return null;
    } // end findPartnerLinkType
    

    
Returns all the portTypes referenced by the process.

Parameters:
theImports
theInterfaces
resolver
Returns:
Throws:
org.apache.tuscany.sca.contribution.processor.ContributionResolveException
    @SuppressWarnings("unchecked")
                                                 Collection<WSDLInterfacetheInterfaces
                                                 ModelResolver resolver,
                                                 ProcessorContext contextthrows ContributionResolveException {
        Set<PortTypethePortTypes = new HashSet<PortType>();
        for (BPELImportElement theImport : theImports) {
            if (theImport.getImportType().equals(.)) {
                
                // Find all the WSDL definitions matching the imported namespace
                List<DefinitionwsdlDefinitions = new ArrayList<Definition>();
                WSDLDefinition theWSDL = theImport.getWSDLDefinition();
                wsdlDefinitions.add(theWSDL.getDefinition());
                for (WSDLDefinition importedWSDLtheWSDL.getImportedDefinitions()) {
                    wsdlDefinitions.add(importedWSDL.getDefinition());
                }
                for (Definition wsdlDefinitionwsdlDefinitions) {
                    Collection<PortTypeportTypes = (Collection<PortType>)wsdlDefinition.getPortTypes().values();
                    
                    // Create WSDLInterface elements for each unique PortType found
                    for (PortType portType : portTypes) {
                    	ifthePortTypes.contains(portType) ) continue;
                    	thePortTypes.addportType );
                    		
                        WSDLObject<PortTypewsdlPortType = theWSDL.getWSDLObject(PortType.classportType.getQName());
                        WSDLInterface wsdlInterface;
                        if (wsdlPortType != null) {
                            // Introspect the WSDL portType and add the resulting WSDLInterface to the resolver
                            try {
                                wsdlInterface = .createWSDLInterface(wsdlPortType.getElement(), theWSDLresolvercontext.getMonitor());
                                wsdlInterface.setWsdlDefinition(theWSDL);
                            } catch (InvalidInterfaceException e) {
                                ContributionResolveException ce = 
                                	new ContributionResolveException("Unable to create WSDLInterface for portType " + portType.getQName(),e);
                                error(context.getMonitor(), "ContributionResolveException"resolverce);
                                throw ce;
                            } // end try
                            resolver.addModel(wsdlInterfacecontext);
                            theInterfaces.add(wsdlInterface);
                        } // end if
                    } // end for
                }
            }
        } // end for
        return thePortTypes;
    } // end getAllPortTypes
    
    
Report a warning.

Parameters:
problems
message
model
    private void warning(Monitor monitorString messageObject modelObject... messageParameters) {
        if (monitor != null) {
            Problem problem = monitor.createProblem(this.getClass().getName(), "impl-bpel-validation-messages".modelmessage, (Object[])messageParameters);
            monitor.problem(problem);
        }
    }

    
Report a error.

Parameters:
problems
message
model
    private void error(Monitor monitorString messageObject modelObject... messageParameters) {
        if (monitor != null) {
            Problem problem = monitor.createProblem(this.getClass().getName(), "impl-bpel-validation-messages".modelmessage, (Object[])messageParameters);
            monitor.problem(problem);
        }
    }

    
Report a exception.

Parameters:
problems
message
model
    private void error(Monitor monitorString messageObject modelException ex) {
        if (monitor != null) {
            Problem problem = monitor.createProblem(this.getClass().getName(), "impl-bpel-validation-messages".modelmessageex);
            monitor.problem(problem);
        }
    }
    
New to GrepCode? Check out our FAQ X