This is a helper class for performing role validation. It is used by
both the RelationSupport and RelationService classes.
Method from javax.management.relation.RoleValidator Detail: |
public static int checkRole(ObjectName relationService,
MBeanServer server,
String relationTypeName,
Role role,
boolean write) throws RelationTypeNotFoundException {
// Get the role information
RoleInfo roleInfo = null;
try
{
roleInfo = (RoleInfo) server.invoke(relationService, "getRoleInfo",
new Object[] { relationTypeName, role.getRoleName() },
new String[] { "java.lang.String", "java.lang.String" });
}
catch (MBeanException mbe)
{
Exception e=mbe.getTargetException();
if (e instanceof RelationTypeNotFoundException)
throw (RelationTypeNotFoundException) e;
if (e instanceof RoleInfoNotFoundException)
return RoleStatus.NO_ROLE_WITH_NAME;
throw new RuntimeException(e.toString());
}
catch (Exception e)
{
throw new RuntimeException(e.toString());
}
// Check if the role is writable
if (write == true && roleInfo.isWritable() == false)
return RoleStatus.ROLE_NOT_WRITABLE;
// Check the cardinality of the role
ArrayList mbeans = (ArrayList) role.getRoleValue();
int beanCount = mbeans.size();
int minimum = roleInfo.getMinDegree();
if (minimum != RoleInfo.ROLE_CARDINALITY_INFINITY && minimum > beanCount)
return RoleStatus.LESS_THAN_MIN_ROLE_DEGREE;
int maximum = roleInfo.getMaxDegree();
if (maximum != RoleInfo.ROLE_CARDINALITY_INFINITY && maximum < beanCount)
return RoleStatus.MORE_THAN_MAX_ROLE_DEGREE;
// Check the MBeans
String className = roleInfo.getRefMBeanClassName();
for (int i = 0; i < mbeans.size(); i++)
{
try
{
ObjectName objectName = (ObjectName) mbeans.get(i);
if (server.isInstanceOf(objectName, className) == false)
return RoleStatus.REF_MBEAN_OF_INCORRECT_CLASS;
}
catch (Exception e)
{
return RoleStatus.REF_MBEAN_NOT_REGISTERED;
}
}
// All done
return 0;
}
Check a role for a relation type |
public static RoleResult checkRoles(ObjectName relationService,
MBeanServer server,
String relationTypeName,
RoleList roleList,
boolean write) throws RelationTypeNotFoundException {
// Set up the return value
RoleList resolved = new RoleList();
RoleUnresolvedList unresolved = new RoleUnresolvedList();
RoleResult result = new RoleResult(resolved, unresolved);
// Check each role
Iterator iterator = roleList.iterator();
while (iterator.hasNext())
{
Role role = (Role) iterator.next();
int status = checkRole(relationService, server, relationTypeName, role,
write);
if (status == 0)
resolved.add(role);
else
unresolved.add(new RoleUnresolved(role.getRoleName(),
role.getRoleValue(), status));
}
// All Done
return result;
}
Check the Roles for a relation Type. |
public static void validateRole(ObjectName relationService,
MBeanServer server,
String relationTypeName,
Role role,
boolean write) throws InvalidRoleValueException, RelationTypeNotFoundException, RoleNotFoundException {
int status = checkRole(relationService, server, relationTypeName, role,
write);
if (status == RoleStatus.NO_ROLE_WITH_NAME)
throw new RoleNotFoundException(role.getRoleName());
if (status == RoleStatus.ROLE_NOT_WRITABLE)
throw new RoleNotFoundException(role.getRoleName() + " not writable");
else if (status != 0)
throw new InvalidRoleValueException(role.getRoleName());
}
Validate a role for a relation Type. |
public static void validateRoles(ObjectName relationService,
MBeanServer server,
String relationTypeName,
RoleList roleList,
boolean write) throws InvalidRoleValueException, RelationTypeNotFoundException, RoleNotFoundException {
Iterator iterator;
// Check for duplicate roles
HashSet roleNames = new HashSet();
iterator = roleList.iterator();
while (iterator.hasNext())
{
Object roleName = iterator.next();
if (roleNames.contains(roleName))
throw new InvalidRoleValueException("Duplicate role " + roleName);
roleNames.add(roleName);
}
// Check the roles
RoleResult result = checkRoles(relationService, server, relationTypeName,
roleList, write);
RoleUnresolvedList errors = result.getRolesUnresolved();
iterator = errors.iterator();
if (iterator.hasNext())
{
RoleUnresolved unresolved = (RoleUnresolved) iterator.next();
int status = unresolved.getProblemType();
if (status == RoleStatus.NO_ROLE_WITH_NAME)
throw new RoleNotFoundException(unresolved.getRoleName());
if (status == RoleStatus.ROLE_NOT_WRITABLE)
throw new RoleNotFoundException(unresolved.getRoleName() + " not writable");
else
throw new InvalidRoleValueException(unresolved.getRoleName());
}
}
Validate the Roles for a relation Type. |