001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.console.util;
019
020 import java.util.Collection;
021 import java.util.Hashtable;
022 import java.util.Set;
023
024 import org.apache.geronimo.kernel.Kernel;
025 import org.apache.geronimo.kernel.KernelRegistry;
026
027 public class SERealmGroupHelper extends RealmHelper {
028
029 private static final String GET_GROUPS_FUNCTION = "getGroups";
030
031 private static final String ADD_GROUP_FUNCTION = "addGroupPrincipal";
032
033 private static final String GROUP_EXISTS_FUNCTION = "groupExists";
034
035 private static final String UPDATE_GROUP_FUNCTION = "updateGroupPrincipal";
036
037 private static final String DELETE_GROUP_FUNCTION = "removeGroupPrincipal";
038
039 private static final String GET_USERS_FUNCTION = "getGroupMembers";
040
041 private static final String[] STRING = { "java.lang.String" };
042
043 private static final String[] HASHTABLE = { "java.util.Hashtable" };
044
045 private static final Kernel kernel = KernelRegistry.getSingleKernel();
046
047 public static String[] getGroups() throws Exception {
048 String[] groups = (String[]) invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME, GET_GROUPS_FUNCTION);
049 return groups;
050 }
051
052 private static void refresh() {
053 try {
054
055 kernel.stopGBean(ObjectNameConstants.SE_REALM_MBEAN_NAME);
056 kernel.startGBean(ObjectNameConstants.SE_REALM_MBEAN_NAME);
057 } catch (Exception e) {
058 }
059 }
060
061 public static void addGroup(String groupName, String[] userList)
062 throws Exception {
063 addGroup(groupName, StringUtils.convertToCommaDelimited(userList));
064 refresh();
065 }
066
067 public static void updateGroup(String groupName, String[] userList)
068 throws Exception {
069 updateGroup(groupName, StringUtils.convertToCommaDelimited(userList));
070 refresh();
071 }
072
073 public static boolean groupExists(String username) throws Exception {
074 Boolean ret;
075 String[] arg = {username};
076 ret = (Boolean) invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME, GROUP_EXISTS_FUNCTION, arg, STRING);
077 return ret.booleanValue();
078 }
079
080 public static void addGroup(String groupName, String userList)
081 throws Exception {
082
083 Hashtable props = new Hashtable();
084 props.put("GroupName", groupName);
085 props.put("Members", userList);
086 Object[] args = {props};
087 invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME, ADD_GROUP_FUNCTION, args, HASHTABLE);
088 }
089
090 public static void updateGroup(String groupName, String userList)
091 throws Exception {
092 Hashtable props = new Hashtable();
093 props.put("GroupName", groupName);
094 props.put("Members", userList);
095 Object[] args = {props};
096
097 invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME, UPDATE_GROUP_FUNCTION, args, HASHTABLE);
098 }
099
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 }