KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > usermanager > GroupRoleUtils


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
// GroupRoleUtils
14
// MJ 09.03.2001
15
//
16
//
17

18 package org.jahia.services.usermanager;
19
20 import org.apache.regexp.RE;
21 import org.apache.regexp.RESyntaxException;
22 import org.jahia.data.fields.JahiaField;
23 import org.jahia.data.fields.LoadFlags;
24 import org.jahia.exceptions.JahiaException;
25 import org.jahia.registries.ServicesRegistry;
26 import org.jahia.services.pages.JahiaPage;
27
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Vector JavaDoc;
31
32
33 /**
34  * Utility class to manipulate groupnames that represent application roles.
35  *
36  * @author MJ
37  */

38
39
40 public class GroupRoleUtils {
41     private static org.apache.log4j.Logger logger =
42             org.apache.log4j.Logger.getLogger (GroupRoleUtils.class);
43
44     private static RE groupNameRE;
45     private static ServicesRegistry sReg;
46     private static JahiaGroupManagerService gMgr;
47
48     static {
49         try {
50
51             // example : 2_4_administrator
52
// is a group defining the administrator role
53
// for application 2 in container 4
54
String JavaDoc groupNamePattern = "^([0-9]+)_([0-9]+)_(.+)$";
55             groupNameRE = new RE (groupNamePattern);
56
57         } catch (RESyntaxException e) {
58             String JavaDoc errorMsg = "RE exception : " + e.getMessage ();
59             logger.error (errorMsg);
60         }
61     }
62
63
64     /**
65      * Translates a programmatic group role name into human readable format.
66      *
67      * @param groupname The group name to check.
68      *
69      * @return A <code>String</code> representing the human readable group name.
70      */

71     public static String JavaDoc translate (String JavaDoc groupname) {
72         if ((groupname != null) && (groupNameRE.match (groupname))) {
73             HashMap JavaDoc roleInfo = decompose (groupname);
74             StringBuffer JavaDoc buf = new StringBuffer JavaDoc ();
75             buf.append ((String JavaDoc) roleInfo.get ("fieldName"));
76             buf.append (":");
77             buf.append ((String JavaDoc) roleInfo.get ("appName"));
78             buf.append (":");
79             buf.append ((String JavaDoc) roleInfo.get ("roleName"));
80
81             return buf.toString ();
82         } else {
83             return groupname;
84         }
85     }
86
87
88     /**
89      * Get the Field ID out of an application role name.
90      *
91      * @param groupname The group name to check.
92      *
93      * @return the associated Field ID, as an <code>int</code>
94      */

95     public static int getfieldID (String JavaDoc groupname) {
96         int fieldID = 0;
97         if ((groupname != null) && (groupNameRE.match (groupname))) {
98             HashMap JavaDoc groupInfo = decompose (groupname);
99             Integer JavaDoc fieldIDObj = (Integer JavaDoc) groupInfo.get ("fieldID");
100             fieldID = fieldIDObj.intValue ();
101         }
102         return fieldID;
103     }
104
105
106     /**
107      * Get a list of groups serving as roles for an application, given the application's ID.
108      *
109      * @param appID The ID of the application to get the roles for.
110      *
111      * @return a <code>Vector</code> of Strings containing all the role names for the application.
112      */

113     public static Vector JavaDoc getRolesForApp (int appID) {
114         Vector JavaDoc rolesList = new Vector JavaDoc ();
115
116         // obtain an instance of the Group Manager Service, and the list of all group names...
117
ServicesRegistry sReg = ServicesRegistry.getInstance ();
118         if (sReg != null) {
119             gMgr = sReg.getJahiaGroupManagerService ();
120             if (gMgr != null) {
121                 Vector JavaDoc groupnames = gMgr.getGroupnameList ();
122                 Enumeration JavaDoc enumeration = groupnames.elements ();
123
124                 // dump all group names matching the specified application ID into a Vector
125
while (enumeration.hasMoreElements ()) {
126                     String JavaDoc groupname = (String JavaDoc) enumeration.nextElement ();
127                     if (isRole (groupname)) {
128                         if (getAppID (groupname) == appID) {
129                             rolesList.add (groupname);
130                         }
131                     }
132                 }
133             }
134         }
135         return rolesList;
136     }
137
138
139     /**
140      * Get a list of groups serving as roles for an application, given the application's name.
141      *
142      * @param appName The name of the application to get the roles for.
143      *
144      * @return a <code>Vector</code> of Strings containing all the role names for the application.
145      */

146     public static Vector JavaDoc getRolesForApp (String JavaDoc appName) {
147         Vector JavaDoc rolesList = new Vector JavaDoc ();
148
149         // obtain an instance of the Group Manager Service, and the list of all group names...
150
sReg = ServicesRegistry.getInstance ();
151         if (sReg != null) {
152             gMgr = sReg.getJahiaGroupManagerService ();
153             if (gMgr != null) {
154                 Vector JavaDoc groupnames = gMgr.getGroupnameList ();
155                 Enumeration JavaDoc enumeration = groupnames.elements ();
156
157                 // dump all group names matching the specified application name into a Vector
158
while (enumeration.hasMoreElements ()) {
159                     String JavaDoc groupname = (String JavaDoc) enumeration.nextElement ();
160                     if (isRole (groupname)) {
161                         if (getAppName (groupname).equals (appName)) {
162                             rolesList.add (groupname);
163                         }
164                     }
165                 }
166             }
167         }
168         return rolesList;
169     }
170
171
172     /**
173      * Get the Application ID out of an application role name.
174      *
175      * @param groupname The group name to check.
176      *
177      * @return the associated Application ID, as an <code>int</code>
178      */

179     public static int getAppID (String JavaDoc groupname) {
180         int appID = 0;
181         if ((groupname != null) && (groupNameRE.match (groupname))) {
182             HashMap JavaDoc groupInfo = decompose (groupname);
183             Integer JavaDoc appIDObj = (Integer JavaDoc) groupInfo.get ("appID");
184             appID = appIDObj.intValue ();
185         }
186         return appID;
187     }
188
189
190     /**
191      * Get the Field name out of an application role name.
192      *
193      * @param groupname The group name to check.
194      *
195      * @return the associated Field name, as a <code>String</code>
196      */

197     public static String JavaDoc getFieldName (String JavaDoc groupname) {
198         String JavaDoc fieldName = "unknown";
199         if ((groupname != null) && (groupNameRE.match (groupname))) {
200             HashMap JavaDoc groupInfo = decompose (groupname);
201             fieldName = (String JavaDoc) groupInfo.get ("fieldName");
202         }
203         return fieldName;
204     }
205
206
207     /**
208      * Get the Application name out of an application role name.
209      *
210      * @param groupname The group name to check.
211      *
212      * @return the associated Application name, as a <code>String</code>
213      */

214     public static String JavaDoc getAppName (String JavaDoc groupname) {
215         String JavaDoc appName = "unknown";
216         if ((groupname != null) && (groupNameRE.match (groupname))) {
217             HashMap JavaDoc groupInfo = decompose (groupname);
218             appName = (String JavaDoc) groupInfo.get ("appName");
219         }
220         return appName;
221     }
222
223
224     /**
225      * Get the Role name out of an application role name.
226      *
227      * @param groupname The group name to check.
228      *
229      * @return the associated Role name, as a <code>String</code>
230      */

231     public static String JavaDoc getRoleName (String JavaDoc groupname) {
232         String JavaDoc roleName = "unknown";
233         if ((groupname != null) && (groupNameRE.match (groupname))) {
234             HashMap JavaDoc groupInfo = decompose (groupname);
235             roleName = (String JavaDoc) groupInfo.get ("roleName");
236         }
237         return roleName;
238     }
239
240
241     /**
242      * Utility method to decompose a programmatic application role name into info values.
243      *
244      * @param groupname The group name to decompose.
245      *
246      * @return A <code>HashMap</code> containing decomposed role info values :
247      * (appID, fieldID, appName, fieldName, roleName).
248      */

249     private static HashMap JavaDoc decompose (String JavaDoc groupname) {
250         // set default values...
251
HashMap JavaDoc roleInfo = new HashMap JavaDoc ();
252
253         // application ID is what's left of 1st undrescore, remains is what's right of it
254
String JavaDoc appIDStr = groupname.substring (0, groupname.indexOf ("_"));
255
256         String JavaDoc remains = groupname.substring ((groupname.indexOf ("_") + 1));
257
258         // fieldID is what's left of 1st underscore in remains, role name is what's right of it
259
String JavaDoc fieldIDStr = remains.substring (0, remains.indexOf ("_"));
260
261         try {
262
263             // get the name of the app
264
String JavaDoc appName = ServicesRegistry.getInstance ().getJahiaApplicationsManagerService ()
265                     .getApplication (Integer.parseInt (appIDStr))
266                     .getName ();
267
268             // get the name of the field
269
String JavaDoc fieldName = null;
270             int fieldID = Integer.parseInt (fieldIDStr);
271             JahiaField field = ServicesRegistry.getInstance ().getJahiaFieldService ().loadField (
272                     fieldID, LoadFlags.NOTHING);
273             if (field == null) {
274                 fieldName = "unknown";
275             } else {
276                 int pageID = field.getPageID ();
277                 JahiaPage page = ServicesRegistry.getInstance ().getJahiaPageService ()
278                         .lookupPage (pageID);
279                 fieldName = page.getTitle ();
280             }
281
282             // get the name of the role
283
String JavaDoc roleName = remains.substring ((remains.indexOf ("_") + 1));
284
285             roleInfo.put ("appID", new Integer JavaDoc (appIDStr));
286             roleInfo.put ("fieldID", new Integer JavaDoc (fieldID));
287             roleInfo.put ("appName", appName);
288             roleInfo.put ("fieldName", fieldName);
289             roleInfo.put ("roleName", roleName);
290
291         } catch (JahiaException je) {
292             //
293
} catch (NumberFormatException JavaDoc nfe) {
294             //
295
}
296
297         return roleInfo;
298     }
299
300
301     /**
302      * Determines if a group represents an application role or not.
303      *
304      * @param groupname The group name to check.
305      *
306      * @return A <code>String</code> representing the human readable group name.
307      */

308     public static boolean isRole (String JavaDoc groupname) {
309         return (groupname != null && groupNameRE.match (groupname));
310     }
311
312 }
313
Popular Tags