0N/A/*
6311N/A * Copyright (c) 2000, 2013, 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/Aimport javax.management.Notification;
0N/Aimport javax.management.ObjectName;
0N/A
6311N/Aimport java.io.InvalidObjectException;
0N/Aimport java.io.IOException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectStreamField;
0N/A
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/A
0N/Aimport java.util.ArrayList;
6311N/Aimport java.util.Arrays;
0N/Aimport java.util.Collections;
6311N/Aimport java.util.HashSet;
0N/Aimport java.util.List;
6311N/Aimport java.util.Set;
0N/A
0N/Aimport com.sun.jmx.mbeanserver.GetPropertyAction;
0N/Aimport static com.sun.jmx.mbeanserver.Util.cast;
0N/A
0N/A/**
0N/A * A notification of a change in the Relation Service.
0N/A * A RelationNotification notification is sent when a relation is created via
0N/A * the Relation Service, or an MBean is added as a relation in the Relation
0N/A * Service, or a role is updated in a relation, or a relation is removed from
0N/A * the Relation Service.
0N/A *
0N/A * <p>The <b>serialVersionUID</b> of this class is <code>-6871117877523310399L</code>.
0N/A *
0N/A * @since 1.5
0N/A */
0N/A@SuppressWarnings("serial") // serialVersionUID not constant
0N/Apublic class RelationNotification extends Notification {
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 = -2126464566505527147L;
0N/A //
0N/A // Serial version for new serial form
0N/A private static final long newSerialVersionUID = -6871117877523310399L;
0N/A //
0N/A // Serializable fields in old serial form
0N/A private static final ObjectStreamField[] oldSerialPersistentFields =
0N/A {
0N/A new ObjectStreamField("myNewRoleValue", ArrayList.class),
0N/A new ObjectStreamField("myOldRoleValue", ArrayList.class),
0N/A new ObjectStreamField("myRelId", String.class),
0N/A new ObjectStreamField("myRelObjName", ObjectName.class),
0N/A new ObjectStreamField("myRelTypeName", String.class),
0N/A new ObjectStreamField("myRoleName", String.class),
0N/A new ObjectStreamField("myUnregMBeanList", ArrayList.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("newRoleValue", List.class),
0N/A new ObjectStreamField("oldRoleValue", List.class),
0N/A new ObjectStreamField("relationId", String.class),
0N/A new ObjectStreamField("relationObjName", ObjectName.class),
0N/A new ObjectStreamField("relationTypeName", String.class),
0N/A new ObjectStreamField("roleName", String.class),
0N/A new ObjectStreamField("unregisterMBeanList", List.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 relationId String Relation identifier of
0N/A * created/removed/updated relation
0N/A * @serialField relationTypeName String Relation type name of
0N/A * created/removed/updated relation
0N/A * @serialField relationObjName ObjectName {@link ObjectName} of
0N/A * the relation MBean of created/removed/updated relation (only if
0N/A * the relation is represented by an MBean)
0N/A * @serialField unregisterMBeanList List List of {@link
0N/A * ObjectName}s of referenced MBeans to be unregistered due to
0N/A * relation removal
0N/A * @serialField roleName String Name of updated role (only for role update)
0N/A * @serialField oldRoleValue List Old role value ({@link
0N/A * ArrayList} of {@link ObjectName}s) (only for role update)
0N/A * @serialField newRoleValue List New role value ({@link
0N/A * ArrayList} of {@link ObjectName}s) (only for role update)
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 // Notification types
0N/A //
0N/A
0N/A /**
0N/A * Type for the creation of an internal relation.
0N/A */
0N/A public static final String RELATION_BASIC_CREATION = "jmx.relation.creation.basic";
0N/A /**
0N/A * Type for the relation MBean added into the Relation Service.
0N/A */
0N/A public static final String RELATION_MBEAN_CREATION = "jmx.relation.creation.mbean";
0N/A /**
0N/A * Type for an update of an internal relation.
0N/A */
0N/A public static final String RELATION_BASIC_UPDATE = "jmx.relation.update.basic";
0N/A /**
0N/A * Type for the update of a relation MBean.
0N/A */
0N/A public static final String RELATION_MBEAN_UPDATE = "jmx.relation.update.mbean";
0N/A /**
0N/A * Type for the removal from the Relation Service of an internal relation.
0N/A */
0N/A public static final String RELATION_BASIC_REMOVAL = "jmx.relation.removal.basic";
0N/A /**
0N/A * Type for the removal from the Relation Service of a relation MBean.
0N/A */
0N/A public static final String RELATION_MBEAN_REMOVAL = "jmx.relation.removal.mbean";
0N/A
0N/A //
0N/A // Private members
0N/A //
0N/A
0N/A /**
0N/A * @serial Relation identifier of created/removed/updated relation
0N/A */
0N/A private String relationId = null;
0N/A
0N/A /**
0N/A * @serial Relation type name of created/removed/updated relation
0N/A */
0N/A private String relationTypeName = null;
0N/A
0N/A /**
0N/A * @serial {@link ObjectName} of the relation MBean of created/removed/updated relation
0N/A * (only if the relation is represented by an MBean)
0N/A */
0N/A private ObjectName relationObjName = null;
0N/A
0N/A /**
0N/A * @serial List of {@link ObjectName}s of referenced MBeans to be unregistered due to
0N/A * relation removal
0N/A */
0N/A private List<ObjectName> unregisterMBeanList = null;
0N/A
0N/A /**
0N/A * @serial Name of updated role (only for role update)
0N/A */
0N/A private String roleName = null;
0N/A
0N/A /**
0N/A * @serial Old role value ({@link ArrayList} of {@link ObjectName}s) (only for role update)
0N/A */
0N/A private List<ObjectName> oldRoleValue = null;
0N/A
0N/A /**
0N/A * @serial New role value ({@link ArrayList} of {@link ObjectName}s) (only for role update)
0N/A */
0N/A private List<ObjectName> newRoleValue = null;
0N/A
0N/A //
0N/A // Constructors
0N/A //
0N/A
0N/A /**
0N/A * Creates a notification for either a relation creation (RelationSupport
0N/A * object created internally in the Relation Service, or an MBean added as a
0N/A * relation) or for a relation removal from the Relation Service.
0N/A *
0N/A * @param notifType type of the notification; either:
0N/A * <P>- RELATION_BASIC_CREATION
0N/A * <P>- RELATION_MBEAN_CREATION
0N/A * <P>- RELATION_BASIC_REMOVAL
0N/A * <P>- RELATION_MBEAN_REMOVAL
0N/A * @param sourceObj source object, sending the notification. This is either
0N/A * an ObjectName or a RelationService object. In the latter case it must be
0N/A * the MBean emitting the notification; the MBean Server will rewrite the
0N/A * source to be the ObjectName under which that MBean is registered.
0N/A * @param sequence sequence number to identify the notification
0N/A * @param timeStamp time stamp
0N/A * @param message human-readable message describing the notification
0N/A * @param id relation id identifying the relation in the Relation
0N/A * Service
0N/A * @param typeName name of the relation type
0N/A * @param objectName ObjectName of the relation object if it is an MBean
0N/A * (null for relations internally handled by the Relation Service)
0N/A * @param unregMBeanList list of ObjectNames of referenced MBeans
0N/A * expected to be unregistered due to relation removal (only for removal,
0N/A * due to CIM qualifiers, can be null)
0N/A *
0N/A * @exception IllegalArgumentException if:
0N/A * <P>- no value for the notification type
0N/A * <P>- the notification type is not RELATION_BASIC_CREATION,
0N/A * RELATION_MBEAN_CREATION, RELATION_BASIC_REMOVAL or
0N/A * RELATION_MBEAN_REMOVAL
0N/A * <P>- no source object
0N/A * <P>- the source object is not a Relation Service
0N/A * <P>- no relation id
0N/A * <P>- no relation type name
0N/A */
0N/A public RelationNotification(String notifType,
0N/A Object sourceObj,
0N/A long sequence,
0N/A long timeStamp,
0N/A String message,
0N/A String id,
0N/A String typeName,
0N/A ObjectName objectName,
0N/A List<ObjectName> unregMBeanList)
0N/A throws IllegalArgumentException {
0N/A
0N/A super(notifType, sourceObj, sequence, timeStamp, message);
0N/A
6311N/A if (!isValidBasic(notifType,sourceObj,id,typeName) || !isValidCreate(notifType)) {
6311N/A throw new IllegalArgumentException("Invalid parameter.");
6311N/A }
6311N/A
6311N/A relationId = id;
6311N/A relationTypeName = typeName;
6311N/A relationObjName = safeGetObjectName(objectName);
6311N/A unregisterMBeanList = safeGetObjectNameList(unregMBeanList);
0N/A }
0N/A
0N/A /**
0N/A * Creates a notification for a role update in a relation.
0N/A *
0N/A * @param notifType type of the notification; either:
0N/A * <P>- RELATION_BASIC_UPDATE
0N/A * <P>- RELATION_MBEAN_UPDATE
0N/A * @param sourceObj source object, sending the notification. This is either
0N/A * an ObjectName or a RelationService object. In the latter case it must be
0N/A * the MBean emitting the notification; the MBean Server will rewrite the
0N/A * source to be the ObjectName under which that MBean is registered.
0N/A * @param sequence sequence number to identify the notification
0N/A * @param timeStamp time stamp
0N/A * @param message human-readable message describing the notification
0N/A * @param id relation id identifying the relation in the Relation
0N/A * Service
0N/A * @param typeName name of the relation type
0N/A * @param objectName ObjectName of the relation object if it is an MBean
0N/A * (null for relations internally handled by the Relation Service)
0N/A * @param name name of the updated role
0N/A * @param newValue new role value (List of ObjectName objects)
0N/A * @param oldValue old role value (List of ObjectName objects)
0N/A *
0N/A * @exception IllegalArgumentException if null parameter
0N/A */
0N/A public RelationNotification(String notifType,
0N/A Object sourceObj,
0N/A long sequence,
0N/A long timeStamp,
0N/A String message,
0N/A String id,
0N/A String typeName,
0N/A ObjectName objectName,
0N/A String name,
0N/A List<ObjectName> newValue,
0N/A List<ObjectName> oldValue
0N/A )
0N/A throws IllegalArgumentException {
0N/A
0N/A super(notifType, sourceObj, sequence, timeStamp, message);
0N/A
6311N/A if (!isValidBasic(notifType,sourceObj,id,typeName) || !isValidUpdate(notifType,name,newValue,oldValue)) {
6311N/A throw new IllegalArgumentException("Invalid parameter.");
6311N/A }
6311N/A
6311N/A relationId = id;
6311N/A relationTypeName = typeName;
6311N/A relationObjName = safeGetObjectName(objectName);
6311N/A
6311N/A roleName = name;
6311N/A oldRoleValue = safeGetObjectNameList(oldValue);
6311N/A newRoleValue = safeGetObjectNameList(newValue);
0N/A }
0N/A
0N/A //
0N/A // Accessors
0N/A //
0N/A
0N/A /**
0N/A * Returns the relation identifier of created/removed/updated relation.
0N/A *
0N/A * @return the relation id.
0N/A */
0N/A public String getRelationId() {
0N/A return relationId;
0N/A }
0N/A
0N/A /**
0N/A * Returns the relation type name of created/removed/updated relation.
0N/A *
0N/A * @return the relation type name.
0N/A */
0N/A public String getRelationTypeName() {
0N/A return relationTypeName;
0N/A }
0N/A
0N/A /**
0N/A * Returns the ObjectName of the
0N/A * created/removed/updated relation.
0N/A *
0N/A * @return the ObjectName if the relation is an MBean, otherwise null.
0N/A */
0N/A public ObjectName getObjectName() {
0N/A return relationObjName;
0N/A }
0N/A
0N/A /**
0N/A * Returns the list of ObjectNames of MBeans expected to be unregistered
0N/A * due to a relation removal (only for relation removal).
0N/A *
0N/A * @return a {@link List} of {@link ObjectName}.
0N/A */
0N/A public List<ObjectName> getMBeansToUnregister() {
277N/A List<ObjectName> result;
0N/A if (unregisterMBeanList != null) {
0N/A result = new ArrayList<ObjectName>(unregisterMBeanList);
0N/A } else {
0N/A result = Collections.emptyList();
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Returns name of updated role of updated relation (only for role update).
0N/A *
0N/A * @return the name of the updated role.
0N/A */
0N/A public String getRoleName() {
0N/A String result = null;
0N/A if (roleName != null) {
0N/A result = roleName;
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Returns old value of updated role (only for role update).
0N/A *
0N/A * @return the old value of the updated role.
0N/A */
0N/A public List<ObjectName> getOldRoleValue() {
277N/A List<ObjectName> result;
0N/A if (oldRoleValue != null) {
0N/A result = new ArrayList<ObjectName>(oldRoleValue);
0N/A } else {
0N/A result = Collections.emptyList();
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Returns new value of updated role (only for role update).
0N/A *
0N/A * @return the new value of the updated role.
0N/A */
0N/A public List<ObjectName> getNewRoleValue() {
277N/A List<ObjectName> result;
0N/A if (newRoleValue != null) {
0N/A result = new ArrayList<ObjectName>(newRoleValue);
0N/A } else {
0N/A result = Collections.emptyList();
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A //
0N/A // Misc
0N/A //
0N/A
0N/A // Initializes members
0N/A //
0N/A // -param notifKind 1 for creation/removal, 2 for update
0N/A // -param notifType type of the notification; either:
0N/A // - RELATION_BASIC_UPDATE
0N/A // - RELATION_MBEAN_UPDATE
0N/A // for an update, or:
0N/A // - RELATION_BASIC_CREATION
0N/A // - RELATION_MBEAN_CREATION
0N/A // - RELATION_BASIC_REMOVAL
0N/A // - RELATION_MBEAN_REMOVAL
0N/A // for a creation or removal
0N/A // -param sourceObj source object, sending the notification. Will always
0N/A // be a RelationService object.
0N/A // -param sequence sequence number to identify the notification
0N/A // -param timeStamp time stamp
0N/A // -param message human-readable message describing the notification
0N/A // -param id relation id identifying the relation in the Relation
0N/A // Service
0N/A // -param typeName name of the relation type
0N/A // -param objectName ObjectName of the relation object if it is an MBean
0N/A // (null for relations internally handled by the Relation Service)
0N/A // -param unregMBeanList list of ObjectNames of MBeans expected to be
0N/A // removed due to relation removal
0N/A // -param name name of the updated role
0N/A // -param newValue new value (List of ObjectName objects)
0N/A // -param oldValue old value (List of ObjectName objects)
0N/A //
0N/A // -exception IllegalArgumentException if:
0N/A // - no value for the notification type
0N/A // - incorrect notification type
0N/A // - no source object
0N/A // - the source object is not a Relation Service
0N/A // - no relation id
0N/A // - no relation type name
0N/A // - no role name (for role update)
0N/A // - no role old value (for role update)
0N/A // - no role new value (for role update)
0N/A
6311N/A private boolean isValidBasic(String notifType, Object sourceObj, String id, String typeName){
6311N/A if (notifType == null || sourceObj == null ||
6311N/A id == null || typeName == null) {
6311N/A return false;
6311N/A }
0N/A
6311N/A if (!(sourceObj instanceof RelationService) &&
6311N/A !(sourceObj instanceof ObjectName)) {
6311N/A return false;
0N/A }
0N/A
6311N/A return true;
6311N/A }
0N/A
6311N/A private boolean isValidCreate(String notifType) {
6311N/A String[] validTypes= {RelationNotification.RELATION_BASIC_CREATION,
6311N/A RelationNotification.RELATION_MBEAN_CREATION,
6311N/A RelationNotification.RELATION_BASIC_REMOVAL,
6311N/A RelationNotification.RELATION_MBEAN_REMOVAL};
6311N/A
6311N/A Set<String> ctSet = new HashSet<String>(Arrays.asList(validTypes));
6311N/A return ctSet.contains(notifType);
6311N/A }
6311N/A
6311N/A private boolean isValidUpdate(String notifType, String name,
6311N/A List<ObjectName> newValue, List<ObjectName> oldValue) {
0N/A
6311N/A if (!(notifType.equals(RelationNotification.RELATION_BASIC_UPDATE)) &&
6311N/A !(notifType.equals(RelationNotification.RELATION_MBEAN_UPDATE))) {
6311N/A return false;
6311N/A }
0N/A
6311N/A if (name == null || oldValue == null || newValue == null) {
6311N/A return false;
6311N/A }
0N/A
6311N/A return true;
6311N/A }
0N/A
6311N/A private ArrayList<ObjectName> safeGetObjectNameList(List<ObjectName> src){
6311N/A ArrayList<ObjectName> dest = null;
6311N/A if (src != null) {
6311N/A dest = new ArrayList<ObjectName>();
6311N/A for (ObjectName item : src) {
6311N/A // NPE thrown if we attempt to add null object
6311N/A dest.add(ObjectName.getInstance(item));
0N/A }
0N/A }
6311N/A return dest;
6311N/A }
0N/A
6311N/A private ObjectName safeGetObjectName(ObjectName src){
6311N/A ObjectName dest = null;
6311N/A if (src != null) {
6311N/A dest = ObjectName.getInstance(src);
0N/A }
6311N/A return dest;
0N/A }
0N/A
0N/A /**
0N/A * Deserializes a {@link RelationNotification} from an {@link ObjectInputStream}.
0N/A */
0N/A private void readObject(ObjectInputStream in)
0N/A throws IOException, ClassNotFoundException {
6311N/A
6311N/A String tmpRelationId, tmpRelationTypeName, tmpRoleName;
6311N/A
6311N/A ObjectName tmpRelationObjName;
6311N/A List<ObjectName> tmpNewRoleValue, tmpOldRoleValue, tmpUnregMBeanList;
6311N/A
0N/A ObjectInputStream.GetField fields = in.readFields();
6311N/A
6311N/A if (compat) {
6311N/A tmpRelationId = (String)fields.get("myRelId", null);
6311N/A tmpRelationTypeName = (String)fields.get("myRelTypeName", null);
6311N/A tmpRoleName = (String)fields.get("myRoleName", null);
6311N/A
6311N/A tmpRelationObjName = (ObjectName)fields.get("myRelObjName", null);
6311N/A tmpNewRoleValue = cast(fields.get("myNewRoleValue", null));
6311N/A tmpOldRoleValue = cast(fields.get("myOldRoleValue", null));
6311N/A tmpUnregMBeanList = cast(fields.get("myUnregMBeanList", null));
0N/A }
6311N/A else {
6311N/A tmpRelationId = (String)fields.get("relationId", null);
6311N/A tmpRelationTypeName = (String)fields.get("relationTypeName", null);
6311N/A tmpRoleName = (String)fields.get("roleName", null);
6311N/A
6311N/A tmpRelationObjName = (ObjectName)fields.get("relationObjName", null);
6311N/A tmpNewRoleValue = cast(fields.get("newRoleValue", null));
6311N/A tmpOldRoleValue = cast(fields.get("oldRoleValue", null));
6311N/A tmpUnregMBeanList = cast(fields.get("unregisterMBeanList", null));
0N/A }
6311N/A
6311N/A // Validate fields we just read, throw InvalidObjectException
6311N/A // if something goes wrong
6311N/A
6311N/A String notifType = super.getType();
6311N/A if (!isValidBasic(notifType,super.getSource(),tmpRelationId,tmpRelationTypeName) ||
6311N/A (!isValidCreate(notifType) &&
6311N/A !isValidUpdate(notifType,tmpRoleName,tmpNewRoleValue,tmpOldRoleValue))) {
6311N/A
6311N/A super.setSource(null);
6311N/A throw new InvalidObjectException("Invalid object read");
0N/A }
6311N/A
6311N/A // assign deserialized vaules to object fields
6311N/A relationObjName = safeGetObjectName(tmpRelationObjName);
6311N/A newRoleValue = safeGetObjectNameList(tmpNewRoleValue);
6311N/A oldRoleValue = safeGetObjectNameList(tmpOldRoleValue);
6311N/A unregisterMBeanList = safeGetObjectNameList(tmpUnregMBeanList);
6311N/A
6311N/A relationId = tmpRelationId;
6311N/A relationTypeName = tmpRelationTypeName;
6311N/A roleName = tmpRoleName;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Serializes a {@link RelationNotification} 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("myNewRoleValue", newRoleValue);
0N/A fields.put("myOldRoleValue", oldRoleValue);
0N/A fields.put("myRelId", relationId);
0N/A fields.put("myRelObjName", relationObjName);
0N/A fields.put("myRelTypeName", relationTypeName);
0N/A fields.put("myRoleName",roleName);
0N/A fields.put("myUnregMBeanList", unregisterMBeanList);
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}