1 19 20 package com.sslexplorer.boot; 21 22 import java.io.Serializable ; 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.List ; 26 import java.util.Properties ; 27 import java.util.StringTokenizer ; 28 29 49 public class DefaultPropertyDefinition implements PropertyDefinition, Comparable <PropertyDefinition>, Serializable { 50 51 52 53 55 protected int type = TYPE_STRING, category; 56 protected String name, typeMeta; 57 protected String defaultValue; 58 protected int sortOrder; 59 protected Object typeMetaObject; 60 protected String messageResourcesKey; 61 protected boolean hidden; 62 protected String validationString; 63 protected transient PropertyClass propertyClass; 64 protected boolean restartRequired; 65 66 static HashMap <String , PropertyValidator> validators = new HashMap <String , PropertyValidator>(); 68 69 72 public DefaultPropertyDefinition() { 73 this(TYPE_UNDEFINED, null, null, 0, null, 0, false); 74 } 75 76 87 public DefaultPropertyDefinition(int type, String name, String typeMeta, int category, String defaultValue, 88 int sortOrder, boolean hidden) { 89 this(type, name, typeMeta, category, defaultValue, sortOrder, hidden, null); 90 } 91 92 104 public DefaultPropertyDefinition(int type, String name, String typeMeta, int category, String defaultValue, 105 int sortOrder, boolean hidden, String validationString) { 106 this(type, name, typeMeta, category, defaultValue, sortOrder, "properties", hidden, validationString); 107 } 108 109 121 public DefaultPropertyDefinition(int type, String name, String typeMeta, int category, String defaultValue, 122 int sortOrder, String messageResourcesKey, boolean hidden) { 123 this(type, name, typeMeta, category, defaultValue, sortOrder, messageResourcesKey, hidden, null); 124 } 125 126 139 public DefaultPropertyDefinition(int type, String name, String typeMeta, int category, String defaultValue, 140 int sortOrder, String messageResourcesKey, boolean hidden, String validationString) { 141 this.type = type; 142 this.name = name; 143 this.typeMeta = Util.trimmedOrBlank(typeMeta); 144 this.category = category; 145 this.defaultValue = defaultValue; 146 this.sortOrder = sortOrder; 147 this.hidden = hidden; 148 this.messageResourcesKey = messageResourcesKey == null ? "properties" : messageResourcesKey; 149 this.validationString = validationString; 150 } 151 152 protected void check() { 153 154 switch (type) { 155 case TYPE_TIME_IN_MS: 156 case TYPE_INTEGER: 157 if (this.validationString == null || this.validationString.equals("")) { 158 this.validationString = "com.sslexplorer.input.validators.IntegerValidator(replacementVariables=" + getPropertyClass().isSupportsReplacementVariablesInValues() + ")"; 159 } 160 break; 161 case TYPE_BOOLEAN: { 162 List <String > l = new ArrayList <String >(); 163 StringTokenizer t = new StringTokenizer (typeMeta == null || typeMeta.equals("") ? "true,false" : typeMeta, ","); 164 while (t.hasMoreTokens()) { 165 l.add(t.nextToken()); 166 } 167 typeMetaObject = l; 168 break; 169 } 170 case TYPE_LIST: { 171 List <TypeMetaListItem> l = new ArrayList <TypeMetaListItem>(); 172 StringTokenizer t = new StringTokenizer (typeMeta, ","); 173 while (t.hasMoreTokens()) { 174 l.add(new TypeMetaListItem(t.nextToken(), this.messageResourcesKey)); 175 } 176 typeMetaObject = l; 177 break; 178 } 179 } 180 } 181 182 187 public String getMessageResourcesKey() { 188 return messageResourcesKey; 189 } 190 191 196 public int getCategory() { 197 return category; 198 } 199 200 203 public void setCategory(int category) { 204 this.category = category; 205 } 206 207 212 public String getName() { 213 return name; 214 } 215 216 222 public void setName(String name) { 223 if(this.name != null) { 224 throw new IllegalStateException ("Already set."); 225 } 226 this.name = name; 227 } 228 229 234 public int getType() { 235 return type; 236 } 237 238 244 public void setType(int type) { 245 if(this.type != TYPE_UNDEFINED) { 246 throw new IllegalStateException ("Type is already set."); 247 } 248 this.type = type; 249 } 250 251 256 public String getTypeMeta() { 257 return typeMeta; 258 } 259 260 265 public void setTypeMeta(String typeMeta) { 266 this.typeMeta = Util.trimmedOrBlank(typeMeta); 267 } 268 269 274 public String getDefaultValue() { 275 return defaultValue==null ? "" : defaultValue; 276 } 277 278 283 public void setDefaultValue(String defaultValue) { 284 this.defaultValue = defaultValue==null ? "" : defaultValue; 285 } 286 287 292 public int getSortOrder() { 293 return sortOrder; 294 } 295 296 301 public void setSortOrder(int sortOrder) { 302 this.sortOrder = sortOrder; 303 } 304 305 310 public Object getTypeMetaObject() { 311 return typeMetaObject; 312 } 313 314 319 public boolean isHidden() { 320 return hidden; 321 } 322 323 326 public void validate(String value, ClassLoader classLoader) throws CodedException { 327 if (validationString == null || validationString.equals("")) { 328 return; 329 } 330 int idx = validationString.indexOf('('); 331 try { 332 PropertyValidator v = getValidator(classLoader, idx == -1 ? validationString : validationString.substring(0, idx)); 333 Properties p = null; 334 if (idx != -1) { 335 if (!validationString.endsWith(")")) { 336 throw new Exception ("Validation string in incorrect format, missing )."); 337 } 338 PropertyList pl = new PropertyList(validationString.substring(idx + 1, validationString.length() - 1), ','); 339 p = pl.getAsNameValuePairs(); 340 } 341 v.validate(this, value, p); 342 } catch (CodedException ce) { 343 throw ce; 344 } catch (Exception e) { 345 throw new Error ("Failed to create validator using '" + validationString + "'. ", e); 346 } 347 } 348 349 354 public int compareTo(PropertyDefinition obj) { 355 int i = new Integer (getCategory()).compareTo(new Integer (obj.getCategory())); 356 return i == 0 ? new Integer (getSortOrder()).compareTo(new Integer (obj.getSortOrder())) : i; 357 } 358 359 362 public boolean isRestartRequired() { 363 return restartRequired; 364 } 365 366 371 public void setRestartRequired(boolean restartRequired) { 372 this.restartRequired = restartRequired; 373 } 374 375 380 public String getValidationString() { 381 return validationString; 382 } 383 384 387 public void setValidationString(String validationString) { 388 this.validationString = validationString; 389 } 390 391 394 public PropertyClass getPropertyClass() { 395 return propertyClass; 396 } 397 398 401 public void init(PropertyClass propertyClass) { 402 this.propertyClass = propertyClass; 403 check(); 404 } 405 406 409 public String toString() { 410 return "[PropertyDefinition name='" + name 411 + "' type=" 412 + type 413 + " category=" 414 + category 415 + " defaultValue='" 416 + defaultValue 417 + "' typeMeta='" 418 + typeMeta 419 + "' sortOrder=" 420 + sortOrder 421 + " messageResourcesKey='" 422 + messageResourcesKey 423 + "' validationString='" 424 + validationString 425 + "'"; 426 } 427 428 static PropertyValidator getValidator(ClassLoader classLoader, String className) throws InstantiationException , IllegalAccessException , 429 ClassNotFoundException { 430 synchronized (validators) { 431 PropertyValidator pv = validators.get(className); 432 if (pv == null) { 433 pv = (PropertyValidator) Class.forName(className, true, classLoader).newInstance(); 434 validators.put(className, pv); 435 } 436 return pv; 437 } 438 } 439 } | Popular Tags |