1 17 18 package org.apache.catalina.mbeans; 19 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 24 import javax.management.MBeanException ; 25 import javax.management.MBeanServer ; 26 import javax.management.MalformedObjectNameException ; 27 import javax.management.ObjectName ; 28 import javax.management.RuntimeOperationsException ; 29 30 import org.apache.catalina.Group; 31 import org.apache.catalina.Role; 32 import org.apache.catalina.User; 33 import org.apache.tomcat.util.modeler.BaseModelMBean; 34 import org.apache.tomcat.util.modeler.ManagedBean; 35 import org.apache.tomcat.util.modeler.Registry; 36 37 44 45 public class UserMBean extends BaseModelMBean { 46 47 48 50 51 60 public UserMBean() 61 throws MBeanException , RuntimeOperationsException { 62 63 super(); 64 65 } 66 67 68 70 71 74 protected Registry registry = MBeanUtils.createRegistry(); 75 76 77 80 protected MBeanServer mserver = MBeanUtils.createServer(); 81 82 83 86 protected ManagedBean managed = 87 registry.findManagedBean("User"); 88 89 90 92 93 96 public String [] getGroups() { 97 98 User user = (User) this.resource; 99 ArrayList results = new ArrayList (); 100 Iterator groups = user.getGroups(); 101 while (groups.hasNext()) { 102 Group group = null; 103 try { 104 group = (Group) groups.next(); 105 ObjectName oname = 106 MBeanUtils.createObjectName(managed.getDomain(), group); 107 results.add(oname.toString()); 108 } catch (MalformedObjectNameException e) { 109 IllegalArgumentException iae = new IllegalArgumentException 110 ("Cannot create object name for group " + group); 111 iae.initCause(e); 112 throw iae; 113 } 114 } 115 return ((String []) results.toArray(new String [results.size()])); 116 117 } 118 119 120 123 public String [] getRoles() { 124 125 User user = (User) this.resource; 126 ArrayList results = new ArrayList (); 127 Iterator roles = user.getRoles(); 128 while (roles.hasNext()) { 129 Role role = null; 130 try { 131 role = (Role) roles.next(); 132 ObjectName oname = 133 MBeanUtils.createObjectName(managed.getDomain(), role); 134 results.add(oname.toString()); 135 } catch (MalformedObjectNameException e) { 136 IllegalArgumentException iae = new IllegalArgumentException 137 ("Cannot create object name for role " + role); 138 iae.initCause(e); 139 throw iae; 140 } 141 } 142 return ((String []) results.toArray(new String [results.size()])); 143 144 } 145 146 147 149 150 155 public void addGroup(String groupname) { 156 157 User user = (User) this.resource; 158 if (user == null) { 159 return; 160 } 161 Group group = user.getUserDatabase().findGroup(groupname); 162 if (group == null) { 163 throw new IllegalArgumentException 164 ("Invalid group name '" + groupname + "'"); 165 } 166 user.addGroup(group); 167 168 } 169 170 171 176 public void addRole(String rolename) { 177 178 User user = (User) this.resource; 179 if (user == null) { 180 return; 181 } 182 Role role = user.getUserDatabase().findRole(rolename); 183 if (role == null) { 184 throw new IllegalArgumentException 185 ("Invalid role name '" + rolename + "'"); 186 } 187 user.addRole(role); 188 189 } 190 191 192 197 public void removeGroup(String groupname) { 198 199 User user = (User) this.resource; 200 if (user == null) { 201 return; 202 } 203 Group group = user.getUserDatabase().findGroup(groupname); 204 if (group == null) { 205 throw new IllegalArgumentException 206 ("Invalid group name '" + groupname + "'"); 207 } 208 user.removeGroup(group); 209 210 } 211 212 213 218 public void removeRole(String rolename) { 219 220 User user = (User) this.resource; 221 if (user == null) { 222 return; 223 } 224 Role role = user.getUserDatabase().findRole(rolename); 225 if (role == null) { 226 throw new IllegalArgumentException 227 ("Invalid role name '" + rolename + "'"); 228 } 229 user.removeRole(role); 230 231 } 232 233 234 } 235 | Popular Tags |