001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package javax.management.relation;
009:
010: import java.io.Serializable;
011: import javax.management.NotCompliantMBeanException;
012:
013: /**
014: * A RoleInfo object represents a role information in a relation type.
015: *
016: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
017: */
018:
019: public class RoleInfo implements Serializable {
020:
021: /**
022: * To specify an unlimited cardinality, -1
023: */
024: public final static int ROLE_CARDINALITY_INFINITY = -1;
025:
026: // Name of the role
027: private String name = null;
028:
029: // Read access mode: true if role is readable
030: private boolean readable = true;
031:
032: // Write access mode: true if role is writable
033: private boolean writable = true;
034:
035: // Description of role
036: private String description = null;
037:
038: // Minimum degree (i.e. minimum number of referenced MBeans in
039: // corresponding role)
040: private int minDegree = 1;
041:
042: // Maximum degree (i.e. maximum number of referenced MBeans in
043: // corresponding role)
044: private int maxDegree = 1;
045:
046: // Name of class of MBean(s) expected to be referenced in corresponding
047: // role
048: private String refMBeanClassName = null;
049:
050: /**
051: * Constructor
052: *
053: * @param name name of the role
054: * @param refMBeanClassName name of the class of MBean(s) expected to
055: * be referenced in corresponding role
056: * @param readable flag to indicate if the corresponding role
057: * can be read
058: * @param writable flag to indicate if the corresponding role
059: * can be set
060: * @param minDegree minimum degree for role, i.e. minimum number of
061: * MBeans to provide in corresponding role
062: * Must be less or equal than maxDegree.
063: * (ROLE_CARDINALITY_INFINITY for unlimited)
064: * @param maxDegree maximum degree for role, i.e. maximum number of
065: * MBeans to provide in corresponding role
066: * Must be greater or equal than minDegree
067: * (ROLE_CARDINALITY_INFINITY for unlimited)
068: * @param description description of the role (can be null)
069: *
070: * @exception IllegalArgumentException if null parameter
071: * @exception InvalidRoleInfoException if the minimum degree is
072: * greater than the maximum degree.
073: * @exception ClassNotFoundException if the class refMBeanClassName
074: * does not exist.
075: * @exception NotCompliantMBeanException if the class refMBeanClassName
076: * is not a MBean class.
077: */
078: public RoleInfo(String name, String refMBeanClassName,
079: boolean readable, boolean writable, int minDegree,
080: int maxDegree, String description)
081: throws IllegalArgumentException, InvalidRoleInfoException,
082: ClassNotFoundException, NotCompliantMBeanException {
083:
084: if (maxDegree < ROLE_CARDINALITY_INFINITY
085: || minDegree < ROLE_CARDINALITY_INFINITY) { // cant not less than the infinity value
086: throw new InvalidRoleInfoException(
087: "Minimum or maximum degree has an illegal value, must be [0, ROLE_CARDINALITY_INFINITY].");
088: }
089:
090: if (maxDegree > ROLE_CARDINALITY_INFINITY
091: && (minDegree == ROLE_CARDINALITY_INFINITY || minDegree > maxDegree)) { // maxDegree >= 0
092: throw new InvalidRoleInfoException("Minimum degree "
093: + minDegree + " is greater than maximum degree "
094: + maxDegree);
095: }
096:
097: init(name, refMBeanClassName, readable, writable, minDegree,
098: maxDegree, description);
099: }
100:
101: /**
102: * Constructor with 1 minDegree and 1 maxDegree,null description
103: *
104: */
105: public RoleInfo(String name, String refMBeanClassName,
106: boolean readable, boolean writable)
107: throws IllegalArgumentException, ClassNotFoundException,
108: NotCompliantMBeanException {
109: init(name, refMBeanClassName, readable, writable, 1, 1, null);
110: }
111:
112: /**
113: * Constructor with true readable,true writable,1 minDegree,1 maxDegree,null description
114: *
115: */
116: public RoleInfo(String theName, String theRefMBeanClassName)
117: throws IllegalArgumentException, ClassNotFoundException,
118: NotCompliantMBeanException {
119: init(theName, theRefMBeanClassName, true, true, 1, 1, null);
120: }
121:
122: /**
123: * Copy constructor
124: *
125: * @exception IllegalArgumentException if null parameter
126: */
127: public RoleInfo(RoleInfo roleInfo) throws IllegalArgumentException {
128:
129: if (roleInfo == null)
130: throw new IllegalArgumentException(
131: "Invalid parameter: roleInfo is null");
132:
133: try {
134: init(roleInfo.getName(), roleInfo.getRefMBeanClassName(),
135: roleInfo.isReadable(), roleInfo.isWritable(),
136: roleInfo.getMinDegree(), roleInfo.getMaxDegree(),
137: roleInfo.getDescription());
138: } catch (NotCompliantMBeanException e) {
139: } catch (ClassNotFoundException e) {
140: }
141: }
142:
143: /**
144: * Returns the name of the role
145: */
146: public String getName() {
147: return name;
148: }
149:
150: /**
151: * Returns read access mode for the role (true if it is readable)
152: */
153: public boolean isReadable() {
154: return readable;
155: }
156:
157: /**
158: * Returns write access mode for the role (true if it is writable)
159: */
160: public boolean isWritable() {
161: return writable;
162: }
163:
164: /**
165: * Returns description text for the role
166: */
167: public String getDescription() {
168: return description;
169: }
170:
171: /**
172: * Returns minimum degree for corresponding role reference
173: */
174: public int getMinDegree() {
175: return minDegree;
176: }
177:
178: /**
179: * Returns maximum degree for corresponding role reference
180: */
181: public int getMaxDegree() {
182: return maxDegree;
183: }
184:
185: /**
186: * Returns name of class of MBean expected to be referenced in
187: * corresponding role
188: */
189: public String getRefMBeanClassName() {
190: return refMBeanClassName;
191: }
192:
193: /**
194: * Returns a boolean to specify if given value is greater or equal than
195: * expected minimum degree (true if yes)
196: *
197: * @param value value
198: *
199: * @return true if greater or equal than minimum degree, false else
200: */
201: public boolean checkMinDegree(int value) {
202:
203: if (value < ROLE_CARDINALITY_INFINITY) {
204: return false;
205: } else if (value == ROLE_CARDINALITY_INFINITY) {
206: if (minDegree != ROLE_CARDINALITY_INFINITY)
207: return false;
208: } else {
209: if (minDegree != ROLE_CARDINALITY_INFINITY
210: && value < minDegree)
211: return false;
212: }
213:
214: return true;
215:
216: }
217:
218: /**
219: * Returns a boolean to specify if given value is less or equal than
220: * expected maximum degree (true if yes)
221: *
222: * @param value value
223: *
224: * @return true if less or equal than maximum degree, false else
225: */
226: public boolean checkMaxDegree(int value) {
227: if (value < ROLE_CARDINALITY_INFINITY) {
228: return false;
229: } else if (value == ROLE_CARDINALITY_INFINITY) {
230: if (maxDegree != ROLE_CARDINALITY_INFINITY)
231: return false;
232: } else {
233: if (maxDegree != ROLE_CARDINALITY_INFINITY
234: && value > maxDegree)
235: return false;
236: }
237: return true;
238:
239: }
240:
241: /**
242: * Prints a string describing the role info
243: */
244: public String toString() {
245: StringBuffer result = new StringBuffer();
246: result.append("role info name: " + name);
247: result.append("; isReadable: " + readable);
248: result.append("; isWritable: " + writable);
249: result.append("; description: " + description);
250: result.append("; minimum degree: " + minDegree);
251: result.append("; maximum degree: " + maxDegree);
252: result.append("; MBean class: " + refMBeanClassName);
253: return result.toString();
254: }
255:
256: // Initialisation
257: private void init(String name, String refMBeanClassName,
258: boolean readable, boolean writable, int minDegree,
259: int maxDegree, String description)
260: throws IllegalArgumentException, ClassNotFoundException,
261: NotCompliantMBeanException {
262:
263: if (name == null || refMBeanClassName == null) {
264: throw new IllegalArgumentException(
265: "Invalid parameter: name & refMBeanClassName can not be null");
266: }
267:
268: this .name = name;
269: this .readable = readable;
270: this .writable = writable;
271: this .description = description;
272: this .minDegree = minDegree;
273: this .maxDegree = maxDegree;
274:
275: // Checks if class name provided for expected MBeans corresponds to
276: // a class which exists
277: // Can throw a ClassNotFoundException
278: // Class refMBeanClass = MBeanServerFactory.getClassLoaderRepository(null).loadClass(refMBeanClassName);
279:
280: // Checks if it is a MBean class
281: // Can throw NotCompliantMBeanException
282:
283: // MBeanIntrospectorSupport.getInstance().checkCompliance(refMBeanClass);
284:
285: this.refMBeanClassName = refMBeanClassName;
286:
287: }
288:
289: }
|