KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > security > impl > db > entity > TurbineRole


1 package org.apache.fulcrum.security.impl.db.entity;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.util.Iterator JavaDoc;
58 import org.apache.fulcrum.security.TurbineSecurity;
59 import org.apache.fulcrum.security.entity.Permission;
60 import org.apache.fulcrum.security.entity.Role;
61 import org.apache.fulcrum.security.entity.SecurityEntity;
62 import org.apache.fulcrum.security.util.PermissionSet;
63 import org.apache.fulcrum.security.util.TurbineSecurityException;
64
65 /**
66  * This class represents a role played by the User associated with the
67  * current Session.
68  *
69  * @author <a HREF="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
70  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
71  * @author <a HREF="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
72  * @author <a HREF="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
73  * @version $Id: TurbineRole.java,v 1.1 2004/11/12 10:25:43 epugh Exp $
74  */

75 public class TurbineRole
76     extends org.apache.fulcrum.security.impl.db.entity.BaseTurbineRole
77     implements Role, Comparable JavaDoc
78 {
79
80     /** The permissions for this role. */
81     private PermissionSet permissionSet = null;
82
83     /**
84      * Constructs a new Role
85      */

86     public TurbineRole()
87     {
88     }
89
90     /**
91      * Constructs a new Role with the sepcified name.
92      *
93      * @param name The name of the new object.
94      */

95     public TurbineRole(String JavaDoc name)
96     {
97         this.setName(name);
98     }
99
100     /**
101      * Returns the set of Permissions associated with this Role.
102      *
103      * @return A PermissionSet.
104      * @exception Exception, a generic exception.
105      */

106     public PermissionSet getPermissions()
107         throws Exception JavaDoc
108     {
109         return permissionSet;
110     }
111
112     /**
113      * Sets the Permissions associated with this Role.
114      *
115      * @param permissionSet A PermissionSet.
116      */

117     public void setPermissions(PermissionSet permissionSet)
118     {
119         this.permissionSet = permissionSet;
120     }
121
122     // These following methods are wrappers around TurbineSecurity
123

124     /**
125      * Creates a new Role in the system.
126      *
127      * @param name The name of the new Role.
128      * @return An object representing the new Role.
129      * @throws TurbineSecurityException if the Role could not be created.
130      */

131     public Role create(String JavaDoc name)
132         throws TurbineSecurityException
133     {
134         //Role role = new Role(name);
135
Role role = new TurbineRole(name);
136         TurbineSecurity.addRole(role);
137         return role;
138     }
139
140     /**
141      * Makes changes made to the Role attributes permanent.
142      *
143      * @throws TurbineSecurityException if there is a problem while
144      * saving data.
145      */

146     public void save()
147         throws TurbineSecurityException
148     {
149         TurbineSecurity.saveRole(this);
150     }
151
152     /**
153      * Removes a role from the system.
154      *
155      * @throws TurbineSecurityException if the Role could not be removed.
156      */

157     public void remove()
158         throws TurbineSecurityException
159     {
160         TurbineSecurity.removeRole(this);
161     }
162
163     /**
164      * Renames the role.
165      *
166      * @param name The new Role name.
167      * @throws TurbineSecurityException if the Role could not be renamed.
168      */

169     public void rename(String JavaDoc name)
170         throws TurbineSecurityException
171     {
172         TurbineSecurity.renameRole(this, name);
173     }
174
175     /**
176      * Grants a Permission to this Role.
177      *
178      * @param permission A Permission.
179      * @throws TurbineSecurityException if there is a problem while assigning
180      * the Permission.
181      */

182     public void grant(Permission permission)
183         throws TurbineSecurityException
184     {
185         TurbineSecurity.grant(this, permission);
186     }
187
188     /**
189      * Grants Permissions from a PermissionSet to this Role.
190      *
191      * @param permissionSet A PermissionSet.
192      * @throws TurbineSecurityException if there is a problem while assigning
193      * the Permissions.
194      */

195     public void grant(PermissionSet permissionSet)
196         throws TurbineSecurityException
197     {
198         Iterator JavaDoc permissions = permissionSet.elements();
199         while(permissions.hasNext())
200         {
201             TurbineSecurity.grant(this, (Permission)permissions.next());
202         }
203     }
204
205     /**
206      * Revokes a Permission from this Role.
207      *
208      * @param permission A Permission.
209      * @throws TurbineSecurityException if there is a problem while unassigning
210      * the Permission.
211      */

212     public void revoke(Permission permission)
213         throws TurbineSecurityException
214     {
215         TurbineSecurity.revoke(this, permission);
216     }
217
218     /**
219      * Revokes Permissions from a PermissionSet from this Role.
220      *
221      * @param permissionSet A PermissionSet.
222      * @throws TurbineSecurityException if there is a problem while unassigning
223      * the Permissions.
224      */

225     public void revoke(PermissionSet permissionSet)
226         throws TurbineSecurityException
227     {
228         Iterator JavaDoc permissions = permissionSet.elements();
229         while(permissions.hasNext())
230         {
231             TurbineSecurity.revoke(this, (Permission)permissions.next());
232         }
233     }
234
235     /**
236      * Used for ordering SecurityObjects.
237      *
238      * @param obj The Object to compare to.
239      * @return -1 if the name of the other object is lexically greater than this
240      * group, 1 if it is lexically lesser, 0 if they are equal.
241      */

242     public int compareTo(Object JavaDoc obj)
243     {
244         if(this.getClass() != obj.getClass())
245             throw new ClassCastException JavaDoc();
246         String JavaDoc name1 = ((SecurityEntity)obj).getName();
247         String JavaDoc name2 = this.getName();
248
249         return name2.compareTo(name1);
250     }
251
252 }
253
Popular Tags