001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package javax.management.relation;
023:
024: import java.util.ArrayList;
025: import java.util.HashSet;
026: import java.util.Iterator;
027:
028: import javax.management.MBeanServer;
029: import javax.management.MBeanException;
030: import javax.management.ObjectName;
031:
032: /**
033: * This is a helper class for performing role validation. It is used by
034: * both the RelationSupport and RelationService classes.<p>
035: *
036: * It is package private and NOT part of the specification.
037: *
038: * <p><b>Revisions:</b>
039: * <p><b>20020311 Adrian Brock:</b>
040: * <ul>
041: * <li>ValidateRole always failed
042: * <li>Throws wrong exception when not writable
043: * </ul>
044: *
045: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
046: * @version $Revision: 57200 $
047: *
048: */
049: class RoleValidator {
050: // Constants ---------------------------------------------------
051:
052: // Static ------------------------------------------------------
053:
054: /**
055: * Check a role for a relation type
056: *
057: * @param relationService the relation service object name
058: * @param server the MBeanServer of the relation service
059: * @param relationTypeName the relation to validate against
060: * @param role the role to validate
061: * @param write pass to true to check for a writable role
062: * @return zero for success or a RoleStatus value for failure
063: * @exception RelationTypeNotFoundException when the relation type
064: * does not exist in the relation service
065: */
066: public static int checkRole(ObjectName relationService,
067: MBeanServer server, String relationTypeName, Role role,
068: boolean write) throws RelationTypeNotFoundException {
069: // Get the role information
070: RoleInfo roleInfo = null;
071: try {
072: roleInfo = (RoleInfo) server.invoke(relationService,
073: "getRoleInfo", new Object[] { relationTypeName,
074: role.getRoleName() }, new String[] {
075: "java.lang.String", "java.lang.String" });
076: } catch (MBeanException mbe) {
077: Exception e = mbe.getTargetException();
078: if (e instanceof RelationTypeNotFoundException)
079: throw (RelationTypeNotFoundException) e;
080: if (e instanceof RoleInfoNotFoundException)
081: return RoleStatus.NO_ROLE_WITH_NAME;
082: throw new RuntimeException(e.toString());
083: } catch (Exception e) {
084: throw new RuntimeException(e.toString());
085: }
086:
087: // Check if the role is writable
088: if (write == true && roleInfo.isWritable() == false)
089: return RoleStatus.ROLE_NOT_WRITABLE;
090:
091: // Check the cardinality of the role
092: ArrayList mbeans = (ArrayList) role.getRoleValue();
093: int beanCount = mbeans.size();
094: int minimum = roleInfo.getMinDegree();
095: if (minimum != RoleInfo.ROLE_CARDINALITY_INFINITY
096: && minimum > beanCount)
097: return RoleStatus.LESS_THAN_MIN_ROLE_DEGREE;
098: int maximum = roleInfo.getMaxDegree();
099: if (maximum != RoleInfo.ROLE_CARDINALITY_INFINITY
100: && maximum < beanCount)
101: return RoleStatus.MORE_THAN_MAX_ROLE_DEGREE;
102:
103: // Check the MBeans
104: String className = roleInfo.getRefMBeanClassName();
105: Iterator iterator = mbeans.iterator();
106: while (iterator.hasNext()) {
107: try {
108: ObjectName objectName = (ObjectName) iterator.next();
109: if (server.isInstanceOf(objectName, className) == false)
110: return RoleStatus.REF_MBEAN_OF_INCORRECT_CLASS;
111: } catch (Exception e) {
112: return RoleStatus.REF_MBEAN_NOT_REGISTERED;
113: }
114: }
115: // All done
116: return 0;
117: }
118:
119: /**
120: * Check the Roles for a relation Type.
121: *
122: * @param relationService the relation service object name
123: * @param server the MBeanServer of the relation service
124: * @param relationTypeName the relation to validate against
125: * @param roleList the roles to validate
126: * @param write pass to true to check for a writable role
127: * @return a RoleResult containing resolved and unresolved roles
128: * @exception RelationTypeNotFoundException when the relation type
129: * does not exist in the relation service
130: */
131: public static RoleResult checkRoles(ObjectName relationService,
132: MBeanServer server, String relationTypeName,
133: RoleList roleList, boolean write)
134: throws RelationTypeNotFoundException {
135: // Set up the return value
136: RoleList resolved = new RoleList();
137: RoleUnresolvedList unresolved = new RoleUnresolvedList();
138: RoleResult result = new RoleResult(resolved, unresolved);
139:
140: // Check each role
141: Iterator iterator = roleList.iterator();
142: while (iterator.hasNext()) {
143: Role role = (Role) iterator.next();
144: int status = checkRole(relationService, server,
145: relationTypeName, role, write);
146: if (status == 0)
147: resolved.add(role);
148: else
149: unresolved.add(new RoleUnresolved(role.getRoleName(),
150: role.getRoleValue(), status));
151: }
152: // All Done
153: return result;
154: }
155:
156: /**
157: * Validate a role for a relation Type.
158: *
159: * @param relationService the relation service object name
160: * @param server the MBeanServer of the relation service
161: * @param relationTypeName the relation to validate against
162: * @param role the role to validate
163: * @param write pass to true to check for a writable role
164: * @exception InvalidRoleValueException when a role does not match its
165: * definition in the relation type's roleinfos
166: * @exception RelationTypeNotFoundException when the relation type
167: * does not exist in the relation service
168: * @exception RoleNotFoundException when a role does not exist
169: * in the role
170: */
171: public static void validateRole(ObjectName relationService,
172: MBeanServer server, String relationTypeName, Role role,
173: boolean write) throws InvalidRoleValueException,
174: RelationTypeNotFoundException, RoleNotFoundException {
175: int status = checkRole(relationService, server,
176: relationTypeName, role, write);
177:
178: if (status == RoleStatus.NO_ROLE_WITH_NAME)
179: throw new RoleNotFoundException(role.getRoleName());
180: if (status == RoleStatus.ROLE_NOT_WRITABLE)
181: throw new RoleNotFoundException(role.getRoleName()
182: + " not writable");
183: else if (status != 0)
184: throw new InvalidRoleValueException(role.getRoleName());
185: }
186:
187: /**
188: * Validate the Roles for a relation Type.
189: *
190: * @param relationService the relation service object name
191: * @param server the MBeanServer of the relation service
192: * @param relationTypeName the relation to validate against
193: * @param roleList the roles to validate
194: * @param write pass to true to check for a writable role
195: * @exception InvalidRoleValueException when there is a duplicate role name
196: * or a role does not match its definition in the
197: * relation type's roleinfos
198: * @exception RelationTypeNotFoundException when the relation type
199: * does not exist in the relation service
200: * @exception RoleNotFoundException when a role does not exist
201: * in the role
202: */
203: public static void validateRoles(ObjectName relationService,
204: MBeanServer server, String relationTypeName,
205: RoleList roleList, boolean write)
206: throws InvalidRoleValueException,
207: RelationTypeNotFoundException, RoleNotFoundException {
208: Iterator iterator;
209:
210: // Check for duplicate roles
211: HashSet roleNames = new HashSet();
212: iterator = roleList.iterator();
213: while (iterator.hasNext()) {
214: Object roleName = iterator.next();
215: if (roleNames.contains(roleName))
216: throw new InvalidRoleValueException("Duplicate role "
217: + roleName);
218: roleNames.add(roleName);
219: }
220:
221: // Check the roles
222: RoleResult result = checkRoles(relationService, server,
223: relationTypeName, roleList, write);
224: RoleUnresolvedList errors = result.getRolesUnresolved();
225: iterator = errors.iterator();
226: if (iterator.hasNext()) {
227: RoleUnresolved unresolved = (RoleUnresolved) iterator
228: .next();
229: int status = unresolved.getProblemType();
230: if (status == RoleStatus.NO_ROLE_WITH_NAME)
231: throw new RoleNotFoundException(unresolved
232: .getRoleName());
233: if (status == RoleStatus.ROLE_NOT_WRITABLE)
234: throw new RoleNotFoundException(unresolved
235: .getRoleName()
236: + " not writable");
237: else
238: throw new InvalidRoleValueException(unresolved
239: .getRoleName());
240: }
241: }
242: }
|