KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > util > SERealmGroupHelper


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Set JavaDoc;
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 JavaDoc GET_GROUPS_FUNCTION = "getGroups";
30
31     private static final String JavaDoc ADD_GROUP_FUNCTION = "addGroupPrincipal";
32
33     private static final String JavaDoc GROUP_EXISTS_FUNCTION = "groupExists";
34
35     private static final String JavaDoc UPDATE_GROUP_FUNCTION = "updateGroupPrincipal";
36
37     private static final String JavaDoc DELETE_GROUP_FUNCTION = "removeGroupPrincipal";
38
39     private static final String JavaDoc GET_USERS_FUNCTION = "getGroupMembers";
40
41     private static final String JavaDoc[] STRING = { "java.lang.String" };
42
43     private static final String JavaDoc[] HASHTABLE = { "java.util.Hashtable" };
44
45     private static final Kernel kernel = KernelRegistry.getSingleKernel();
46
47     public static String JavaDoc[] getGroups() throws Exception JavaDoc {
48         String JavaDoc[] groups = (String JavaDoc[]) 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 JavaDoc e) {
58         }
59     }
60
61     public static void addGroup(String JavaDoc groupName, String JavaDoc[] userList)
62             throws Exception JavaDoc {
63         addGroup(groupName, StringUtils.convertToCommaDelimited(userList));
64         refresh();
65     }
66
67     public static void updateGroup(String JavaDoc groupName, String JavaDoc[] userList)
68             throws Exception JavaDoc {
69         updateGroup(groupName, StringUtils.convertToCommaDelimited(userList));
70         refresh();
71     }
72
73     public static boolean groupExists(String JavaDoc username) throws Exception JavaDoc {
74         Boolean JavaDoc ret;
75         String JavaDoc[] arg = {username};
76         ret = (Boolean JavaDoc) invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME, GROUP_EXISTS_FUNCTION, arg, STRING);
77         return ret.booleanValue();
78     }
79
80     public static void addGroup(String JavaDoc groupName, String JavaDoc userList)
81             throws Exception JavaDoc {
82
83         Hashtable JavaDoc props = new Hashtable JavaDoc();
84         props.put("GroupName", groupName);
85         props.put("Members", userList);
86         Object JavaDoc[] args = {props};
87         invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME, ADD_GROUP_FUNCTION, args, HASHTABLE);
88     }
89
90     public static void updateGroup(String JavaDoc groupName, String JavaDoc userList)
91             throws Exception JavaDoc {
92         Hashtable JavaDoc props = new Hashtable JavaDoc();
93         props.put("GroupName", groupName);
94         props.put("Members", userList);
95         Object JavaDoc[] args = {props};
96
97         invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME, UPDATE_GROUP_FUNCTION, args, HASHTABLE);
98     }
99
100     public static void deleteGroup(String JavaDoc groupName) throws Exception JavaDoc {
101         String JavaDoc[] args = {groupName};
102         invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME, DELETE_GROUP_FUNCTION, args, STRING);
103         refresh();
104     }
105
106     public static Set JavaDoc getUsers(String JavaDoc groupName) throws Exception JavaDoc {
107         Set JavaDoc ret = null;
108         String JavaDoc[] arg = {groupName};
109         ret = (Set JavaDoc) invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME, GET_USERS_FUNCTION, arg, STRING);
110         return ret;
111     }
112
113     public static boolean isGroupMember(String JavaDoc groupName, String JavaDoc username)
114             throws Exception JavaDoc {
115         Collection JavaDoc users = getUsersAsCollection(groupName);
116         return (users.contains(username));
117     }
118
119     private static Collection JavaDoc getUsersAsCollection(String JavaDoc groupName)
120             throws Exception JavaDoc {
121         return getUsers(groupName);
122     }
123
124 }
Popular Tags