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.maven.configuration.BeanConfigurationException;
25  import org.apache.maven.configuration.BeanConfigurationPathTranslator;
26  import org.apache.maven.configuration.BeanConfigurationRequest;
27  import org.apache.maven.configuration.BeanConfigurationValuePreprocessor;
28  import org.apache.maven.configuration.BeanConfigurator;
29  import org.codehaus.plexus.component.annotations.Component;
30  import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
31  import org.codehaus.plexus.component.configurator.converters.composite.ObjectWithFieldsConverter;
32  import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
33  import org.codehaus.plexus.component.configurator.converters.lookup.DefaultConverterLookup;
34  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
35  import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
36  import org.codehaus.plexus.configuration.PlexusConfiguration;
37  import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
38  import org.codehaus.plexus.util.xml.Xpp3Dom;
39  
40  /**
41   * <strong>Warning:</strong> This is an internal class that is only public for technical reasons, it is not part of the
42   * public API. In particular, this class can be changed or deleted without prior notice.
43   * 
44   * @author Benjamin Bentmann
45   */
46  @Component( role = BeanConfigurator.class )
47  public class DefaultBeanConfigurator
48      implements BeanConfigurator
49  {
50  
51      private final ConverterLookup converterLookup = new DefaultConverterLookup();
52  
53      public void configureBean( BeanConfigurationRequest request )
54          throws BeanConfigurationException
55      {
56          if ( request == null )
57          {
58              throw new IllegalArgumentException( "bean configuration request not specified" );
59          }
60  
61          if ( request.getBean() == null )
62          {
63              throw new IllegalArgumentException( "bean to be configured not specified" );
64          }
65  
66          Object configuration = request.getConfiguration();
67          if ( configuration == null )
68          {
69              return;
70          }
71  
72          PlexusConfiguration plexusConfig;
73          if ( configuration instanceof PlexusConfiguration )
74          {
75              plexusConfig = (PlexusConfiguration) configuration;
76          }
77          else if ( configuration instanceof Xpp3Dom )
78          {
79              plexusConfig = new XmlPlexusConfiguration( (Xpp3Dom) configuration );
80          }
81          else
82          {
83              throw new BeanConfigurationException( "unsupported bean configuration source ("
84                  + configuration.getClass().getName() + ")" );
85          }
86  
87          if ( request.getConfigurationElement() != null )
88          {
89              plexusConfig = plexusConfig.getChild( request.getConfigurationElement() );
90          }
91  
92          ClassLoader classLoader = request.getClassLoader();
93          if ( classLoader == null )
94          {
95              classLoader = request.getBean().getClass().getClassLoader();
96          }
97  
98          BeanExpressionEvaluator evaluator = new BeanExpressionEvaluator( request );
99  
100         ObjectWithFieldsConverter converter = new ObjectWithFieldsConverter();
101 
102         try
103         {
104             converter.processConfiguration( converterLookup, request.getBean(), classLoader, plexusConfig, evaluator );
105         }
106         catch ( ComponentConfigurationException e )
107         {
108             throw new BeanConfigurationException( e.getMessage(), e );
109         }
110     }
111 
112     static class BeanExpressionEvaluator
113         implements TypeAwareExpressionEvaluator
114     {
115 
116         private final BeanConfigurationValuePreprocessor preprocessor;
117 
118         private final BeanConfigurationPathTranslator translator;
119 
120         public BeanExpressionEvaluator( BeanConfigurationRequest request )
121         {
122             preprocessor = request.getValuePreprocessor();
123             translator = request.getPathTranslator();
124         }
125 
126         public Object evaluate( String expression, Class<?> type )
127             throws ExpressionEvaluationException
128         {
129             if ( preprocessor != null )
130             {
131                 try
132                 {
133                     return preprocessor.preprocessValue( expression, type );
134                 }
135                 catch ( BeanConfigurationException e )
136                 {
137                     throw new ExpressionEvaluationException( e.getMessage(), e );
138                 }
139             }
140             return expression;
141         }
142 
143         public Object evaluate( String expression )
144             throws ExpressionEvaluationException
145         {
146             return evaluate( expression, null );
147         }
148 
149         public File alignToBaseDirectory( File file )
150         {
151             if ( translator != null )
152             {
153                 return translator.translatePath( file );
154             }
155             return file;
156         }
157 
158     }
159 
160 }