Coverage Report - org.codehaus.plexus.component.composition.DefaultCompositionResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultCompositionResolver
83 %
15/18
100 %
8/8
2,5
 
 1  
 package org.codehaus.plexus.component.composition;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2006 Codehaus Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *      http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.util.List;
 20  
 
 21  
 import org.codehaus.plexus.PlexusConstants;
 22  
 import org.codehaus.plexus.component.repository.ComponentDescriptor;
 23  
 import org.codehaus.plexus.component.repository.ComponentRequirement;
 24  
 import org.codehaus.plexus.component.repository.ComponentRequirementList;
 25  
 import org.codehaus.plexus.util.StringUtils;
 26  
 import org.codehaus.plexus.util.dag.CycleDetectedException;
 27  
 import org.codehaus.plexus.util.dag.DAG;
 28  
 
 29  
 
 30  
 /**
 31  
  * @author Jason van Zyl
 32  
  * @author <a href="mailto:michal.maczka@dimatics.com">Michal Maczka</a>
 33  
  * @version $Id$
 34  
  */
 35  63
 public class DefaultCompositionResolver
 36  
     implements CompositionResolver
 37  
 {
 38  63
     private DAG dag = new DAG();
 39  
 
 40  
     public void addComponentDescriptor( ComponentDescriptor<?> componentDescriptor )
 41  
         throws CycleDetectedInComponentGraphException
 42  
     {
 43  2432
         String key = getDAGKey( componentDescriptor.getRole(), componentDescriptor.getRoleHint() );
 44  
 
 45  2433
         List<ComponentRequirement> requirements = componentDescriptor.getRequirements();
 46  
 
 47  2434
         for ( ComponentRequirement requirement : requirements )
 48  
         {
 49  
             try
 50  
             {
 51  751
                 if ( requirement instanceof ComponentRequirementList )
 52  
                 {
 53  84
                     for ( String hint : ( (ComponentRequirementList) requirement ).getRoleHints() )
 54  
                     {
 55  168
                         dag.addEdge( key, getDAGKey( requirement.getRole(), hint ) );
 56  168
                     }
 57  
                 }
 58  
                 else
 59  
                 {
 60  667
                     dag.addEdge( key, getDAGKey( requirement.getRole(), requirement.getRoleHint() ) );
 61  
                 }
 62  
             }
 63  0
             catch ( CycleDetectedException e )
 64  
             {
 65  0
                 throw new CycleDetectedInComponentGraphException( "Cyclic requirement detected", e );
 66  751
             }
 67  751
         }
 68  2433
     }
 69  
 
 70  
     /**
 71  
      * @see org.codehaus.plexus.component.composition.CompositionResolver#getRequirements(String,String)
 72  
      */
 73  
     public List getRequirements( String role, String roleHint )
 74  
     {
 75  2
         return dag.getChildLabels( getDAGKey( role, roleHint ) );
 76  
     }
 77  
 
 78  
 
 79  
     /**
 80  
      * @see org.codehaus.plexus.component.composition.CompositionResolver#findRequirements(String,String)
 81  
      */
 82  
     public List findRequirements( String role, String roleHint )
 83  
     {
 84  0
         return dag.getParentLabels( getDAGKey( role, roleHint ) );
 85  
     }
 86  
 
 87  
     private String getDAGKey( String role, String roleHint )
 88  
     {
 89  3269
         return role + SEPARATOR_CHAR
 90  
             + ( StringUtils.isNotEmpty( roleHint ) ? roleHint : PlexusConstants.PLEXUS_DEFAULT_HINT );
 91  
     }
 92  
 }