View Javadoc
1   package org.apache.maven.plugins.enforcer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.plugin.logging.Log;
29  import org.codehaus.plexus.PlexusContainer;
30  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
31  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
32  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
33  
34  /**
35   * Default implementation of the EnforcementRuleHelper interface. This is used to help retrieve information from the
36   * session and provide useful elements like the log.
37   *
38   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
39   * @version $Id: DefaultEnforcementRuleHelper.java 1802595 2017-07-21 13:39:48Z rfscholte $
40   */
41  public class DefaultEnforcementRuleHelper
42      implements EnforcerRuleHelper
43  {
44  
45      /** The log. */
46      private Log log;
47  
48      /** The evaluator. */
49      private ExpressionEvaluator evaluator;
50  
51      /** The container. */
52      private PlexusContainer container;
53  
54      /**
55       * Instantiates a new default enforcement rule helper.
56       *
57       * @param session the session
58       * @param evaluator the evaluator
59       * @param log the log
60       * @param container the container
61       */
62      public DefaultEnforcementRuleHelper( MavenSession session, ExpressionEvaluator evaluator, Log log,
63                                           PlexusContainer container )
64      {
65          this.evaluator = evaluator;
66          this.log = log;
67          if ( container != null )
68          {
69              this.container = container;
70          }
71          else
72          {
73              this.container = session.getContainer();
74          }
75      }
76  
77      @Override
78      public Log getLog()
79      {
80          return log;
81      }
82  
83      @Override
84      public File alignToBaseDirectory( File theFile )
85      {
86          return evaluator.alignToBaseDirectory( theFile );
87      }
88  
89      @Override
90      public Object evaluate( String theExpression )
91          throws ExpressionEvaluationException
92      {
93          return evaluator.evaluate( theExpression );
94      }
95  
96      @Override
97      public <T> T getComponent( Class<T> clazz )
98          throws ComponentLookupException
99      {
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 }