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   * @version $Id$
26   */
27  public abstract class ComponentDescriptorExtractorSupport
28      implements ComponentDescriptorExtractor
29  {
30      protected Map<String, ComponentDescriptor<?>> getDefaultsByRole( final ComponentDescriptor<?>[] roleDefaults )
31      {
32          Map<String, ComponentDescriptor<?>> defaultsByRole = new HashMap<String, ComponentDescriptor<?>>();
33  
34          if ( roleDefaults != null )
35          {
36              for (ComponentDescriptor<?> roleDefault : roleDefaults) {
37                  String role = roleDefault.getRole();
38  
39                  if (role == null) {
40                      throw new IllegalArgumentException("Invalid role defaults; found null role in: " + roleDefault);
41                  }
42  
43                  defaultsByRole.put(role, roleDefault);
44              }
45          }
46  
47          return defaultsByRole;
48      }
49  
50      protected void applyDefaults( ComponentDescriptor<?> descriptor, Map<String, ComponentDescriptor<?>> defaultsByRole )
51      {
52          assert descriptor != null;
53          assert defaultsByRole != null;
54  
55          if ( defaultsByRole.containsKey( descriptor.getRole() ) )
56          {
57              ComponentDescriptor<?> defaults = defaultsByRole.get( descriptor.getRole() );
58  
59              if ( descriptor.getInstantiationStrategy() == null )
60              {
61                  descriptor.setInstantiationStrategy( defaults.getInstantiationStrategy() );
62              }
63          }
64      }
65  }