View Javadoc
1   package org.apache.maven.configuration.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  
24  import org.apache.commons.lang3.Validate;
25  import org.apache.maven.configuration.BeanConfigurationException;
26  import org.apache.maven.configuration.BeanConfigurationPathTranslator;
27  import org.apache.maven.configuration.BeanConfigurationRequest;
28  import org.apache.maven.configuration.BeanConfigurationValuePreprocessor;
29  import org.apache.maven.configuration.BeanConfigurator;
30  import org.codehaus.plexus.component.annotations.Component;
31  import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
32  import org.codehaus.plexus.component.configurator.converters.composite.ObjectWithFieldsConverter;
33  import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
34  import org.codehaus.plexus.component.configurator.converters.lookup.DefaultConverterLookup;
35  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
36  import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
37  import org.codehaus.plexus.configuration.PlexusConfiguration;
38  import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
39  import org.codehaus.plexus.util.xml.Xpp3Dom;
40  
41  /**
42   * <strong>Warning:</strong> This is an internal class that is only public for technical reasons, it is not part of the
43   * public API. In particular, this class can be changed or deleted without prior notice.
44   *
45   * @author Benjamin Bentmann
46   */
47  @Component( role = BeanConfigurator.class )
48  public class DefaultBeanConfigurator
49      implements BeanConfigurator
50  {
51  
52      private final ConverterLookup converterLookup = new DefaultConverterLookup();
53  
54      public void configureBean( BeanConfigurationRequest request )
55          throws BeanConfigurationException
56      {
57          Validate.notNull( request, "request cannot be null" );
58          Validate.notNull( request.getBean(), "request.bean cannot be null" );
59  
60          Object configuration = request.getConfiguration();
61          if ( configuration == null )
62          {
63              return;
64          }
65  
66          PlexusConfiguration plexusConfig;
67          if ( configuration instanceof PlexusConfiguration )
68          {
69              plexusConfig = (PlexusConfiguration) configuration;
70          }
71          else if ( configuration instanceof Xpp3Dom )
72          {
73              plexusConfig = new XmlPlexusConfiguration( (Xpp3Dom) configuration );
74          }
75          else
76          {
77              throw new BeanConfigurationException( "unsupported bean configuration source ("
78                  + configuration.getClass().getName() + ")" );
79          }
80  
81          if ( request.getConfigurationElement() != null )
82          {
83              plexusConfig = plexusConfig.getChild( request.getConfigurationElement() );
84          }
85  
86          ClassLoader classLoader = request.getClassLoader();
87          if ( classLoader == null )
88          {
89              classLoader = request.getBean().getClass().getClassLoader();
90          }
91  
92          BeanExpressionEvaluator evaluator = new BeanExpressionEvaluator( request );
93  
94          ObjectWithFieldsConverter converter = new ObjectWithFieldsConverter();
95  
96          try
97          {
98              converter.processConfiguration( converterLookup, request.getBean(), classLoader, plexusConfig, evaluator );
99          }
100         catch ( ComponentConfigurationException e )
101         {
102             throw new BeanConfigurationException( e.getMessage(), e );
103         }
104     }
105 
106     static class BeanExpressionEvaluator
107         implements TypeAwareExpressionEvaluator
108     {
109 
110         private final BeanConfigurationValuePreprocessor preprocessor;
111 
112         private final BeanConfigurationPathTranslator translator;
113 
114         public BeanExpressionEvaluator( BeanConfigurationRequest request )
115         {
116             preprocessor = request.getValuePreprocessor();
117             translator = request.getPathTranslator();
118         }
119 
120         public Object evaluate( String expression, Class<?> type )
121             throws ExpressionEvaluationException
122         {
123             if ( preprocessor != null )
124             {
125                 try
126                 {
127                     return preprocessor.preprocessValue( expression, type );
128                 }
129                 catch ( BeanConfigurationException e )
130                 {
131                     throw new ExpressionEvaluationException( e.getMessage(), e );
132                 }
133             }
134             return expression;
135         }
136 
137         public Object evaluate( String expression )
138             throws ExpressionEvaluationException
139         {
140             return evaluate( expression, null );
141         }
142 
143         public File alignToBaseDirectory( File file )
144         {
145             if ( translator != null )
146             {
147                 return translator.translatePath( file );
148             }
149             return file;
150         }
151 
152     }
153 
154 }