KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > security > AuthorityManagerBean


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on 30.06.2004
33  */

34 package com.nightlabs.ipanema.security;
35 import java.rmi.RemoteException JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.List JavaDoc;
39
40 import javax.ejb.CreateException JavaDoc;
41 import javax.ejb.EJBException JavaDoc;
42 import javax.ejb.SessionBean JavaDoc;
43 import javax.ejb.SessionContext JavaDoc;
44 import javax.jdo.JDOObjectNotFoundException;
45 import javax.jdo.PersistenceManager;
46 import javax.jdo.Query;
47
48 import com.nightlabs.ModuleException;
49 import com.nightlabs.ipanema.base.BaseSessionBeanImpl;
50 import com.nightlabs.ipanema.security.id.AuthorityID;
51 import com.nightlabs.ipanema.security.id.RoleGroupID;
52 import com.nightlabs.ipanema.security.id.UserID;
53 import com.nightlabs.ipanema.servermanager.IpanemaServerManager;
54
55 /**
56  * @ejb.bean name="ipanema/ejb/IpanemaBaseBean/AuthorityManager"
57  * jndi-name="ipanema/ejb/IpanemaBaseBean/AuthorityManager"
58  * type="Stateless"
59  *
60  * @ejb.util generate = "physical"
61  **/

