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      /** the max size allowed. */
42      long maxsize = 10000;
43  
44      /** the min size allowed. */
45      long minsize = 0;
46  
47      /** The error msg. */
48      private String errorMsg;
49  
50      /** The log. */
51      private Log log;
52  
53      /*
54       * (non-Javadoc)
55       *
56       * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper)
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 ( files.length == 0 )
65          {
66              try
67              {
68                  MavenProject project = (MavenProject) helper.evaluate( "${project}" );
69                  files = new File[1];
70                  files[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       * (non-Javadoc)
88       *
89       * @see org.apache.maven.enforcer.rule.api.EnforcerRule#isCacheable()
90       */
91      public boolean isCacheable()
92      {
93          return false;
94      }
95  
96      /*
97       * (non-Javadoc)
98       *
99       * @see org.apache.maven.enforcer.rule.api.EnforcerRule#isResultValid(org.apache.maven.enforcer.rule.api.EnforcerRule)
100      */
101     public boolean isResultValid( EnforcerRule cachedRule )
102     {
103         return false;
104     }
105 
106     /* (non-Javadoc)
107      * @see org.apache.maven.plugins.enforcer.AbstractRequireFiles#checkFile(java.io.File)
108      */
109     boolean checkFile( File file )
110     {
111         if (file == null)
112         {
113             //if we get here and it's null, treat it as a success.
114             return true;
115         }
116 
117         // check the file now
118         if ( file.exists() )
119         {
120             long length = file.length();
121             if ( length < minsize )
122             {
123                 this.errorMsg = ( file + " size (" + length + ") too small. Min. is " + minsize );
124                 return false;
125             }
126             else if ( length > maxsize )
127             {
128                 this.errorMsg = ( file + " size (" + length + ") too large. Max. is " + maxsize );
129                 return false;
130             }
131             else
132             {
133 
134                 this.log.debug( file +
135                     " size (" +
136                     length +
137                     ") is OK (" +
138                     ( minsize == maxsize || minsize == 0 ? ( "max. " + maxsize )
139                                     : ( "between " + minsize + " and " + maxsize ) ) + " byte)." );
140 
141                 return true;
142             }
143         }
144         else
145         {
146             this.errorMsg = ( file + " does not exist!" );
147             return false;
148         }
149     }
150 
151     /* (non-Javadoc)
152      * @see org.apache.maven.plugins.enforcer.AbstractRequireFiles#getErrorMsg()
153      */
154     String getErrorMsg()
155     {
156         return this.errorMsg;
157     }
158 }