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 org.apache.maven.enforcer.rule.api.EnforcerRule;
023import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
024
025/**
026 * This rule checks that certain environment variable is set.
027 *
028 * @author <a href='mailto:marvin[at]marvinformatics[dot]com'>Marvin Froeder</a>
029 */
030public class RequireEnvironmentVariable
031    extends AbstractPropertyEnforcerRule
032{
033
034    /**
035     * Specify the required variable.
036     */
037    public String variableName = null;
038
039    /**
040     * @param variableName the variable name
041     * 
042     * @deprecated the visibility will be reduced to private with the next major version
043     * @see {@link #setVariableName(String)}
044     * @see {@link #getVariableName()}
045     */
046    public final void setVariableName( String variableName )
047    {
048        this.variableName = variableName;
049    }
050    
051    public final String getVariableName()
052    {
053        return variableName;
054    }
055
056    @Override
057    public String resolveValue( EnforcerRuleHelper helper )
058    {
059        String envValue = System.getenv( variableName );
060        return envValue;
061    }
062
063    public boolean isCacheable()
064    {
065        // environment variables won't change while maven is on the run
066        return true;
067    }
068
069    public boolean isResultValid( EnforcerRule cachedRule )
070    {
071        // this rule shall always have the same result, since environment
072        // variables are set before maven is launched
073        return true;
074    }
075
076    public String getCacheId()
077    {
078        return variableName;
079    }
080
081    @Override
082    public String getPropertyName()
083    {
084        return variableName;
085    }
086
087    @Override
088    public String getName()
089    {
090        return "Environment variable";
091    }
092}