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      /**
56       * {@inheritDoc}
57       */
58      public void execute( EnforcerRuleHelper helper )
59          throws EnforcerRuleException
60      {
61          this.log = helper.getLog();
62  
63          // if the file is already defined, use that. Otherwise get the main artifact.
64          if ( getFiles().length == 0 )
65          {
66              try
67              {
68                  MavenProject project = (MavenProject) helper.evaluate( "${project}" );
69                  setFiles( new File[1] );
70                  getFiles()[0] = project.getArtifact().getFile();
71  
72                  super.execute( helper );
73              }
74              catch ( ExpressionEvaluationException e )
75              {
76                  throw new EnforcerRuleException( "Unable to retrieve the project.", e );
77              }
78          }
79          else
80          {
81              super.execute( helper );
82          }
83  
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      public boolean isCacheable()
90      {
91          return false;
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      public boolean isResultValid( EnforcerRule cachedRule )
98      {
99          return false;
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     boolean checkFile( File file )
106     {
107         if ( file == null )
108         {
109             // if we get here and it's null, treat it as a success.
110             return true;
111         }
112 
113         // check the file now
114         if ( file.exists() )
115         {
116             long length = file.length();
117             if ( length < minsize )
118             {
119                 this.errorMsg = ( file + " size (" + length + ") too small. Min. is " + minsize );
120                 return false;
121             }
122             else if ( length > maxsize )
123             {
124                 this.errorMsg = ( file + " size (" + length + ") too large. Max. is " + maxsize );
125                 return false;
126             }
127             else
128             {
129 
130                 this.log.debug( file
131                     + " size ("
132                     + length
133                     + ") is OK ("
134                     + ( minsize == maxsize || minsize == 0 ? ( "max. " + maxsize )
135                                     : ( "between " + minsize + " and " + maxsize ) ) + " byte)." );
136 
137                 return true;
138             }
139         }
140         else
141         {
142             this.errorMsg = ( file + " does not exist!" );
143             return false;
144         }
145     }
146 
147     /**
148      * {@inheritDoc}
149      */
150     String getErrorMsg()
151     {
152         return this.errorMsg;
153     }
154 
155     public long getMaxsize()
156     {
157         return maxsize;
158     }
159 
160     public void setMaxsize( long maxsize )
161     {
162         this.maxsize = maxsize;
163     }
164 
165     public long getMinsize()
166     {
167         return minsize;
168     }
169 
170     public void setMinsize( long minsize )
171     {
172         this.minsize = minsize;
173     }
174 }