001package org.apache.maven.plugins.enforcer;
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.List;
024import java.util.Map;
025
026import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
027import org.apache.maven.execution.MavenSession;
028import org.apache.maven.plugin.logging.Log;
029import org.codehaus.plexus.PlexusContainer;
030import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
031import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
032import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
033
034/**
035 * Default implementation of the EnforcementRuleHelper interface. This is used to help retrieve information from the
036 * session and provide useful elements like the log.
037 *
038 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
039 * @version $Id: DefaultEnforcementRuleHelper.java 805190 2009-08-17 22:30:49Z hboutemy $
040 */
041public class DefaultEnforcementRuleHelper
042    implements EnforcerRuleHelper
043{
044
045    /** The log. */
046    Log log;
047
048    /** The evaluator. */
049    ExpressionEvaluator evaluator;
050
051    /** The session. */
052    MavenSession session;
053
054    /** The container. */
055    PlexusContainer container;
056
057    /**
058     * Instantiates a new default enforcement rule helper.
059     *
060     * @param session the session
061     * @param evaluator the evaluator
062     * @param log the log
063     * @param container the container
064     */
065    public DefaultEnforcementRuleHelper( MavenSession session, ExpressionEvaluator evaluator, Log log,
066                                         PlexusContainer container )
067    {
068        this.evaluator = evaluator;
069        this.log = log;
070        this.session = session;
071        if ( container != null )
072        {
073            this.container = container;
074        }
075        else
076        {
077            this.container = session.getContainer();
078        }
079    }
080
081    /*
082     * (non-Javadoc)
083     *
084     * @see org.apache.maven.enforcer.rule.api.EnforcerRuleHelper#getLog()
085     */
086    public Log getLog()
087    {
088        return log;
089    }
090
091    /*
092     * (non-Javadoc)
093     *
094     * @see org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator#alignToBaseDirectory(java.io.File)
095     */
096    public File alignToBaseDirectory( File theFile )
097    {
098        return evaluator.alignToBaseDirectory( theFile );
099    }
100
101    /*
102     * (non-Javadoc)
103     *
104     * @see org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator#evaluate(java.lang.String)
105     */
106    public Object evaluate( String theExpression )
107        throws ExpressionEvaluationException
108    {
109        return evaluator.evaluate( theExpression );
110    }
111
112    /*
113     * (non-Javadoc)
114     *
115     * @see org.apache.maven.shared.enforcer.rule.api.EnforcerRuleHelper#getRuntimeInformation()
116     */
117    public Object getComponent( Class clazz )
118        throws ComponentLookupException
119    {
120        return getComponent( clazz.getName() );
121    }
122
123    /*
124     * (non-Javadoc)
125     *
126     * @see org.apache.maven.enforcer.rule.api.EnforcerRuleHelper#lookup(java.lang.String)
127     */
128    public Object getComponent( String theComponentKey )
129        throws ComponentLookupException
130    {
131        return container.lookup( theComponentKey );
132    }
133
134    /*
135     * (non-Javadoc)
136     *
137     * @see org.apache.maven.enforcer.rule.api.EnforcerRuleHelper#lookup(java.lang.String, java.lang.String)
138     */
139    public Object getComponent( String theRole, String theRoleHint )
140        throws ComponentLookupException
141    {
142        return container.lookup( theRole, theRoleHint );
143    }
144
145    /*
146     * (non-Javadoc)
147     *
148     * @see org.apache.maven.enforcer.rule.api.EnforcerRuleHelper#lookupList(java.lang.String)
149     */
150    public List getComponentList( String theRole )
151        throws ComponentLookupException
152    {
153        return container.lookupList( theRole );
154    }
155
156    /*
157     * (non-Javadoc)
158     *
159     * @see org.apache.maven.enforcer.rule.api.EnforcerRuleHelper#lookupMap(java.lang.String)
160     */
161    public Map getComponentMap( String theRole )
162        throws ComponentLookupException
163    {
164        return container.lookupMap( theRole );
165    }
166
167    /*
168     * (non-Javadoc)
169     *
170     * @see org.apache.maven.enforcer.rule.api.EnforcerRuleHelper#getContainer()
171     */
172    public PlexusContainer getContainer()
173    {
174        return container;
175    }
176}