View Javadoc
1   /******************************************************************************
2    * BSAccountImpl - This object implements the BSAccount API
3    * 
4    * BSAccount - BuckoSoft Web Account Manager 
5    * Copyright(c) 2007 - Dick Balaska and BuckoSoft, Corp.
6    * 
7    */
8   
9   package com.buckosoft.BSAccount;
10  
11  import javax.servlet.http.HttpServletRequest;
12  
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  import org.springframework.web.util.WebUtils;
16  
17  import com.buckosoft.BSAccount.db.Database;
18  import com.buckosoft.BSAccount.db.DatabaseImpl;
19  import com.buckosoft.BSAccount.domain.BSAccount;
20  import com.buckosoft.BSAccount.domain.BSAccountUser;
21  import com.buckosoft.BSAccount.domain.BSAccountUserWebSession;
22  import com.buckosoft.BSAccount.domain.UserFactory;
23  
24  /** This class implements the BSAccount API.
25   * @author dick
26   *
27   */
28  public class BSAccountManImpl implements BSAccountMan {
29  	protected final Log log = LogFactory.getLog(getClass());
30  	private	Database	db;
31  	private	UserFactory	userFactory = null;
32  	private	String		appName = "unknownApp";
33  	private	boolean		helloForwards = false;
34  
35  	/* (non-Javadoc)
36  	 * @see com.buckosoft.BSAccount.BSAccountMan#userFactory()
37  	 */
38  	public 	void	setUserFactory(UserFactory userFactory) {
39  		this.userFactory = userFactory;
40  	}
41  
42  	/* (non-Javadoc)
43  	 * @see com.buckosoft.BSAccount.BSAccountMan#getNewUser()
44  	 */
45  	public BSAccountUser getNewUser() {
46  		return(this.userFactory.getNewUser());
47  	}
48  	public BSAccountUser	getUser(BSAccount account) {
49  		return(this.userFactory.getUser(account));
50  	}
51  
52  	/* (non-Javadoc)
53  	 * @see com.buckosoft.BSAccount.BSAccountMan#getNewUserWebSession(com.buckosoft.BSAccount.domain.BSAccountUser,java.lang.String)
54  	 */
55  	public BSAccountUserWebSession getNewUserWebSession(BSAccountUser user) {
56  		return(this.userFactory.getNewUserWebSession(user));
57  	}
58  
59  
60  	/* (non-Javadoc)
61  	 * @see com.buckosoft.BSAccount.BSAccountMan#getUserWebSession(javax.servlet.http.HttpServletRequest)
62  	 */
63  	public BSAccountUserWebSession getUserWebSession(HttpServletRequest request) {
64  		BSAccountUserWebSession userWebSession = (BSAccountUserWebSession) WebUtils.getSessionAttribute(request, "userWebSession");
65  		BSAccountUser	user;
66  		if (userWebSession == null) {
67  			user = this.getNewUser();
68  			userWebSession = this.getNewUserWebSession(user);
69  			request.getSession().setAttribute("userWebSession", userWebSession);
70  		}
71  		String s = request.getParameter("jumpApp");
72  		if (s != null) {
73  			long l = Long.parseLong(s);
74  			int id = db.getAppJumperUser(l);
75  			if (id == 0) {
76  				log.warn("jumpApp failed for long " + l);
77  				return(userWebSession);
78  			}
79  			db.deleteAppJumper(id, l);
80  			BSAccount a = db.getAccount(id);
81  			userWebSession.getUser().setAccount(a);
82  			log.info("Coercing login for " + a.getUsername());
83  		}
84  		return(userWebSession);
85  	}
86  
87  	@Override
88  	public long getAppJumper(BSAccount account) {
89  		if (!account.isRegisteredUser())
90  			return(0);
91  		int i = (int)(Math.random() * Integer.MAX_VALUE);
92  		long l = (int)(Math.random() * Integer.MAX_VALUE) + (long)((long)i << Integer.SIZE);
93  		db.setAppJumper(account.getUserId(), l);
94  		return l;
95  	}
96  
97  	@Override
98  	public boolean isUserWebSession(HttpServletRequest request) {
99  		BSAccountUserWebSession userWebSession = (BSAccountUserWebSession) WebUtils.getSessionAttribute(request, "userWebSession");
100 		return(userWebSession != null);
101 	}
102 
103 	/* (non-Javadoc)
104 	 * @see com.buckosoft.BSAccount.BSAccountMan#getAppName()
105 	 */
106 	public String getAppName() {
107 		return(this.appName);
108 	}
109 
110 	/* (non-Javadoc)
111 	 * @see com.buckosoft.BSAccount.BSAccountMan#setAppName(java.lang.String)
112 	 */
113 	public void setAppName(String appName) {
114 		this.appName = appName;
115 	}
116 
117 
118 	/* (non-Javadoc)
119 	 * @see com.buckosoft.BSAccount.BSAccountMan#isHelloForwards()
120 	 */
121 	public boolean isHelloForwards() {
122 		return(this.helloForwards);
123 	}
124 
125 	/* (non-Javadoc)
126 	 * @see com.buckosoft.BSAccount.BSAccountMan#setHelloForwards(boolean)
127 	 */
128 	public void setHelloForwards(boolean helloForwards) {
129 		this.helloForwards = helloForwards;
130 	}
131 
132 	/** Call setInit() after instantiating and configuring the object */ 
133 	public void setInit(String unused) {
134 		log.info("Init");
135 
136 //		ClassPathResource res = new ClassPathResource("BSAccountDatabase.xml");
137 //		XmlBeanFactory factory = new XmlBeanFactory(res);
138 		db = new DatabaseImpl();
139 		if (this.userFactory == null) {
140 			this.userFactory = new SimpleUserFactory();
141 			log.info("Using default SimpleUserFactory");
142 		}
143 
144 		log.info("Init done");
145 	}
146 
147 	/* (non-Javadoc)
148 	 * @see com.buckosoft.BSAccount.BSAccount#getAccount(java.lang.String, java.lang.String)
149 	 */
150 	public BSAccount getAccount(String username, String password) {
151 		log.info("Get account for " + username + " / xxx");
152 		BSAccount account = db.getAccount(username, password);
153 		return(account);
154 	}
155 
156 }