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 = null;
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          ClassLoader classLoader = request.getClassLoader();
88          if ( classLoader == null )
89          {
90              classLoader = request.getBean().getClass().getClassLoader();
91          }
92  
93          BeanExpressionEvaluator evaluator = new BeanExpressionEvaluator( request );
94  
95          ObjectWithFieldsConverter converter = new ObjectWithFieldsConverter();
96  
97          try
98          {
99              converter.processConfiguration( converterLookup, request.getBean(), classLoader, plexusConfig, evaluator );
100         }
101         catch ( ComponentConfigurationException e )
102         {
103             throw new BeanConfigurationException( e.getMessage(), e );
104         }
105     }
106 
107     static class BeanExpressionEvaluator
108         implements TypeAwareExpressionEvaluator
109     {
110 
111         private final BeanConfigurationValuePreprocessor preprocessor;
112 
113         private final BeanConfigurationPathTranslator translator;
114 
115         public BeanExpressionEvaluator( BeanConfigurationRequest request )
116         {
117             preprocessor = request.getValuePreprocessor();
118             translator = request.getPathTranslator();
119         }
120 
121         public Object evaluate( String expression, Class<?> type )
122             throws ExpressionEvaluationException
123         {
124             if ( preprocessor != null )
125             {
126                 try
127                 {
128                     return preprocessor.preprocessValue( expression, type );
129                 }
130                 catch ( BeanConfigurationException e )
131                 {
132                     throw new ExpressionEvaluationException( e.getMessage(), e );
133                 }
134             }
135             return expression;
136         }
137 
138         public Object evaluate( String expression )
139             throws ExpressionEvaluationException
140         {
141             return evaluate( expression, null );
142         }
143 
144         public File alignToBaseDirectory( File file )
145         {
146             if ( translator != null )
147             {
148                 return translator.translatePath( file );
149             }
150             return file;
151         }
152 
153     }
154 
155 }