KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > core > role > lib > RoleProperty


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2000-2005 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy, Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================
26 $Id: RoleProperty.java,v 1.2 2005/07/06 15:36:01 moroy Exp $
27 ====================================================================*/

28
29 package org.objectweb.util.explorer.core.role.lib;
30
31 import java.util.Arrays JavaDoc;
32 import java.util.Comparator JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Vector JavaDoc;
37
38 import org.objectweb.util.explorer.ExplorerUtils;
39 import org.objectweb.util.explorer.core.common.lib.BindingFeature;
40 import org.objectweb.util.explorer.core.role.api.Role;
41 import org.objectweb.util.explorer.core.role.api.RoleDescription;
42 import org.objectweb.util.explorer.core.role.api.RoleFeeder;
43 import org.objectweb.util.explorer.core.role.api.RoleManager;
44 import org.objectweb.util.explorer.core.role.api.RoleProvider;
45
46 /**
47  * <p>
48  * Basic implementation of the RoleProperty component.
49  * </p>
50  *
51  * <p>
52  * This class is able to accept <code>RoleDescription</code> objects.
53  * And then, it may provide a role defined by an ID and an array of inherited
54  * roles for an array of roles. The corresponding algorithm browses the tree
55  * by privileging its width.
56  * </p>
57  *
58  * <p>
59  * The following pictures presents an example of roles inheritance.
60  * </p>
61  * <img SRC="role_inheritance.png">
62  * <p>
63  * For instance:
64  * <ul>
65  * <li>a call to <code>getInheritedRoles({3,5})</code> will provide a array containing
66  * the roles defined by the given ID: <code>{3,5,6,8,9,12}</code></li>.
67  * <li>a call to <code>getInheritedRoles({0})</code> will provide a array containing
68  * the roles defined by the given ID: <code>{0,1,4,2,3,7,5,6,10,11,8,9,12}</code></li>.
69  * </ul>
70  * </p>
71  *
72  * <p>
73  * Limit: The algorithm corresponding to the <code>getInheritedRoles()</code> method doesn't know the level of each role it treats.
74  * That's why a call to <code>getInheritedRoles({1,5})</code> will provide a array containing
75  * the roles defined by the given ID: <code>{1,5,3,8,9,6,10,12}</code> and not <code>{1,5,3,6,10,8,9,12}</code>
76  * as we could waiting for.
77  *
78  * </p>
79  *
80  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>,
81  * <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>.
82  *
83  * @version 0.1
84  */

