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