1 /**
2 *
3 * Copyright 2004, 2005 The Apache Software Foundation or its licensors, as applicable.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.console.util;
19
20 import java.util.Collection;
21 import java.util.Hashtable;
22 import java.util.Set;
23
24 import org.apache.geronimo.kernel.Kernel;
25 import org.apache.geronimo.kernel.KernelRegistry;
26
27 public class SERealmGroupHelper extends RealmHelper {
28
29 private static final String GET_GROUPS_FUNCTION = "getGroups";
30
31 private static final String ADD_GROUP_FUNCTION = "addGroupPrincipal";
32
33 private static final String GROUP_EXISTS_FUNCTION = "groupExists";
34
35 private static final String UPDATE_GROUP_FUNCTION = "updateGroupPrincipal";
36
37 private static final String DELETE_GROUP_FUNCTION = "removeGroupPrincipal";
38
39 private static final String GET_USERS_FUNCTION = "getGroupMembers";
40
41 private static final String[] STRING = { "java.lang.String" };
42
43 private static final String[] HASHTABLE = { "java.util.Hashtable" };
44
45 private static final Kernel kernel = KernelRegistry.getSingleKernel();
46
47 public static String[] getGroups() throws Exception {
48 String[] groups = (String[]) invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME, GET_GROUPS_FUNCTION);
49 return groups;
50 }
51
52 private static void refresh() {
53 try {
54
55 kernel.stopGBean(ObjectNameConstants.SE_REALM_MBEAN_NAME);
56 kernel.startGBean(ObjectNameConstants.SE_REALM_MBEAN_NAME);
57 } catch (Exception e) {
58 }
59 }
60
61 public static void addGroup(String groupName, String[] userList)
62 throws Exception {
63 addGroup(groupName, StringUtils.convertToCommaDelimited(userList));
64 refresh();
65 }
66
67 public static void updateGroup(String groupName, String[] userList)
68 throws Exception {
69 updateGroup(groupName, StringUtils.convertToCommaDelimited(userList));
70 refresh();
71 }
72
73 public static boolean groupExists(String username) throws Exception {
74 Boolean ret;
75 String[] arg = {username};
76 ret = (Boolean) invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME, GROUP_EXISTS_FUNCTION, arg, STRING);
77 return ret.booleanValue();
78 }
79
80 public static void addGroup(String groupName, String userList)
81 throws Exception {
82
83 Hashtable props = new Hashtable();
84 props.put("GroupName", groupName);
85 props.put("Members", userList);
86 Object[] args = {props};
87 invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME, ADD_GROUP_FUNCTION, args, HASHTABLE);
88 }
89
90 public static void updateGroup(String groupName, String userList)
91 throws Exception {
92 Hashtable props = new Hashtable();
93 props.put("GroupName", groupName);
94 props.put("Members", userList);
95 Object[] args = {props};
96
97 invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME, UPDATE_GROUP_FUNCTION, args, HASHTABLE);
98 }
99
100 public static void deleteGroup(String groupName) throws Exception {
101 String[] args = {groupName};
102 invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME, DELETE_GROUP_FUNCTION, args, STRING);
103 refresh();
104 }
105
106 public static Set getUsers(String groupName) throws Exception {
107 Set ret = null;
108 String[] arg = {groupName};
109 ret = (Set) invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME, GET_USERS_FUNCTION, arg, STRING);
110 return ret;
111 }
112
113 public static boolean isGroupMember(String groupName, String username)
114 throws Exception {
115 Collection users = getUsersAsCollection(groupName);
116 return (users.contains(username));
117 }
118
119 private static Collection getUsersAsCollection(String groupName)
120 throws Exception {
121 return getUsers(groupName);
122 }
123
124 }