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