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 GroupMBean extends BaseModelMBean { 46 47 48 50 51 60 public GroupMBean() 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("Group"); 88 89 90 92 93 96 public String [] getRoles() { 97 98 Group group = (Group) this.resource; 99 ArrayList results = new ArrayList (); 100 Iterator roles = group.getRoles(); 101 while (roles.hasNext()) { 102 Role role = null; 103 try { 104 role = (Role) roles.next(); 105 ObjectName oname = 106 MBeanUtils.createObjectName(managed.getDomain(), role); 107 results.add(oname.toString()); 108 } catch (MalformedObjectNameException e) { 109 IllegalArgumentException iae = new IllegalArgumentException 110 ("Cannot create object name for role " + role); 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 [] getUsers() { 124 125 Group group = (Group) this.resource; 126 ArrayList results = new ArrayList (); 127 Iterator users = group.getUsers(); 128 while (users.hasNext()) { 129 User user = null; 130 try { 131 user = (User) users.next(); 132 ObjectName oname = 133 MBeanUtils.createObjectName(managed.getDomain(), user); 134 results.add(oname.toString()); 135 } catch (MalformedObjectNameException e) { 136 IllegalArgumentException iae = new IllegalArgumentException 137 ("Cannot create object name for user " + user); 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 addRole(String rolename) { 156 157 Group group = (Group) this.resource; 158 if (group == null) { 159 return; 160 } 161 Role role = group.getUserDatabase().findRole(rolename); 162 if (role == null) { 163 throw new IllegalArgumentException 164 ("Invalid role name '" + rolename + "'"); 165 } 166 group.addRole(role); 167 168 } 169 170 171 176 public void removeRole(String rolename) { 177 178 Group group = (Group) this.resource; 179 if (group == null) { 180 return; 181 } 182 Role role = group.getUserDatabase().findRole(rolename); 183 if (role == null) { 184 throw new IllegalArgumentException 185 ("Invalid role name '" + rolename + "'"); 186 } 187 group.removeRole(role); 188 189 } 190 191 192 } 193 | Popular Tags |