001package org.apache.maven.plugins.enforcer.utils;
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.EnforcerRuleException;
023import org.apache.maven.model.DistributionManagement;
024import org.apache.maven.project.MavenProject;
025
026/**
027 * @author Karl Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmarbaise@apache.org</a>
028 */
029public class DistributionManagementCheck
030{
031    private DistributionManagement distributionManagement;
032
033    public DistributionManagementCheck( MavenProject project )
034    {
035        this.distributionManagement = project.getOriginalModel().getDistributionManagement();
036    }
037
038    public void execute( boolean isAllowRepository, boolean isAllowSnapshotRepository, boolean isAllowSite )
039        throws EnforcerRuleException
040    {
041        if ( hasDistributionManagement() )
042        {
043            if ( !isAllowRepository && hasRepository() )
044            {
045                throw new EnforcerRuleException( "You have defined a repository in distributionManagement." );
046            }
047            else if ( !isAllowSnapshotRepository && hasSnapshotRepository() )
048            {
049                throw new EnforcerRuleException( "You have defined a snapshotRepository in distributionManagement." );
050            }
051            else if ( !isAllowSite && hasSite() )
052            {
053                throw new EnforcerRuleException( "You have defined a site in distributionManagement." );
054            }
055        }
056    }
057
058    private boolean hasRepository()
059    {
060        return getDistributionManagement().getRepository() != null;
061    }
062
063    public DistributionManagement getDistributionManagement()
064    {
065        return distributionManagement;
066    }
067
068    public void setDistributionManagement( DistributionManagement distributionManagement )
069    {
070        this.distributionManagement = distributionManagement;
071    }
072
073    private boolean hasSnapshotRepository()
074    {
075        return getDistributionManagement().getSnapshotRepository() != null;
076    }
077
078    private boolean hasSite()
079    {
080        return getDistributionManagement().getSite() != null;
081    }
082
083    private boolean hasDistributionManagement()
084    {
085        return getDistributionManagement() != null;
086    }
087}