001package org.apache.maven.script.ant;
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
022import java.io.File;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.Map;
026import java.util.Set;
027
028import org.apache.maven.artifact.Artifact;
029import org.apache.maven.plugin.logging.Log;
030import org.apache.maven.project.MavenProject;
031import org.apache.tools.ant.PropertyHelper;
032import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
033import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
034import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
035
036/**
037 * Makes the ${expressions} used in Maven available to Ant as properties.
038 *
039 * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
040 */
041public class AntPropertyHelper
042    extends PropertyHelper
043{
044    private static final String DEPENDENCY_PREFIX = "maven.dependency.";
045    private Log log;
046    private ExpressionEvaluator exprEvaluator;
047    private MavenProject mavenProject;
048    private Map<String, String> artifactMap = new HashMap<String, String>();
049
050    /**
051     * @deprecated use the other constructor
052     * @param project
053     * @param l
054     */
055    public AntPropertyHelper( MavenProject project, Log l )
056    {
057        mavenProject = project;
058        log = l;
059    }
060
061    /**
062     * @deprecated use {@link #AntPropertyHelper(ExpressionEvaluator, Set, Log)} to resolve maven.dependency.* properties
063     * @param exprEvaluator
064     * @param l
065     */
066    public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Log l )
067    {
068        this( exprEvaluator, Collections.<Artifact>emptySet(), l );
069    }
070
071    /**
072     * @param exprEvaluator
073     * @param artifacts
074     * @param l
075     */
076    public AntPropertyHelper( ExpressionEvaluator exprEvaluator, Set<Artifact> artifacts, Log l )
077    {
078        this.mavenProject = null;
079        this.exprEvaluator = exprEvaluator;
080        this.log = l;
081
082        for ( Artifact artifact : artifacts )
083        {
084            String key = DEPENDENCY_PREFIX + artifact.getGroupId() + "." + artifact.getArtifactId()
085                + ( artifact.getClassifier() != null ? "." + artifact.getClassifier() : "" )
086                + ( artifact.getType() != null ? "." + artifact.getType() : "" ) + ".path";
087
088            log.debug( "Storing: " + key + "=" + artifact.getFile().getPath() );
089
090            artifactMap.put( key, artifact.getFile().getPath() );
091        }
092    }
093
094    /**
095     * @see org.apache.tools.ant.PropertyHelper#getPropertyHook(java.lang.String, java.lang.String, boolean)
096     */
097    public synchronized Object getPropertyHook( String ns, String name, boolean user )
098    {
099        if ( log.isDebugEnabled() )
100        {
101            log.debug( "getProperty(ns=" + ns + ", name=" + name + ", user=" + user + ")" );
102        }
103
104        /* keep old behaviour */
105        if ( mavenProject != null )
106        {
107            return getPropertyHook( ns, name, user, mavenProject );
108        }
109
110
111        Object val = null;
112
113        if ( name.startsWith( DEPENDENCY_PREFIX ) )
114        {
115            val = (String) artifactMap.get( name );
116        }
117
118        if ( val == null )
119        {
120            try
121            {
122                val = exprEvaluator.evaluate( "${" + name + "}" );
123            }
124            catch ( ExpressionEvaluationException e )
125            {
126                if ( log.isErrorEnabled() )
127                {
128                    log.error( "Failed to evaluate expression", e );
129                }
130            }
131        }
132
133        if ( val == null )
134        {
135            val = super.getPropertyHook( ns, name, user );
136
137            if ( val == null )
138            {
139                val = System.getProperty( name );
140            }
141        }
142
143        return val;
144    }
145
146    /**
147     * @deprecated added to keep backwards compatibility
148     * @param ns
149     * @param name
150     * @param user
151     * @param mavenProject
152     * @return The property value.
153     */
154    private Object getPropertyHook( String ns, String name, boolean user, MavenProject mavenProject )
155    {
156        Object val = null;
157        try
158        {
159            if ( name.startsWith( DEPENDENCY_PREFIX ) )
160            {
161                val = (String) artifactMap.get( name );
162            }
163            else if ( name.startsWith( "project." ) )
164            {
165                val = ReflectionValueExtractor.evaluate(
166                    name,
167                    mavenProject,
168                    true
169                );
170            }
171            else if ( name.equals( "basedir" ) )
172            {
173                val = ReflectionValueExtractor.evaluate(
174                    "basedir.path",
175                    mavenProject,
176                    false
177                );
178            }
179        }
180        catch ( Exception e )
181        {
182            if ( log.isWarnEnabled() )
183            {
184                log.warn( "Error evaluating expression '" + name + "'", e );
185            }
186        }
187
188        if ( val == null )
189        {
190            val = super.getPropertyHook( ns, name, user );
191            if ( val == null )
192            {
193                val = System.getProperty( name );
194            }
195        }
196
197        if ( val instanceof File )
198        {
199            val = ( (File) val ).getAbsoluteFile();
200        }
201
202        return val;
203    }
204}