KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > faces > ValidatorManager


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.faces;
6 import java.util.HashMap JavaDoc;
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 /**
12  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
13  * @since Aug 13, 2004
14  * @version $Id: ValidatorManager.java,v 1.2 2004/09/09 03:22:19 tuan08 Exp $
15  */

16 public class ValidatorManager extends HashMap JavaDoc {
17   private boolean cacheValidator_ ;
18   
19   public ValidatorManager(ConfigurationManager manager) throws Exception JavaDoc {
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 JavaDoc clazz) {
30     if(cacheValidator_) return createValidator(clazz) ;
31     String JavaDoc 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 JavaDoc clazz) {
42     Validator result = null ;
43     try {
44       result = (Validator) clazz.newInstance() ;
45     } catch (Exception JavaDoc ex) {
46       ex.printStackTrace() ;
47     }
48     return result ;
49   }
50 }
Popular Tags