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.definitions.impl;
 
 import java.util.List;
 import java.util.Map;
 
 
Provides a concrete implementation for a SCADefinitionsBuilder

Version:
$Rev: 967109 $ $Date: 2010-07-23 17:30:46 +0300 (Fri, 23 Jul 2010) $
 
 public class DefinitionsBuilderImpl implements DefinitionsBuilder {
 
     public void build(Definitions scaDefnsthrows DefinitionsBuilderException {
         Map<QNameIntentdefinedIntents = new HashMap<QNameIntent>();
         for (Intent intent : scaDefns.getIntents()) {
             definedIntents.put(intent.getName(), intent);
         }
 
         Map<QNamePolicySetdefinedPolicySets = new HashMap<QNamePolicySet>();
         for (PolicySet policySet : scaDefns.getPolicySets()) {
             definedPolicySets.put(policySet.getName(), policySet);
         }
 
         Map<QNameBindingTypedefinedBindingTypes = new HashMap<QNameBindingType>();
         for (BindingType bindingType : scaDefns.getBindingTypes()) {
             definedBindingTypes.put(bindingType.getType(), bindingType);
         }
 
         Map<QNameImplementationTypedefinedImplTypes = new HashMap<QNameImplementationType>();
         for (ImplementationType implType : scaDefns.getImplementationTypes()) {
             definedImplTypes.put(implType.getType(), implType);
         }
 
         //filling up the maps removes all duplicate entries... so fill this unique lists
         //into the scaDefns.
         scaDefns.getIntents().clear();
         scaDefns.getPolicySets().clear();
         scaDefns.getBindingTypes().clear();
         scaDefns.getImplementationTypes().clear();
 
         scaDefns.getIntents().addAll(definedIntents.values());
         scaDefns.getPolicySets().addAll(definedPolicySets.values());
         scaDefns.getBindingTypes().addAll(definedBindingTypes.values());
         scaDefns.getImplementationTypes().addAll(definedImplTypes.values());
 
         buildPolicyIntents(scaDefnsdefinedIntents);
         buildPolicySets(scaDefnsdefinedPolicySetsdefinedIntents);
         buildBindingTypes(scaDefnsdefinedBindingTypesdefinedIntents);
         buildImplementationTypes(scaDefnsdefinedImplTypesdefinedIntents);
     }
 
     private void buildBindingTypes(Definitions scaDefns,
                                    Map<QNameBindingTypedefinedBindingTypes,
                                    Map<QNameIntentdefinedIntentsthrows DefinitionsBuilderException {
         for (BindingType bindingType : scaDefns.getBindingTypes()) {
             buildAlwaysProvidedIntents(bindingTypedefinedIntents);
             buildMayProvideIntents(bindingTypedefinedIntents);
         }
 
     }
 
     private void buildImplementationTypes(Definitions scaDefns,
                                           Map<QNameImplementationTypedefinedImplTypes,
                                           Map<QNameIntentdefinedIntentsthrows DefinitionsBuilderException {
         for (ImplementationType implType : scaDefns.getImplementationTypes()) {
             buildAlwaysProvidedIntents(implTypedefinedIntents);
            buildMayProvideIntents(implTypedefinedIntents);
        }
    }
    private void buildPolicyIntents(Definitions scaDefnsMap<QNameIntentdefinedIntents)
        throws DefinitionsBuilderException {
        for (Intent policyIntent : scaDefns.getIntents()) {
            if (!policyIntent.getRequiredIntents().isEmpty()) {
                buildProfileIntent(policyIntentdefinedIntents);
            }
            if (!policyIntent.getQualifiedIntents().isEmpty()) {
                buildQualifiedIntent(policyIntentdefinedIntents);
            }
        }
    }
    private void buildPolicySets(Definitions scaDefns,
                                 Map<QNamePolicySetdefinedPolicySets,
                                 Map<QNameIntentdefinedIntentsthrows DefinitionsBuilderException {
        for (PolicySet policySet : scaDefns.getPolicySets()) {
            buildProvidedIntents(policySetdefinedIntents);
            buildIntentsInMappedPolicies(policySetdefinedIntents);
            buildReferredPolicySets(policySetdefinedPolicySets);
        }
        for (PolicySet policySet : scaDefns.getPolicySets()) {
            for (PolicySet referredPolicySet : policySet.getReferencedPolicySets()) {
                includeReferredPolicySets(policySetreferredPolicySet);
            }
        }
    }
    private void buildProfileIntent(Intent policyIntentMap<QNameIntentdefinedIntents)
        throws DefinitionsBuilderException {
        //FIXME: Need to check for cyclic references first i.e an A requiring B and then B requiring A... 
        if (policyIntent != null) {
            //resolve all required intents
            List<IntentrequiredIntents = new ArrayList<Intent>();
            for (Intent requiredIntent : policyIntent.getRequiredIntents()) {
                if (requiredIntent.isUnresolved()) {
                    Intent resolvedRequiredIntent = definedIntents.get(requiredIntent.getName());
                    if (resolvedRequiredIntent != null) {
                        requiredIntents.add(resolvedRequiredIntent);
                    } else {
                        throw new DefinitionsBuilderException("Required Intent - " + requiredIntent
                            + " not found for ProfileIntent "
                            + policyIntent);
                    }
                } else {
                    requiredIntents.add(requiredIntent);
                }
            }
            policyIntent.getRequiredIntents().clear();
            policyIntent.getRequiredIntents().addAll(requiredIntents);
        }
    }
    private void buildQualifiedIntent(Intent policyIntentMap<QNameIntentdefinedIntents)
        throws DefinitionsBuilderException {
        /*
        if (policyIntent != null) {
            //resolve the qualifiable intent
            Intent qualifiableIntent = policyIntent.getQualifiableIntent();
            if (qualifiableIntent.isUnresolved()) {
                Intent resolvedQualifiableIntent = definedIntents.get(qualifiableIntent.getName());
                if (resolvedQualifiableIntent != null) {
                    policyIntent.setQualifiableIntent(resolvedQualifiableIntent);
                } else {
                    throw new DefinitionsBuilderException("Qualifiable Intent - " + qualifiableIntent
                        + " not found for QualifiedIntent "
                        + policyIntent);
                }
            }
        }
        */
    }
    private void buildAlwaysProvidedIntents(ExtensionType extensionTypeMap<QNameIntentdefinedIntents)
        throws DefinitionsBuilderException {
        if (extensionType != null) {
            // resolve all provided intents
            List<IntentalwaysProvided = new ArrayList<Intent>();
            for (Intent providedIntent : extensionType.getAlwaysProvidedIntents()) {
                if (providedIntent.isUnresolved()) {
                    Intent resolvedProvidedIntent = definedIntents.get(providedIntent.getName());
                    if (resolvedProvidedIntent != null) {
                        alwaysProvided.add(resolvedProvidedIntent);
                    } else {
                        throw new DefinitionsBuilderException("Always Provided Intent - " + providedIntent
                            + " not found for ExtensionType "
                            + extensionType);
                    }
                } else {
                    alwaysProvided.add(providedIntent);
                }
            }
            extensionType.getAlwaysProvidedIntents().clear();
            extensionType.getAlwaysProvidedIntents().addAll(alwaysProvided);
        }
    }
    private void buildMayProvideIntents(ExtensionType extensionTypeMap<QNameIntentdefinedIntents)
        throws DefinitionsBuilderException {
        if (extensionType != null) {
            // resolve all provided intents
            List<IntentmayProvide = new ArrayList<Intent>();
            for (Intent providedIntent : extensionType.getMayProvidedIntents()) {
                if (providedIntent.isUnresolved()) {
                    Intent resolvedProvidedIntent = definedIntents.get(providedIntent.getName());
                    if (resolvedProvidedIntent != null) {
                        mayProvide.add(resolvedProvidedIntent);
                    } else {
                        throw new DefinitionsBuilderException("May Provide Intent - " + providedIntent
                            + " not found for ExtensionType "
                            + extensionType);
                    }
                } else {
                    mayProvide.add(providedIntent);
                }
            }
            extensionType.getMayProvidedIntents().clear();
            extensionType.getMayProvidedIntents().addAll(mayProvide);
        }
    }
    private void buildProvidedIntents(PolicySet policySetMap<QNameIntentdefinedIntents)
        throws DefinitionsBuilderException {
        if (policySet != null) {
            //resolve all provided intents
            List<IntentprovidedIntents = new ArrayList<Intent>();
            for (Intent providedIntent : policySet.getProvidedIntents()) {
                if (providedIntent.isUnresolved()) {
                    Intent resolvedProvidedIntent = definedIntents.get(providedIntent.getName());
                    if (resolvedProvidedIntent != null) {
                        providedIntents.add(resolvedProvidedIntent);
                    } else {
                        throw new DefinitionsBuilderException("Provided Intent - " + providedIntent
                            + " not found for PolicySet "
                            + policySet);
                    }
                } else {
                    providedIntents.add(providedIntent);
                }
            }
            policySet.getProvidedIntents().clear();
            policySet.getProvidedIntents().addAll(providedIntents);
        }
    }
    private void buildIntentsInMappedPolicies(PolicySet policySetMap<QNameIntentdefinedIntents)
        throws DefinitionsBuilderException {
        for (IntentMap intentMap : policySet.getIntentMaps()) {
            for (Qualifier qualifier : intentMap.getQualifiers()) {
                Intent mappedIntent = qualifier.getIntent();
                if (mappedIntent.isUnresolved()) {
                    Intent resolvedMappedIntent = definedIntents.get(mappedIntent.getName());
                    if (resolvedMappedIntent != null) {
                        qualifier.setIntent(resolvedMappedIntent);
                    } else {
                        throw new DefinitionsBuilderException("Mapped Intent - " + mappedIntent
                            + " not found for PolicySet "
                            + policySet);
                    }
                }
            }
        }
    }
    private void buildReferredPolicySets(PolicySet policySetMap<QNamePolicySetdefinedPolicySets)
        throws DefinitionsBuilderException {
        List<PolicySetreferredPolicySets = new ArrayList<PolicySet>();
        for (PolicySet referredPolicySet : policySet.getReferencedPolicySets()) {
            if (referredPolicySet.isUnresolved()) {
                PolicySet resolvedReferredPolicySet = definedPolicySets.get(referredPolicySet.getName());
                if (resolvedReferredPolicySet != null) {
                    referredPolicySets.add(resolvedReferredPolicySet);
                } else {
                    throw new DefinitionsBuilderException("Referred PolicySet - " + referredPolicySet
                        + "not found for PolicySet - "
                        + policySet);
                }
            } else {
                referredPolicySets.add(referredPolicySet);
            }
        }
        policySet.getReferencedPolicySets().clear();
        policySet.getReferencedPolicySets().addAll(referredPolicySets);
    }
    private void includeReferredPolicySets(PolicySet policySetPolicySet referredPolicySet) {
        for (PolicySet furtherReferredPolicySet : referredPolicySet.getReferencedPolicySets()) {
            includeReferredPolicySets(referredPolicySetfurtherReferredPolicySet);
        }
        policySet.getPolicies().addAll(referredPolicySet.getPolicies());
        policySet.getIntentMaps().addAll(referredPolicySet.getIntentMaps());
    }
New to GrepCode? Check out our FAQ X