001 package org.apache.maven.configuration.internal; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022 import java.io.File; 023 024 import org.apache.maven.configuration.BeanConfigurationException; 025 import org.apache.maven.configuration.BeanConfigurationPathTranslator; 026 import org.apache.maven.configuration.BeanConfigurationRequest; 027 import org.apache.maven.configuration.BeanConfigurationValuePreprocessor; 028 import org.apache.maven.configuration.BeanConfigurator; 029 import org.codehaus.plexus.component.annotations.Component; 030 import org.codehaus.plexus.component.configurator.ComponentConfigurationException; 031 import org.codehaus.plexus.component.configurator.converters.composite.ObjectWithFieldsConverter; 032 import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup; 033 import org.codehaus.plexus.component.configurator.converters.lookup.DefaultConverterLookup; 034 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; 035 import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator; 036 import org.codehaus.plexus.configuration.PlexusConfiguration; 037 import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration; 038 import org.codehaus.plexus.util.xml.Xpp3Dom; 039 040 /** 041 * <strong>Warning:</strong> This is an internal class that is only public for technical reasons, it is not part of the 042 * public API. In particular, this class can be changed or deleted without prior notice. 043 * 044 * @author Benjamin Bentmann 045 */ 046 @Component( role = BeanConfigurator.class ) 047 public class DefaultBeanConfigurator 048 implements BeanConfigurator 049 { 050 051 private final ConverterLookup converterLookup = new DefaultConverterLookup(); 052 053 public void configureBean( BeanConfigurationRequest request ) 054 throws BeanConfigurationException 055 { 056 if ( request == null ) 057 { 058 throw new IllegalArgumentException( "bean configuration request not specified" ); 059 } 060 061 if ( request.getBean() == null ) 062 { 063 throw new IllegalArgumentException( "bean to be configured not specified" ); 064 } 065 066 Object configuration = request.getConfiguration(); 067 if ( configuration == null ) 068 { 069 return; 070 } 071 072 PlexusConfiguration plexusConfig = null; 073 if ( configuration instanceof PlexusConfiguration ) 074 { 075 plexusConfig = (PlexusConfiguration) configuration; 076 } 077 else if ( configuration instanceof Xpp3Dom ) 078 { 079 plexusConfig = new XmlPlexusConfiguration( (Xpp3Dom) configuration ); 080 } 081 else 082 { 083 throw new BeanConfigurationException( "unsupported bean configuration source (" 084 + configuration.getClass().getName() + ")" ); 085 } 086 087 if ( request.getConfigurationElement() != null ) 088 { 089 plexusConfig = plexusConfig.getChild( request.getConfigurationElement() ); 090 } 091 092 ClassLoader classLoader = request.getClassLoader(); 093 if ( classLoader == null ) 094 { 095 classLoader = request.getBean().getClass().getClassLoader(); 096 } 097 098 BeanExpressionEvaluator evaluator = new BeanExpressionEvaluator( request ); 099 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 }