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 1802595 2017-07-21 13:39:48Z rfscholte $
040 */
041public class DefaultEnforcementRuleHelper
042    implements EnforcerRuleHelper
043{
044
045    /** The log. */
046    private Log log;
047
048    /** The evaluator. */
049    private ExpressionEvaluator evaluator;
050
051    /** The container. */
052    private PlexusContainer container;
053
054    /**
055     * Instantiates a new default enforcement rule helper.
056     *
057     * @param session the session
058     * @param evaluator the evaluator
059     * @param log the log
060     * @param container the container
061     */
062    public DefaultEnforcementRuleHelper( MavenSession session, ExpressionEvaluator evaluator, Log log,
063                                         PlexusContainer container )
064    {
065        this.evaluator = evaluator;
066        this.log = log;
067        if ( container != null )
068        {
069            this.container = container;
070        }
071        else
072        {
073            this.container = session.getContainer();
074        }
075    }
076
077    @Override
078    public Log getLog()
079    {
080        return log;
081    }
082
083    @Override
084    public File alignToBaseDirectory( File theFile )
085    {
086        return evaluator.alignToBaseDirectory( theFile );
087    }
088
089    @Override
090    public Object evaluate( String theExpression )
091        throws ExpressionEvaluationException
092    {
093        return evaluator.evaluate( theExpression );
094    }
095
096    @Override
097    public <T> T getComponent( Class<T> clazz )
098        throws ComponentLookupException
099    {
100        return container.lookup( clazz );
101    }
102
103    @Override
104    public Object getComponent( String theComponentKey )
105        throws ComponentLookupException
106    {
107        return container.lookup( theComponentKey );
108    }
109
110    @Override
111    public Object getComponent( String theRole, String theRoleHint )
112        throws ComponentLookupException
113    {
114        return container.lookup( theRole, theRoleHint );
115    }
116
117    @Override
118    public List<Object> getComponentList( String theRole )
119        throws ComponentLookupException
120    {
121        return container.lookupList( theRole );
122    }
123
124    @Override
125    public Map<String, Object> getComponentMap( String theRole )
126        throws ComponentLookupException
127    {
128        return container.lookupMap( theRole );
129    }
130    
131    @Override
132    public <T> T getComponent( Class<T> clazz, String roleHint )
133        throws ComponentLookupException
134    {
135        return container.lookup( clazz, roleHint );
136    }
137
138    @Override
139    public PlexusContainer getContainer()
140    {
141        return container;
142    }
143}