62 public abstract class AuthorityManagerBean extends BaseSessionBeanImpl implements SessionBean JavaDoc
63 {
64     // TODO this should be removed. The PersistenceManagers MUST always be closed! It was for testing reasons,
65
// only (because of a JPOX bug).
66
public static final boolean CLOSE_PM = true;
67     
68     /**
69      * @see com.nightlabs.ipanema.base.BaseSessionBeanImpl#setSessionContext(javax.ejb.SessionContext)
70      */

71     public void setSessionContext(SessionContext JavaDoc sessionContext)
72             throws EJBException JavaDoc, RemoteException JavaDoc
73     {
74         super.setSessionContext(sessionContext);
75     }
76
77     /**
78      * @ejb.create-method
79      * @ejb.permission role-name="AuthorityManager-read"
80      */

81     public void ejbCreate() throws CreateException JavaDoc
82     {
83 // try
84
// {
85
// System.out.println("UserManagerBean by " + this.getPrincipalString());
86
// }
87
// catch (Exception e)
88
// {
89
// throw new CreateException(e.getMessage());
90
// }
91
}
92     /**
93      * @see javax.ejb.SessionBean#ejbRemove()
94      *
95      * @ejb.permission unchecked="true"
96      */

97     public void ejbRemove() throws EJBException JavaDoc, RemoteException JavaDoc { }
98
99     /**
100      * @ejb.interface-method
101      * @ejb.permission role-name="AuthorityManager-write"
102      **/

103     public void createAuthority(String JavaDoc authorityID, String JavaDoc name, String JavaDoc description)
104         throws SecurityException JavaDoc
105     {
106         try {
107             PersistenceManager pm = getPersistenceManager();
108             try {
109                 Authority authority = new Authority(authorityID);
110                 authority.setName(null, name);
111                 authority.setDescription(null, description);
112                 pm.makePersistent(authority);
113             } finally {
114                 if (CLOSE_PM) pm.close();
115             }
116         } catch (Exception JavaDoc x) {
117             throw new SecurityException JavaDoc(x);
118         }
119     }
120     
121     /**
122      * @ejb.interface-method
123      * @ejb.permission role-name="AuthorityManager-read"
124      **/

125     public Authority getAuthority(String JavaDoc authorityID, String JavaDoc [] fetchGroups)
126         throws SecurityException JavaDoc
127     {
128         try
129         {
130             PersistenceManager pm = getPersistenceManager();
131
132             if (fetchGroups != null)
133         pm.getFetchPlan().setGroups(fetchGroups);
134       else
135         pm.getFetchPlan().clearGroups();
136
137             try
138             {
139                 pm.getExtent(Authority.class, true);
140                 try
141                 {
142                     Object JavaDoc o = pm.getObjectById(AuthorityID.create(authorityID), true);
143                     return (Authority)pm.detachCopy(o);
144                 }
145                 catch (JDOObjectNotFoundException x)
146                 {
147                     throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!");
148                 }
149             }
150             finally
151             {
152                 if (CLOSE_PM) pm.close();
153             }
154         }
155         catch (Exception JavaDoc x)
156         {
157             throw new SecurityException JavaDoc(x);
158         }
159     }
160     
161     /**
162      * @ejb.interface-method
163      * @ejb.permission role-name="AuthorityManager-read"
164      **/

165     public AuthoritySearchResult searchAuthorities (
166             String JavaDoc searchStr, boolean exact, int itemsPerPage, int pageIndex, int includeMask)
167         throws SecurityException JavaDoc
168     {
169         try
170         {
171             PersistenceManager pm = getPersistenceManager();
172
173 // if (fetchGroups != null)
174
// pm.getFetchPlan().setGroups(fetchGroups);
175
// else
176
// pm.getFetchPlan().clearGroups();
177
try
178             {
179                 AuthoritySearchResult result = Authority.searchAuthorities(pm, searchStr, exact, itemsPerPage, pageIndex);
180                 result.makeTransient(includeMask);
181                 return result;
182             }
183             finally
184             {
185                 if (CLOSE_PM) pm.close();
186             }
187         }
188         catch (Exception JavaDoc x)
189         {
190             throw new SecurityException JavaDoc(x);
191         }
192     }
193     
194     /**
195      * @throws ModuleException
196      * @ejb.interface-method
197      * @ejb.permission role-name="AuthorityManager-read"
198      **/

199     public List JavaDoc getAllAuthorities()
200     throws ModuleException
201     {
202       PersistenceManager pm = getPersistenceManager();
203       try
204       {
205         Query query = pm.newQuery(pm.getExtent(Authority.class, true));
206         Collection JavaDoc c = (Collection JavaDoc)query.execute();
207         List JavaDoc result = new ArrayList JavaDoc(pm.detachCopyAll(c));
208         return result;
209       }
210       finally
211       {
212         pm.close();
213       }
214     }
215     
216     /**
217      * @ejb.interface-method
218      * @ejb.permission role-name="AuthorityManager-read"
219      **/

220     public RoleGroupRefSearchResult searchRoleGroupRefs(
221             String JavaDoc authorityID,
222             String JavaDoc searchStr, boolean exact, int itemsPerPage, int pageIndex, int includeMask)
223         throws SecurityException JavaDoc
224     {
225         try {
226             PersistenceManager pm = getPersistenceManager();
227             try {
228                 pm.getExtent(Authority.class, true);
229                 Authority authority;
230                 try {
231                     authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true);
232                 } catch (JDOObjectNotFoundException x) {
233                     throw new AuthorityNotFoundException("Authority \""+authorityID+"\" could not be found in organisation \""+getOrganisationID()+"\"!");
234                 }
235                 
236                 RoleGroupRefSearchResult result = authority.searchRoleGroupRefs(
237                         searchStr, exact, itemsPerPage, pageIndex);
238                 result.makeTransient(includeMask);
239                 return result;
240             } finally {
241                 if (CLOSE_PM) pm.close();
242             }
243         } catch (Exception JavaDoc x) {
244             throw new SecurityException JavaDoc(x);
245         }
246     }
247
248     /**
249      * @ejb.interface-method
250      * @ejb.permission role-name="AuthorityManager-read"
251      **/

252     public UserRefSearchResult searchUserRefs(
253             String JavaDoc authorityID,
254             String JavaDoc searchStr, boolean exact, int itemsPerPage, int pageIndex, int includeMask)
255         throws SecurityException JavaDoc
256     {
257         try {
258             PersistenceManager pm = getPersistenceManager();
259             try {
260                 pm.getExtent(Authority.class, true);
261                 Authority authority;
262                 try {
263                     authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true);
264                 } catch (JDOObjectNotFoundException x) {
265                     throw new AuthorityNotFoundException("Authority \""+authorityID+"\" could not be found in organisation \""+getOrganisationID()+"\"!");
266                 }
267                 
268                 UserRefSearchResult result = authority.searchUserRefs(
269                         searchStr, exact, itemsPerPage, pageIndex);
270                 result.makeTransient(includeMask);
271                 return result;
272             } finally {
273                 if (CLOSE_PM) pm.close();
274             }
275         } catch (Exception JavaDoc x) {
276             throw new SecurityException JavaDoc(x);
277         }
278     }
279
280     // ******************************************************************
281
// *** Methods for management of links between UserRefs and RoleRefs
282
// ******************************************************************
283

