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 java.util.ArrayList;
023import java.util.List;
024
025import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
026import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
027import org.apache.maven.model.Profile;
028import org.apache.maven.project.MavenProject;
029import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
030import org.codehaus.plexus.util.StringUtils;
031
032/**
033 * This rule checks that some profiles are active.
034 *
035 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
036 */
037public class RequireActiveProfile
038    extends AbstractNonCacheableEnforcerRule
039{
040
041    /** Comma separated list of profiles to check.
042     *  
043     * @see {@link #setProfiles(String)}
044     * @see {@link #getProfiles()}
045     */
046    private String profiles = null;
047
048    /** If all profiles must be active. If false, only one must be active
049     *
050     * @see {@link #setAll(boolean)}
051     * @see {@link #isAll()}
052     */
053    private boolean all = true;
054    
055    public final String getProfiles()
056    {
057        return profiles;
058    }
059    
060    public final void setProfiles( String profiles )
061    {
062        this.profiles = profiles;
063    }
064    
065    public final boolean isAll()
066    {
067        return all;
068    }
069    
070    public final void setAll( boolean all )
071    {
072        this.all = all;
073    }
074
075    @Override
076    public void execute( EnforcerRuleHelper theHelper )
077        throws EnforcerRuleException
078    {
079        List<String> missingProfiles = new ArrayList<String>();
080        try
081        {
082            MavenProject project = (MavenProject) theHelper.evaluate( "${project}" );
083            if ( StringUtils.isNotEmpty( profiles ) )
084            {
085                String[] profs = profiles.split( "," );
086                for ( String profile : profs )
087                {
088                    if ( !isProfileActive( project, profile ) )
089                    {
090                        missingProfiles.add( profile );
091                    }
092                }
093
094                boolean fail = false;
095                if ( !missingProfiles.isEmpty() )
096                {
097                    if ( all || missingProfiles.size() == profs.length )
098                    {
099                      fail = true;
100                    }
101                }
102
103                if ( fail )
104                {
105                    String message = getMessage();
106                    StringBuilder buf = new StringBuilder();
107                    if ( message != null )
108                    {
109                        buf.append( message + "\n" );
110                    }
111
112                    for ( String profile : missingProfiles )
113                    {
114                        buf.append( "Profile \"" + profile + "\" is not activated.\n" );
115                    }
116
117                    throw new EnforcerRuleException( buf.toString() );
118                }
119
120            }
121
122        }
123        catch ( ExpressionEvaluationException e )
124        {
125            throw new EnforcerRuleException( "Unable to retrieve the project.", e );
126        }
127
128    }
129
130    /**
131     * Checks if profile is active.
132     *
133     * @param project the project
134     * @param profileName the profile name
135     * @return <code>true</code> if profile is active, otherwise <code>false</code>
136     */
137    protected boolean isProfileActive( MavenProject project, String profileName )
138    {
139        List<Profile> activeProfiles = project.getActiveProfiles();
140        if ( activeProfiles != null && !activeProfiles.isEmpty() )
141        {
142            for ( Profile profile : activeProfiles )
143            {
144                if ( profile.getId().equals( profileName ) )
145                {
146                    return true;
147                }
148            }
149        }
150
151        return false;
152    }
153}