001package org.apache.maven.plugins.enforcer; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.File; 023 024import org.apache.maven.enforcer.rule.api.EnforcerRule; 025import org.apache.maven.enforcer.rule.api.EnforcerRuleException; 026import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; 027import org.apache.maven.plugin.logging.Log; 028import org.apache.maven.project.MavenProject; 029import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; 030 031/** 032 * Rule to validate the main artifact is within certain size constraints. 033 * 034 * @author brianf 035 * @author Roman Stumm 036 */ 037public class RequireFilesSize 038 extends AbstractRequireFiles 039{ 040 041 private static final long MAXSIZE = 10000; 042 043 /** the max size allowed. */ 044 private long maxsize = MAXSIZE; 045 046 /** the min size allowed. */ 047 private long minsize = 0; 048 049 /** The error msg. */ 050 private String errorMsg; 051 052 /** The log. */ 053 private Log log; 054 055 @Override 056 public void execute( EnforcerRuleHelper helper ) 057 throws EnforcerRuleException 058 { 059 this.log = helper.getLog(); 060 061 // if the file is already defined, use that. Otherwise get the main artifact. 062 if ( getFiles().length == 0 ) 063 { 064 try 065 { 066 MavenProject project = (MavenProject) helper.evaluate( "${project}" ); 067 setFiles( new File[1] ); 068 getFiles()[0] = project.getArtifact().getFile(); 069 070 super.execute( helper ); 071 } 072 catch ( ExpressionEvaluationException e ) 073 { 074 throw new EnforcerRuleException( "Unable to retrieve the project.", e ); 075 } 076 } 077 else 078 { 079 super.execute( helper ); 080 } 081 082 } 083 084 @Override 085 public boolean isCacheable() 086 { 087 return false; 088 } 089 090 @Override 091 public boolean isResultValid( EnforcerRule cachedRule ) 092 { 093 return false; 094 } 095 096 @Override 097 boolean checkFile( File file ) 098 { 099 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}