1 22 package javax.management.relation; 23 24 import java.util.ArrayList ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 28 import javax.management.MBeanServer ; 29 import javax.management.MBeanException ; 30 import javax.management.ObjectName ; 31 32 49 class RoleValidator 50 { 51 53 55 67 public static int checkRole(ObjectName relationService, MBeanServer server, 68 String relationTypeName, Role role, boolean write) 69 throws RelationTypeNotFoundException 70 { 71 RoleInfo roleInfo = null; 73 try 74 { 75 roleInfo = (RoleInfo ) server.invoke(relationService, "getRoleInfo", 76 new Object [] { relationTypeName, role.getRoleName() }, 77 new String [] { "java.lang.String", "java.lang.String" }); 78 } 79 catch (MBeanException mbe) 80 { 81 Exception e=mbe.getTargetException(); 82 if (e instanceof RelationTypeNotFoundException ) 83 throw (RelationTypeNotFoundException ) e; 84 if (e instanceof RoleInfoNotFoundException ) 85 return RoleStatus.NO_ROLE_WITH_NAME; 86 throw new RuntimeException (e.toString()); 87 } 88 catch (Exception e) 89 { 90 throw new RuntimeException (e.toString()); 91 } 92 93 if (write == true && roleInfo.isWritable() == false) 95 return RoleStatus.ROLE_NOT_WRITABLE; 96 97 ArrayList mbeans = (ArrayList ) role.getRoleValue(); 99 int beanCount = mbeans.size(); 100 int minimum = roleInfo.getMinDegree(); 101 if (minimum != RoleInfo.ROLE_CARDINALITY_INFINITY && minimum > beanCount) 102 return RoleStatus.LESS_THAN_MIN_ROLE_DEGREE; 103 int maximum = roleInfo.getMaxDegree(); 104 if (maximum != RoleInfo.ROLE_CARDINALITY_INFINITY && maximum < beanCount) 105 return RoleStatus.MORE_THAN_MAX_ROLE_DEGREE; 106 107 String className = roleInfo.getRefMBeanClassName(); 109 Iterator iterator = mbeans.iterator(); 110 while (iterator.hasNext()) 111 { 112 try 113 { 114 ObjectName objectName = (ObjectName ) iterator.next(); 115 if (server.isInstanceOf(objectName, className) == false) 116 return RoleStatus.REF_MBEAN_OF_INCORRECT_CLASS; 117 } 118 catch (Exception e) 119 { 120 return RoleStatus.REF_MBEAN_NOT_REGISTERED; 121 } 122 } 123 return 0; 125 } 126 127 139 public static RoleResult checkRoles(ObjectName relationService, 140 MBeanServer server, String relationTypeName, RoleList roleList, 141 boolean write) 142 throws RelationTypeNotFoundException 143 { 144 RoleList resolved = new RoleList (); 146 RoleUnresolvedList unresolved = new RoleUnresolvedList (); 147 RoleResult result = new RoleResult (resolved, unresolved); 148 149 Iterator iterator = roleList.iterator(); 151 while (iterator.hasNext()) 152 { 153 Role role = (Role ) iterator.next(); 154 int status = checkRole(relationService, server, relationTypeName, role, 155 write); 156 if (status == 0) 157 resolved.add(role); 158 else 159 unresolved.add(new RoleUnresolved (role.getRoleName(), 160 role.getRoleValue(), status)); 161 } 162 return result; 164 } 165 166 181 public static void validateRole(ObjectName relationService, 182 MBeanServer server, String relationTypeName, Role role, boolean write) 183 throws InvalidRoleValueException , RelationTypeNotFoundException , 184 RoleNotFoundException 185 { 186 int status = checkRole(relationService, server, relationTypeName, role, 187 write); 188 189 if (status == RoleStatus.NO_ROLE_WITH_NAME) 190 throw new RoleNotFoundException (role.getRoleName()); 191 if (status == RoleStatus.ROLE_NOT_WRITABLE) 192 throw new RoleNotFoundException (role.getRoleName() + " not writable"); 193 else if (status != 0) 194 throw new InvalidRoleValueException (role.getRoleName()); 195 } 196 197 213 public static void validateRoles(ObjectName relationService, 214 MBeanServer server, String relationTypeName, RoleList roleList, 215 boolean write) 216 throws InvalidRoleValueException , RelationTypeNotFoundException , 217 RoleNotFoundException 218 { 219 Iterator iterator; 220 221 HashSet roleNames = new HashSet (); 223 iterator = roleList.iterator(); 224 while (iterator.hasNext()) 225 { 226 Object roleName = iterator.next(); 227 if (roleNames.contains(roleName)) 228 throw new InvalidRoleValueException ("Duplicate role " + roleName); 229 roleNames.add(roleName); 230 } 231 232 RoleResult result = checkRoles(relationService, server, relationTypeName, 234 roleList, write); 235 RoleUnresolvedList errors = result.getRolesUnresolved(); 236 iterator = errors.iterator(); 237 if (iterator.hasNext()) 238 { 239 RoleUnresolved unresolved = (RoleUnresolved ) iterator.next(); 240 int status = unresolved.getProblemType(); 241 if (status == RoleStatus.NO_ROLE_WITH_NAME) 242 throw new RoleNotFoundException (unresolved.getRoleName()); 243 if (status == RoleStatus.ROLE_NOT_WRITABLE) 244 throw new RoleNotFoundException (unresolved.getRoleName() + " not writable"); 245 else 246 throw new InvalidRoleValueException (unresolved.getRoleName()); 247 } 248 } 249 } 250 | Popular Tags |