0001: /**
0002: * The XMOJO Project 5
0003: * Copyright © 2003 XMOJO.org. All rights reserved.
0004:
0005: * NO WARRANTY
0006:
0007: * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
0008: * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
0009: * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
0010: * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
0011: * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0012: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
0013: * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
0014: * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
0015: * REPAIR OR CORRECTION.
0016:
0017: * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
0018: * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
0019: * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
0020: * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
0021: * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
0022: * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
0023: * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
0024: * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
0025: * SUCH DAMAGES.
0026: **/package javax.management.relation;
0027:
0028: import java.util.Map;
0029: import java.util.List;
0030: import java.util.HashMap;
0031: import java.util.ArrayList;
0032: import java.util.Iterator;
0033:
0034: import javax.management.ObjectName;
0035: import javax.management.MBeanServer;
0036: import javax.management.MBeanRegistration;
0037: import javax.management.MBeanException;
0038: import javax.management.ReflectionException;
0039: import javax.management.InstanceNotFoundException;
0040:
0041: /**
0042: * <P>A RelationSupport object is used internally by the Relation Service to
0043: * represent simple relations (only roles, no properties or methods), with an
0044: * unlimited number of roles, of any relation type. As internal representation,
0045: * it is not exposed to the user.
0046: * <P>
0047: * RelationSupport class conforms to the design patterns of standard MBean.
0048: * So the user can decide to instantiate a RelationSupport object himself as a
0049: * MBean (as it follows the MBean design patterns), to register it in the
0050: * MBean Server, and then to add it in the Relation Service.
0051: * <P>
0052: * The user can also, when creating his own MBean relation class, have it
0053: * extending RelationSupport, to retrieve the implementations of required
0054: * interfaces (see below).
0055: * <P>
0056: * It is also possible to have in a user relation MBean class a member being
0057: * a RelationSupport object, and to implement the required interfaces by
0058: * delegating all to this member.
0059: * <P>
0060: * RelationSupport implements the Relation interface (to be handled by the
0061: * Relation Service).
0062: * <P>
0063: * It implements also the MBeanRegistration interface to be able to retrieve
0064: * the MBean Server where it is registered (if registered as a MBean) to
0065: * access to its Relation Service.
0066: */
0067: public class RelationSupport implements RelationSupportMBean,
0068: MBeanRegistration {
0069: // Relation identifier (expected to be unique in the Relation Service
0070: // where the RelationSupport object will be added)
0071: String relId = null;
0072:
0073: // Relation type name (must be known in the Relation Service where the
0074: // relation will be added)
0075: String relTypeName = null;
0076:
0077: RoleList roleList = null;
0078: ObjectName objectName = null;
0079:
0080: // ObjectName of the Relation Service where the relation will be added
0081: // REQUIRED if the RelationSupport is created by the user to be registered
0082: // as a MBean, as it will have to access the Relation Service via the MBean
0083: // Server to perform the check regarding the relation type.
0084: // Is null if current object is directly created by the Relation Service,
0085: // as the object will directly access it.
0086: ObjectName relServiceName = null;
0087:
0088: // Reference to the MBean Server where the Relation Service is
0089: // registered
0090: // REQUIRED if the RelationSupport is created by the user to be registered
0091: // as a MBean, as it will have to access the Relation Service via the MBean
0092: // Server to perform the check regarding the relation type.
0093: // If the Relationbase object is created by the Relation Service (use of
0094: // createRelation() method), this is null as not needed, direct access to
0095: // the Relation Service.
0096: // If the Relationbase object is created by the user and registered as a
0097: // MBean, this is set by the preRegister() method below.
0098: MBeanServer mbeanServer = null;
0099:
0100: Boolean isInRelation = new Boolean(false);
0101:
0102: private HashMap roleToValueTable = new HashMap();
0103:
0104: /**
0105: * Creates object.
0106: * <P>This constructor has to be used when the user relation MBean implements
0107: * the interfaces expected to be supported by a relation by delegating to a
0108: * RelationSupport object. This object needs to know the Relation Service
0109: * expected to handle the relation. So it has to know the MBean Server
0110: * where the Relation Service is registered.
0111: * <P>
0112: * According to a limitation, a relation MBean must be registered in the
0113: * same MBean Server as the Relation Service expected to handle it. So the
0114: * user relation MBean has to be created and registered, and then the
0115: * wrapped RelationSupport object can be created with identified MBean Server.
0116: * <P>
0117: * Nothing is done at the Relation Service level, i.e. the RelationSupport
0118: * object is not added, and no check if the provided values are correct.
0119: * The object is always created, EXCEPT if:
0120: * <P> - one required parameter is not provided
0121: * <P> - the same name is used for two roles.
0122: * <P>
0123: * To be handled as a relation, the object has then to be added in the
0124: * Relation Service using the Relation Service method addRelation().
0125: * <P>
0126: * @param theRelId - relation identifier, to identify the relation in
0127: * the Relation Service. Expected to be unique in the given
0128: * Relation Service.
0129: *
0130: * @param theRelServiceName - ObjectName of the Relation Service where
0131: * the relation will be registered. It is required as this is
0132: * the Relation Service that is aware of the definition of
0133: * the relation type of given relation, so that will be able
0134: * to check update operations (set).
0135: *
0136: * @param theRelServiceMBeanServer - MBean Server where the wrapping MBean
0137: * is or will be registered. Expected to be the MBean Server
0138: * where the Relation Service is or will be registered.
0139: *
0140: * @param theRelTypeName - Name of relation type.
0141: * Expected to have been created in given Relation Service.
0142: *
0143: * @param theRoleList - list of roles (Role objects) to initialised the
0144: * relation. Can be null. Expected to conform to relation info
0145: * in associated relation type.
0146: *
0147: * @exception InvalidRoleValueException - if the same name is used for two roles.
0148: *
0149: * @exception java.lang.IllegalArgumentException - if a required value
0150: * (Relation Service Object Name, etc.) is not provided as parameter.
0151: */
0152: public RelationSupport(String theRelId,
0153: ObjectName theRelServiceName,
0154: MBeanServer theRelServiceMBeanServer,
0155: String theRelTypeName, RoleList theRoleList)
0156: throws InvalidRoleValueException, IllegalArgumentException {
0157: if (theRelServiceName == null || theRelId == null
0158: || theRelTypeName == null) {
0159: throw new IllegalArgumentException(
0160: RelationErrs.ERR_ILLEGAL_ARG);
0161: }
0162:
0163: if (theRoleList == null)
0164: throw new InvalidRoleValueException(
0165: "null RoleList is not possible!!!");
0166:
0167: if (this .isDuplicateRolePresent(theRoleList))
0168: throw new InvalidRoleValueException(
0169: RelationErrs.ERR_INVALID_ROLE);
0170:
0171: initRoleToValueTable(theRoleList);
0172: this .relId = theRelId;
0173: this .relServiceName = theRelServiceName;
0174: this .mbeanServer = theRelServiceMBeanServer;
0175: this .relTypeName = theRelTypeName;
0176: this .roleList = theRoleList;
0177: }
0178:
0179: /**
0180: * Creates object.
0181: * <P>This constructor has to be used when the RelationSupport object will
0182: * be registered as a MBean by the user, or when creating a user relation
0183: * MBean those class extends RelationSupport. Nothing is done at the
0184: * Relation Service level, i.e. the RelationSupport object is not added,
0185: * and no check if the provided values are correct. The object is always
0186: * created, EXCEPT if:
0187: * <P> - one mandatory parameter is not provided
0188: * <P> - the same name is used for two roles.
0189: * <P>
0190: * To be handled as a relation, the object has then to be added in the
0191: * Relation Service using Relation Service method addRelation().
0192: *
0193: * @param theRelId - relation identifier, to identify the relation in the
0194: * Relation Service. Expected to be unique in the given
0195: * Relation Service.
0196: *
0197: * @param theRelServiceName - ObjectName of the Relation Service where the
0198: * relation will be registered. It is required as this is the
0199: * Relation Service that is aware of the definition of the
0200: * relation type of given relation, so that will be able to
0201: * check update operations (set).
0202: *
0203: * @param theRelTypeName - Name of relation type.
0204: * Expected to have been created in given Relation Service.
0205: *
0206: * @param theRoleList - list of roles (Role objects) to initialised the
0207: * relation. Can be null. Expected to conform to relation info
0208: * in associated relation type.
0209: *
0210: * @exception InvalidRoleValueException - if the same name is used for two roles.
0211: *
0212: * @exception java.lang.IllegalArgumentException - if a required value
0213: * (Relation Service Object Name, etc.) is not provided as parameter.
0214: */
0215: public RelationSupport(String theRelId,
0216: ObjectName theRelServiceName, String theRelTypeName,
0217: RoleList theRoleList) throws InvalidRoleValueException,
0218: IllegalArgumentException {
0219: if (theRelId == null || theRelServiceName == null
0220: || theRelTypeName == null)
0221: throw new IllegalArgumentException(
0222: RelationErrs.ERR_ILLEGAL_ARG);
0223:
0224: if (this .isDuplicateRolePresent(theRoleList))
0225: throw new InvalidRoleValueException(
0226: RelationErrs.ERR_INVALID_ROLE);
0227:
0228: initRoleToValueTable(theRoleList);
0229: this .relId = theRelId;
0230: this .relServiceName = theRelServiceName;
0231: this .relTypeName = theRelTypeName;
0232: this .roleList = theRoleList;
0233: }
0234:
0235: /**
0236: * Returns all roles present in the relation
0237: *
0238: * @return a RoleResult object, including a RoleList (for roles
0239: * succcessfully retrieved) and a RoleUnresolvedList
0240: * (for roles not readable).
0241: *
0242: * @exception RelationServiceNotRegisteredException - if the Relation
0243: * Service is not registered in the MBean Server
0244: */
0245: public RoleResult getAllRoles()
0246: throws RelationServiceNotRegisteredException {
0247: if (!(mbeanServer.isRegistered(this .relServiceName)))
0248: throw new RelationServiceNotRegisteredException(
0249: RelationErrs.ERR_REL_SERV_NOT_FOUND);
0250:
0251: RoleResult result = null;
0252:
0253: // Get the RoleResult from a local friend method. This call does not
0254: // occur from RelationService and the relationService will also be null
0255: result = getAllRolesFriend(null);
0256:
0257: return result;
0258: }
0259:
0260: /**
0261: * Retrieves MBeans referenced in the various roles of the relation.
0262: *
0263: * @return a HashMap mapping:
0264: * ObjectName -> ArrayList of String (role names)
0265: */
0266: public Map getReferencedMBeans() {
0267: HashMap refMBeanMap = new HashMap();
0268:
0269: //Easiest way is to get the RoleValues from the local table and form a map.
0270: Iterator iter1 = roleToValueTable.values().iterator();
0271:
0272: while (iter1.hasNext()) {
0273: Role role = (Role) (iter1.next());
0274: String roleName = role.getRoleName();
0275:
0276: //retrieves the ObjectNames of the MBean from RoleValue
0277: ArrayList refMBeanList = (ArrayList) (role.getRoleValue());
0278:
0279: Iterator iter2 = refMBeanList.iterator();
0280: while (iter2.hasNext()) {
0281: ObjectName mbeanName = (ObjectName) (iter2.next());
0282:
0283: ArrayList list = (ArrayList) (refMBeanMap
0284: .get(mbeanName));
0285: boolean flag = false;
0286: if (list == null) {
0287: list = new ArrayList();
0288: flag = true;
0289: }
0290: list.add(roleName);
0291: if (flag) {
0292: refMBeanMap.put(mbeanName, list);
0293: }
0294: }
0295: }
0296:
0297: return refMBeanMap;
0298: }
0299:
0300: /**
0301: * Returns relation identifier (used to uniquely identify the relation
0302: * inside the Relation Service).
0303: *
0304: * @return Relation identifier
0305: */
0306: public String getRelationId() {
0307: return this .relId;
0308: }
0309:
0310: /**
0311: * Returns ObjectName of the Relation Service handling the relation
0312: *
0313: * @return ObjectName of the Relation Service handling the relation
0314: */
0315: public ObjectName getRelationServiceName() {
0316: return this .relServiceName;
0317: }
0318:
0319: /**
0320: * Returns name of associated relation type.
0321: *
0322: * @return The name of associated relation type
0323: */
0324: public String getRelationTypeName() {
0325: return this .relTypeName;
0326: }
0327:
0328: /**
0329: * Retrieves role value for given role name.
0330: * <P>Checks if the role exists and is readable according to the relation type.
0331: *
0332: * @param theRoleName - name of role
0333: *
0334: * @return the ArrayList of ObjectName objects being the role value
0335: *
0336: * @exception java.lang.IllegalArgumentException - if null role name
0337: *
0338: * @exception RoleNotFoundException - if:
0339: * <BR/> - there is no role with given name
0340: * <BR/> - the role is not readable.
0341: *
0342: * @exception RelationServiceNotRegisteredException - if the Relation
0343: * Service is not registered in the MBean Server
0344: */
0345: public List getRole(String theRoleName)
0346: throws IllegalArgumentException, RoleNotFoundException,
0347: RelationServiceNotRegisteredException {
0348: if (theRoleName == null)
0349: throw new IllegalArgumentException(
0350: RelationErrs.ERR_ILLEGAL_ARG);
0351:
0352: if (!(mbeanServer.isRegistered(this .relServiceName)))
0353: throw new RelationServiceNotRegisteredException(
0354: RelationErrs.ERR_REL_SERV_NOT_FOUND);
0355:
0356: ArrayList toRet = null;
0357: toRet = (ArrayList) (getRoleFriend(theRoleName, false, null));
0358:
0359: return toRet;
0360: }
0361:
0362: /**
0363: * Returns the number of MBeans currently referenced in the given role
0364: *
0365: * @param theRoleName - name of role
0366: *
0367: * @return the number of currently referenced MBeans in that role
0368: *
0369: * @exception java.lang.IllegalArgumentException - if null role name
0370: *
0371: * @exception RoleNotFoundException - if there is no role with given name
0372: */
0373: public Integer getRoleCardinality(String theRoleName)
0374: throws IllegalArgumentException, RoleNotFoundException {
0375: if (theRoleName == null) {
0376: throw new IllegalArgumentException(
0377: RelationErrs.ERR_ILLEGAL_ARG);
0378: }
0379:
0380: Role role = (Role) (roleToValueTable.get(theRoleName));
0381:
0382: if (role == null) {
0383: throw new RoleNotFoundException();
0384: }
0385:
0386: ArrayList roleValue = (ArrayList) (role.getRoleValue());
0387:
0388: return new Integer(roleValue.size());
0389: }
0390:
0391: /**
0392: * Retrieves values of roles with given names.
0393: * <P>Checks for each role if it exists and is readable according to
0394: * the relation type.
0395: *
0396: * @param theRoleNameArray - array of names of roles to be retrieved
0397: *
0398: * @return a RoleResult object, including a RoleList (for roles
0399: * succcessfully retrieved) and a RoleUnresolvedList
0400: * (for roles not retrieved).
0401: *
0402: * @exception java.lang.IllegalArgumentException - if null role name
0403: *
0404: * @exception RelationServiceNotRegisteredException - if the Relation
0405: * Service is not registered in the MBean Server
0406: */
0407: public RoleResult getRoles(String[] theRoleNameArray)
0408: throws IllegalArgumentException,
0409: RelationServiceNotRegisteredException {
0410: if (theRoleNameArray == null)
0411: throw new IllegalArgumentException(
0412: RelationErrs.ERR_ILLEGAL_ARG);
0413:
0414: if (!(mbeanServer.isRegistered(this .relServiceName)))
0415: throw new RelationServiceNotRegisteredException();
0416:
0417: return getRolesFriend(theRoleNameArray, null);
0418: }
0419:
0420: /**
0421: * Callback used by the Relation Service when a MBean referenced in a role
0422: * is unregistered.
0423: * <P>The Relation Service will call this method to let the relation take
0424: * action to reflect the impact of such unregistration.
0425: * BEWARE. the user is not expected to call this method.
0426: * <P>
0427: * Current implementation is to set the role with its current value
0428: * (list of ObjectNames of referenced MBeans) without the unregistered one.
0429: *
0430: * @param theObjName - ObjectName of unregistered MBean
0431: *
0432: * @param theRoleName - name of role where the MBean is referenced
0433: *
0434: * @exception java.lang.IllegalArgumentException - if null parameter
0435: *
0436: * @exception RoleNotFoundException - if role does not exist in
0437: * the relation or is not writable
0438: *
0439: * @exception InvalidRoleValueException - if role value does not conform to
0440: * the associated role info (this will never happen when
0441: * called from the Relation Service)
0442: *
0443: * @exception RelationServiceNotRegisteredException - if the Relation
0444: * Service is not registered in the MBean Server
0445: *
0446: * @exception RelationTypeNotFoundException - if the relation type has not
0447: * been declared in the Relation Service.
0448: *
0449: * @exception RelationNotFoundException - if this method is called for a
0450: * relation MBean not added in the Relation Service.
0451: */
0452: public void handleMBeanUnregistration(ObjectName theObjName,
0453: String theRoleName) throws IllegalArgumentException,
0454: RoleNotFoundException, InvalidRoleValueException,
0455: RelationServiceNotRegisteredException,
0456: RelationTypeNotFoundException, RelationNotFoundException {
0457: if (theObjName == null || theRoleName == null) {
0458: throw new IllegalArgumentException(
0459: RelationErrs.ERR_ILLEGAL_ARG);
0460: }
0461:
0462: Role role = (Role) (roleToValueTable.get(theRoleName));
0463:
0464: if (role == null) {
0465: throw new RoleNotFoundException();
0466: }
0467:
0468: ArrayList roleValue = (ArrayList) (role.getRoleValue());
0469: //Remove the UnregisteredMBean Name from the RoleValue and set it.
0470: //Use clone..that would be better.
0471: ArrayList toSet = (ArrayList) (roleValue.clone());
0472: toSet.remove(theObjName);
0473: //construct a new Role.
0474: Role toSetRole = new Role(theRoleName, toSet);
0475: setRoleFriend(toSetRole, false, null);
0476: }
0477:
0478: /**
0479: * Returns an internal flag specifying if the object is still handled by
0480: * the Relation Service.
0481: *
0482: * @return Internal flag specifying if the object is still handled by
0483: * the Relation Service.
0484: * Specified by:
0485: * isInRelationService in interface RelationSupportMBean
0486: */
0487: public Boolean isInRelationService() {
0488: return this .isInRelation;
0489: }
0490:
0491: /**
0492: * Description copied from interface: MBeanRegistration.
0493: * <P>
0494: * Allows the MBean to perform any operations needed after having been
0495: * de-registered in the MBean server.
0496: * <P>
0497: * Specified by:
0498: * postDeregister in interface MBeanRegistration
0499: */
0500: public void postDeregister() {
0501: //Method to satisfy MBeanRegistration Interface.. Probably can be used later
0502: }
0503:
0504: /**
0505: * Description copied from interface: MBeanRegistration.
0506: * <P>
0507: * Allows the MBean to perform any operations needed after having been
0508: * registered in the MBean server or after the registration has failed.
0509: * <P>
0510: * Specified by:
0511: * postRegister in interface MBeanRegistration
0512: * Tags copied from interface: MBeanRegistration
0513: *
0514: * @param registrationDone - Indicates whether or not the MBean has been
0515: * successfully registered in the MBean server. The value
0516: * false means that the registration phase has failed
0517: */
0518: public void postRegister(Boolean registrationDone) {
0519: //Method to satisfy MBeanRegistration Interface.. Probably can be used later
0520: }
0521:
0522: /**
0523: * Description copied from interface: MBeanRegistration.
0524: * <P>
0525: * Allows the MBean to perform any operations it needs before being
0526: * de-registered by the MBean server.
0527: * <P>
0528: * Specified by:
0529: * preDeregister in interface MBeanRegistration
0530: * Tags copied from interface: MBeanRegistration
0531: *
0532: * @exception java.langException - This exception should be caught by the
0533: * MBean server and re-thrown as an MBeanRegistrationException.
0534: */
0535: public void preDeregister() throws Exception {
0536: //Method to satisfy MBeanRegistration Interface.. Probably can be used later
0537: }
0538:
0539: /**
0540: * Description copied from interface: MBeanRegistration.
0541: * <P>
0542: * Allows the MBean to perform any operations it needs before being
0543: * registered in the MBean server. If the name of the MBean is not specified,
0544: * the MBean can provide a name for its registration. If any exception is
0545: * raised, the MBean will not be registered in the MBean server.
0546: * <P>
0547: * Specified by:
0548: * preRegister in interface MBeanRegistration
0549: * Tags copied from interface: MBeanRegistration
0550: *
0551: * @param server - The MBean server in which the MBean will be registered.
0552: *
0553: * @param name - The object name of the MBean.
0554: *
0555: * @return The name of the MBean registered.
0556: *
0557: * @exception java.lang.Exception - This exception should be caught by the
0558: * MBean server and re-thrown as an MBeanRegistrationException.
0559: */
0560: public ObjectName preRegister(MBeanServer server, ObjectName name)
0561: throws Exception {
0562: this .mbeanServer = server;
0563: this .objectName = name;
0564:
0565: return name;
0566: }
0567:
0568: /**
0569: * Returns all roles in the relation without checking read mode
0570: *
0571: * @return a RoleList
0572: */
0573: public RoleList retrieveAllRoles() {
0574: return new RoleList(new ArrayList(roleToValueTable.values()));
0575: }
0576:
0577: /**
0578: * Sets the flag to specify that it is handled or not by the Relation Service
0579: * BEWARE, this method has to be exposed for the user relation MBeans, as
0580: * the Relation Service will access them through their management interface.
0581: * It is RECOMMENDED NOT to use this method. Using it does not affect the
0582: * registration of the relation object in the Relation Service, but will
0583: * provide wrong information about it!!!!
0584: * <P>
0585: * Specified by:
0586: * setRelationServiceManagementFlag in interface RelationSupportMBean
0587: *
0588: * @param theFlg If true the flag is handled by the relation service false otherwise
0589: *
0590: * @exception java.lang.IllegalArgumentException - if null parameter
0591: */
0592: public void setRelationServiceManagementFlag(Boolean theFlg)
0593: throws IllegalArgumentException {
0594: if (theFlg == null)
0595: throw new IllegalArgumentException(
0596: RelationErrs.ERR_ILLEGAL_ARG);
0597:
0598: this .isInRelation = theFlg;
0599: }
0600:
0601: /**
0602: * Sets the given role.
0603: * <P>Will check the role according to its corresponding role definition
0604: * provided in relation's relation type.
0605: * Will send a notification (RelationNotification with type
0606: * RELATION_BASIC_UPDATE or RELATION_MBEAN_UPDATE, depending if the
0607: * relation is a MBean or not).
0608: *
0609: * @param theRole - role to be set (name and new value)
0610: *
0611: * @exception java.lang.IllegalArgumentException - if null role
0612: *
0613: * @exception RoleNotFoundException - if the role is not writable (no test
0614: * on the write access mode performed when initialising the role)
0615: *
0616: * @exception InvalidRoleValueException - if value provided for role is not valid, i.e.:
0617: * - the number of referenced MBeans in given value is less
0618: * than expected minimum degree
0619: * - the number of referenced MBeans in provided value exceeds
0620: * expected maximum degree
0621: * - one referenced MBean in the value is not an Object of the
0622: * MBean class expected for that role
0623: * - a MBean provided for that role does not exist
0624: *
0625: * @exception RelationServiceNotRegisteredException - if the Relation Service
0626: * is not registered in the MBean Server
0627: *
0628: * @exception RelationTypeNotFoundException - if the relation type has not
0629: * been declared in the Relation Service
0630: *
0631: * @exception RelationNotFoundException - if the relation has not been added
0632: * in the Relation Service.
0633: */
0634: public void setRole(Role theRole) throws IllegalArgumentException,
0635: InvalidRoleValueException, RelationNotFoundException,
0636: RelationServiceNotRegisteredException,
0637: RelationTypeNotFoundException, RoleNotFoundException {
0638: //This method should actually call the RelationService's updateRoleMap().
0639: //That will take care of updating this Role and sending the Notification
0640: //Note: Only the RelationService is capable of sending
0641: //RelationNotifications and not any other Object.
0642:
0643: if (theRole == null) {
0644: throw new IllegalArgumentException(
0645: RelationErrs.ERR_ILLEGAL_ARG);
0646: }
0647:
0648: if (!(mbeanServer.isRegistered(this .relServiceName)))
0649: throw new RelationServiceNotRegisteredException();
0650:
0651: setRoleFriend(theRole, false, null);
0652: }
0653:
0654: /**
0655: * Sets the given roles.
0656: * <P>Will check the role according to its corresponding role definition
0657: * provided in relation's relation type will send one notification
0658: * (RelationNotification with type RELATION_BASIC_UPDATE or
0659: * RELATION_MBEAN_UPDATE, depending if the relation is a MBean or not)
0660: * per updated role.
0661: *
0662: * @param theRoleList - list of roles to be set
0663: *
0664: * @return a RoleResult object, including a RoleList (for roles
0665: * succcessfully set) and a RoleUnresolvedList (for roles not set).
0666: *
0667: * @exception java.lang.IllegalArgumentException - if null role name
0668: *
0669: * @exception RelationServiceNotRegisteredException - if the Relation
0670: * Service is not registered in the MBean Server
0671: *
0672: * @exception RelationTypeNotFoundException - if the relation type has not
0673: * been declared in the Relation Service.
0674: *
0675: * @exception RelationNotFoundException - if the relation MBean has not
0676: * been added in the Relation Service.
0677: */
0678: public RoleResult setRoles(RoleList theRoleList)
0679: throws IllegalArgumentException,
0680: RelationServiceNotRegisteredException,
0681: RelationTypeNotFoundException, RelationNotFoundException {
0682:
0683: if (theRoleList == null)
0684: throw new IllegalArgumentException(
0685: RelationErrs.ERR_ILLEGAL_ARG);
0686:
0687: if (!(mbeanServer.isRegistered(relServiceName)))
0688: throw new RelationServiceNotRegisteredException(
0689: RelationErrs.ERR_REL_SERV_NOT_FOUND);
0690:
0691: return setRolesFriend(theRoleList, false, null);
0692: }
0693:
0694: //************************ package methods ****************************//
0695:
0696: void handleMBeanUnregistrationFriend(ObjectName theObjName,
0697: String theRoleName, RelationService service)
0698: throws IllegalArgumentException, RoleNotFoundException,
0699: InvalidRoleValueException,
0700: RelationServiceNotRegisteredException,
0701: RelationTypeNotFoundException, RelationNotFoundException {
0702: if (theObjName == null || theRoleName == null) {
0703: throw new IllegalArgumentException(
0704: RelationErrs.ERR_ILLEGAL_ARG);
0705: }
0706:
0707: Role role = (Role) (roleToValueTable.get(theRoleName));
0708:
0709: if (role == null) {
0710: throw new RoleNotFoundException();
0711: }
0712:
0713: ArrayList roleValue = (ArrayList) (role.getRoleValue());
0714: //Remove the UnregisteredMBean Name from the RoleValue and set it.
0715: //Use clone..that would be better.
0716: ArrayList toSet = (ArrayList) (roleValue.clone());
0717: toSet.remove(theObjName);
0718: //construct a new Role.
0719: Role toSetRole = new Role(theRoleName, toSet);
0720: setRoleFriend(toSetRole, true, service);
0721: }
0722:
0723: //*****************Methods private to RelationSupport ********************//
0724:
0725: private boolean isDuplicateRolePresent(RoleList list) {
0726: boolean toRet = false;
0727:
0728: if (list != null) {
0729: for (int i = 0; i < list.size(); i++) {
0730: Role role1 = (Role) (list.get(i));
0731: for (int j = 0; j < list.size(); j++) {
0732: Role role2 = (Role) (list.get(j));
0733: if (i != j) {
0734: if (role1.getRoleName().trim().equals(
0735: role2.getRoleName().trim())) {
0736: toRet = true;
0737: break;
0738: }
0739: }
0740: }
0741: if (toRet == true) {
0742: break;
0743: }
0744:
0745: }
0746: }
0747:
0748: /*
0749: for(int i=0;i<list.size();i++)
0750: {
0751: for(int j=list.size();j<0;j--)
0752: {
0753: Role role1 = (Role)(list.get(i));
0754: Role role2 = (Role)(list.get(j));
0755: if(role1.getRoleName().trim().equals(role2.getRoleName().trim()))
0756: {
0757: toRet = true;
0758: break;
0759: }
0760: }
0761: if(toRet == true)
0762: {
0763: break;
0764: }
0765: }
0766: */
0767:
0768: return toRet;
0769: }
0770:
0771: private Role getRoleFromRoleList(String roleName) {
0772: for (int i = 0; i < roleList.size(); i++) {
0773: Role temp = (Role) (roleList.get(i));
0774: if (temp.getRoleName().equals(roleName)) {
0775: return temp;
0776: }
0777: }
0778:
0779: return null;
0780: }
0781:
0782: private List getRoleValueFromRoleList(String roleName) {
0783: for (int i = 0; i < roleList.size(); i++) {
0784: Role temp = (Role) (roleList.get(i));
0785: if (temp.getRoleName().equals(roleName)) {
0786: return temp.getRoleValue();
0787: }
0788: }
0789:
0790: return null;
0791: }
0792:
0793: private RoleInfo getRoleInfoFromList(ArrayList roleInfoList,
0794: String roleName) {
0795: RoleInfo toRet = null;
0796:
0797: for (int i = 0; i < roleInfoList.size(); i++) {
0798: toRet = (RoleInfo) (roleInfoList.get(i));
0799: if (roleName.equals(toRet.getName())) {
0800: return toRet;
0801: }
0802: }
0803: return toRet;
0804: }
0805:
0806: private ArrayList getRoleNamesGivenObjectName(ObjectName name) {
0807: ArrayList toRet = new ArrayList();
0808: for (int i = 0; i < roleList.size(); i++) {
0809: Role temp = (Role) (roleList.get(i));
0810: ArrayList roleValueList = (ArrayList) (this
0811: .getRoleValueFromRoleList(temp.getRoleName()));
0812:
0813: for (int j = 0; j < roleValueList.size(); j++) {
0814: ObjectName toComp = (ObjectName) (roleValueList.get(j));
0815: if (toComp.toString().equals(name.toString())) {
0816: toRet.add(temp.getRoleName());
0817: }
0818: }
0819: }
0820:
0821: return toRet;
0822: }
0823:
0824: private void print(String s) {
0825: System.out.println(s);
0826: }
0827:
0828: private void printLine() {
0829: System.out
0830: .println("_____________________________________________________________________________________________________________");
0831: }
0832:
0833: private void printLineBet() {
0834: System.out
0835: .println("\t\t\t______________________________________________________");
0836: }
0837:
0838: private void printException(Exception theExc) {
0839: theExc.printStackTrace();
0840: StringBuffer excName = new StringBuffer();
0841: Exception exc = null;
0842:
0843: if (theExc instanceof MBeanException) {
0844: boolean isMBeanExcFlg = true;
0845: excName.append("MBeanException->");
0846: Exception exc1 = theExc;
0847: while (isMBeanExcFlg) {
0848: exc = ((MBeanException) exc1).getTargetException();
0849: if (!(exc instanceof MBeanException)) {
0850: isMBeanExcFlg = false;
0851: } else {
0852: exc1 = exc;
0853: excName.append("MBeanException->");
0854: }
0855: }
0856: } else {
0857: exc = theExc;
0858: }
0859:
0860: excName.append((exc.getClass()).getName());
0861: print("Exception: " + excName.toString());
0862: print(exc.getMessage());
0863: print("\nEXITING...\n");
0864: System.exit(1);
0865: }
0866:
0867: // print a list
0868: private void printList(ArrayList theList) {
0869: if (theList == null) {
0870: return;
0871: }
0872:
0873: StringBuffer listDesc = new StringBuffer();
0874:
0875: for (Iterator iter = theList.iterator(); iter.hasNext();) {
0876: listDesc.append("\t");
0877: Object currObj = iter.next();
0878: listDesc.append(currObj.toString());
0879: if (iter.hasNext()) {
0880: listDesc.append(",\n");
0881: }
0882: }
0883:
0884: print(listDesc.toString());
0885: return;
0886: }
0887:
0888: private void printRoleResult(RoleResult theRoleResult) {
0889: print("\tPrinting RoleResult");
0890: if (theRoleResult != null) {
0891: try {
0892: RoleList roleList = theRoleResult.getRoles();
0893: print("\t-> Printing the RoleList");
0894: printList(roleList);
0895: RoleUnresolvedList roleUnresList = theRoleResult
0896: .getRolesUnresolved();
0897: print("\t-> Printing the RoleUnresolvedList");
0898: printList(roleUnresList);
0899: } catch (Exception e) {
0900: print("\t!!! Could not print RoleResult!!!");
0901: printException(e);
0902: }
0903: }
0904: }
0905:
0906: private void initRoleToValueTable(RoleList theRoleList)
0907: throws InvalidRoleValueException {
0908: if (theRoleList == null) {
0909: return;
0910: }
0911:
0912: synchronized (roleToValueTable) {
0913: for (Iterator roleIter = theRoleList.iterator(); roleIter
0914: .hasNext();) {
0915: Role currRole = (Role) (roleIter.next());
0916: String currRoleName = currRole.getRoleName();
0917:
0918: if (roleToValueTable.containsKey(currRoleName)) {
0919: throw new InvalidRoleValueException("");
0920: }
0921:
0922: roleToValueTable.put(new String(currRoleName),
0923: (Role) (currRole.clone()));
0924: }
0925: }
0926: }
0927:
0928: /**
0929: * This method is a friend method to getAllRoles.
0930: */
0931: RoleResult getAllRolesFriend(RelationService service)
0932: throws RelationServiceNotRegisteredException {
0933: ArrayList roleNamesList = new ArrayList(roleToValueTable
0934: .keySet());
0935: String[] roleNames = new String[roleNamesList.size()];
0936: int i = 0;
0937: Iterator iter = roleNamesList.iterator();
0938: String roleName = null;
0939:
0940: while (iter.hasNext()) {
0941: roleName = (String) (iter.next());
0942: roleNames[i] = roleName;
0943: i++;
0944: }
0945:
0946: RoleResult result = getRolesFriend(roleNames, service);
0947: return result;
0948: }
0949:
0950: /**
0951: * This method returns an RoleResult provided a string[] of roleNames.
0952: */
0953: RoleResult getRolesFriend(String[] theRoleNameArray,
0954: RelationService service)
0955: throws RelationServiceNotRegisteredException {
0956: if (theRoleNameArray == null) {
0957: throw new IllegalArgumentException();
0958: }
0959:
0960: RoleList roleList = new RoleList();
0961: RoleUnresolvedList roleUnresList = new RoleUnresolvedList();
0962:
0963: for (int i = 0; i < theRoleNameArray.length; i++) {
0964: String roleName = theRoleNameArray[i];
0965: Object result = null;
0966:
0967: try {
0968: result = getRoleFriend(roleName, true, service);
0969: } catch (RoleNotFoundException exc) {
0970: return null;
0971: }
0972:
0973: if (result instanceof Role) {
0974: roleList.add((Role) result);
0975:
0976: } else if (result instanceof RoleUnresolved) {
0977: roleUnresList.add((RoleUnresolved) result);
0978: }
0979: }
0980:
0981: RoleResult toRet = new RoleResult(roleList, roleUnresList);
0982: return toRet;
0983: }
0984:
0985: /**
0986: * This method gets the Role/RoleUnresolved object provided a RoleName.
0987: */
0988: Object getRoleFriend(String theRoleName, boolean theMultiRoleFlg,
0989: RelationService service) throws IllegalArgumentException,
0990: RoleNotFoundException,
0991: RelationServiceNotRegisteredException {
0992: //This variable will indicate the problem type in the Role
0993: int problemType = 0;
0994: Role role = null;
0995: //First get the Role from the RoleTable.
0996: role = (Role) (roleToValueTable.get(theRoleName));
0997:
0998: if (role == null) {
0999: problemType = RoleStatus.NO_ROLE_WITH_NAME;
1000:
1001: } else {
1002:
1003: if (service != null) {
1004: try {
1005: problemType = (service.checkRoleReading(
1006: theRoleName, relTypeName)).intValue();
1007: } catch (RelationTypeNotFoundException ee) {
1008: throw new RuntimeException(ee.getMessage());
1009: }
1010: } else {
1011: //Invoke to find out if the RoleReading is allowed.
1012: try {
1013: Object[] params = new Object[] { theRoleName,
1014: relTypeName };
1015: String[] sig = new String[] { "java.lang.String",
1016: "java.lang.String" };
1017:
1018: problemType = ((Integer) (mbeanServer.invoke(
1019: relServiceName, "checkRoleReading", params,
1020: sig))).intValue();
1021:
1022: } catch (MBeanException me) {
1023: throw new RuntimeException(me.getMessage());
1024: } catch (ReflectionException re) {
1025: throw new RuntimeException(re.getMessage());
1026: } catch (InstanceNotFoundException ie) {
1027: throw new RelationServiceNotRegisteredException(ie
1028: .getMessage());
1029: }
1030: }
1031: }
1032:
1033: Object result = null;
1034: if (problemType == 0) {
1035: //The role has been retrieved
1036: if (!(theMultiRoleFlg)) {
1037: result = (ArrayList) (((ArrayList) (role.getRoleValue()))
1038: .clone());
1039: } else {
1040: result = (Role) (role.clone());
1041: }
1042:
1043: } else if (problemType != 0) {
1044: // Role not retrieved
1045:
1046: if (!(theMultiRoleFlg)) {
1047: try {
1048: if (problemType == RoleStatus.NO_ROLE_WITH_NAME
1049: || problemType == RoleStatus.ROLE_NOT_READABLE
1050: || problemType == RoleStatus.ROLE_NOT_WRITABLE) {
1051: throw new RoleNotFoundException();
1052: } else {
1053: throw new InvalidRoleValueException();
1054: }
1055: } catch (InvalidRoleValueException exc) {
1056: throw new RuntimeException(exc.getMessage());
1057: }
1058: } else {
1059: result = new RoleUnresolved(theRoleName, null,
1060: problemType);
1061: }
1062: }
1063:
1064: return result;
1065: }
1066:
1067: /**
1068: * This method is a localfriend to setRoles.
1069: */
1070: RoleResult setRolesFriend(RoleList theRoleList,
1071: boolean theRelServCallFlg, RelationService theRelServ)
1072: throws IllegalArgumentException,
1073: RelationTypeNotFoundException, RelationNotFoundException {
1074: if (theRoleList == null) {
1075: throw new IllegalArgumentException();
1076: }
1077:
1078: RoleList roleList = new RoleList();
1079: RoleUnresolvedList roleUnresList = new RoleUnresolvedList();
1080: Iterator iter = theRoleList.iterator();
1081:
1082: try {
1083: while (iter.hasNext()) {
1084: Role role = (Role) (iter.next());
1085: Object roleResult = null;
1086: try {
1087: roleResult = setRoleFriend(role, theRelServCallFlg,
1088: theRelServ);
1089: } catch (Exception e) {
1090: }
1091:
1092: if (roleResult instanceof Role) {
1093: roleList.add((Role) roleResult);
1094: } else if (roleResult instanceof RoleUnresolved) {
1095: roleUnresList.add((RoleUnresolved) roleResult);
1096: }
1097: }
1098: } catch (IllegalArgumentException e) {
1099: throw new RuntimeException(e.getMessage());
1100: }
1101:
1102: return new RoleResult(roleList, roleUnresList);
1103: }
1104:
1105: /**
1106: * This method sets the value of a Role.
1107: */
1108: Object setRoleFriend(Role theRole, boolean theMultiRoleFlg,
1109: RelationService service) throws IllegalArgumentException,
1110: RoleNotFoundException, InvalidRoleValueException,
1111: RelationTypeNotFoundException, RelationNotFoundException,
1112: RelationServiceNotRegisteredException {
1113: if (theRole == null) {
1114: throw new IllegalArgumentException();
1115: }
1116:
1117: //It is very similar to getRoleFriend method implementation.
1118: String roleName = theRole.getRoleName();
1119: int problemType = 0;
1120:
1121: Role role = (Role) (roleToValueTable.get(roleName));
1122:
1123: ArrayList originalValue = null;
1124: Boolean initFlag = null;
1125: if (role == null) {
1126: originalValue = new ArrayList();
1127: initFlag = new Boolean(true);
1128: } else {
1129: initFlag = new Boolean(false);
1130: originalValue = (ArrayList) (role.getRoleValue());
1131: }
1132:
1133: //check role writing before updating the table.
1134: try {
1135: if (service != null) {
1136: problemType = (service.checkRoleWriting(theRole,
1137: relTypeName, initFlag)).intValue();
1138: } else {
1139: Object[] params = new Object[] { theRole, relTypeName,
1140: initFlag };
1141: String[] sig = new String[] {
1142: "javax.management.relation.Role",
1143: "java.lang.String", "java.lang.Boolean" };
1144: problemType = ((Integer) (mbeanServer
1145: .invoke(relServiceName, "checkRoleWriting",
1146: params, sig))).intValue();
1147: }
1148: } catch (MBeanException me) {
1149: if (me.getTargetException() instanceof RelationTypeNotFoundException) {
1150: throw (RelationTypeNotFoundException) (me
1151: .getTargetException());
1152: } else {
1153: throw new RuntimeException(me.getMessage());
1154: }
1155: } catch (InstanceNotFoundException ie) {
1156: throw new RelationServiceNotRegisteredException(ie
1157: .getMessage());
1158: } catch (Exception e) {
1159: throw new RuntimeException(e.getMessage());
1160: }
1161:
1162: Object toRet = null;
1163:
1164: if (problemType == 0) {
1165: //Means role can be set.
1166: //Before updating the table send a role updateNotification
1167: if (!initFlag.booleanValue()) {
1168: Object[] params = new Object[] { relId, theRole,
1169: originalValue };
1170: String[] sig = new String[] { "java.lang.String",
1171: "javax.management.relation.Role",
1172: "java.util.List" };
1173:
1174: try {
1175: if (service != null) {
1176: service.sendRoleUpdateNotification(relId,
1177: theRole, originalValue);
1178: service.updateRoleMap(relId, theRole,
1179: originalValue);
1180: } else {
1181: mbeanServer.invoke(relServiceName,
1182: "sendRoleUpdateNotification", params,
1183: sig);
1184:
1185: //update the roleMap
1186: mbeanServer.invoke(relServiceName,
1187: "updateRoleMap", params, sig);
1188: }
1189: } catch (InstanceNotFoundException ie) {
1190: throw new RelationServiceNotRegisteredException((ie
1191: .getMessage()));
1192: } catch (MBeanException me) {
1193: if (me.getTargetException() instanceof RelationNotFoundException) {
1194: throw ((RelationNotFoundException) (me
1195: .getTargetException()));
1196: } else {
1197: throw new RuntimeException(me.getMessage());
1198: }
1199: } catch (Exception e) {
1200: throw new RuntimeException(e.getMessage());
1201: }
1202: }
1203: roleToValueTable.put(new String(roleName), (Role) (theRole
1204: .clone()));
1205: if (theMultiRoleFlg) {
1206: toRet = theRole;
1207: }
1208: } else {
1209: //Role cannot be set.
1210: if (!(theMultiRoleFlg)) {
1211: //try
1212: //{
1213: if (problemType == RoleStatus.NO_ROLE_WITH_NAME
1214: || problemType == RoleStatus.ROLE_NOT_READABLE
1215: || problemType == RoleStatus.ROLE_NOT_WRITABLE) {
1216: throw new RoleNotFoundException();
1217: } else {
1218: throw new InvalidRoleValueException();
1219: }
1220: //}catch(InvalidRoleValueException ie)
1221: //{
1222: // throw new RuntimeException(ie.getMessage());
1223: //}
1224: } else {
1225: toRet = new RoleUnresolved(roleName, theRole
1226: .getRoleValue(), problemType);
1227: }
1228: }
1229: return toRet;
1230: }
1231: }
|