85 public class RoleProperty
86      extends BindingFeature
87   implements RoleFeeder, RoleProvider, RoleManager
88 {
89
90     //==================================================================
91
//
92
// Internal States.
93
//
94
// ==================================================================
95

96     /** The map where the Roles are stored. */
97     protected Map JavaDoc roleMap_ = null;
98
99     /** This map contains the first level of inherited roles of a role. */
100     protected Map JavaDoc inheritedRolesMap_ = null;
101     
102     /** The list of the current roles ids. */
103     protected String JavaDoc[] currentRoleIds_ = new String JavaDoc[]{RoleProvider.DEFAULT_ROLE};
104     
105     // ==================================================================
106
//
107
// Constructors.
108
//
109
// ==================================================================
110

111     /**
112      * Default Constructor.
113      */

114     public RoleProperty(){
115         roleMap_ = new HashMap JavaDoc();
116         inheritedRolesMap_ = new HashMap JavaDoc();
117     }
118     
119     // ==================================================================
120
//
121
// Internal method.
122
//
123
// ==================================================================
124

125     /**
126      * Added the inherited role ids of the given roleIds in a list.
127      * Note that the initial role ids list are included into the returned list
128      * @param roleIds An array of role ids.
129      * @param roleIdsList The vector in which the inherited roles ids have to be added.
130      */

131     protected void buildInheritedRolesList(String JavaDoc[] roleIds, Vector JavaDoc roleIdsList){
132         
133         // Adds the role ids into the list
134
for(int i=0 ; i<roleIds.length ; i++){
135             if(!roleIdsList.contains(roleIds[i])){
136                 roleIdsList.add(roleIds[i]);
137             }
138         }
139         
140         // Creates a list containing all the role extended by the given roles array.
141
Vector JavaDoc allExtendedRoleIds = new Vector JavaDoc();
142         for(int i=0 ; i<roleIds.length ; i++){
143             String JavaDoc[] inheritedRoles = (String JavaDoc[])inheritedRolesMap_.get(roleIds[i]);
144             String JavaDoc roleId = null;
145             // The inherited role might be null if the defined role doesn't exist into the role map.
146
if(inheritedRoles!=null){
147                 for(int j=0 ; j<inheritedRoles.length ; j++){
148                     roleId = inheritedRoles[j];
149                     if(roleMap_.containsKey(roleId) && !roleIdsList.contains(roleId)) {
150                         allExtendedRoleIds.add(roleId);
151                     }
152                 }
153             }
154         }
155
156         // Adds the roles ids into the list and call this method with the new array
157
if(allExtendedRoleIds.size()!=0){
158             buildInheritedRolesList((String JavaDoc[])allExtendedRoleIds.toArray(new String JavaDoc[allExtendedRoleIds.size()]), roleIdsList);
159         }
160         
161     }
162
163     /**
164      * Provides the inherited roles of the given role.
165      * If no inherited node is defined, an empty array is return.
166      * @param role The role.
167      * @return An array containing the inherited roles for the given role.
168      */

169     protected String JavaDoc[] provideInheritedRoleIds(String JavaDoc[] roleIds){
170         Vector JavaDoc roleList = new Vector JavaDoc();
171         buildInheritedRolesList(roleIds, roleList);
172         if(!roleList.contains(RoleProvider.DEFAULT_ROLE)){
173             roleList.add(RoleProvider.DEFAULT_ROLE);
174         }
175         return (String JavaDoc[])roleList.toArray(new String JavaDoc[roleList.size()]);
176     }
177         
178     /**
179      * Adds the contents of the given role description into the current list of
180      * inherited roles.
181      * @param roleId The ID of the role.
182      * @param roleDesc The description of the role to add.
183      */

184     protected void mergeInheritedRoles(String JavaDoc roleId, RoleDescription roleDesc){
185         String JavaDoc[] currentInheritedRoles = (String JavaDoc[])inheritedRolesMap_.get(roleId);
186         String JavaDoc[] inheritedRolesToAdd = roleDesc.getInheritance();
187         inheritedRolesMap_.put(roleId, ExplorerUtils.mergeArrays(currentInheritedRoles, inheritedRolesToAdd));
188     }
189     
190     /**
191      * Adds the given role description into the current role definition.
192      * @param currentRole The current role definition.
193      * @param roleDesc The role description to add.
194      * @return The current role merged with the given role description.
195      */

196     protected void mergeRole(Role currentRole, RoleDescription roleDesc){
197         if(!roleDesc.isConcrete())
198             currentRole.setConcrete(false);
199     }
200     
201     /**
202      * Stores the given roleDesc into the
203      */

204     protected void storeRoleDesc(RoleDescription roleDesc){
205         Role theRole = null;
206         String JavaDoc roleId = roleDesc.getId();
207         if(roleMap_.containsKey(roleId)){
208             mergeRole((Role)roleMap_.get(roleId), roleDesc);
209             mergeInheritedRoles(roleId, roleDesc);
210         } else {
211             theRole = new DefaultRole(roleId);
212             theRole.setConcrete(roleDesc.isConcrete());
213             roleMap_.put(roleId, theRole);
214             inheritedRolesMap_.put(roleId, roleDesc.getInheritance()!=null?roleDesc.getInheritance():new String JavaDoc[]{});
215         }
216     }
217
218     protected void fixCurrentRoleIds(String JavaDoc[] roleIds){
219         if(roleIds!=null) {
220             currentRoleIds_ = roleIds;
221         } else {
222             currentRoleIds_ = new String JavaDoc[0];
223         }
224     }
225     
226     // ==================================================================
227
//
228
// Public methods surcharging BindingFeature class.
229
//
230
// ==================================================================
231

232     /* (non-Javadoc)
233      * @see org.objectweb.util.explorer.core.common.lib.BindingFeature#clientFc()
234      */

235     public String JavaDoc[] clientFc() {
236         return new String JavaDoc[]{RoleFeeder.ROLE_FEEDER, RoleProvider.ROLE_PROVIDER};
237     }
238
239     // ==================================================================
240
//
241
// Public methods for RoleFeeder interface.
242
//
243
// ==================================================================
244

245     /* (non-Javadoc)
246      * @see org.objectweb.util.explorer.core.role.api.RoleFeeder#feed(org.objectweb.util.explorer.core.role.api.RoleDescription)
247      */

248     public void feed(RoleDescription roleDesc) {
249         storeRoleDesc(roleDesc);
250     }
251
252     // ==================================================================
253
//
254
// Public methods for RoleProvider interface.
255
//
256
// ==================================================================
257

258     /* (non-Javadoc)
259      * @see org.objectweb.util.explorer.core.role.api.RoleProvider#getRole(java.lang.String)
260      */

261     public Role getRole(String JavaDoc id) {
262         return (Role)roleMap_.get(id);
263     }
264
265     /* (non-Javadoc)
266      * @see org.objectweb.util.explorer.core.role.api.RoleProvider#getRoleIds(org.objectweb.util.explorer.core.role.api.Role[])
267      */

268     public String JavaDoc[] getRoleIds(Role[] roles) {
269         String JavaDoc[] roleIds = new String JavaDoc[roles.length];
270         for (int i = 0; i < roles.length; i++) {
271             roleIds[i] = roles[i].getId();
272         }
273         return roleIds;
274     }
275     
276     /* (non-Javadoc)
277      * @see org.objectweb.util.explorer.core.role.api.RoleProvider#getRoles(java.lang.String[])
278      */

279     public Role[] getRoles(String JavaDoc[] roleIds) {
280         Vector JavaDoc v = new Vector JavaDoc();
281         Role r = null;
282         for (int i = 0; i < roleIds.length; i++) {
283             r = getRole(roleIds[i]);
284             if(r!=null){
285                 v.add(r);
286             }
287         }
288         return (Role[])v.toArray(new Role[v.size()]);
289     }
290         
291     /* (non-Javadoc)
292      * @see org.objectweb.util.explorer.core.role.api.RoleProvider#getInheritedRoleIds(java.lang.String[])
293      */

294     public String JavaDoc[] getInheritedRoleIds(String JavaDoc[] roleIds) {
295         return provideInheritedRoleIds(roleIds);
296     }
297     
298     /* (non-Javadoc)
299      * @see org.objectweb.util.explorer.core.role.api.RoleProvider#getDefaultRole()
300      */

301     public Role getDefaultRole() {
302         return (Role)roleMap_.get(RoleProvider.DEFAULT_ROLE);
303     }
304     
305     /* (non-Javadoc)
306      * @see org.objectweb.util.explorer.core.role.api.RoleProvider#getRoleList()
307      */

308     public String JavaDoc[] getRoleList() {
309         return getRoleList(false);
310     }
311     
312     /* (non-Javadoc)
313      * @see org.objectweb.util.explorer.core.role.api.RoleProvider#getRoleList(boolean)
314      */

315     public String JavaDoc[] getRoleList(boolean alphabeticOrder) {
316         return getRoleList(false, false);
317     }
318
319     /* (non-Javadoc)
320      * @see org.objectweb.util.explorer.core.role.api.RoleProvider#getRoleList(boolean, boolean)
321      */

322     public String JavaDoc[] getRoleList(boolean alphabeticOrder, boolean excludedAbstractRoles) {
323         String JavaDoc[] roleIds = (String JavaDoc[])roleMap_.keySet().toArray(new String JavaDoc[roleMap_.size()]);
324         if(alphabeticOrder){
325             Arrays.sort(roleIds, new RoleComparator());
326         }
327         if(excludedAbstractRoles){
328             List JavaDoc l = new Vector JavaDoc();
329             for(int i=0;i<roleIds.length;i++){
330                 if(getRole(roleIds[i]).isConcrete()){
331                     l.add(roleIds[i]);
332                 }
333             }
334             return (String JavaDoc[])l.toArray(new String JavaDoc[l.size()]);
335         }
336         return roleIds;
337     }
338     
339     // ==================================================================
340
//
341
// Public methods for RoleManager interface.
342
//
343
// ==================================================================
344

345     /* (non-Javadoc)
346      * @see org.objectweb.util.explorer.core.role.api.RoleManager#getCurrentRoleIds()
347      */

348     public String JavaDoc[] getCurrentRoleIds() {
349         return currentRoleIds_;
350     }
351     
352     /* (non-Javadoc)
353      * @see org.objectweb.util.explorer.core.role.api.RoleManager#setCurrentRoleIds(java.lang.String[])
354      */

355     public void setCurrentRoleIds(String JavaDoc[] roleIds) {
356         fixCurrentRoleIds(roleIds);
357     }
358     
359     // ==================================================================
360
//
361
// Private class.
362
//
363
// ==================================================================
364

365     /**
366      * This class compares two roles depending on their alphabetic order.
367      *
368      * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>,
369      * <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>.
370      *
371      * @version 0.1
372      */

373     private final class RoleComparator implements Comparator JavaDoc{
374
375         protected int compare(String JavaDoc id1, String JavaDoc id2) {
376             return id1.compareTo(id2);
377         }
378
379         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
380             return compare((String JavaDoc)o1,(String JavaDoc)o2);
381         }
382
383     }
384 }
385
386
Popular Tags