FileDocCategorySizeDatePackage
FileValidator.javaAPI DocGlassfish v2 API6605Thu May 17 17:51:54 BST 2007com.sun.enterprise.admin.servermgmt

FileValidator

public class FileValidator extends Validator
This class performs the file related validations such as
    existence of the file read, write & execute permissions, whether the file is a directory or a file
NOT THREAD SAFE

Fields Summary
public static final String
validConstraints
The valid constraint set.
private static final com.sun.enterprise.util.i18n.StringManager
strMgr
i18n strings manager object
private String
constraints
The current constraint set.
Constructors Summary
public FileValidator(String name, String constraints)
Constructs a new FileValidator object.

param
name The name of the entity that will be validated. This name is used in the error message.
param
constraints The constaint set that will be checked for any given file during validation.


                                                
        
    
        super(name, java.lang.String.class);

        if (isValidConstraints(constraints))
        {
            this.constraints = constraints;
        }
    
Methods Summary
public java.lang.StringgetConstraints()

return
Returns the current constraint set.

        return constraints;
    
booleanisValidConstraints(java.lang.String constraints)
Checks if the given constraint set is a subset of valid constraint set.

param
constraints
return
Returns true if the given constraint set is subset or equal to the valid constraint set - "drwx".

        if (constraints == null) { return false; }
        final int length = constraints.length();
        if ((length == 0) || (length > 4)) { return false; }
        boolean isValid = true;
        for (int i = 0; i < length; i++)
        {
            char ch = constraints.charAt(i);
            switch (ch)
            {
                case 'r" :
                case 'w" :
                case 'x" :
                case 'd" :
                    continue;
                default :
                    isValid = false;
                    break;
            }
        }
        return isValid;
    
public java.lang.StringsetConstraints(java.lang.String constraints)
Sets the current constraint set to the given set if it is a valid constriant set.

        if (isValidConstraints(constraints))
        {
            this.constraints = constraints;
        }
        return this.constraints;
    
public voidvalidate(java.lang.Object str)
Validates the given File.

param
str Must be the absolute path of the File that will be validated.
throws
InvalidConfigException

        super.validate(str);
        new StringValidator(getName()).validate(str);
        File f = new File((String)str);
        validateConstraints(f);
    
voidvalidateConstraints(java.io.File file)
Validates the given File against the current constraint set.

        final File f = FileUtils.safeGetCanonicalFile(file);
        final String constriants = getConstraints();
        char[] ca = constriants.toCharArray();
        for (int i = 0; i < ca.length; i++)
        {
            switch (ca[i])
            {
                case 'r" :
                    if (!f.canRead())
                    {
                        throw new InvalidConfigException(
                            strMgr.getString("fileValidator.no_read", 
                                             f.getAbsolutePath()));
                    }
                    break;
                case 'w" :
                    if (!f.canWrite())
                    {
                        throw new InvalidConfigException(
                            strMgr.getString("fileValidator.no_write", 
                                             f.getAbsolutePath()));
                    }
                    break;
                case 'd" :
                    if (!f.isDirectory())
                    {
                        throw new InvalidConfigException(
                            strMgr.getString("fileValidator.not_a_dir", 
                                             f.getAbsolutePath()));
                    }
                    break;
                case 'x" :
                    //do what
                    break;
                default :
                    break;
            }
        }