View Javadoc
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  public class DefaultCompositionResolver
36      implements CompositionResolver
37  {
38      private DAG dag = new DAG();
39  
40      public void addComponentDescriptor( ComponentDescriptor<?> componentDescriptor )
41          throws CycleDetectedInComponentGraphException
42      {
43          String key = getDAGKey( componentDescriptor.getRole(), componentDescriptor.getRoleHint() );
44  
45          List<ComponentRequirement> requirements = componentDescriptor.getRequirements();
46  
47          for ( ComponentRequirement requirement : requirements )
48          {
49              try
50              {
51                  if ( requirement instanceof ComponentRequirementList )
52                  {
53                      for ( String hint : ( (ComponentRequirementList) requirement ).getRoleHints() )
54                      {
55                          dag.addEdge( key, getDAGKey( requirement.getRole(), hint ) );
56                      }
57                  }
58                  else
59                  {
60                      dag.addEdge( key, getDAGKey( requirement.getRole(), requirement.getRoleHint() ) );
61                  }
62              }
63              catch ( CycleDetectedException e )
64              {
65                  throw new CycleDetectedInComponentGraphException( "Cyclic requirement detected", e );
66              }
67          }
68      }
69  
70      /**
71       * @see org.codehaus.plexus.component.composition.CompositionResolver#getRequirements(String,String)
72       */
73      public List getRequirements( String role, String roleHint )
74      {
75          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          return dag.getParentLabels( getDAGKey( role, roleHint ) );
85      }
86  
87      private String getDAGKey( String role, String roleHint )
88      {
89          return role + SEPARATOR_CHAR
90              + ( StringUtils.isNotEmpty( roleHint ) ? roleHint : PlexusConstants.PLEXUS_DEFAULT_HINT );
91      }
92  }