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    /*
076     * (non-Javadoc)
077     *
078     * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper)
079     */
080    public void execute( EnforcerRuleHelper theHelper )
081        throws EnforcerRuleException
082    {
083        List<String> missingProfiles = new ArrayList<String>();
084        try
085        {
086            MavenProject project = (MavenProject) theHelper.evaluate( "${project}" );
087            if ( StringUtils.isNotEmpty( profiles ) )
088            {
089                String[] profs = profiles.split( "," );
090                for ( String profile : profs )
091                {
092                    if ( !isProfileActive( project, profile ) )
093                    {
094                        missingProfiles.add( profile );
095                    }
096                }
097
098                boolean fail = false;
099                if ( !missingProfiles.isEmpty() )
100                {
101                    if ( all || missingProfiles.size() == profs.length )
102                    {
103                      fail = true;
104                    }
105                }
106
107                if ( fail )
108                {
109                    String message = getMessage();
110                    StringBuilder buf = new StringBuilder();
111                    if ( message != null )
112                    {
113                        buf.append( message + "\n" );
114                    }
115
116                    for ( String profile : missingProfiles )
117                    {
118                        buf.append( "Profile \"" + profile + "\" is not activated.\n" );
119                    }
120
121                    throw new EnforcerRuleException( buf.toString() );
122                }
123
124            }
125
126        }
127        catch ( ExpressionEvaluationException e )
128        {
129            throw new EnforcerRuleException( "Unable to retrieve the project.", e );
130        }
131
132    }
133
134    /**
135     * Checks if profile is active.
136     *
137     * @param project the project
138     * @param profileName the profile name
139     * @return <code>true</code> if profile is active, otherwise <code>false</code>
140     */
141    protected boolean isProfileActive( MavenProject project, String profileName )
142    {
143        @SuppressWarnings( "unchecked" )
144        List<Profile> activeProfiles = project.getActiveProfiles();
145        if ( activeProfiles != null && !activeProfiles.isEmpty() )
146        {
147            for ( Profile profile : activeProfiles )
148            {
149                if ( profile.getId().equals( profileName ) )
150                {
151                    return true;
152                }
153            }
154        }
155
156        return false;
157    }
158}