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   * @author Jason van Zyl
31   * @author <a href="mailto:michal.maczka@dimatics.com">Michal Maczka</a>
32   */
33  public class DefaultCompositionResolver implements CompositionResolver {
34      private DAG dag = new DAG();
35  
36      public void addComponentDescriptor(ComponentDescriptor<?> componentDescriptor)
37              throws CycleDetectedInComponentGraphException {
38          String key = getDAGKey(componentDescriptor.getRole(), componentDescriptor.getRoleHint());
39  
40          List<ComponentRequirement> requirements = componentDescriptor.getRequirements();
41  
42          for (ComponentRequirement requirement : requirements) {
43              try {
44                  if (requirement instanceof ComponentRequirementList) {
45                      for (String hint : ((ComponentRequirementList) requirement).getRoleHints()) {
46                          dag.addEdge(key, getDAGKey(requirement.getRole(), hint));
47                      }
48                  } else {
49                      dag.addEdge(key, getDAGKey(requirement.getRole(), requirement.getRoleHint()));
50                  }
51              } catch (CycleDetectedException e) {
52                  throw new CycleDetectedInComponentGraphException("Cyclic requirement detected", e);
53              }
54          }
55      }
56  
57      /**
58       * @see org.codehaus.plexus.component.composition.CompositionResolver#getRequirements(String,String)
59       */
60      public List getRequirements(String role, String roleHint) {
61          return dag.getChildLabels(getDAGKey(role, roleHint));
62      }
63  
64      /**
65       * @see org.codehaus.plexus.component.composition.CompositionResolver#findRequirements(String,String)
66       */
67      public List findRequirements(String role, String roleHint) {
68          return dag.getParentLabels(getDAGKey(role, roleHint));
69      }
70  
71      private String getDAGKey(String role, String roleHint) {
72          return role
73                  + SEPARATOR_CHAR
74                  + (StringUtils.isNotEmpty(roleHint) ? roleHint : PlexusConstants.PLEXUS_DEFAULT_HINT);
75      }
76  }