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    private String variableName = null;
038
039    /**
040     * @param variableName the variable name
041     * 
042     * @see #setVariableName(String)
043     * @see #getVariableName()
044     */
045    public final void setVariableName( String variableName )
046    {
047        this.variableName = variableName;
048    }
049    
050    public final String getVariableName()
051    {
052        return variableName;
053    }
054
055    @Override
056    public String resolveValue( EnforcerRuleHelper helper )
057    {
058        String envValue = System.getenv( variableName );
059        return envValue;
060    }
061
062    @Override
063    public boolean isCacheable()
064    {
065        // environment variables won't change while maven is on the run
066        return true;
067    }
068
069    @Override
070    public boolean isResultValid( EnforcerRule cachedRule )
071    {
072        // this rule shall always have the same result, since environment
073        // variables are set before maven is launched
074        return true;
075    }
076
077    @Override
078    public String getCacheId()
079    {
080        return variableName;
081    }
082
083    @Override
084    public String getPropertyName()
085    {
086        return variableName;
087    }
088
089    @Override
090    public String getName()
091    {
092        return "Environment variable";
093    }
094}