1 23 24 package com.sun.enterprise.admin.servermgmt; 25 26 import java.io.File ; 27 28 import com.sun.enterprise.util.io.FileUtils; 29 import com.sun.enterprise.util.i18n.StringManager; 30 31 39 public class FileValidator extends Validator 40 { 41 44 public static String validConstraints = "drwx"; 45 46 49 private static final StringManager strMgr = 50 StringManager.getManager(FileValidator.class); 51 52 55 private String constraints = "r"; 56 57 64 public FileValidator(String name, String constraints) 65 { 66 super(name, java.lang.String .class); 67 68 if (isValidConstraints(constraints)) 69 { 70 this.constraints = constraints; 71 } 72 } 73 74 77 public String getConstraints() 78 { 79 return constraints; 80 } 81 82 86 public String setConstraints(String constraints) 87 { 88 if (isValidConstraints(constraints)) 89 { 90 this.constraints = constraints; 91 } 92 return this.constraints; 93 } 94 95 100 public void validate(Object str) throws InvalidConfigException 101 { 102 super.validate(str); 103 new StringValidator(getName()).validate(str); 104 File f = new File ((String )str); 105 validateConstraints(f); 106 } 107 108 111 void validateConstraints(File file) throws InvalidConfigException 112 { 113 final File f = FileUtils.safeGetCanonicalFile(file); 114 final String constriants = getConstraints(); 115 char[] ca = constriants.toCharArray(); 116 for (int i = 0; i < ca.length; i++) 117 { 118 switch (ca[i]) 119 { 120 case 'r' : 121 if (!f.canRead()) 122 { 123 throw new InvalidConfigException( 124 strMgr.getString("fileValidator.no_read", 125 f.getAbsolutePath())); 126 } 127 break; 128 case 'w' : 129 if (!f.canWrite()) 130 { 131 throw new InvalidConfigException( 132 strMgr.getString("fileValidator.no_write", 133 f.getAbsolutePath())); 134 } 135 break; 136 case 'd' : 137 if (!f.isDirectory()) 138 { 139 throw new InvalidConfigException( 140 strMgr.getString("fileValidator.not_a_dir", 141 f.getAbsolutePath())); 142 } 143 break; 144 case 'x' : 145 break; 147 default : 148 break; 149 } 150 } 151 } 152 153 159 boolean isValidConstraints(String constraints) 160 { 161 if (constraints == null) { return false; } 162 final int length = constraints.length(); 163 if ((length == 0) || (length > 4)) { return false; } 164 boolean isValid = true; 165 for (int i = 0; i < length; i++) 166 { 167 char ch = constraints.charAt(i); 168 switch (ch) 169 { 170 case 'r' : 171 case 'w' : 172 case 'x' : 173 case 'd' : 174 continue; 175 default : 176 isValid = false; 177 break; 178 } 179 } 180 return isValid; 181 } 182 } 183 | Popular Tags |