001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.enforcer.rules.files;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023
024import java.io.File;
025import java.util.Collections;
026import java.util.Objects;
027
028import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
029import org.apache.maven.project.MavenProject;
030
031/**
032 * Rule to validate the main artifact is within certain size constraints.
033 *
034 * @author brianf
035 * @author Roman Stumm
036 */
037@Named("requireFilesSize")
038public final class RequireFilesSize extends AbstractRequireFiles {
039
040    private static final long MAXSIZE = 10000;
041
042    /** the max size allowed. */
043    private long maxsize = MAXSIZE;
044
045    /** the min size allowed. */
046    private long minsize = 0;
047
048    /** The error msg. */
049    private String errorMsg;
050
051    private final MavenProject project;
052
053    @Inject
054    public RequireFilesSize(MavenProject project) {
055        this.project = Objects.requireNonNull(project);
056    }
057
058    @Override
059    public void execute() throws EnforcerRuleException {
060
061        // if the file is already defined, use that. Otherwise get the main artifact.
062        if (getFiles().isEmpty()) {
063            setFilesList(Collections.singletonList(project.getArtifact().getFile()));
064            super.execute();
065        } else {
066            super.execute();
067        }
068    }
069
070    @Override
071    public String getCacheId() {
072        // non cached rule - return null
073        return null;
074    }
075
076    @Override
077    boolean checkFile(File file) {
078        if (file == null) {
079            // if we get here and it's null, treat it as a success.
080            return true;
081        }
082
083        // check the file now
084        if (file.exists()) {
085            long length = file.length();
086            if (length < minsize) {
087                this.errorMsg = (file + " size (" + length + ") too small. Min. is " + minsize);
088                return false;
089            } else if (length > maxsize) {
090                this.errorMsg = (file + " size (" + length + ") too large. Max. is " + maxsize);
091                return false;
092            } else {
093
094                getLog().debug(() -> file
095                        + " size ("
096                        + length
097                        + ") is OK ("
098                        + (minsize == maxsize || minsize == 0
099                                ? ("max. " + maxsize)
100                                : ("between " + minsize + " and " + maxsize))
101                        + " byte).");
102
103                return true;
104            }
105        } else {
106            this.errorMsg = (file + " does not exist!");
107            return false;
108        }
109    }
110
111    @Override
112    String getErrorMsg() {
113        return this.errorMsg;
114    }
115
116    public void setMaxsize(long maxsize) {
117        this.maxsize = maxsize;
118    }
119
120    public void setMinsize(long minsize) {
121        this.minsize = minsize;
122    }
123}