1
2
3
4
5
6
7
8
9
10
11 package gov.bnl.gums.hibernate;
12
13 import gov.bnl.gums.AccountPoolMapperDB;
14 import gov.bnl.gums.ManualAccountMapperDB;
15 import java.util.*;
16 import javax.naming.OperationNotSupportedException;
17 import net.sf.hibernate.*;
18 import net.sf.hibernate.type.StringType;
19 import net.sf.hibernate.type.Type;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 /***
24 *
25 * @author carcassi
26 */
27 public class HibernateMapping implements ManualAccountMapperDB, AccountPoolMapperDB {
28 private Log log = LogFactory.getLog(HibernateMapping.class);
29
30 private HibernatePersistenceFactory factory;
31 private String map;
32
33 /*** Creates a new instance of HibernateMapping */
34 public HibernateMapping(HibernatePersistenceFactory factory, String map) {
35 this.factory = factory;
36 this.map = map;
37 }
38
39 public String retrieveMapping(String userDN) {
40 Session session = null;
41 Transaction tx = null;
42 try {
43 log.trace("Retrieving mapping from map '" + map + "' for DN '" + userDN + "'");
44 session = factory.retrieveSessionFactory().openSession();
45 tx = session.beginTransaction();
46 Mapping map = retrieveMapping(session, tx, userDN);
47 tx.commit();
48 if (map == null) return null;
49 return map.getAccount();
50
51 } catch (Exception e) {
52 log.error("Couldn't retrieve mapping from '" + map + "' for DN '" + userDN + "'", e);
53 if (tx != null) {
54 try {
55 tx.rollback();
56 } catch (Exception e1) {
57 log.error("Hibernate error: rollback failed", e1);
58 throw new RuntimeException("Database errors: " + e.getMessage() + " - " + e1.getMessage(), e);
59 }
60 }
61 throw new RuntimeException("Database error: " + e.getMessage(), e);
62 } finally {
63 if (session != null) {
64 try {
65 session.close();
66 } catch (Exception e1) {
67 log.error("Hibernate error: couldn't close session", e1);
68 throw new RuntimeException("Database error: " + e1.getMessage(), e1);
69 }
70 }
71 }
72 }
73
74 private Mapping retrieveMapping(Session session, Transaction tx, String userDN) throws Exception{
75 Query q;
76 q = session.createQuery("FROM Mapping m WHERE m.map = ? AND m.dn = ?");
77 q.setString(0, map);
78 q.setString(1, userDN);
79 return (Mapping) q.uniqueResult();
80 }
81
82 public boolean removeMapping(String userDN) {
83 Session session = null;
84 Transaction tx = null;
85 try {
86 log.trace("Removing mapping from map '" + map + "' for DN '" + userDN + "'");
87 session = factory.retrieveSessionFactory().openSession();
88 tx = session.beginTransaction();
89 boolean result = removeMapping(session, tx, userDN);
90 tx.commit();
91 return result;
92
93 } catch (Exception e) {
94 log.error("Couldn't remove mapping from '" + map + "' for DN '" + userDN + "'", e);
95 if (tx != null) {
96 try {
97 tx.rollback();
98 } catch (Exception e1) {
99 log.error("Hibernate error: rollback failed", e1);
100 throw new RuntimeException("Database errors: " + e.getMessage() + " - " + e1.getMessage(), e);
101 }
102 }
103 throw new RuntimeException("Database error: " + e.getMessage(), e);
104 } finally {
105 if (session != null) {
106 try {
107 session.close();
108 } catch (Exception e1) {
109 log.error("Hibernate error: couldn't close session", e1);
110 throw new RuntimeException("Database error: " + e1.getMessage(), e1);
111 }
112 }
113 }
114 }
115
116 private boolean removeMapping(Session session, Transaction tx, String userDN) throws Exception {
117 int n = session.delete("FROM Mapping m WHERE m.map = ? AND m.dn = ?", new Object[] {map, userDN}, new Type[] {new StringType(), new StringType()});
118 return n > 0;
119 }
120
121 public void createMapping(String userDN, String account) {
122 Session session = null;
123 Transaction tx = null;
124 try {
125 log.trace("Creating mapping for map '" + map + "' DN '" + userDN + "' -> '" + account + "'");
126 session = factory.retrieveSessionFactory().openSession();
127 tx = session.beginTransaction();
128
129
130
131
132 createMapping(session, tx, userDN, account);
133 tx.commit();
134
135 } catch (Exception e) {
136 log.error("Couldn't create mapping to '" + map + "'", e);
137 if (tx != null) {
138 try {
139 tx.rollback();
140 } catch (Exception e1) {
141 log.error("Hibernate error: rollback failed", e1);
142 throw new RuntimeException("Database errors: " + e.getMessage() + " - " + e1.getMessage(), e);
143 }
144 }
145 throw new RuntimeException("Database error: " + e.getMessage(), e);
146 } finally {
147 if (session != null) {
148 try {
149 session.close();
150 } catch (Exception e1) {
151 log.error("Hibernate error: couldn't close session", e1);
152 throw new RuntimeException("Database error: " + e1.getMessage(), e1);
153 }
154 }
155 }
156 }
157
158 private void createMapping(Session session, Transaction tx, String userDN, String account) throws Exception {
159 Mapping hMapping = new Mapping();
160 hMapping.setMap(map);
161 hMapping.setDn(userDN);
162 hMapping.setAccount(account);
163 session.save(hMapping);
164 }
165
166 public void addAccount(String account) {
167 Session session = null;
168 Transaction tx = null;
169 try {
170 log.trace("Adding account '" + account + "' to pool '" + map + "'");
171 session = factory.retrieveSessionFactory().openSession();
172 tx = session.beginTransaction();
173
174
175
176
177 createMapping(session, tx, null, account);
178 tx.commit();
179
180 } catch (Exception e) {
181 log.error("Couldn't add account to pool '" + map + "'", e);
182 if (tx != null) {
183 try {
184 tx.rollback();
185 } catch (Exception e1) {
186 log.error("Hibernate error: rollback failed", e1);
187 throw new RuntimeException("Database errors: " + e.getMessage() + " - " + e1.getMessage(), e);
188 }
189 }
190 throw new RuntimeException("Database error: " + e.getMessage(), e);
191 } finally {
192 if (session != null) {
193 try {
194 session.close();
195 } catch (Exception e1) {
196 log.error("Hibernate error: couldn't close session", e1);
197 throw new RuntimeException("Database error: " + e1.getMessage(), e1);
198 }
199 }
200 }
201 }
202
203 public String assignAccount(String userDN) {
204 Session session = null;
205 Transaction tx = null;
206 try {
207 log.trace("Assing an account to '" + userDN + "' from pool '" + map + "'");
208 session = factory.retrieveSessionFactory().openSession();
209 tx = session.beginTransaction();
210 Query q;
211 q = session.createQuery("FROM Mapping m WHERE m.map = ? AND m.dn is null ORDER BY m.account LIMIT 1");
212 q.setString(0, map);
213 Mapping mapping = (Mapping) q.uniqueResult();
214 if (mapping == null) {
215 tx.commit();
216 return null;
217 }
218 mapping.setDn(userDN);
219 tx.commit();
220 return mapping.getAccount();
221
222 } catch (Exception e) {
223 log.error("Couldn't assign account to '" + userDN + "' from pool '" + map + "'", e);
224 if (tx != null) {
225 try {
226 tx.rollback();
227 } catch (Exception e1) {
228 log.error("Hibernate error: rollback failed", e1);
229 throw new RuntimeException("Database errors: " + e.getMessage() + " - " + e1.getMessage(), e);
230 }
231 }
232 throw new RuntimeException("Database error: " + e.getMessage(), e);
233 } finally {
234 if (session != null) {
235 try {
236 session.close();
237 } catch (Exception e1) {
238 log.error("Hibernate error: couldn't close session", e1);
239 throw new RuntimeException("Database error: " + e1.getMessage(), e1);
240 }
241 }
242 }
243 }
244
245 public void unassignUser(String userDN) {
246 Session session = null;
247 Transaction tx = null;
248 try {
249 log.trace("Unassign account for user '" + userDN + "' from pool '" + map + "'");
250 session = factory.retrieveSessionFactory().openSession();
251 tx = session.beginTransaction();
252 Query q;
253 q = session.createQuery("FROM Mapping m WHERE m.map = ? AND m.dn = ? ORDER BY m.account LIMIT 1");
254 q.setString(0, map);
255 q.setString(1, userDN);
256 Mapping mapping = (Mapping) q.uniqueResult();
257 if (mapping == null) {
258 tx.commit();
259 }
260 mapping.setDn(null);
261 tx.commit();
262
263 } catch (Exception e) {
264 log.error("Couldn't unassign account for user '" + userDN + "' from pool '" + map + "'", e);
265 if (tx != null) {
266 try {
267 tx.rollback();
268 } catch (Exception e1) {
269 log.error("Hibernate error: rollback failed", e1);
270 throw new RuntimeException("Database errors: " + e.getMessage() + " - " + e1.getMessage(), e);
271 }
272 }
273 throw new RuntimeException("Database error: " + e.getMessage(), e);
274 } finally {
275 if (session != null) {
276 try {
277 session.close();
278 } catch (Exception e1) {
279 log.error("Hibernate error: couldn't close session", e1);
280 throw new RuntimeException("Database error: " + e1.getMessage(), e1);
281 }
282 }
283 }
284 }
285
286 public String retrieveAccount(String userDN) {
287 Session session = null;
288 Transaction tx = null;
289 try {
290 log.trace("Retrieving account for user '" + userDN + "' from pool '" + map + "'");
291 session = factory.retrieveSessionFactory().openSession();
292 tx = session.beginTransaction();
293 Query q;
294 q = session.createQuery("FROM Mapping m WHERE m.map = ? AND m.dn = ? ORDER BY m.account LIMIT 1");
295 q.setString(0, map);
296 q.setString(1, userDN);
297 Mapping mapping = (Mapping) q.uniqueResult();
298 tx.commit();
299 if (mapping == null) return null;
300 return mapping.getAccount();
301
302 } catch (Exception e) {
303 log.error("Couldn't retrieve account for user '" + userDN + "' from pool '" + map + "'", e);
304 if (tx != null) {
305 try {
306 tx.rollback();
307 } catch (Exception e1) {
308 log.error("Hibernate error: rollback failed", e1);
309 throw new RuntimeException("Database errors: " + e.getMessage() + " - " + e1.getMessage(), e);
310 }
311 }
312 throw new RuntimeException("Database error: " + e.getMessage(), e);
313 } finally {
314 if (session != null) {
315 try {
316 session.close();
317 } catch (Exception e1) {
318 log.error("Hibernate error: couldn't close session", e1);
319 throw new RuntimeException("Database error: " + e1.getMessage(), e1);
320 }
321 }
322 }
323 }
324
325 public java.util.List retrieveUsersNotUsedSince(java.util.Date date) {
326 throw new UnsupportedOperationException("retrieveUsersNotUsedSince is not supported anymore");
327 }
328
329 public java.util.Map retrieveAccountMap() {
330 Session session = null;
331 Transaction tx = null;
332 try {
333 log.trace("Retrieving map for pool '" + map + "'");
334 session = factory.retrieveSessionFactory().openSession();
335 tx = session.beginTransaction();
336 Query q;
337 q = session.createQuery("FROM Mapping m WHERE m.map = ? AND m.dn is not null");
338 q.setString(0, map);
339 List mappings = (List) q.list();
340 Iterator iter = mappings.iterator();
341 Map map = new Hashtable();
342 while (iter.hasNext()) {
343 Mapping mapping = (Mapping) iter.next();
344 map.put(mapping.getDn(), mapping.getAccount());
345 }
346 tx.commit();
347 return map;
348
349 } catch (Exception e) {
350 log.error("Couldn't retrieve map for pool '" + map + "'", e);
351 if (tx != null) {
352 try {
353 tx.rollback();
354 } catch (Exception e1) {
355 log.error("Hibernate error: rollback failed", e1);
356 throw new RuntimeException("Database errors: " + e.getMessage() + " - " + e1.getMessage(), e);
357 }
358 }
359 throw new RuntimeException("Database error: " + e.getMessage(), e);
360 } finally {
361 if (session != null) {
362 try {
363 session.close();
364 } catch (Exception e1) {
365 log.error("Hibernate error: couldn't close session", e1);
366 throw new RuntimeException("Database error: " + e1.getMessage(), e1);
367 }
368 }
369 }
370 }
371
372 }