284     /**
285      * @ejb.interface-method
286      * @ejb.permission role-name="AuthorityManager-write"
287      * @ejb.transaction type = "Required"
288      */

289     public void createUserRef(String JavaDoc authorityID, String JavaDoc userID)
290         throws SecurityException JavaDoc
291     {
292         try {
293             PersistenceManager pm = getPersistenceManager();
294             try {
295                 pm.getExtent(Authority.class, true);
296                 pm.getExtent(User.class, true);
297
298                 Authority authority;
299                 try {
300                     authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true);
301                 } catch (JDOObjectNotFoundException x) {
302                     throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!");
303                 }
304                 User user;
305                 try {
306                     user = (User)pm.getObjectById(UserID.create(getOrganisationID(), userID), true);
307                 } catch (JDOObjectNotFoundException x) {
308                     throw new UserNotFoundException("User \""+userID+"\" not found in organisation \""+getOrganisationID()+"\"!");
309                 }
310
311                 authority.createUserRef(user);
312             } finally {
313                 if (CLOSE_PM) pm.close();
314             }
315         } catch (SecurityException JavaDoc x) {
316             throw x;
317         } catch (Exception JavaDoc x) {
318             throw new SecurityException JavaDoc(x);
319         }
320     }
321     
322     /**
323      * @ejb.interface-method
324      * @ejb.permission role-name="AuthorityManager-write"
325      * @ejb.transaction type = "Required"
326      */

327     public void destroyUserRef(String JavaDoc authorityID, String JavaDoc userID)
328         throws SecurityException JavaDoc
329     {
330         try {
331             IpanemaServerManager ism = getIpanemaServerManager();
332             try {
333                 PersistenceManager pm = getPersistenceManager();
334                 try {
335                     pm.getExtent(Authority.class, true);
336     // pm.getExtent(User.class, true);
337

338                     Authority authority;
339                     try {
340                         authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true);
341                     } catch (JDOObjectNotFoundException x) {
342                         throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!");
343                     }
344     // User user;
345
// try {
346
// user = (User)pm.getObjectById(UserID.create(getOrganisationID(), userID), true);
347
// } catch (JDOObjectNotFoundException x) {
348
// throw new UserNotFoundException("User \""+userID+"\" not found in organisation \""+getOrganisationID()+"\"!");
349
// }
350

351                     authority.destroyUserRef(userID);
352                     ism.ipanemaSecurity_flushCache();
353                 } finally {
354                     if (CLOSE_PM) pm.close();
355                 }
356             } finally {
357                 ism.close();
358             }
359         } catch (SecurityException JavaDoc x) {
360             throw x;
361         } catch (Exception JavaDoc x) {
362             throw new SecurityException JavaDoc(x);
363         }
364     }
365     
366     /**
367      * @ejb.interface-method
368      * @ejb.permission role-name="AuthorityManager-write"
369      * @ejb.transaction type = "Required"
370      */

371     public void destroyRoleGroupRef(String JavaDoc authorityID, String JavaDoc roleGroupID)
372         throws SecurityException JavaDoc
373     {
374         try {
375             IpanemaServerManager ism = getIpanemaServerManager();
376             try {
377                 PersistenceManager pm = getPersistenceManager();
378                 try {
379                     pm.getExtent(Authority.class, true);
380     // pm.getExtent(RoleGroup.class, true);
381

382                     Authority authority;
383                     try {
384                         authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true);
385                     } catch (JDOObjectNotFoundException x) {
386                         throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!");
387                     }
388     // RoleGroup roleGroup;
389
// try {
390
// roleGroup = (RoleGroup)pm.getObjectById(RoleGroupID.create(roleGroupID), true);
391
// } catch (JDOObjectNotFoundException x) {
392
// throw new UserNotFoundException("RoleGroup \""+roleGroupID+"\" not found in organisation \""+getOrganisationID()+"\"!");
393
// }
394

395                     authority.destroyRoleGroupRef(roleGroupID);
396                     ism.ipanemaSecurity_flushCache();
397                 } finally {
398                     if (CLOSE_PM) pm.close();
399                 }
400             } finally {
401                 ism.close();
402             }
403         } catch (SecurityException JavaDoc x) {
404             throw x;
405         } catch (Exception JavaDoc x) {
406             throw new SecurityException JavaDoc(x);
407         }
408     }
409
410     /**
411      * @ejb.interface-method
412      * @ejb.permission role-name="AuthorityManager-read"
413      * @ejb.transaction type = "Required"
414      */

