KickJava   Java API By Example, From Geeks To Geeks.

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


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 16.06.2004
33  */

34 package com.nightlabs.ipanema.security;
35
36 import java.io.Serializable JavaDoc;
37 import java.security.Principal JavaDoc;
38
39 import javax.jdo.PersistenceManager;
40 import javax.jdo.spi.PersistenceCapable;
41
42 import org.jboss.security.SimplePrincipal;
43
44
45 /**
46  * @author marco
47  */

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

57 public class RoleRef implements Serializable JavaDoc
58 {
59     
60     /**
61      * @jdo.field persistence-modifier="persistent" primary-key="true"
62      * @jdo.column length="100"
63      */

64     private String JavaDoc authorityID;
65     
66     /**
67      * This is the organisationID to which the user belongs. Within one organisation,
68      * all the users have their organisation's ID stored here, thus it's the same
69      * value for all of them. Even if the User object represents another organisation,
70      * this member is the organisationID to which the user logs in.
71      *
72      * @jdo.field persistence-modifier="persistent" primary-key="true"
73      * @jdo.column length="100"
74      */

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

81     private String JavaDoc userID;
82
83     /**
84      * @jdo.field persistence-modifier="persistent" primary-key="true"
85      * @jdo.column length="100"
86      */

87     private String JavaDoc roleID;
88
89     protected Authority authority;
90
91     /**
92      * @jdo.field persistence-modifier="persistent"
93      * @jdo.field-vendor-extension vendor-name="jpox" key="map-field" value="roleRefs"
94      */

95     protected UserRef userRef;
96
97     private Role role;
98
99     private int referenceCount = 0;
100
101     public RoleRef() { }
102     
103     public RoleRef(Authority _authority, UserRef _userRef, Role _role)
104     {
105         if (_authority == null)
106             throw new NullPointerException JavaDoc("authority must not be null!");
107         if (_userRef == null)
108             throw new NullPointerException JavaDoc("userRef must not be null!");
109         if (_role == null)
110             throw new NullPointerException JavaDoc("role must not be null!");
111
112         this.authorityID = _authority.getAuthorityID();
113         this.organisationID = _userRef.getOrganisationID();
114         this.userID = _userRef.getUserID();
115         this.roleID = _role.getRoleID();
116         this.authority = _authority;
117         this.userRef = _userRef;
118         this.role = _role;
119     }
120
121     /**
122      * @return Returns the organisationID.
123      */

124     public String JavaDoc getOrganisationID() {
125         return organisationID;
126     }
127     /**
128      * @return Returns the userID.
129      */

130     public String JavaDoc getUserID() {
131         return userID;
132     }
133     /**
134      * @return Returns the roleID.
135      */

136     public String JavaDoc getRoleID() {
137         return roleID;
138     }
139
140     /**
141      * @return Returns the referenceCount.
142      */

143     public int getReferenceCount() {
144         return referenceCount;
145     }
146
147     /**
148      * This method increments the reference count.
149      * @return Returns the new reference count after incrementation.
150      */

151     public int incrementReferenceCount(int incRefCount)
152     {
153         return (referenceCount+=incRefCount);
154     }
155
156     /**
157      * This method decrements the reference count.
158      * @return Returns the new reference count after decrementation.
159      */

160     public int decrementReferenceCount(int decRefCount)
161     {
162         return (referenceCount-=decRefCount);
163     }
164
165     /**
166      * @return Returns the user.
167      */

168     public UserRef getUserRef() {
169         return userRef;
170     }
171     
172     /**
173      * @return Returns the role.
174      */

175     public Role getRole() {
176         return role;
177     }
178
179     public static final int INCLUDE_NONE = 0;
180 // public static final int INCLUDE_USER = 0x1;
181
// public static final int INCLUDE_USER_PERSON = INCLUDE_USER | 0x2;
182
// public static final int INCLUDE_USER_ROLEGROUPS = INCLUDE_USER | 0x4;
183
public static final int INCLUDE_ROLE = 0x8;
184     public static final int INCLUDE_ROLE_ROLEGROUPS = INCLUDE_ROLE | 0x10;
185
186     public void makeTransient(int includeMask)
187     {
188         PersistenceManager pm = ((PersistenceCapable)this).jdoGetPersistenceManager();
189         if (pm == null)
190             return;
191         
192         pm.retrieve(this);
193 // if ((includeMask & INCLUDE_USER) != 0)
194
// getUser();
195

196         if ((includeMask & INCLUDE_ROLE) != 0)
197             getRole();
198         
199         pm.makeTransient(this);
200         
201 // if ((includeMask & INCLUDE_USER) != 0) {
202
// int userIncludeMask = 0;
203
// if ((includeMask & INCLUDE_USER_PERSON) != 0)
204
// userIncludeMask = userIncludeMask | User.INCLUDE_PERSON;
205
// if ((includeMask & INCLUDE_USER_ROLEGROUPS) != 0)
206
// userIncludeMask = userIncludeMask | User.INCLUDE_ROLEGROUPS;
207
//
208
// this.user.makeTransient(userIncludeMask);
209
// }
210
// else
211
// this.user = null;
212

213         if ((includeMask & INCLUDE_ROLE) != 0) {
214             int roleIncludeMask = 0;
215             if ((includeMask & INCLUDE_ROLE_ROLEGROUPS) != 0)
216                 roleIncludeMask = roleIncludeMask | Role.INCLUDE_ROLEGROUPS;
217
218             this.role.makeTransient(roleIncludeMask);
219         }
220         else
221             this.role = null;
222     }
223
224     protected transient String JavaDoc thisString = null;
225     /**
226      * @see java.lang.Object#toString()
227      */

228     public String JavaDoc toString() {
229         if (thisString == null) {
230             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
231             sb.append(this.getClass().getName());
232             sb.append('{');
233             sb.append(userID);
234             sb.append('@');
235             sb.append(organisationID);
236             sb.append(',');
237             sb.append(roleID);
238             sb.append('}');
239         }
240         return thisString;
241     }
242
243     /**
244      * @see java.lang.Object#equals(java.lang.Object)
245      */

246     public boolean equals(Object JavaDoc obj)
247     {
248         if (obj == this)
249             return true;
250
251         if (!(obj instanceof RoleRef))
252             return false;
253
254         RoleRef other = (RoleRef) obj;
255         return
256             this.organisationID.equals(other.organisationID) &&
257             this.userID.equals(other.userID) &&
258             this.roleID.equals(other.roleID);
259     }
260     /**
261      * @see java.lang.Object#hashCode()
262      */

263     public int hashCode()
264     {
265         return organisationID.hashCode() ^ userID.hashCode() ^ roleID.hashCode();
266     }
267
268     protected transient Principal rolePrincipal = null;
269     public Principal getRolePrincipal()
270     {
271         if (rolePrincipal == null)
272             rolePrincipal = new SimplePrincipal(roleID);
273         
274         return rolePrincipal;
275     }
276     /**
277      * @return Returns the authority.
278      */

279     public Authority getAuthority() {
280         return authority;
281     }
282 }
283
Popular Tags