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;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.maven.enforcer.rule.api.EnforcerRule;
027import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
028import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
029
030/**
031 * Contains the common code to compare an array of files against a requirement.
032 *
033 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
034 */
035public abstract class AbstractRequireFiles
036    extends AbstractStandardEnforcerRule
037{
038
039    /** Array of files to check. */
040    private File[] files;
041
042    /** if null file handles should be allowed. If they are allowed, it means treat it as a success. */
043    private boolean allowNulls = false;
044
045    // check the file for the specific condition
046    /**
047     * Check one file.
048     *
049     * @param file the file
050     * @return <code>true</code> if successful
051     */
052    abstract boolean checkFile( File file );
053
054    // return standard error message
055    /**
056     * Gets the error msg.
057     *
058     * @return the error msg
059     */
060    abstract String getErrorMsg();
061
062    @Override
063    public void execute( EnforcerRuleHelper helper )
064        throws EnforcerRuleException
065    {
066
067        if ( !allowNulls && files.length == 0 )
068        {
069            throw new EnforcerRuleException( "The file list is empty and Null files are disabled." );
070        }
071
072        List<File> failures = new ArrayList<File>();
073        for ( File file : files )
074        {
075            if ( !allowNulls && file == null )
076            {
077                failures.add( file );
078            }
079            else if ( !checkFile( file ) )
080            {
081                failures.add( file );
082            }
083        }
084
085        // if anything was found, log it with the optional message.
086        if ( !failures.isEmpty() )
087        {
088            String message = getMessage();
089            
090            StringBuilder buf = new StringBuilder();
091            if ( message != null )
092            {
093                buf.append( message + "\n" );
094            }
095            buf.append( getErrorMsg() );
096
097            for ( File file : failures )
098            {
099                if ( file != null )
100                {
101                    buf.append( file.getAbsolutePath() + "\n" );
102                }
103                else
104                {
105                    buf.append( "(an empty filename was given and allowNulls is false)\n" );
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 ( int i = 0; i < items.length; i++ )
133            {
134                Object item = items[i];
135                hash = 31 * hash + ( item == null ? 0 : item.hashCode() );
136            }
137        }
138        return hash;
139    }
140
141    @Override
142    public boolean isCacheable()
143    {
144        return true;
145    }
146
147    @Override
148    public boolean isResultValid( EnforcerRule cachedRule )
149    {
150        return true;
151    }
152
153    public File[] getFiles()
154    {
155        return files;
156    }
157
158    public void setFiles( File[] files )
159    {
160        this.files = files;
161    }
162
163    public boolean isAllowNulls()
164    {
165        return allowNulls;
166    }
167
168    public void setAllowNulls( boolean allowNulls )
169    {
170        this.allowNulls = allowNulls;
171    }
172}