415     public UserRef getUserRef(String JavaDoc authorityID, String JavaDoc userID, int includeMask)
416         throws SecurityException JavaDoc
417     {
418         try {
419             PersistenceManager pm = getPersistenceManager();
420             try {
421                 pm.getExtent(Authority.class, true);
422                 pm.getExtent(User.class, true);
423
424                 Authority authority;
425                 try {
426                     authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true);
427                 } catch (JDOObjectNotFoundException x) {
428                     throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!");
429                 }
430                 UserRef userRef = authority.getUserRef(userID);
431                 if (userRef == null)
432                     throw new UserRefNotFoundException("UserRef for User \""+userID+"\" not found in authority \""+authorityID+"\" in organisation \""+getOrganisationID()+"\"!");
433                 
434                 userRef.makeTransient(includeMask);
435                 
436                 return userRef;
437             } finally {
438                 if (CLOSE_PM) pm.close();
439             }
440         } catch (SecurityException JavaDoc x) {
441             throw x;
442         } catch (Exception JavaDoc x) {
443             throw new SecurityException JavaDoc(x);
444         }
445     }
446     
447     /**
448      * @ejb.interface-method
449      * @ejb.permission role-name="AuthorityManager-write"
450      * @ejb.transaction type = "Required"
451      */

452     public void createRoleGroupRef(String JavaDoc authorityID, String JavaDoc roleGroupID)
453         throws SecurityException JavaDoc
454     {
455         try {
456             PersistenceManager pm = getPersistenceManager();
457             try {
458                 pm.getExtent(Authority.class, true);
459                 pm.getExtent(RoleGroup.class, true);
460
461                 Authority authority;
462                 try {
463                     authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true);
464                 } catch (JDOObjectNotFoundException x) {
465                     throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!");
466                 }
467                 RoleGroup roleGroup;
468                 try {
469                     roleGroup = (RoleGroup)pm.getObjectById(RoleGroupID.create(roleGroupID), true);
470                 } catch (JDOObjectNotFoundException x) {
471                     throw new UserNotFoundException("RoleGroup \""+roleGroupID+"\" not found in organisation \""+getOrganisationID()+"\"!");
472                 }
473
474                 authority.createRoleGroupRef(roleGroup);
475             } finally {
476                 if (CLOSE_PM) pm.close();
477             }
478         } catch (SecurityException JavaDoc x) {
479             throw x;
480         } catch (Exception JavaDoc x) {
481             throw new SecurityException JavaDoc(x);
482         }
483     }
484     
485     /**
486      * @ejb.interface-method
487      * @ejb.permission role-name="AuthorityManager-read"
488      * @ejb.transaction type = "Required"
489      */

490     public RoleGroupRef getRoleGroupRef(String JavaDoc authorityID, String JavaDoc roleGroupID, int includeMask)
491         throws SecurityException JavaDoc
492     {
493         try {
494             PersistenceManager pm = getPersistenceManager();
495             try {
496                 pm.getExtent(Authority.class, true);
497                 pm.getExtent(User.class, true);
498
499                 Authority authority;
500                 try {
501                     authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true);
502                 } catch (JDOObjectNotFoundException x) {
503                     throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!");
504                 }
505                 RoleGroupRef roleGroupRef = authority.getRoleGroupRef(roleGroupID);
506                 if (roleGroupRef == null)
507                     throw new RoleGroupRefNotFoundException("RoleGroupRef for RoleGroup \""+roleGroupID+"\" not found in authority \""+authorityID+"\" in organisation \""+getOrganisationID()+"\"!");
508                 
509                 roleGroupRef.makeTransient(includeMask);
510                 
511                 return roleGroupRef;
512             } finally {
513                 if (CLOSE_PM) pm.close();
514             }
515         } catch (SecurityException JavaDoc x) {
516             throw x;
517         } catch (Exception JavaDoc x) {
518             throw new SecurityException JavaDoc(x);
519         }
520     }
521
522     /**
523      * @param authorityID
524      * @param userID
525      * @param roleGroupID
526      * @throws SecurityException
527      *
528      * @ejb.interface-method
529      * @ejb.permission role-name="AuthorityManager-write"
530      * @ejb.transaction type = "Required"
531      */

