1 package org.apache.maven.plugins.enforcer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
33
34
35
36
37 public class RequireFilesSize
38 extends AbstractRequireFiles
39 {
40
41
42 long maxsize = 10000;
43
44
45 long minsize = 0;
46
47
48 private String errorMsg;
49
50
51 private Log log;
52
53
54
55
56
57
58 public void execute( EnforcerRuleHelper helper )
59 throws EnforcerRuleException
60 {
61 this.log = helper.getLog();
62
63
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
88
89
90
91 public boolean isCacheable()
92 {
93 return false;
94 }
95
96
97
98
99
100
101 public boolean isResultValid( EnforcerRule cachedRule )
102 {
103 return false;
104 }
105
106
107
108
109 boolean checkFile( File file )
110 {
111 if (file == null)
112 {
113
114 return true;
115 }
116
117
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
152
153
154 String getErrorMsg()
155 {
156 return this.errorMsg;
157 }
158 }