View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2013-9-27 下午07:54:41
4    */
5   package gboat2.web.service;
6   
7   import gboat2.web.Activator;
8   import gboat2.web.exception.ValidateException;
9   import gboat2.web.validate.AbstractValidator;
10  import gboat2.web.validators.NomalUserValidator;
11  import gboat2.web.validators.NotNullValidator;
12  import gboat2.web.validators.SuperValidator;
13  
14  import java.util.HashMap;
15  import java.util.List;
16  import java.util.Map;
17  import java.util.concurrent.CopyOnWriteArrayList;
18  
19  import javax.annotation.PostConstruct;
20  
21  
22  /**
23   * 
24   * 内部用户继承此类来实现验证逻辑和用户权限的转换
25   * @author zhangxj-a
26   * @since jdk1.6
27   * @date 2013-9-28
28   *  
29   */
30  public abstract class AbstractUserAuthService implements IUserAuthService {
31  	
32  	private static volatile List<AbstractValidator> validateList = new CopyOnWriteArrayList<AbstractValidator>();
33  
34  
35  	//验证超级用户和普通用户
36  	static {
37  		validateList.add(new NotNullValidator(Activator.LOCAL_BUNDLE.getBundleContext()));
38  		validateList.add(new SuperValidator(Activator.LOCAL_BUNDLE.getBundleContext()));
39  		validateList.add(new NomalUserValidator(Activator.LOCAL_BUNDLE.getBundleContext()));
40  	}
41  	
42  	
43  	@Override
44  	public final boolean validateUser(String userName, String password) throws ValidateException {
45  		Map<String,Object> paraMap = new HashMap<String,Object>();
46  		paraMap.put("username", userName);
47  		paraMap.put("password", password);
48  		for (AbstractValidator validate : validateList) {
49  			validate.validate(paraMap);
50  		}
51  		return true;
52  	}
53  	
54  	
55  	/**
56  	 * 有扩展需求:通过调用此方法增加
57  	 * @param validate
58  	 */
59  	protected void addValidator(AbstractValidator validate){
60  		validateList.add(validate);
61  	}
62  	
63  	/**
64  	 * 移除自己的验证
65  	 * @param validate
66  	 */
67  	protected void removeValidator(AbstractValidator validate){
68  		validateList.remove(validate);
69  	}
70  	
71  	/**
72  	 * 功能:初始化方法
73  	 * 复写此方法可完成此服务的一些初始化操作:比如扩展的验证器
74  	 * eg.  protected void init(){addValidator(new new NotNullValidator(Activator.LOCAL_BUNDLE.getBundleContext()) ) }
75  	 * 注意:复写时只复写方法即可不用带注解:PostConstruct;符合多态原理
76  	 */
77  	@PostConstruct
78  	protected void init(){
79  	}
80  
81  }