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 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.maven.enforcer.rule.api.EnforcerRule;
27 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
28 import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
29
30 /**
31 * Contains the common code to compare an array of files against a requirement.
32 *
33 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
34 */
35 public abstract class AbstractRequireFiles
36 extends AbstractStandardEnforcerRule
37 {
38
39 /** Array of files to check. */
40 private File[] files;
41
42 /** if null file handles should be allowed. If they are allowed, it means treat it as a success. */
43 private boolean allowNulls = false;
44
45 // check the file for the specific condition
46 /**
47 * Check one file.
48 *
49 * @param file the file
50 * @return <code>true</code> if successful
51 */
52 abstract boolean checkFile( File file );
53
54 // return standard error message
55 /**
56 * Gets the error msg.
57 *
58 * @return the error msg
59 */
60 abstract String getErrorMsg();
61
62 @Override
63 public void execute( EnforcerRuleHelper helper )
64 throws EnforcerRuleException
65 {
66
67 if ( !allowNulls && files.length == 0 )
68 {
69 throw new EnforcerRuleException( "The file list is empty and Null files are disabled." );
70 }
71
72 List<File> failures = new ArrayList<>();
73 for ( File file : files )
74 {
75 if ( !allowNulls && file == null )
76 {
77 failures.add( file );
78 }
79 else if ( !checkFile( file ) )
80 {
81 failures.add( file );
82 }
83 }
84
85 // if anything was found, log it with the optional message.
86 if ( !failures.isEmpty() )
87 {
88 String message = getMessage();
89
90 StringBuilder buf = new StringBuilder();
91 if ( message != null )
92 {
93 buf.append( message + System.lineSeparator() );
94 }
95 buf.append( getErrorMsg() );
96
97 for ( File file : failures )
98 {
99 if ( file != null )
100 {
101 buf.append( file.getAbsolutePath() + System.lineSeparator() );
102 }
103 else
104 {
105 buf.append( "(an empty filename was given and allowNulls is false)" + System.lineSeparator() );
106 }
107 }
108
109 throw new EnforcerRuleException( buf.toString() );
110 }
111 }
112
113 @Override
114 public String getCacheId()
115 {
116 return Integer.toString( hashCode( files ) );
117 }
118
119 /**
120 * Calculates a hash code for the specified array as <code>Arrays.hashCode()</code> would do. Unfortunately, the
121 * mentioned method is only available for Java 1.5 and later.
122 *
123 * @param items The array for which to compute the hash code, may be <code>null</code>.
124 * @return The hash code for the array.
125 */
126 private static int hashCode( Object[] items )
127 {
128 int hash = 0;
129 if ( items != null )
130 {
131 hash = 1;
132 for ( Object item : items )
133 {
134 hash = 31 * hash + ( item == null ? 0 : item.hashCode() );
135 }
136 }
137 return hash;
138 }
139
140 @Override
141 public boolean isCacheable()
142 {
143 return true;
144 }
145
146 @Override
147 public boolean isResultValid( EnforcerRule cachedRule )
148 {
149 return true;
150 }
151
152 public File[] getFiles()
153 {
154 return files;
155 }
156
157 public void setFiles( File[] files )
158 {
159 this.files = files;
160 }
161
162 public boolean isAllowNulls()
163 {
164 return allowNulls;
165 }
166
167 public void setAllowNulls( boolean allowNulls )
168 {
169 this.allowNulls = allowNulls;
170 }
171 }