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