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 mode for computing the size when the files are directories. */
049    private boolean recursive = false;
050
051    /** The error msg. */
052    private String errorMsg;
053
054    private final MavenProject project;
055
056    @Inject
057    public RequireFilesSize(MavenProject project) {
058        this.project = Objects.requireNonNull(project);
059    }
060
061    @Override
062    public void execute() throws EnforcerRuleException {
063
064        // if the file is already defined, use that. Otherwise get the main artifact.
065        if (getFiles().isEmpty()) {
066            setFilesList(Collections.singletonList(project.getArtifact().getFile()));
067            super.execute();
068        } else {
069            super.execute();
070        }
071    }
072
073    @Override
074    public String getCacheId() {
075        // non cached rule - return null
076        return null;
077    }
078
079    @Override
080    boolean checkFile(File file) {
081        if (file == null) {
082            // if we get here and it's null, treat it as a success.
083            return true;
084        }
085
086        // check the file now
087        if (file.exists()) {
088            long length = computeLength(file);
089            if (length < minsize) {
090                this.errorMsg = (file + " size (" + length + ") too small. Min. is " + minsize);
091                return false;
092            } else if (length > maxsize) {
093                this.errorMsg = (file + " size (" + length + ") too large. Max. is " + maxsize);
094                return false;
095            } else {
096
097                getLog().debug(() -> file
098                        + " size ("
099                        + length
100                        + ") is OK ("
101                        + (minsize == maxsize || minsize == 0
102                                ? ("max. " + maxsize)
103                                : ("between " + minsize + " and " + maxsize))
104                        + " byte).");
105
106                return true;
107            }
108        } else {
109            this.errorMsg = (file + " does not exist!");
110            return false;
111        }
112    }
113
114    private long computeLength(File file) {
115        File[] files = file.listFiles();
116        long length = file.length();
117        if (files == null || !recursive) {
118            return length;
119        }
120        for (File child : files) {
121            length += computeLength(child);
122        }
123        return length;
124    }
125
126    @Override
127    String getErrorMsg() {
128        return this.errorMsg;
129    }
130
131    public void setMaxsize(long maxsize) {
132        this.maxsize = maxsize;
133    }
134
135    public void setMinsize(long minsize) {
136        this.minsize = minsize;
137    }
138
139    public void setRecursive(boolean recursive) {
140        this.recursive = recursive;
141    }
142}