001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.plugins.enforcer.internal; 020 021import java.io.File; 022import java.util.HashMap; 023import java.util.List; 024import java.util.Map; 025import java.util.function.Supplier; 026 027import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; 028import org.apache.maven.execution.MavenSession; 029import org.apache.maven.plugin.logging.Log; 030import org.codehaus.plexus.PlexusContainer; 031import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; 032import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; 033import org.codehaus.plexus.component.repository.exception.ComponentLookupException; 034 035/** 036 * Default implementation of the EnforcementRuleHelper interface. This is used to help retrieve information from the 037 * session and provide useful elements like the log. 038 * 039 * @author <a href="mailto:brianf@apache.org">Brian Fox</a> 040 */ 041public class DefaultEnforcementRuleHelper implements EnforcerRuleHelper { 042 043 /** The log. */ 044 private Log log; 045 046 /** The evaluator. */ 047 private ExpressionEvaluator evaluator; 048 049 /** The container. */ 050 private PlexusContainer container; 051 052 /** A cache. */ 053 private Map<String, Object> cache; 054 055 /** 056 * Instantiates a new default enforcement rule helper. 057 * 058 * @param session the session 059 * @param evaluator the evaluator 060 * @param log the log 061 * @param container the container 062 */ 063 public DefaultEnforcementRuleHelper( 064 MavenSession session, ExpressionEvaluator evaluator, Log log, PlexusContainer container) { 065 this.evaluator = evaluator; 066 this.log = log; 067 if (container != null) { 068 this.container = container; 069 } else { 070 this.container = session.getContainer(); 071 } 072 073 this.cache = new HashMap<>(); 074 } 075 076 @Override 077 public Log getLog() { 078 return log; 079 } 080 081 @Override 082 public File alignToBaseDirectory(File theFile) { 083 return evaluator.alignToBaseDirectory(theFile); 084 } 085 086 @Override 087 public Object evaluate(String theExpression) throws ExpressionEvaluationException { 088 return evaluator.evaluate(theExpression); 089 } 090 091 @Override 092 public <T> T getComponent(Class<T> clazz) throws ComponentLookupException { 093 return container.lookup(clazz); 094 } 095 096 @Override 097 public Object getComponent(String theComponentKey) throws ComponentLookupException { 098 return container.lookup(theComponentKey); 099 } 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}