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 File[] files;
41
42 /** if null file handles should be allowed. If they are allowed, it means treat it as a success. */
43 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 /*
63 * (non-Javadoc)
64 *
65 * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper)
66 */
67 public void execute( EnforcerRuleHelper helper )
68 throws EnforcerRuleException
69 {
70
71 if ( !allowNulls && files.length == 0 )
72 {
73 throw new EnforcerRuleException( "The file list is empty and Null files are disabled." );
74 }
75
76 List<File> failures = new ArrayList<File>();
77 for ( File file : files )
78 {
79 if ( !allowNulls && file == null )
80 {
81 failures.add( file );
82 }
83 else if ( !checkFile( file ) )
84 {
85 failures.add( file );
86 }
87 }
88
89 // if anything was found, log it with the optional message.
90 if ( !failures.isEmpty() )
91 {
92 StringBuilder buf = new StringBuilder();
93 if ( message != null )
94 {
95 buf.append( message + "\n" );
96 }
97 buf.append( getErrorMsg() );
98
99 for ( File file : failures )
100 {
101 if ( file != null )
102 {
103 buf.append( file.getAbsolutePath() + "\n" );
104 }
105 else
106 {
107 buf.append( "(an empty filename was given and allowNulls is false)\n" );
108 }
109 }
110
111 throw new EnforcerRuleException( buf.toString() );
112 }
113 }
114
115 /**
116 * If your rule is cacheable, you must return a unique id when parameters or conditions change that would cause the
117 * result to be different. Multiple cached results are stored based on their id. The easiest way to do this is to
118 * return a hash computed from the values of your parameters. If your rule is not cacheable, then the result here is
119 * not important, you may return anything.
120 *
121 * @return the cache id
122 */
123 public String getCacheId()
124 {
125 return Integer.toString( hashCode( files ) );
126 }
127
128 /**
129 * Calculates a hash code for the specified array as <code>Arrays.hashCode()</code> would do. Unfortunately, the
130 * mentioned method is only available for Java 1.5 and later.
131 *
132 * @param items The array for which to compute the hash code, may be <code>null</code>.
133 * @return The hash code for the array.
134 */
135 private static int hashCode( Object[] items )
136 {
137 int hash = 0;
138 if ( items != null )
139 {
140 hash = 1;
141 for ( int i = 0; i < items.length; i++ )
142 {
143 Object item = items[i];
144 hash = 31 * hash + ( item == null ? 0 : item.hashCode() );
145 }
146 }
147 return hash;
148 }
149
150 /**
151 * This tells the system if the results are cacheable at all. Keep in mind that during forked builds and other
152 * things, a given rule may be executed more than once for the same project. This means that even things that change
153 * from project to project may still be cacheable in certain instances.
154 *
155 * @return <code>true</code> if rule is cacheable
156 */
157 public boolean isCacheable()
158 {
159 return true;
160 }
161
162 /**
163 * If the rule is cacheable and the same id is found in the cache, the stored results are passed to this method to
164 * allow double checking of the results. Most of the time this can be done by generating unique ids, but sometimes
165 * the results of objects returned by the helper need to be queried. You may for example, store certain objects in
166 * your rule and then query them later.
167 *
168 * @param cachedRule the cached rule
169 * @return <code>true</code> if the stored results are valid for the same id.
170 */
171 public boolean isResultValid( EnforcerRule cachedRule )
172 {
173 return true;
174 }
175 }