1 5 package org.exoplatform.faces; 6 import java.util.HashMap ; 7 import javax.faces.validator.Validator; 8 import org.exoplatform.container.configuration.ConfigurationManager; 9 import org.exoplatform.container.configuration.ServiceConfiguration; 10 import org.exoplatform.container.configuration.ValueParam; 11 16 public class ValidatorManager extends HashMap { 17 private boolean cacheValidator_ ; 18 19 public ValidatorManager(ConfigurationManager manager) throws Exception { 20 ServiceConfiguration sconf = manager.getServiceConfiguration(getClass()) ; 21 ValueParam param = sconf.getValueParam("cache.validator") ; 22 if(param != null) { 23 cacheValidator_ = "true".equals(param.getValue()) ; 24 } else { 25 cacheValidator_ = false ; 26 } 27 } 28 29 public Validator getValidator(Class clazz) { 30 if(cacheValidator_) return createValidator(clazz) ; 31 String key = clazz.getName() ; 32 Validator result = (Validator) get(key) ; 33 if(result != null) return result ; 34 synchronized(this) { 35 result = createValidator(clazz) ; 36 put(key, result) ; 37 } 38 return result ; 39 } 40 41 private Validator createValidator(Class clazz) { 42 Validator result = null ; 43 try { 44 result = (Validator) clazz.newInstance() ; 45 } catch (Exception ex) { 46 ex.printStackTrace() ; 47 } 48 return result ; 49 } 50 } | Popular Tags |