1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 27 import java.util.logging.Logger ; 28 import java.util.Hashtable ; 29 import com.sun.logging.LogDomains; 30 import com.sun.enterprise.config.ConfigBean; 31 32 44 45 46 47 public class AttrType { 48 String name; 49 String type; 50 boolean optional = false; Hashtable _specRules; 53 54 final static protected Logger _logger = LogDomains.getLogger(LogDomains.APPVERIFY_LOGGER); 55 public AttrType(final String name, final String type, final boolean optional) { 56 this.name = name; 57 this.type = type; 58 this.optional = optional; 59 _specRules = new Hashtable (); 60 } 61 62 63 public String getName() { 64 return name; 65 } 66 67 public String getType() { 68 return type; 69 } 70 71 public boolean getOptional(){ 72 return optional; 73 } 74 75 76 void addRuleValue(String ruleName, Object ruleValue) 77 { 78 if(ruleValue!=null) 79 _specRules.put(ruleName, ruleValue); 80 81 } 82 83 Object getRuleValue(String ruleName) 84 { 85 return _specRules.get(ruleName); 86 } 87 88 public void validate(final Object value, final ValidationContext valCtx) 89 { 90 if (!optional && null == value){ 92 reportAttributeError(valCtx, "nullValueForMandatoryAttribute", 93 "Attribute ''{0}'' is mandatory. A null value is not allowed", 94 new Object []{name}); 95 } 96 validateKeyChanges(value, valCtx); 98 99 validateUniqueness(value, valCtx); 101 102 validateReferences(value, valCtx); 104 105 } 106 107 private void validateKeyChanges(final Object value, final ValidationContext valCtx) 108 { 109 String key = valCtx.getPrimaryKeyName(); 110 if (key == null || !key.equals(name)) 111 return; 112 113 if(valCtx.isUPDATE()) 114 { 115 if(name.equals(valCtx.name)) 116 { 117 reportAttributeError(valCtx, "primarykeychangeattempt", 119 "Cannot change a primary key", 120 new Object [] {valCtx.name}); 121 } 122 } 123 else if(valCtx.isSET() || valCtx.isADD() || valCtx.isVALIDATE()) 124 { 125 if ( (valCtx.classObject instanceof ConfigBean) && (valCtx.value instanceof ConfigBean) ) 126 { 127 128 if(!isAttrValueUniqueAmongSiblings((ConfigBean)valCtx.classObject, (ConfigBean)valCtx.value, key, null)) 129 { 130 reportAttributeError(valCtx, "keyDuplication", 131 "Element with the same attribute value ({0} = {1}) already exists.", 132 new Object [] {key, value}); 133 } 134 } 135 136 } 137 else if(valCtx.isDELETE()) 138 return; 140 } 141 142 private void validateUniqueness(final Object value, final ValidationContext valCtx) 143 { 144 String [] belongsTo = (String [])getRuleValue("belongs-to"); if (belongsTo==null || belongsTo.length==0) 146 return; 147 NameListMgr nameListMgr = valCtx.getNameListMgr(); 148 if(nameListMgr==null) 149 return; 150 String xpathForValue = getFutureXPathForValidatingAttribute(valCtx); 151 String [] valuesToTest = ((String )value).split(","); 152 153 for(int i=0; i<belongsTo.length; i++) 154 { 155 if(belongsTo[i]!=null) 156 { 157 for(int j=0; j<valuesToTest.length; j++) 158 { 159 if(valCtx.isDELETE()) 160 { 161 if(nameListMgr.isValueInNameDomainReferenced( 162 belongsTo[i], valuesToTest[j], xpathForValue)) 163 { 164 String elementPrintName = GenericValidator. 165 getConfigElementPrintName(xpathForValue, true, true); 166 String refXPath= nameListMgr.getDomainValueReferenceeXPath( 167 belongsTo[i], valuesToTest[j], xpathForValue); 168 String refPrintName = GenericValidator. 169 getConfigElementPrintName(refXPath, true, true); 170 reportAttributeError(valCtx, "isReferenced", 171 "Element {0} can not be deleted because it is referenced from {1}", 172 new Object [] {elementPrintName, refPrintName}); 173 } 174 } 175 else 176 { 177 if(!nameListMgr.isUniqueValueInNameDomain(belongsTo[i], 178 valuesToTest[j], 179 xpathForValue)) 180 { 181 reportAttributeError(valCtx, "notUniqueInList", 182 "Attribute value ({0} = {1}) is not unique in {2}.", 183 new Object [] {name, valuesToTest[j], 184 nameListMgr.getDescriptionForNameDomain(belongsTo[i])}); 185 } 186 } 187 } 188 } 189 } 190 } 191 192 private void validateReferences(final Object value, final ValidationContext valCtx) 193 { 194 String [] referencesTo = (String [])getRuleValue("references-to"); if (referencesTo==null || referencesTo.length==0 || value==null) 196 return; 197 NameListMgr nameListMgr = valCtx.getNameListMgr(); 198 if(nameListMgr==null) 199 return; 200 if(valCtx.isDELETE()) 201 return; 203 String xpathForValue = getFutureXPathForValidatingAttribute(valCtx); 204 String [] valuesToTest = ((String )value).split(","); 205 206 for(int i=0; i<referencesTo.length; i++) 207 { 208 if(referencesTo[i]!=null) 209 { 210 for(int j=0; j<valuesToTest.length; j++) 211 { 212 if(!nameListMgr.isValueInNameDomain(referencesTo[i], 214 valuesToTest[j], 215 xpathForValue)) 216 { 217 reportAttributeError(valCtx, "notFoundInList", 219 "Attribute value ({0} = {1}) is not found in {2}.", 220 new Object [] {name, valuesToTest[j], nameListMgr.getDescriptionForNameDomain(referencesTo[i])}); 221 } 222 } 223 } 224 } 225 } 226 229 private boolean isAttrValueUniqueAmongSiblings(ConfigBean parent, ConfigBean cb, String attrName, String newValue) 230 { 231 if(parent==null || cb==null) 232 return true; 233 ConfigBean[] cbs = parent.getChildBeansByName(cb.name()); 234 String value = newValue!=null?newValue:cb.getAttributeValue(attrName); 235 if(cbs == null) 236 return true; 237 for(int i=0; i<cbs.length; i++) 238 { 239 if( ((Object )cbs[i] != (Object )cb) && 240 value.equals(cbs[i].getAttributeValue(attrName))) 241 return false; 242 } 243 return true; 244 } 245 246 249 public ConfigBean[] getAllSiblingsForConfigBean(ConfigBean cb) 250 { 251 ConfigBean parent = (ConfigBean)cb.parent(); 252 if(parent==null) 253 return new ConfigBean[]{cb}; 254 else 255 return parent.getChildBeansByName(cb.name()); 256 } 257 258 261 private String [] getAttrValuesFromSiblings(ConfigBean cb, String attrName, boolean bIncludingThis) 262 { 263 ConfigBean[] cbs = getAllSiblingsForConfigBean(cb); 264 if(cbs == null) 265 return new String [0]; 266 int iStrsLen = cbs.length; 267 if(!bIncludingThis) 268 iStrsLen--; 269 if(iStrsLen<=0) 270 return new String [0]; 271 String [] strs = new String [iStrsLen]; 272 int iStr = 0; 273 for(int i=0; i<cbs.length; i++) 274 { 275 if(bIncludingThis || (Object )cbs[i]!=(Object )cb) 276 strs[iStr++] = cbs[i].getAttributeValue(attrName); 277 } 278 return strs; 279 } 280 protected String getFutureXPathForValidatingAttribute(ValidationContext valCtx) 281 { 282 if(valCtx.isSET() || valCtx.isADD() || valCtx.isVALIDATE()) 283 { 284 String [] tokens = XPathHelper.extractTokens(((ConfigBean)valCtx.value).getAbsoluteXPath("")); 285 if(valCtx.classObject==null) { 287 return "/" + tokens[tokens.length-1] + "/@" + name; 288 } 289 return ((ConfigBean)valCtx.classObject).getXPath() + "/" + 290 tokens[tokens.length-1] + "/@" + name; 291 } 292 else if(valCtx.isUPDATE()) 293 { 294 return ((ConfigBean)valCtx.classObject).getXPath() + "/@" + name; 295 } 296 else if(valCtx.isDELETE()) 297 { 298 if(valCtx.value instanceof ConfigBean) 299 return ((ConfigBean)valCtx.value).getXPath() + "/@" + name; 300 } 301 return null; 302 } 303 protected String getValueForAttribute(String attrName, ValidationContext valCtx) 304 { 305 if(attrName==null || valCtx.getTargetBean()==null) 306 return null; 307 if(attrName.startsWith("@")) 308 attrName = attrName.substring(1); 309 return valCtx.getTargetBean().getAttributeValue(attrName); 310 } 311 312 protected void reportAttributeError(ValidationContext valCtx, 313 String msgNameSuffix, String defaultMsg, Object [] values) 314 { 315 ReportHelper.reportAttributeError(valCtx, 316 msgNameSuffix, defaultMsg, values); 317 318 } 319 } 320 321 | Popular Tags |