001: /**
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */package javax.management.relation;
008:
009: import java.io.Serializable;
010: import javax.management.NotCompliantMBeanException;
011:
012: /**
013: * @version $Revision: 1.7 $
014: */
015: public class RoleInfo implements Serializable {
016: private static final long serialVersionUID = 2504952983494636987L;
017:
018: public static int ROLE_CARDINALITY_INFINITY = -1;
019:
020: private String name;
021: private String description;
022: //name of the class of MBean(s) expected to be referenced in corresponding role
023: private String referencedMBeanClassName;
024: private boolean isWritable;
025: private boolean isReadable;
026: private int minDegree;
027: private int maxDegree;
028:
029: public RoleInfo(String roleName, String mbeanClassName,
030: boolean isReadable, boolean isWritable, int minNumber,
031: int maxNumber, String description)
032: throws IllegalArgumentException, InvalidRoleInfoException,
033: ClassNotFoundException, NotCompliantMBeanException {
034: initialize(roleName, mbeanClassName, isReadable, isWritable,
035: minNumber, maxNumber, description);
036: }
037:
038: public RoleInfo(String roleName, String mbeanClassName,
039: boolean isReadable, boolean isWritable)
040: throws IllegalArgumentException, ClassNotFoundException,
041: NotCompliantMBeanException {
042: try {
043: // set cardinality to default 1, 1
044: initialize(roleName, mbeanClassName, isReadable,
045: isWritable, 1, 1, null);
046: } catch (InvalidRoleInfoException ignored) {
047: }
048: }
049:
050: public RoleInfo(String roleName, String mbeanClassName)
051: throws IllegalArgumentException, ClassNotFoundException,
052: NotCompliantMBeanException {
053: try {
054: // read, write and cardinality set default values
055: initialize(roleName, mbeanClassName, true, true, 1, 1, null);
056: } catch (InvalidRoleInfoException ignored) {
057: }
058: }
059:
060: public RoleInfo(RoleInfo info) throws IllegalArgumentException {
061: if (info == null) {
062: throw new IllegalArgumentException(
063: "RoleInfo cannot be null");
064: }
065:
066: try {
067: initialize(info.getName(), info.getRefMBeanClassName(),
068: info.isReadable(), info.isWritable(), info
069: .getMinDegree(), info.getMaxDegree(), info
070: .getDescription());
071: } catch (Exception ignored) {
072: // The provided role info is valid, no reason why this one would fail
073: }
074: }
075:
076: private void initialize(String roleName, String mbeanClassName,
077: boolean isReadable, boolean isWritable, int minNumber,
078: int maxNumber, String description)
079: throws IllegalArgumentException, InvalidRoleInfoException,
080: ClassNotFoundException, NotCompliantMBeanException {
081: if (roleName == null)
082: throw new IllegalArgumentException("Null Role name");
083: if (mbeanClassName == null)
084: throw new IllegalArgumentException("Null MBean class Name");
085: this .name = roleName;
086: this .isReadable = isReadable;
087: this .isWritable = isWritable;
088: this .description = description;
089: // check our cardinality is valid ie) Max is not less than Min
090: checkValidCardinality(maxNumber, minNumber);
091: this .maxDegree = maxNumber;
092: this .minDegree = minNumber;
093: this .referencedMBeanClassName = mbeanClassName;
094: }
095:
096: public String getName() {
097: return name;
098: }
099:
100: public boolean isReadable() {
101: return isReadable;
102: }
103:
104: public boolean isWritable() {
105: return isWritable;
106: }
107:
108: public String getDescription() {
109: return description;
110: }
111:
112: public int getMinDegree() {
113: return minDegree;
114: }
115:
116: public int getMaxDegree() {
117: return maxDegree;
118: }
119:
120: public String getRefMBeanClassName() {
121: return referencedMBeanClassName;
122: }
123:
124: // return true if the given value is greater than or equal to the expected maximum
125: public boolean checkMaxDegree(int maxNumber) {
126: if (maxNumber >= ROLE_CARDINALITY_INFINITY
127: && (maxDegree == ROLE_CARDINALITY_INFINITY || (maxNumber != ROLE_CARDINALITY_INFINITY && maxNumber <= maxDegree))) {
128: return true;
129: }
130: return false;
131: }
132:
133: // returns true if the given value is greater or equal to the expected minimum value
134: public boolean checkMinDegree(int minNumber) {
135: if (minNumber >= ROLE_CARDINALITY_INFINITY
136: && (minDegree == ROLE_CARDINALITY_INFINITY || minNumber >= minDegree)) {
137: return true;
138: }
139: return false;
140: }
141:
142: public String toString() {
143: StringBuffer result = new StringBuffer("Name: ");
144: result.append(name);
145: result.append("; isReadable: ").append(isReadable);
146: result.append("; isWritable: ").append(isWritable);
147: result.append("; description: ").append(description);
148: result.append("; minimum degree: ").append(minDegree);
149: result.append("; maximum degree: ").append(maxDegree);
150: result.append("; MBean class: ").append(
151: referencedMBeanClassName);
152: return result.toString();
153: }
154:
155: private void checkValidCardinality(int maxNumber, int minNumber)
156: throws InvalidRoleInfoException {
157: if (maxNumber != ROLE_CARDINALITY_INFINITY
158: && (minNumber == ROLE_CARDINALITY_INFINITY || minNumber > maxNumber)) {
159: throw new InvalidRoleInfoException(
160: "Role cardinality is invalid");
161: } else if (minNumber < ROLE_CARDINALITY_INFINITY
162: || maxNumber < ROLE_CARDINALITY_INFINITY) {
163: throw new InvalidRoleInfoException(
164: "Role cardinality is invalid");
165: }
166: }
167: }
|