KickJava   Java API By Example, From Geeks To Geeks.

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


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
36 import java.io.Serializable JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.Date JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.Map JavaDoc;
42
43 import javax.jdo.PersistenceManager;
44 import javax.jdo.spi.PersistenceCapable;
45
46 import org.apache.log4j.Logger;
47
48 /**
49  * @author marco
50  */

51
52 /**
53  * @jdo.persistence-capable
54  * identity-type = "application"
55  * objectid-class = "com.nightlabs.ipanema.security.id.UserRefID"
56  * detachable = "true"
57  *
58  * @jdo.inheritance strategy="new-table"
59  */

60 public class UserRef implements Serializable JavaDoc // , InstanceCallbacks
61
{
62     public static Logger LOGGER = Logger.getLogger(UserRef.class);
63
64
65     /**
66      * @jdo.field primary-key="true"
67      * @jdo.column length="100"
68      */

69     private String JavaDoc authorityID;
70
71     /**
72      * @jdo.field primary-key="true"
73      * @jdo.column length="100"
74      */

75     private String JavaDoc organisationID;
76
77     /**
78      * @jdo.field primary-key="true"
79      * @jdo.column length="100"
80      */

81     private String JavaDoc userID;
82
83
84
85     /**
86      * !mapped-by="userRefs"
87      * @jdo.field persistence-modifier="persistent"
88      * @jdo.field-vendor-extension vendor-name="jpox" key="map-field" value="userRefs"
89      */

90     private Authority authority;
91
92     /**
93      * !mapped-by="userRefs"
94      * @jdo.field persistence-modifier="persistent"
95      * @jdo.field-vendor-extension vendor-name="jpox" key="map-field" value="userRefs"
96      */

97     private User user;
98
99
100
101     /**
102      * This flag controls whether this UserRef should be shown in lists etc.. If it's true, it
103      * means, the UserRef has been created manually. If the UserRef
104      * only exists, because the User belongs to a UserGroup, it is false and the UserRef
105      * will be automatically deleted, if the last UserGroup containing this userRef gets
106      * removed from the authority.
107      *
108      * @jdo.field persistence-modifier="persistent"
109      */

110     private boolean visible;
111
112     /**
113      * @jdo.field persistence-modifier="persistent"
114      */

115     private int userGroupReferenceCount = 0;
116
117     /**
118      * key: String roleGroupID<br/>
119      * value: RoleGroupRef roleGroupRef
120      * <br/><br/>
121      * UserRef (m) - (n) RoleGroupRef
122      *
123      * @jdo.field
124      * persistence-modifier="persistent"
125      * collection-type="map"
126      * key-type="java.lang.String"
127      * value-type="RoleGroupRef"
128      *
129      * @jdo.join
130      *
131      * @jdo.map-vendor-extension vendor-name="jpox" key="key-field" value="roleGroupID"
132      */

133     private Map JavaDoc roleGroupRefs = new HashMap JavaDoc();
134
135
136     /**
137      * key: String roleID<br/>
138      * value: RoleRef roleRef
139      * <br/><br/>
140      * UserRef (1) - (n) RoleRef
141      *
142      * @jdo.field
143      * persistence-modifier="persistent"
144      * collection-type="map"
145      * key-type="java.lang.String"
146      * value-type="RoleRef"
147      * mapped-by="userRef"
148      *
149      * @jdo.map-vendor-extension vendor-name="jpox" key="key-field" value="roleID"
150      * @jdo.map-vendor-extension vendor-name="jpox" key="clear-on-delete" value="true"
151      */

152     private Map JavaDoc roleRefs = new HashMap JavaDoc();
153
154     private Date JavaDoc changeDT;
155
156     public UserRef() { }
157
158     public UserRef(Authority _authority, User _user, boolean _visible)
159     {
160         this.authorityID = _authority.getAuthorityID();
161         this.organisationID = _user.getOrganisationID();
162         this.userID = _user.getUserID();
163         this.authority = _authority;
164         this.user = _user;
165         this.visible = _visible;
166         this.changeDT = new Date JavaDoc();
167     }
168
169     /**
170      * @return Returns the authorityID.
171      */

172     public String JavaDoc getAuthorityID() {
173         return authorityID;
174     }
175     /**
176      * @return Returns the organisationID.
177      */

178     public String JavaDoc getOrganisationID() {
179         return organisationID;
180     }
181     /**
182      * @return Returns the userID.
183      */

184     public String JavaDoc getUserID() {
185         return userID;
186     }
187
188     /**
189      * @return Returns the authority.
190      */

191     public Authority getAuthority() {
192         return authority;
193     }
194     /**
195      * @return Returns the user.
196      */

197     public User getUser() {
198         return user;
199     }
200     
201     /**
202      * @return Returns the visible.
203      */

204     public boolean isVisible() {
205         return visible;
206     }
207     /**
208      * @param visible The visible to set.
209      */

210     public void setVisible(boolean visible) {
211         this.visible = visible;
212     }
213     
214     /**
215      * @return Returns the userGroupReferenceCount.
216      */

217     public int getUserGroupReferenceCount() {
218         return userGroupReferenceCount;
219     }
220     
221     /**
222      * @return Returns the new userGroupReferenceCount.
223      */

224     protected int incrementUserGroupReferenceCount(int inc) {
225         return (userGroupReferenceCount += inc);
226     }
227     
228     /**
229      * @return Returns the new userGroupReferenceCount.
230      */

231     protected int decrementUserGroupReferenceCount(int dec) {
232         return (userGroupReferenceCount -= dec);
233     }
234
235     /**
236      * @return Returns the changeDT.
237      */

238     public Date JavaDoc getChangeDT() {
239         return changeDT;
240     }
241     /**
242      * @param changeDT The changeDT to set.
243      */

244     public void setChangeDT(Date JavaDoc changeDT) {
245         this.changeDT = changeDT;
246     }
247
248     protected transient String JavaDoc login = null;
249     /**
250      * This method returns the login of this user in the form {userID}@{organisationID}.
251      * The login is the system wide unique identifier for a user.
252      *
253      * @return Returns the login as {userID}@{organisationID}
254      */

255     public String JavaDoc getLogin()
256     {
257         if (login == null) {
258             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
259             sb.append(getUserID());
260             sb.append('@');
261             sb.append(getOrganisationID());
262             login = sb.toString();
263         }
264         return login;
265     }
266
267     /**
268      * This method adds a roleGroup. It automatically generates a RoleGroupRef
269      * and adds all the necessary RoleRef instances
270      * for the direct joins to the roles.
271      *
272      * @param roleGroup
273      */

274     public void addRoleGroupRef(RoleGroupRef roleGroupRef)
275     {
276         if (roleGroupRef == null)
277             throw new NullPointerException JavaDoc("roleGroupRef must not be null!");
278
279         String JavaDoc roleGroupID = roleGroupRef.getRoleGroupID();
280         if (roleGroupRefs.containsKey(roleGroupID))
281             return;
282
283         if (authority.getRoleGroupRef(roleGroupID) == null)
284             throw new IllegalStateException JavaDoc("RoleGroup \""+roleGroupID+"\" not registered in authority \""+authorityID+"\"!");
285
286         roleGroupRefs.put(roleGroupID, roleGroupRef);
287         roleGroupRef._addUserRef(this);
288         
289         RoleGroup roleGroup = roleGroupRef.getRoleGroup();
290         for (Iterator JavaDoc it = roleGroup.getRoles().iterator(); it.hasNext(); ) {
291             Role role = (Role) it.next();
292             _addRole(role, 1);
293         }
294         changeDT = new Date JavaDoc();
295     }
296
297     public void removeRoleGroupRef(RoleGroupRef roleGroupRef)
298     {
299         if (roleGroupRef == null)
300             throw new NullPointerException JavaDoc("roleGroup must not be null!");
301         
302         if (!authorityID.equals(roleGroupRef.getAuthorityID()))
303             throw new IllegalArgumentException JavaDoc("userRef.authorityID != roleGroupRef.authorityID");
304         
305         removeRoleGroupRef(roleGroupRef.getRoleGroupID());
306     }
307
308     public void removeRoleGroupRef(String JavaDoc roleGroupID)
309     {
310         if (roleGroupID == null)
311             throw new NullPointerException JavaDoc("roleGroupID must not be null!");
312         
313         RoleGroupRef roleGroupRef = (RoleGroupRef)roleGroupRefs.get(roleGroupID);
314         
315         if (roleGroupRef == null)
316             return;
317
318         for (Iterator JavaDoc it = roleGroupRef.getRoleGroup().getRoles().iterator(); it.hasNext(); ) {
319             Role role = (Role) it.next();
320             _removeRole(role, 1);
321         } // for (Iterator it = roleGroupRef.getRoleGroup().getRoles().iterator(); it.hasNext(); ) {
322

323         roleGroupRefs.remove(roleGroupID);
324         roleGroupRef._removeUserRef(this);
325         changeDT = new Date JavaDoc();
326     }
327     
328     /**
329      * This method is called internally by addRoleGroup(...) and by
330      * RoleGroup.addRole(...). It increments the reference counter of
331      * the RoleRef and creates a RoleRef if not yet existing.
332      */

333     protected void _addRole(Role role, int incRefCount)
334     {
335         if (role == null)
336             throw new NullPointerException JavaDoc("role must not be null!");
337         
338         if (incRefCount < 1)
339             throw new IllegalArgumentException JavaDoc("incRefCount must be >= 1");
340
341         String JavaDoc roleID = role.getRoleID();
342         RoleRef roleRef = (RoleRef)roleRefs.get(roleID);
343         if (roleRef == null) {
344             roleRef = new RoleRef(authority, this, role);
345             roleRefs.put(roleID, roleRef);
346         }
347         roleRef.incrementReferenceCount(incRefCount);
348         changeDT = new Date JavaDoc();
349     }
350
351     protected void _removeRole(Role role, int decRefCount)
352     {
353         String JavaDoc roleID = role.getRoleID();
354         RoleRef roleRef = (RoleRef)roleRefs.get(roleID);
355         if (roleRef == null) {
356             LOGGER.warn("_removeRole(\""+roleID+"\"): The User \""+toString()+"\" does not contain a RoleRef for this role!");
357             return;
358         }
359         
360         if (decRefCount < 1)
361             throw new IllegalArgumentException JavaDoc("decRefCount must be >= 1");
362         
363         if (roleRef.decrementReferenceCount(decRefCount) <= 0) {
364             if (roleRef.getReferenceCount() < 0)
365                 LOGGER.warn("_removeRole(\""+roleID+"\"): referenceCount < 0!!! user=\""+toString()+"\"!");
366
367             roleRefs.remove(roleID);
368         } // if (roleRef.decrementReferenceCount() <= 0) {
369
changeDT = new Date JavaDoc();
370     }
371     
372     public Collection JavaDoc getRoleGroupRefs()
373     {
374         return roleGroupRefs.values();
375     }
376
377     public Collection JavaDoc getRoleRefs()
378     {
379         return roleRefs.values();
380     }
381     
382     public boolean containsRoleRef(String JavaDoc roleID)
383     {
384         return roleRefs.containsKey(roleID);
385     }
386
387     protected transient String JavaDoc thisString = null;
388     public String JavaDoc toString()
389     {
390         if (thisString == null) {
391             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
392             sb.append(this.getClass().getName());
393             sb.append('{');
394             sb.append(getLogin()); // because the login is created in jdoPostLoad anyway.
395
sb.append('}');
396             thisString = sb.toString();
397         }
398         return thisString;
399     }
400
401     /**
402      * @see java.lang.Object#equals(java.lang.Object)
403      */

404     public boolean equals(Object JavaDoc obj) {
405         if (obj == this)
406             return true;
407
408         if (!(obj instanceof User))
409             return false;
410
411         User other = (User)obj;
412         return
413             this.getOrganisationID().equals(other.getOrganisationID())
414             &&
415             this.getUserID().equals(other.getUserID());
416     }
417
418     /**
419      * @see java.lang.Object#hashCode()
420      */

421     public int hashCode() {
422         return this.getOrganisationID().hashCode() ^ this.getUserID().hashCode();
423     }
424
425     public static int INCLUDE_NONE = 0;
426     public static int INCLUDE_AUTHORITY = 0x1;
427     public static int INCLUDE_USER = 0x2;
428     public static int INCLUDE_ROLEGROUPREFS = 0x4;
429 // public static int INCLUDE_ROLEREFS = 0x8;
430
public static int INCLUDE_ALL = Integer.MAX_VALUE;
431
432     public void makeTransient(int includeMask)
433     {
434         PersistenceManager pm = ((PersistenceCapable)this).jdoGetPersistenceManager();
435         if (pm == null)
436             return;
437
438         if ((INCLUDE_AUTHORITY & includeMask) != 0)
439             getAuthority().makeTransient(Authority.INCLUDE_NONE);
440
441         if ((INCLUDE_USER & includeMask) != 0)
442             getUser().makeTransient(User.INCLUDE_NONE);
443
444         Map JavaDoc tmpRoleGroupRefs = null;
445         Map JavaDoc tmpRoleRefs = null;
446         
447         if ((INCLUDE_ROLEGROUPREFS & includeMask) != 0) {
448             tmpRoleGroupRefs = new HashMap JavaDoc();
449             for (Iterator JavaDoc it = getRoleGroupRefs().iterator(); it.hasNext(); ) {
450                 RoleGroupRef roleGroupRef = (RoleGroupRef)it.next();
451                 roleGroupRef.makeTransient(RoleGroupRef.INCLUDE_NONE);
452                 tmpRoleGroupRefs.put(roleGroupRef.getRoleGroupID(), roleGroupRef);
453             }
454         }
455
456         pm.makeTransient(this);
457
458         if ((INCLUDE_AUTHORITY & includeMask) == 0)
459             authority = null;
460
461         if ((INCLUDE_USER & includeMask) == 0)
462             user = null;
463
464         roleGroupRefs = tmpRoleGroupRefs;
465         roleRefs = tmpRoleRefs;
466     }
467
468 // /**
469
// * @see javax.jdo.InstanceCallbacks#jdoPostLoad()
470
// */
471
// public void jdoPostLoad() {
472
// getLogin();
473
// }
474
// /**
475
// * @see javax.jdo.InstanceCallbacks#jdoPreStore()
476
// */
477
// public void jdoPreStore() {
478
// // noop
479
// }
480
// /**
481
// * @see javax.jdo.InstanceCallbacks#jdoPreClear()
482
// */
483
// public void jdoPreClear() {
484
// // noop
485
// }
486
// /**
487
// * @see javax.jdo.InstanceCallbacks#jdoPreDelete()
488
// */
489
// public void jdoPreDelete() {
490
// // noop
491
// }
492

493 }
494
Popular Tags