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: */package org.apache.geronimo.console.util;
017:
018: import java.util.Collection;
019: import java.util.Hashtable;
020: import java.util.Set;
021:
022: import org.apache.geronimo.kernel.Kernel;
023: import org.apache.geronimo.kernel.KernelRegistry;
024:
025: public class SERealmGroupHelper extends RealmHelper {
026:
027: private static final String GET_GROUPS_FUNCTION = "getGroups";
028:
029: private static final String ADD_GROUP_FUNCTION = "addGroupPrincipal";
030:
031: private static final String GROUP_EXISTS_FUNCTION = "groupExists";
032:
033: private static final String UPDATE_GROUP_FUNCTION = "updateGroupPrincipal";
034:
035: private static final String DELETE_GROUP_FUNCTION = "removeGroupPrincipal";
036:
037: private static final String GET_USERS_FUNCTION = "getGroupMembers";
038:
039: private static final String[] STRING = { "java.lang.String" };
040:
041: private static final String[] HASHTABLE = { "java.util.Hashtable" };
042:
043: private static final Kernel kernel = KernelRegistry
044: .getSingleKernel();
045:
046: public static String[] getGroups() throws Exception {
047: String[] groups = (String[]) invoke(
048: ObjectNameConstants.SE_REALM_MBEAN_NAME,
049: GET_GROUPS_FUNCTION);
050: return groups;
051: }
052:
053: private static void refresh() {
054: try {
055:
056: kernel.stopGBean(ObjectNameConstants.SE_REALM_MBEAN_NAME);
057: kernel.startGBean(ObjectNameConstants.SE_REALM_MBEAN_NAME);
058: } catch (Exception e) {
059: }
060: }
061:
062: public static void addGroup(String groupName, String[] userList)
063: throws Exception {
064: addGroup(groupName, StringUtils
065: .convertToCommaDelimited(userList));
066: refresh();
067: }
068:
069: public static void updateGroup(String groupName, String[] userList)
070: throws Exception {
071: updateGroup(groupName, StringUtils
072: .convertToCommaDelimited(userList));
073: refresh();
074: }
075:
076: public static boolean groupExists(String username) throws Exception {
077: Boolean ret;
078: String[] arg = { username };
079: ret = (Boolean) invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME,
080: GROUP_EXISTS_FUNCTION, arg, STRING);
081: return ret.booleanValue();
082: }
083:
084: public static void addGroup(String groupName, String userList)
085: throws Exception {
086:
087: Hashtable props = new Hashtable();
088: props.put("GroupName", groupName);
089: props.put("Members", userList);
090: Object[] args = { props };
091: invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME,
092: ADD_GROUP_FUNCTION, args, HASHTABLE);
093: }
094:
095: public static void updateGroup(String groupName, String userList)
096: throws Exception {
097: Hashtable props = new Hashtable();
098: props.put("GroupName", groupName);
099: props.put("Members", userList);
100: Object[] args = { props };
101:
102: invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME,
103: UPDATE_GROUP_FUNCTION, args, HASHTABLE);
104: }
105:
106: public static void deleteGroup(String groupName) throws Exception {
107: String[] args = { groupName };
108: invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME,
109: DELETE_GROUP_FUNCTION, args, STRING);
110: refresh();
111: }
112:
113: public static Set getUsers(String groupName) throws Exception {
114: Set ret = null;
115: String[] arg = { groupName };
116: ret = (Set) invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME,
117: GET_USERS_FUNCTION, arg, STRING);
118: return ret;
119: }
120:
121: public static boolean isGroupMember(String groupName,
122: String username) throws Exception {
123: Collection users = getUsersAsCollection(groupName);
124: return (users.contains(username));
125: }
126:
127: private static Collection getUsersAsCollection(String groupName)
128: throws Exception {
129: return getUsers(groupName);
130: }
131:
132: }
|