1 package org.apache.fulcrum.security.torque;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.sql.Connection;
22 import java.util.List;
23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.fulcrum.security.entity.User;
27 import org.apache.fulcrum.security.spi.AbstractUserManager;
28 import org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity;
29 import org.apache.fulcrum.security.util.DataBackendException;
30 import org.apache.fulcrum.security.util.UnknownEntityException;
31 import org.apache.fulcrum.security.util.UserSet;
32 import org.apache.torque.NoRowsException;
33 import org.apache.torque.TooManyRowsException;
34 import org.apache.torque.TorqueException;
35 import org.apache.torque.util.Transaction;
36
37
38
39
40
41
42
43 public abstract class TorqueAbstractUserManager extends AbstractUserManager {
44
45
46 private static final long serialVersionUID = 2050218990148719292L;
47
48
49
50
51 @Override
52 public void configure(Configuration conf) throws ConfigurationException {
53 super.configure(conf);
54 }
55
56
57
58
59
60
61
62
63
64
65 protected abstract <T extends User> List<T> doSelectAllUsers(Connection con) throws TorqueException;
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 protected abstract <T extends User> T doSelectByName(String name, Connection con)
81 throws NoRowsException, TooManyRowsException, TorqueException;
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 protected abstract <T extends User> T doSelectById(Integer id, Connection con)
97 throws NoRowsException, TooManyRowsException, TorqueException;
98
99
100
101
102
103
104
105
106
107 @Override
108 public synchronized void removeUser(User user) throws DataBackendException, UnknownEntityException {
109 try {
110 ((TorqueAbstractSecurityEntity) user).delete();
111 } catch (TorqueException e) {
112 throw new DataBackendException("Removing User '" + user.getName() + "' failed", e);
113 }
114 }
115
116
117
118
119
120
121
122
123
124 @Override
125 protected synchronized <T extends User> T persistNewUser(T user) throws DataBackendException {
126 try {
127 TorqueAbstractSecurityEntity u = (TorqueAbstractSecurityEntity) user;
128 u.save();
129 } catch (Exception e) {
130 throw new DataBackendException("Adding User '" + user.getName() + "' failed", e);
131 }
132
133 return user;
134 }
135
136
137
138
139
140
141
142
143
144 @Override
145 public synchronized void saveUser(User user) throws DataBackendException, UnknownEntityException {
146 if (checkExists(user)) {
147 try {
148 TorqueAbstractSecurityEntity u = (TorqueAbstractSecurityEntity) user;
149 u.setNew(false);
150 u.save();
151 } catch (Exception e) {
152 throw new DataBackendException("Saving User '" + user.getName() + "' failed", e);
153 }
154 } else {
155 throw new UnknownEntityException("Unknown user '" + user + "'");
156 }
157 }
158
159
160
161
162
163
164
165
166
167
168
169 @Override
170 public boolean checkExists(String userName) throws DataBackendException {
171 boolean exists = false;
172
173 Connection con = null;
174
175 try {
176 con = Transaction.begin();
177
178 doSelectByName(userName, con);
179
180 Transaction.commit(con);
181 con = null;
182
183 exists = true;
184 } catch (NoRowsException e) {
185 exists = false;
186 } catch (TooManyRowsException e) {
187 throw new DataBackendException("Multiple Users with same username '" + userName + "'");
188 } catch (TorqueException e) {
189 throw new DataBackendException("Error retrieving user information", e);
190 } finally {
191 if (con != null) {
192 Transaction.safeRollback(con);
193 }
194 }
195
196 return exists;
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211 @Override
212 public <T extends User> T getUser(String userName) throws UnknownEntityException, DataBackendException {
213 T user = null;
214 Connection con = null;
215
216 try {
217 con = Transaction.begin();
218
219 user = doSelectByName(userName.toLowerCase(), con);
220
221
222 ((TorqueAbstractSecurityEntity) user).retrieveAttachedObjects(con, false);
223
224 Transaction.commit(con);
225 con = null;
226 } catch (NoRowsException e) {
227 throw new UnknownEntityException("Unknown user '" + userName + "'");
228 } catch (TooManyRowsException e) {
229 throw new DataBackendException("Multiple Users with same username '" + userName + "'");
230 } catch (TorqueException e) {
231 throw new DataBackendException("Error retrieving user information", e);
232 } finally {
233 if (con != null) {
234 Transaction.safeRollback(con);
235 }
236 }
237
238 return user;
239 }
240
241
242
243
244
245
246
247
248 @Override
249 public <T extends User> UserSet<T> getAllUsers() throws DataBackendException {
250 UserSet<T> userSet = new UserSet<T>();
251 Connection con = null;
252
253 try {
254 con = Transaction.begin();
255
256 List<User> users = doSelectAllUsers(con);
257
258 for (User user : users) {
259
260 ((TorqueAbstractSecurityEntity) user).retrieveAttachedObjects(con, false);
261
262 userSet.add(user);
263 }
264
265 Transaction.commit(con);
266 con = null;
267 } catch (TorqueException e) {
268 throw new DataBackendException("Error retrieving all users", e);
269 } finally {
270 if (con != null) {
271 Transaction.safeRollback(con);
272 }
273 }
274
275 return userSet;
276 }
277
278
279
280
281
282
283
284
285
286
287 @Override
288 public <T extends User> T getUserById(Object id) throws DataBackendException, UnknownEntityException {
289 T user;
290
291 if (id != null && id instanceof Integer) {
292 Connection con = null;
293
294 try {
295 con = Transaction.begin();
296
297 user = doSelectById((Integer) id, con);
298
299
300 ((TorqueAbstractSecurityEntity) user).retrieveAttachedObjects(con, false);
301
302 Transaction.commit(con);
303 con = null;
304 } catch (NoRowsException e) {
305 throw new UnknownEntityException("User with id '" + id + "' does not exist.", e);
306 } catch (TorqueException e) {
307 throw new DataBackendException("Error retrieving user information", e);
308 } finally {
309 if (con != null) {
310 Transaction.safeRollback(con);
311 }
312 }
313 } else {
314 throw new UnknownEntityException("Invalid user id '" + id + "'");
315 }
316
317 return user;
318 }
319
320 }