0N/A/*
2362N/A * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.management.relation;
0N/A
0N/A
0N/Aimport com.sun.jmx.mbeanserver.GetPropertyAction;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectStreamField;
0N/Aimport java.io.Serializable;
0N/Aimport java.security.AccessController;
0N/A
0N/Aimport javax.management.MBeanServer;
0N/A
0N/Aimport javax.management.NotCompliantMBeanException;
0N/A
0N/A/**
0N/A * A RoleInfo object summarises a role in a relation type.
0N/A *
0N/A * <p>The <b>serialVersionUID</b> of this class is <code>2504952983494636987L</code>.
0N/A *
0N/A * @since 1.5
0N/A */
0N/A@SuppressWarnings("serial") // serialVersionUID not constant
0N/Apublic class RoleInfo implements Serializable {
0N/A
0N/A // Serialization compatibility stuff:
0N/A // Two serial forms are supported in this class. The selected form depends
0N/A // on system property "jmx.serial.form":
0N/A // - "1.0" for JMX 1.0
0N/A // - any other value for JMX 1.1 and higher
0N/A //
0N/A // Serial version for old serial form
0N/A private static final long oldSerialVersionUID = 7227256952085334351L;
0N/A //
0N/A // Serial version for new serial form
0N/A private static final long newSerialVersionUID = 2504952983494636987L;
0N/A //
0N/A // Serializable fields in old serial form
0N/A private static final ObjectStreamField[] oldSerialPersistentFields =
0N/A {
0N/A new ObjectStreamField("myName", String.class),
0N/A new ObjectStreamField("myIsReadableFlg", boolean.class),
0N/A new ObjectStreamField("myIsWritableFlg", boolean.class),
0N/A new ObjectStreamField("myDescription", String.class),
0N/A new ObjectStreamField("myMinDegree", int.class),
0N/A new ObjectStreamField("myMaxDegree", int.class),
0N/A new ObjectStreamField("myRefMBeanClassName", String.class)
0N/A };
0N/A //
0N/A // Serializable fields in new serial form
0N/A private static final ObjectStreamField[] newSerialPersistentFields =
0N/A {
0N/A new ObjectStreamField("name", String.class),
0N/A new ObjectStreamField("isReadable", boolean.class),
0N/A new ObjectStreamField("isWritable", boolean.class),
0N/A new ObjectStreamField("description", String.class),
0N/A new ObjectStreamField("minDegree", int.class),
0N/A new ObjectStreamField("maxDegree", int.class),
0N/A new ObjectStreamField("referencedMBeanClassName", String.class)
0N/A };
0N/A //
0N/A // Actual serial version and serial form
0N/A private static final long serialVersionUID;
0N/A /**
0N/A * @serialField name String Role name
0N/A * @serialField isReadable boolean Read access mode: <code>true</code> if role is readable
0N/A * @serialField isWritable boolean Write access mode: <code>true</code> if role is writable
0N/A * @serialField description String Role description
0N/A * @serialField minDegree int Minimum degree (i.e. minimum number of referenced MBeans in corresponding role)
0N/A * @serialField maxDegree int Maximum degree (i.e. maximum number of referenced MBeans in corresponding role)
0N/A * @serialField referencedMBeanClassName String Name of class of MBean(s) expected to be referenced in corresponding role
0N/A */
0N/A private static final ObjectStreamField[] serialPersistentFields;
0N/A private static boolean compat = false;
0N/A static {
0N/A try {
0N/A GetPropertyAction act = new GetPropertyAction("jmx.serial.form");
0N/A String form = AccessController.doPrivileged(act);
0N/A compat = (form != null && form.equals("1.0"));
0N/A } catch (Exception e) {
0N/A // OK : Too bad, no compat with 1.0
0N/A }
0N/A if (compat) {
0N/A serialPersistentFields = oldSerialPersistentFields;
0N/A serialVersionUID = oldSerialVersionUID;
0N/A } else {
0N/A serialPersistentFields = newSerialPersistentFields;
0N/A serialVersionUID = newSerialVersionUID;
0N/A }
0N/A }
0N/A //
0N/A // END Serialization compatibility stuff
0N/A
0N/A //
0N/A // Public constants
0N/A //
0N/A
0N/A /**
0N/A * To specify an unlimited cardinality.
0N/A */
0N/A public static final int ROLE_CARDINALITY_INFINITY = -1;
0N/A
0N/A //
0N/A // Private members
0N/A //
0N/A
0N/A /**
0N/A * @serial Role name
0N/A */
0N/A private String name = null;
0N/A
0N/A /**
0N/A * @serial Read access mode: <code>true</code> if role is readable
0N/A */
0N/A private boolean isReadable;
0N/A
0N/A /**
0N/A * @serial Write access mode: <code>true</code> if role is writable
0N/A */
0N/A private boolean isWritable;
0N/A
0N/A /**
0N/A * @serial Role description
0N/A */
0N/A private String description = null;
0N/A
0N/A /**
0N/A * @serial Minimum degree (i.e. minimum number of referenced MBeans in corresponding role)
0N/A */
0N/A private int minDegree;
0N/A
0N/A /**
0N/A * @serial Maximum degree (i.e. maximum number of referenced MBeans in corresponding role)
0N/A */
0N/A private int maxDegree;
0N/A
0N/A /**
0N/A * @serial Name of class of MBean(s) expected to be referenced in corresponding role
0N/A */
0N/A private String referencedMBeanClassName = null;
0N/A
0N/A //
0N/A // Constructors
0N/A //
0N/A
0N/A /**
0N/A * Constructor.
0N/A *
0N/A * @param roleName name of the role.
0N/A * @param mbeanClassName name of the class of MBean(s) expected to
0N/A * be referenced in corresponding role. If an MBean <em>M</em> is in
0N/A * this role, then the MBean server must return true for
0N/A * {@link MBeanServer#isInstanceOf isInstanceOf(M, mbeanClassName)}.
0N/A * @param read flag to indicate if the corresponding role
0N/A * can be read
0N/A * @param write flag to indicate if the corresponding role
0N/A * can be set
0N/A * @param min minimum degree for role, i.e. minimum number of
0N/A * MBeans to provide in corresponding role
0N/A * Must be less than or equal to <tt>max</tt>.
0N/A * (ROLE_CARDINALITY_INFINITY for unlimited)
0N/A * @param max maximum degree for role, i.e. maximum number of
0N/A * MBeans to provide in corresponding role
0N/A * Must be greater than or equal to <tt>min</tt>
0N/A * (ROLE_CARDINALITY_INFINITY for unlimited)
0N/A * @param descr description of the role (can be null)
0N/A *
0N/A * @exception IllegalArgumentException if null parameter
0N/A * @exception InvalidRoleInfoException if the minimum degree is
0N/A * greater than the maximum degree.
0N/A * @exception ClassNotFoundException As of JMX 1.2, this exception
0N/A * can no longer be thrown. It is retained in the declaration of
0N/A * this class for compatibility with existing code.
0N/A * @exception NotCompliantMBeanException if the class mbeanClassName
0N/A * is not a MBean class.
0N/A */
0N/A public RoleInfo(String roleName,
0N/A String mbeanClassName,
0N/A boolean read,
0N/A boolean write,
0N/A int min,
0N/A int max,
0N/A String descr)
0N/A throws IllegalArgumentException,
0N/A InvalidRoleInfoException,
0N/A ClassNotFoundException,
0N/A NotCompliantMBeanException {
0N/A
0N/A init(roleName,
0N/A mbeanClassName,
0N/A read,
0N/A write,
0N/A min,
0N/A max,
0N/A descr);
0N/A return;
0N/A }
0N/A
0N/A /**
0N/A * Constructor.
0N/A *
0N/A * @param roleName name of the role
0N/A * @param mbeanClassName name of the class of MBean(s) expected to
0N/A * be referenced in corresponding role. If an MBean <em>M</em> is in
0N/A * this role, then the MBean server must return true for
0N/A * {@link MBeanServer#isInstanceOf isInstanceOf(M, mbeanClassName)}.
0N/A * @param read flag to indicate if the corresponding role
0N/A * can be read
0N/A * @param write flag to indicate if the corresponding role
0N/A * can be set
0N/A *
0N/A * <P>Minimum and maximum degrees defaulted to 1.
0N/A * <P>Description of role defaulted to null.
0N/A *
0N/A * @exception IllegalArgumentException if null parameter
0N/A * @exception ClassNotFoundException As of JMX 1.2, this exception
0N/A * can no longer be thrown. It is retained in the declaration of
0N/A * this class for compatibility with existing code.
0N/A * @exception NotCompliantMBeanException As of JMX 1.2, this
0N/A * exception can no longer be thrown. It is retained in the
0N/A * declaration of this class for compatibility with existing code.
0N/A */
0N/A public RoleInfo(String roleName,
0N/A String mbeanClassName,
0N/A boolean read,
0N/A boolean write)
0N/A throws IllegalArgumentException,
0N/A ClassNotFoundException,
0N/A NotCompliantMBeanException {
0N/A
0N/A try {
0N/A init(roleName,
0N/A mbeanClassName,
0N/A read,
0N/A write,
0N/A 1,
0N/A 1,
0N/A null);
0N/A } catch (InvalidRoleInfoException exc) {
0N/A // OK : Can never happen as the minimum
0N/A // degree equals the maximum degree.
0N/A }
0N/A
0N/A return;
0N/A }
0N/A
0N/A /**
0N/A * Constructor.
0N/A *
0N/A * @param roleName name of the role
0N/A * @param mbeanClassName name of the class of MBean(s) expected to
0N/A * be referenced in corresponding role. If an MBean <em>M</em> is in
0N/A * this role, then the MBean server must return true for
0N/A * {@link MBeanServer#isInstanceOf isInstanceOf(M, mbeanClassName)}.
0N/A *
0N/A * <P>IsReadable and IsWritable defaulted to true.
0N/A * <P>Minimum and maximum degrees defaulted to 1.
0N/A * <P>Description of role defaulted to null.
0N/A *
0N/A * @exception IllegalArgumentException if null parameter
0N/A * @exception ClassNotFoundException As of JMX 1.2, this exception
0N/A * can no longer be thrown. It is retained in the declaration of
0N/A * this class for compatibility with existing code.
0N/A * @exception NotCompliantMBeanException As of JMX 1.2, this
0N/A * exception can no longer be thrown. It is retained in the
0N/A * declaration of this class for compatibility with existing code.
0N/A */
0N/A public RoleInfo(String roleName,
0N/A String mbeanClassName)
0N/A throws IllegalArgumentException,
0N/A ClassNotFoundException,
0N/A NotCompliantMBeanException {
0N/A
0N/A try {
0N/A init(roleName,
0N/A mbeanClassName,
0N/A true,
0N/A true,
0N/A 1,
0N/A 1,
0N/A null);
0N/A } catch (InvalidRoleInfoException exc) {
0N/A // OK : Can never happen as the minimum
0N/A // degree equals the maximum degree.
0N/A }
0N/A
0N/A return;
0N/A }
0N/A
0N/A /**
0N/A * Copy constructor.
0N/A *
0N/A * @param roleInfo the <tt>RoleInfo</tt> instance to be copied.
0N/A *
0N/A * @exception IllegalArgumentException if null parameter
0N/A */
0N/A public RoleInfo(RoleInfo roleInfo)
0N/A throws IllegalArgumentException {
0N/A
0N/A if (roleInfo == null) {
0N/A // Revisit [cebro] Localize message
0N/A String excMsg = "Invalid parameter.";
0N/A throw new IllegalArgumentException(excMsg);
0N/A }
0N/A
0N/A try {
0N/A init(roleInfo.getName(),
0N/A roleInfo.getRefMBeanClassName(),
0N/A roleInfo.isReadable(),
0N/A roleInfo.isWritable(),
0N/A roleInfo.getMinDegree(),
0N/A roleInfo.getMaxDegree(),
0N/A roleInfo.getDescription());
0N/A } catch (InvalidRoleInfoException exc3) {
0N/A // OK : Can never happen as the minimum degree and the maximum
0N/A // degree were already checked at the time the roleInfo
0N/A // instance was created.
0N/A }
0N/A }
0N/A
0N/A //
0N/A // Accessors
0N/A //
0N/A
0N/A /**
0N/A * Returns the name of the role.
0N/A *
0N/A * @return the name of the role.
0N/A */
0N/A public String getName() {
0N/A return name;
0N/A }
0N/A
0N/A /**
0N/A * Returns read access mode for the role (true if it is readable).
0N/A *
0N/A * @return true if the role is readable.
0N/A */
0N/A public boolean isReadable() {
0N/A return isReadable;
0N/A }
0N/A
0N/A /**
0N/A * Returns write access mode for the role (true if it is writable).
0N/A *
0N/A * @return true if the role is writable.
0N/A */
0N/A public boolean isWritable() {
0N/A return isWritable;
0N/A }
0N/A
0N/A /**
0N/A * Returns description text for the role.
0N/A *
0N/A * @return the description of the role.
0N/A */
0N/A public String getDescription() {
0N/A return description;
0N/A }
0N/A
0N/A /**
0N/A * Returns minimum degree for corresponding role reference.
0N/A *
0N/A * @return the minimum degree.
0N/A */
0N/A public int getMinDegree() {
0N/A return minDegree;
0N/A }
0N/A
0N/A /**
0N/A * Returns maximum degree for corresponding role reference.
0N/A *
0N/A * @return the maximum degree.
0N/A */
0N/A public int getMaxDegree() {
0N/A return maxDegree;
0N/A }
0N/A
0N/A /**
0N/A * <p>Returns name of type of MBean expected to be referenced in
0N/A * corresponding role.</p>
0N/A *
0N/A * @return the name of the referenced type.
0N/A */
0N/A public String getRefMBeanClassName() {
0N/A return referencedMBeanClassName;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the <tt>value</tt> parameter is greater than or equal to
0N/A * the expected minimum degree, false otherwise.
0N/A *
0N/A * @param value the value to be checked
0N/A *
0N/A * @return true if greater than or equal to minimum degree, false otherwise.
0N/A */
0N/A public boolean checkMinDegree(int value) {
0N/A if (value >= ROLE_CARDINALITY_INFINITY &&
0N/A (minDegree == ROLE_CARDINALITY_INFINITY
0N/A || value >= minDegree)) {
0N/A return true;
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the <tt>value</tt> parameter is lower than or equal to
0N/A * the expected maximum degree, false otherwise.
0N/A *
0N/A * @param value the value to be checked
0N/A *
0N/A * @return true if lower than or equal to maximum degree, false otherwise.
0N/A */
0N/A public boolean checkMaxDegree(int value) {
0N/A if (value >= ROLE_CARDINALITY_INFINITY &&
0N/A (maxDegree == ROLE_CARDINALITY_INFINITY ||
0N/A (value != ROLE_CARDINALITY_INFINITY &&
0N/A value <= maxDegree))) {
0N/A return true;
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a string describing the role info.
0N/A *
0N/A * @return a description of the role info.
0N/A */
0N/A public String toString() {
0N/A StringBuilder result = new StringBuilder();
0N/A result.append("role info name: " + name);
0N/A result.append("; isReadable: " + isReadable);
0N/A result.append("; isWritable: " + isWritable);
0N/A result.append("; description: " + description);
0N/A result.append("; minimum degree: " + minDegree);
0N/A result.append("; maximum degree: " + maxDegree);
0N/A result.append("; MBean class: " + referencedMBeanClassName);
0N/A return result.toString();
0N/A }
0N/A
0N/A //
0N/A // Misc
0N/A //
0N/A
0N/A // Initialization
0N/A private void init(String roleName,
0N/A String mbeanClassName,
0N/A boolean read,
0N/A boolean write,
0N/A int min,
0N/A int max,
0N/A String descr)
0N/A throws IllegalArgumentException,
0N/A InvalidRoleInfoException {
0N/A
0N/A if (roleName == null ||
0N/A mbeanClassName == null) {
0N/A // Revisit [cebro] Localize message
0N/A String excMsg = "Invalid parameter.";
0N/A throw new IllegalArgumentException(excMsg);
0N/A }
0N/A
0N/A name = roleName;
0N/A isReadable = read;
0N/A isWritable = write;
0N/A if (descr != null) {
0N/A description = descr;
0N/A }
0N/A
0N/A boolean invalidRoleInfoFlg = false;
0N/A StringBuilder excMsgStrB = new StringBuilder();
0N/A if (max != ROLE_CARDINALITY_INFINITY &&
0N/A (min == ROLE_CARDINALITY_INFINITY ||
0N/A min > max)) {
0N/A // Revisit [cebro] Localize message
0N/A excMsgStrB.append("Minimum degree ");
0N/A excMsgStrB.append(min);
0N/A excMsgStrB.append(" is greater than maximum degree ");
0N/A excMsgStrB.append(max);
0N/A invalidRoleInfoFlg = true;
0N/A
0N/A } else if (min < ROLE_CARDINALITY_INFINITY ||
0N/A max < ROLE_CARDINALITY_INFINITY) {
0N/A // Revisit [cebro] Localize message
0N/A excMsgStrB.append("Minimum or maximum degree has an illegal value, must be [0, ROLE_CARDINALITY_INFINITY].");
0N/A invalidRoleInfoFlg = true;
0N/A }
0N/A if (invalidRoleInfoFlg) {
0N/A throw new InvalidRoleInfoException(excMsgStrB.toString());
0N/A }
0N/A minDegree = min;
0N/A maxDegree = max;
0N/A
0N/A referencedMBeanClassName = mbeanClassName;
0N/A
0N/A return;
0N/A }
0N/A
0N/A /**
0N/A * Deserializes a {@link RoleInfo} from an {@link ObjectInputStream}.
0N/A */
0N/A private void readObject(ObjectInputStream in)
0N/A throws IOException, ClassNotFoundException {
0N/A if (compat)
0N/A {
0N/A // Read an object serialized in the old serial form
0N/A //
0N/A ObjectInputStream.GetField fields = in.readFields();
0N/A name = (String) fields.get("myName", null);
0N/A if (fields.defaulted("myName"))
0N/A {
0N/A throw new NullPointerException("myName");
0N/A }
0N/A isReadable = fields.get("myIsReadableFlg", false);
0N/A if (fields.defaulted("myIsReadableFlg"))
0N/A {
0N/A throw new NullPointerException("myIsReadableFlg");
0N/A }
0N/A isWritable = fields.get("myIsWritableFlg", false);
0N/A if (fields.defaulted("myIsWritableFlg"))
0N/A {
0N/A throw new NullPointerException("myIsWritableFlg");
0N/A }
0N/A description = (String) fields.get("myDescription", null);
0N/A if (fields.defaulted("myDescription"))
0N/A {
0N/A throw new NullPointerException("myDescription");
0N/A }
0N/A minDegree = fields.get("myMinDegree", 0);
0N/A if (fields.defaulted("myMinDegree"))
0N/A {
0N/A throw new NullPointerException("myMinDegree");
0N/A }
0N/A maxDegree = fields.get("myMaxDegree", 0);
0N/A if (fields.defaulted("myMaxDegree"))
0N/A {
0N/A throw new NullPointerException("myMaxDegree");
0N/A }
0N/A referencedMBeanClassName = (String) fields.get("myRefMBeanClassName", null);
0N/A if (fields.defaulted("myRefMBeanClassName"))
0N/A {
0N/A throw new NullPointerException("myRefMBeanClassName");
0N/A }
0N/A }
0N/A else
0N/A {
0N/A // Read an object serialized in the new serial form
0N/A //
0N/A in.defaultReadObject();
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Serializes a {@link RoleInfo} to an {@link ObjectOutputStream}.
0N/A */
0N/A private void writeObject(ObjectOutputStream out)
0N/A throws IOException {
0N/A if (compat)
0N/A {
0N/A // Serializes this instance in the old serial form
0N/A //
0N/A ObjectOutputStream.PutField fields = out.putFields();
0N/A fields.put("myName", name);
0N/A fields.put("myIsReadableFlg", isReadable);
0N/A fields.put("myIsWritableFlg", isWritable);
0N/A fields.put("myDescription", description);
0N/A fields.put("myMinDegree", minDegree);
0N/A fields.put("myMaxDegree", maxDegree);
0N/A fields.put("myRefMBeanClassName", referencedMBeanClassName);
0N/A out.writeFields();
0N/A }
0N/A else
0N/A {
0N/A // Serializes this instance in the new serial form
0N/A //
0N/A out.defaultWriteObject();
0N/A }
0N/A }
0N/A
0N/A}