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.io.File;
023
024import org.apache.maven.enforcer.rule.api.EnforcerRule;
025import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
026import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
027import org.apache.maven.plugin.logging.Log;
028import org.apache.maven.project.MavenProject;
029import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
030
031/**
032 * Rule to validate the main artifact is within certain size constraints.
033 *
034 * @author brianf
035 * @author Roman Stumm
036 */
037public class RequireFilesSize
038    extends AbstractRequireFiles
039{
040
041    /** the max size allowed. */
042    long maxsize = 10000;
043
044    /** the min size allowed. */
045    long minsize = 0;
046
047    /** The error msg. */
048    private String errorMsg;
049
050    /** The log. */
051    private Log log;
052
053    /*
054     * (non-Javadoc)
055     *
056     * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper)
057     */
058    public void execute( EnforcerRuleHelper helper )
059        throws EnforcerRuleException
060    {
061        this.log = helper.getLog();
062
063        // if the file is already defined, use that. Otherwise get the main artifact.
064        if ( files.length == 0 )
065        {
066            try
067            {
068                MavenProject project = (MavenProject) helper.evaluate( "${project}" );
069                files = new File[1];
070                files[0] = project.getArtifact().getFile();
071
072                super.execute( helper );
073            }
074            catch ( ExpressionEvaluationException e )
075            {
076                throw new EnforcerRuleException( "Unable to retrieve the project.", e );
077            }
078        }
079        else
080        {
081            super.execute( helper );
082        }
083
084    }
085
086    /*
087     * (non-Javadoc)
088     *
089     * @see org.apache.maven.enforcer.rule.api.EnforcerRule#isCacheable()
090     */
091    public boolean isCacheable()
092    {
093        return false;
094    }
095
096    /*
097     * (non-Javadoc)
098     *
099     * @see org.apache.maven.enforcer.rule.api.EnforcerRule#isResultValid(org.apache.maven.enforcer.rule.api.EnforcerRule)
100     */
101    public boolean isResultValid( EnforcerRule cachedRule )
102    {
103        return false;
104    }
105
106    /* (non-Javadoc)
107     * @see org.apache.maven.plugins.enforcer.AbstractRequireFiles#checkFile(java.io.File)
108     */
109    boolean checkFile( File file )
110    {
111        if ( file == null )
112        {
113            //if we get here and it's null, treat it as a success.
114            return true;
115        }
116
117        // check the file now
118        if ( file.exists() )
119        {
120            long length = file.length();
121            if ( length < minsize )
122            {
123                this.errorMsg = ( file + " size (" + length + ") too small. Min. is " + minsize );
124                return false;
125            }
126            else if ( length > maxsize )
127            {
128                this.errorMsg = ( file + " size (" + length + ") too large. Max. is " + maxsize );
129                return false;
130            }
131            else
132            {
133
134                this.log.debug( file
135                    + " size ("
136                    + length
137                    + ") is OK ("
138                    + ( minsize == maxsize || minsize == 0 ? ( "max. " + maxsize )
139                                    : ( "between " + minsize + " and " + maxsize ) ) + " byte)." );
140
141                return true;
142            }
143        }
144        else
145        {
146            this.errorMsg = ( file + " does not exist!" );
147            return false;
148        }
149    }
150
151    /* (non-Javadoc)
152     * @see org.apache.maven.plugins.enforcer.AbstractRequireFiles#getErrorMsg()
153     */
154    String getErrorMsg()
155    {
156        return this.errorMsg;
157    }
158}