View Javadoc
1   /*
2    * Copyright (C) 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.codehaus.plexus.maven.plugin;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.codehaus.plexus.component.repository.cdc.ComponentDescriptor;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  /**
27   * Support for {@link ComponentDescriptorExtractor} implementations.
28   *
29   * @version $Id$
30   */
31  public abstract class ComponentDescriptorExtractorSupport
32      implements ComponentDescriptorExtractor
33  {
34      protected final Logger log = LoggerFactory.getLogger(getClass());
35  
36      protected Map getDefaultsByRole(final ComponentDescriptor[] roleDefaults) {
37          Map defaultsByRole = new HashMap();
38  
39          if (roleDefaults != null) {
40              for (int i = 0; i < roleDefaults.length; i++) {
41                  String role = roleDefaults[i].getRole();
42  
43                  if (role == null) {
44                      throw new IllegalArgumentException("Invalid role defaults; found null role in: " + roleDefaults[i]);
45                  }
46  
47                  defaultsByRole.put(role, roleDefaults[i]);
48              }
49          }
50  
51          return defaultsByRole;
52      }
53  
54      protected void applyDefaults(final ComponentDescriptor descriptor, final Map defaultsByRole) {
55          assert descriptor != null;
56          assert defaultsByRole != null;
57  
58          if (defaultsByRole.containsKey(descriptor.getRole())) {
59              ComponentDescriptor defaults = (ComponentDescriptor) defaultsByRole.get(descriptor.getRole());
60  
61              if (descriptor.getInstantiationStrategy() == null) {
62                  descriptor.setInstantiationStrategy(defaults.getInstantiationStrategy());
63              }
64          }
65      }
66  }