532     public void addRoleGroupRefToUserRef(String JavaDoc authorityID, String JavaDoc userID, String JavaDoc roleGroupID)
533         throws SecurityException JavaDoc
534     {
535         try {
536             IpanemaServerManager ism = getIpanemaServerManager();
537             try {
538                 PersistenceManager pm = getPersistenceManager();
539                 try {
540     // pm.getExtent(Authority.class, true);
541
pm.getExtent(User.class, true);
542                     pm.getExtent(RoleGroup.class, true);
543     
544                     Authority authority;
545                     try {
546                         authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true);
547                     } catch (JDOObjectNotFoundException x) {
548                         throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!");
549                     }
550                     
551                     UserRef userRef = authority.getUserRef(userID);
552                     if (userRef == null)
553                         throw new UserRefNotFoundException("UserRef for user \""+userID+"\" not found in authority \""+authorityID+"\" in organisation \""+getOrganisationID()+"\"!");
554     
555                     RoleGroupRef roleGroupRef = authority.getRoleGroupRef(roleGroupID);
556                     if (roleGroupRef == null)
557                         throw new RoleGroupRefNotFoundException("RoleGroupRef for roleGroup \""+roleGroupID+"\" not found in authority \""+authorityID+"\" in organisation \""+getOrganisationID()+"\"!");
558     
559                     userRef.addRoleGroupRef(roleGroupRef);
560                     ism.ipanemaSecurity_flushCache(userID);
561                 } finally {
562                     if (CLOSE_PM) pm.close();
563                 }
564             } finally {
565                 ism.close();
566             }
567         } catch (SecurityException JavaDoc x) {
568             throw x;
569         } catch (Exception JavaDoc x) {
570             throw new SecurityException JavaDoc(x);
571         }
572     }
573
574     /**
575      * @param String authorityID
576      * @param userID
577      * @param roleGroupID
578      * @throws SecurityException
579      *
580      * @ejb.interface-method
581      * @ejb.permission role-name="AuthorityManager-write"
582      * @ejb.transaction type = "Required"
583      */

584     public void removeRoleGroupRefFromUserRef(String JavaDoc authorityID, String JavaDoc userID, String JavaDoc roleGroupID)
585         throws SecurityException JavaDoc
586     {
587         try {
588             IpanemaServerManager ism = getIpanemaServerManager();
589             try {
590                 PersistenceManager pm = getPersistenceManager();
591                 try {
592     // pm.getExtent(Authority.class, true);
593
pm.getExtent(User.class, true);
594                     pm.getExtent(RoleGroup.class, true);
595     
596                     Authority authority;
597                     try {
598                         authority = (Authority)pm.getObjectById(AuthorityID.create(authorityID), true);
599                     } catch (JDOObjectNotFoundException x) {
600                         throw new AuthorityNotFoundException("Authority \""+authorityID+"\" not found in organisation \""+getOrganisationID()+"\"!");
601                     }
602                     
603                     UserRef userRef = authority.getUserRef(userID);
604                     if (userRef == null)
605                         throw new UserRefNotFoundException("UserRef for user \""+userID+"\" not found in authority \""+authorityID+"\" in organisation \""+getOrganisationID()+"\"!");
606                     
607                     RoleGroupRef roleGroupRef = authority.getRoleGroupRef(roleGroupID);
608                     if (roleGroupRef == null)
609                         throw new UserRefNotFoundException("RoleGroupRef for roleGroup \""+roleGroupID+"\" not found in authority \""+authorityID+"\" in organisation \""+getOrganisationID()+"\"!");
610     
611                     userRef.removeRoleGroupRef(roleGroupRef);
612                     ism.ipanemaSecurity_flushCache(userID);
613                 } finally {
614                     if (CLOSE_PM) pm.close();
615                 }
616             } finally {
617                 ism.close();
618             }
619         } catch (SecurityException JavaDoc x) {
620             throw x;
621         } catch (Exception JavaDoc x) {
622             throw new SecurityException JavaDoc(x);
623         }
624     }
625 }
Popular Tags