View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.enforcer.rules.files;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  
24  import java.io.File;
25  import java.util.Collections;
26  import java.util.Objects;
27  
28  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
29  import org.apache.maven.project.MavenProject;
30  
31  /**
32   * Rule to validate the main artifact is within certain size constraints.
33   *
34   * @author brianf
35   * @author Roman Stumm
36   */
37  @Named("requireFilesSize")
38  public final class RequireFilesSize extends AbstractRequireFiles {
39  
40      private static final long MAXSIZE = 10000;
41  
42      /** the max size allowed. */
43      private long maxsize = MAXSIZE;
44  
45      /** the min size allowed. */
46      private long minsize = 0;
47  
48      /** the mode for computing the size when the files are directories. */
49      private boolean recursive = false;
50  
51      /** The error msg. */
52      private String errorMsg;
53  
54      private final MavenProject project;
55  
56      @Inject
57      public RequireFilesSize(MavenProject project) {
58          this.project = Objects.requireNonNull(project);
59      }
60  
61      @Override
62      public void execute() throws EnforcerRuleException {
63  
64          // if the file is already defined, use that. Otherwise get the main artifact.
65          if (getFiles().isEmpty()) {
66              setFilesList(Collections.singletonList(project.getArtifact().getFile()));
67              super.execute();
68          } else {
69              super.execute();
70          }
71      }
72  
73      @Override
74      public String getCacheId() {
75          // non cached rule - return null
76          return null;
77      }
78  
79      @Override
80      boolean checkFile(File file) {
81          if (file == null) {
82              // if we get here and it's null, treat it as a success.
83              return true;
84          }
85  
86          // check the file now
87          if (file.exists()) {
88              long length = computeLength(file);
89              if (length < minsize) {
90                  this.errorMsg = (file + " size (" + length + ") too small. Min. is " + minsize);
91                  return false;
92              } else if (length > maxsize) {
93                  this.errorMsg = (file + " size (" + length + ") too large. Max. is " + maxsize);
94                  return false;
95              } else {
96  
97                  getLog().debug(() -> file
98                          + " size ("
99                          + 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 }