View Javadoc
1   package org.apache.maven.plugins.enforcer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  
24  import org.apache.maven.enforcer.rule.api.EnforcerRule;
25  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
26  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
27  import org.apache.maven.plugin.logging.Log;
28  import org.apache.maven.project.MavenProject;
29  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
30  
31  /**
32   * Rule to validate the main artifact is within certain size constraints.
33   *
34   * @author brianf
35   * @author Roman Stumm
36   */
37  public class RequireFilesSize
38      extends AbstractRequireFiles
39  {
40  
41      private static final long MAXSIZE = 10000;
42  
43      /** the max size allowed. */
44      private long maxsize = MAXSIZE;
45  
46      /** the min size allowed. */
47      private long minsize = 0;
48  
49      /** The error msg. */
50      private String errorMsg;
51  
52      /** The log. */
53      private Log log;
54  
55      @Override
56      public void execute( EnforcerRuleHelper helper )
57          throws EnforcerRuleException
58      {
59          this.log = helper.getLog();
60  
61          // if the file is already defined, use that. Otherwise get the main artifact.
62          if ( getFiles().length == 0 )
63          {
64              try
65              {
66                  MavenProject project = (MavenProject) helper.evaluate( "${project}" );
67                  setFiles( new File[1] );
68                  getFiles()[0] = project.getArtifact().getFile();
69  
70                  super.execute( helper );
71              }
72              catch ( ExpressionEvaluationException e )
73              {
74                  throw new EnforcerRuleException( "Unable to retrieve the project.", e );
75              }
76          }
77          else
78          {
79              super.execute( helper );
80          }
81  
82      }
83  
84      @Override
85      public boolean isCacheable()
86      {
87          return false;
88      }
89  
90      @Override
91      public boolean isResultValid( EnforcerRule cachedRule )
92      {
93          return false;
94      }
95  
96      @Override
97      boolean checkFile( File file )
98      {
99          if ( file == null )
100         {
101             // if we get here and it's null, treat it as a success.
102             return true;
103         }
104 
105         // check the file now
106         if ( file.exists() )
107         {
108             long length = file.length();
109             if ( length < minsize )
110             {
111                 this.errorMsg = ( file + " size (" + length + ") too small. Min. is " + minsize );
112                 return false;
113             }
114             else if ( length > maxsize )
115             {
116                 this.errorMsg = ( file + " size (" + length + ") too large. Max. is " + maxsize );
117                 return false;
118             }
119             else
120             {
121 
122                 this.log.debug( file
123                     + " size ("
124                     + length
125                     + ") is OK ("
126                     + ( minsize == maxsize || minsize == 0 ? ( "max. " + maxsize )
127                                     : ( "between " + minsize + " and " + maxsize ) ) + " byte)." );
128 
129                 return true;
130             }
131         }
132         else
133         {
134             this.errorMsg = ( file + " does not exist!" );
135             return false;
136         }
137     }
138 
139     @Override
140     String getErrorMsg()
141     {
142         return this.errorMsg;
143     }
144 
145     public long getMaxsize()
146     {
147         return maxsize;
148     }
149 
150     public void setMaxsize( long maxsize )
151     {
152         this.maxsize = maxsize;
153     }
154 
155     public long getMinsize()
156     {
157         return minsize;
158     }
159 
160     public void setMinsize( long minsize )
161     {
162         this.minsize = minsize;
163     }
164 }