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 error msg. */
49      private String errorMsg;
50  
51      private final MavenProject project;
52  
53      @Inject
54      public RequireFilesSize(MavenProject project) {
55          this.project = Objects.requireNonNull(project);
56      }
57  
58      @Override
59      public void execute() throws EnforcerRuleException {
60  
61          // if the file is already defined, use that. Otherwise get the main artifact.
62          if (getFiles().isEmpty()) {
63              setFilesList(Collections.singletonList(project.getArtifact().getFile()));
64              super.execute();
65          } else {
66              super.execute();
67          }
68      }
69  
70      @Override
71      public String getCacheId() {
72          // non cached rule - return null
73          return null;
74      }
75  
76      @Override
77      boolean checkFile(File file) {
78          if (file == null) {
79              // if we get here and it's null, treat it as a success.
80              return true;
81          }
82  
83          // check the file now
84          if (file.exists()) {
85              long length = file.length();
86              if (length < minsize) {
87                  this.errorMsg = (file + " size (" + length + ") too small. Min. is " + minsize);
88                  return false;
89              } else if (length > maxsize) {
90                  this.errorMsg = (file + " size (" + length + ") too large. Max. is " + maxsize);
91                  return false;
92              } else {
93  
94                  getLog().debug(() -> file
95                          + " size ("
96                          + length
97                          + ") is OK ("
98                          + (minsize == maxsize || minsize == 0
99                                  ? ("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 }