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