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     * @deprecated the visibility will be reduced to private with the next major version
044     * @see {@link #setProfiles(String)}
045     * @see {@link #getProfiles()}
046     */
047    public String profiles = null;
048
049    /** If all profiles must be active. If false, only one must be active
050     *
051     * @deprecated the visibility will be reduced to private with the next major version
052     * @see {@link #setAll(boolean)}
053     * @see {@link #isAll()}
054     */
055    public boolean all = true;
056    
057    public final String getProfiles()
058    {
059        return profiles;
060    }
061    
062    public final void setProfiles( String profiles )
063    {
064        this.profiles = profiles;
065    }
066    
067    public final boolean isAll()
068    {
069        return all;
070    }
071    
072    public final void setAll( boolean all )
073    {
074        this.all = all;
075    }
076
077    /*
078     * (non-Javadoc)
079     *
080     * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper)
081     */
082    public void execute( EnforcerRuleHelper theHelper )
083        throws EnforcerRuleException
084    {
085        List<String> missingProfiles = new ArrayList<String>();
086        try
087        {
088            MavenProject project = (MavenProject) theHelper.evaluate( "${project}" );
089            if ( StringUtils.isNotEmpty( profiles ) )
090            {
091                String[] profs = profiles.split( "," );
092                for ( String profile : profs )
093                {
094                    if ( !isProfileActive( project, profile ) )
095                    {
096                        missingProfiles.add( profile );
097                    }
098                }
099
100                boolean fail = false;
101                if ( !missingProfiles.isEmpty() )
102                {
103                    fail = true;
104                    // if (all && missingProfiles.size() != profs.length)
105                    // {
106                    // fail = true;
107                    // }
108                    // else
109                    // {
110                    // if (!all && missingProfiles.size() >= (profs.length -1))
111                    // {
112                    // fail = true;
113                    // }
114                    // }
115                }
116
117                if ( fail )
118                {
119                    String message = getMessage();
120                    StringBuilder buf = new StringBuilder();
121                    if ( message != null )
122                    {
123                        buf.append( message + "\n" );
124                    }
125
126                    for ( String profile : missingProfiles )
127                    {
128                        buf.append( "Profile \"" + profile + "\" is not activated.\n" );
129                    }
130
131                    throw new EnforcerRuleException( buf.toString() );
132                }
133
134            }
135
136        }
137        catch ( ExpressionEvaluationException e )
138        {
139            throw new EnforcerRuleException( "Unable to retrieve the project.", e );
140        }
141
142    }
143
144    /**
145     * Checks if profile is active.
146     *
147     * @param project the project
148     * @param profileName the profile name
149     * @return <code>true</code> if profile is active, otherwise <code>false</code>
150     */
151    protected boolean isProfileActive( MavenProject project, String profileName )
152    {
153        @SuppressWarnings( "unchecked" )
154        List<Profile> activeProfiles = project.getActiveProfiles();
155        if ( activeProfiles != null && !activeProfiles.isEmpty() )
156        {
157            for ( Profile profile : activeProfiles )
158            {
159                if ( profile.getId().equals( profileName ) )
160                {
161                    return true;
162                }
163            }
164        }
165
166        return false;
167    }
168}