View Javadoc

1   /*
2    * $Id: User.java,v 1.6 2006/01/10 18:26:32 tymoteusz Exp $
3    * 
4    */ 
5   
6   package net.sourceforge.jdbdump.connect;
7   
8   import java.util.Properties;
9   import java.util.regex.Matcher;
10  import java.util.regex.Pattern;
11  
12  public class User extends Properties {
13  
14  	private static final long serialVersionUID = 849771558466348087L;
15  	private Properties userData = new Properties();
16  
17  	/***
18  	 * for serialization purpouses only 
19  	 */
20  	private User(){
21  	}
22  
23  	/***
24  	 *  
25  	 * @param login User's login name
26  	 * @param password User's password
27  	 * @param email User's e-mail address. May be null, but if specified has to be valid.
28  	 * @throws IllegalArgumentException Invalid e-mail address
29  	 */
30  	public User(String login, String password, String email) throws IllegalArgumentException{
31  		if ((email!=null) && !isValidEmail(email)){
32  			throw new IllegalArgumentException();
33  		}
34  		userData.setProperty("login", login);
35  		userData.setProperty("password", password);
36  		if(email != null){
37  			userData.setProperty("email", email);
38  		}
39  	}
40  	
41  	public Object setLogin(String login) throws IllegalArgumentException{
42  		if((login == null) || login.length()<1){
43  			throw new IllegalArgumentException();
44  		}
45  		String old = userData.getProperty("login");
46  		userData.setProperty("login", login);
47  		return old;
48  	}
49  	
50  	public Object setPassword(String password) throws IllegalArgumentException{
51  		if((password == null) || password.length()<1){
52  			throw new IllegalArgumentException();
53  		}
54  		String old = userData.getProperty("password");
55  		userData.setProperty("password", password);
56  		return old;
57  	}
58  	
59  	public Object setEmail(String email) throws IllegalArgumentException{
60  		if((email!=null) && !isValidEmail(email)){
61  			throw new IllegalArgumentException();
62  		}
63  		String old = userData.getProperty("email");
64  		userData.setProperty("email", email);
65  		return old;
66  	}
67  	
68  	public String getLogin(){
69  		return userData.getProperty("password");
70  	}
71  	
72  	public String getPassword(){
73  		return userData.getProperty("password");
74  	}
75  	
76  	public String getEmail(){
77  		return userData.getProperty("email");
78  	}
79  	
80  	
81  	/***
82  	 * Performs simple regex email validation
83  	 * 
84  	 * @param email e-mail adress to validate
85  	 * @return true if address is valid, else false
86  	 */
87  	private static boolean isValidEmail(String email){
88  		Pattern p = Pattern.compile("[//p{Alnum}//._-]+@[//p{Alnum}]{[//p{Alnum}//._-]*[//p{Alnum}]}?");
89  		Matcher m = p.matcher(email);
90  		return m.matches();
91  	}
92  }