8008120: Improve JMX class checking

Improve JMX class checking

Reviewed-by: mchung, dfuchs, alanb, skoivu
This commit is contained in:
Dmitry Samersoff 2013-03-31 22:59:14 +04:00
parent 07ca78ead7
commit 87a26efe0d

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -28,6 +28,7 @@ package javax.management.relation;
import javax.management.Notification; import javax.management.Notification;
import javax.management.ObjectName; import javax.management.ObjectName;
import java.io.InvalidObjectException;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
@ -37,8 +38,11 @@ import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import com.sun.jmx.mbeanserver.GetPropertyAction; import com.sun.jmx.mbeanserver.GetPropertyAction;
import static com.sun.jmx.mbeanserver.Util.cast; import static com.sun.jmx.mbeanserver.Util.cast;
@ -256,21 +260,14 @@ public class RelationNotification extends Notification {
super(notifType, sourceObj, sequence, timeStamp, message); super(notifType, sourceObj, sequence, timeStamp, message);
// Can throw IllegalArgumentException if (!isValidBasic(notifType,sourceObj,id,typeName) || !isValidCreate(notifType)) {
initMembers(1, throw new IllegalArgumentException("Invalid parameter.");
notifType, }
sourceObj,
sequence, relationId = id;
timeStamp, relationTypeName = typeName;
message, relationObjName = safeGetObjectName(objectName);
id, unregisterMBeanList = safeGetObjectNameList(unregMBeanList);
typeName,
objectName,
unregMBeanList,
null,
null,
null);
return;
} }
/** /**
@ -313,21 +310,17 @@ public class RelationNotification extends Notification {
super(notifType, sourceObj, sequence, timeStamp, message); super(notifType, sourceObj, sequence, timeStamp, message);
// Can throw IllegalArgumentException if (!isValidBasic(notifType,sourceObj,id,typeName) || !isValidUpdate(notifType,name,newValue,oldValue)) {
initMembers(2, throw new IllegalArgumentException("Invalid parameter.");
notifType, }
sourceObj,
sequence, relationId = id;
timeStamp, relationTypeName = typeName;
message, relationObjName = safeGetObjectName(objectName);
id,
typeName, roleName = name;
objectName, oldRoleValue = safeGetObjectNameList(oldValue);
null, newRoleValue = safeGetObjectNameList(newValue);
name,
newValue,
oldValue);
return;
} }
// //
@ -463,83 +456,64 @@ public class RelationNotification extends Notification {
// - no role name (for role update) // - no role name (for role update)
// - no role old value (for role update) // - no role old value (for role update)
// - no role new value (for role update) // - no role new value (for role update)
private void initMembers(int notifKind,
String notifType,
Object sourceObj,
long sequence,
long timeStamp,
String message,
String id,
String typeName,
ObjectName objectName,
List<ObjectName> unregMBeanList,
String name,
List<ObjectName> newValue,
List<ObjectName> oldValue)
throws IllegalArgumentException {
boolean badInitFlg = false; private boolean isValidBasic(String notifType, Object sourceObj, String id, String typeName){
if (notifType == null || sourceObj == null ||
if (notifType == null || id == null || typeName == null) {
sourceObj == null || return false;
(!(sourceObj instanceof RelationService) &&
!(sourceObj instanceof ObjectName)) ||
id == null ||
typeName == null) {
badInitFlg = true;
} }
if (notifKind == 1) { if (!(sourceObj instanceof RelationService) &&
!(sourceObj instanceof ObjectName)) {
if ((!(notifType.equals(RelationNotification.RELATION_BASIC_CREATION))) return false;
&&
(!(notifType.equals(RelationNotification.RELATION_MBEAN_CREATION)))
&&
(!(notifType.equals(RelationNotification.RELATION_BASIC_REMOVAL)))
&&
(!(notifType.equals(RelationNotification.RELATION_MBEAN_REMOVAL)))
) {
// Creation/removal
badInitFlg = true;
} }
} else if (notifKind == 2) { return true;
if (((!(notifType.equals(RelationNotification.RELATION_BASIC_UPDATE)))
&&
(!(notifType.equals(RelationNotification.RELATION_MBEAN_UPDATE))))
|| name == null ||
oldValue == null ||
newValue == null) {
// Role update
badInitFlg = true;
}
} }
if (badInitFlg) { private boolean isValidCreate(String notifType) {
String excMsg = "Invalid parameter."; String[] validTypes= {RelationNotification.RELATION_BASIC_CREATION,
throw new IllegalArgumentException(excMsg); RelationNotification.RELATION_MBEAN_CREATION,
RelationNotification.RELATION_BASIC_REMOVAL,
RelationNotification.RELATION_MBEAN_REMOVAL};
Set<String> ctSet = new HashSet<String>(Arrays.asList(validTypes));
return ctSet.contains(notifType);
} }
relationId = id; private boolean isValidUpdate(String notifType, String name,
relationTypeName = typeName; List<ObjectName> newValue, List<ObjectName> oldValue) {
relationObjName = objectName;
if (unregMBeanList != null) { if (!(notifType.equals(RelationNotification.RELATION_BASIC_UPDATE)) &&
unregisterMBeanList = new ArrayList<ObjectName>(unregMBeanList); !(notifType.equals(RelationNotification.RELATION_MBEAN_UPDATE))) {
return false;
} }
if (name != null) {
roleName = name; if (name == null || oldValue == null || newValue == null) {
return false;
} }
if (oldValue != null) {
oldRoleValue = new ArrayList<ObjectName>(oldValue); return true;
} }
if (newValue != null) {
newRoleValue = new ArrayList<ObjectName>(newValue); private ArrayList<ObjectName> safeGetObjectNameList(List<ObjectName> src){
ArrayList<ObjectName> dest = null;
if (src != null) {
dest = new ArrayList<ObjectName>();
for (ObjectName item : src) {
// NPE thrown if we attempt to add null object
dest.add(ObjectName.getInstance(item));
} }
return; }
return dest;
}
private ObjectName safeGetObjectName(ObjectName src){
ObjectName dest = null;
if (src != null) {
dest = ObjectName.getInstance(src);
}
return dest;
} }
/** /**
@ -547,53 +521,56 @@ public class RelationNotification extends Notification {
*/ */
private void readObject(ObjectInputStream in) private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException { throws IOException, ClassNotFoundException {
if (compat)
{ String tmpRelationId, tmpRelationTypeName, tmpRoleName;
// Read an object serialized in the old serial form
// ObjectName tmpRelationObjName;
List<ObjectName> tmpNewRoleValue, tmpOldRoleValue, tmpUnregMBeanList;
ObjectInputStream.GetField fields = in.readFields(); ObjectInputStream.GetField fields = in.readFields();
newRoleValue = cast(fields.get("myNewRoleValue", null));
if (fields.defaulted("myNewRoleValue")) if (compat) {
{ tmpRelationId = (String)fields.get("myRelId", null);
throw new NullPointerException("newRoleValue"); tmpRelationTypeName = (String)fields.get("myRelTypeName", null);
tmpRoleName = (String)fields.get("myRoleName", null);
tmpRelationObjName = (ObjectName)fields.get("myRelObjName", null);
tmpNewRoleValue = cast(fields.get("myNewRoleValue", null));
tmpOldRoleValue = cast(fields.get("myOldRoleValue", null));
tmpUnregMBeanList = cast(fields.get("myUnregMBeanList", null));
} }
oldRoleValue = cast(fields.get("myOldRoleValue", null)); else {
if (fields.defaulted("myOldRoleValue")) tmpRelationId = (String)fields.get("relationId", null);
{ tmpRelationTypeName = (String)fields.get("relationTypeName", null);
throw new NullPointerException("oldRoleValue"); tmpRoleName = (String)fields.get("roleName", null);
tmpRelationObjName = (ObjectName)fields.get("relationObjName", null);
tmpNewRoleValue = cast(fields.get("newRoleValue", null));
tmpOldRoleValue = cast(fields.get("oldRoleValue", null));
tmpUnregMBeanList = cast(fields.get("unregisterMBeanList", null));
} }
relationId = (String) fields.get("myRelId", null);
if (fields.defaulted("myRelId")) // Validate fields we just read, throw InvalidObjectException
{ // if something goes wrong
throw new NullPointerException("relationId");
} String notifType = super.getType();
relationObjName = (ObjectName) fields.get("myRelObjName", null); if (!isValidBasic(notifType,super.getSource(),tmpRelationId,tmpRelationTypeName) ||
if (fields.defaulted("myRelObjName")) (!isValidCreate(notifType) &&
{ !isValidUpdate(notifType,tmpRoleName,tmpNewRoleValue,tmpOldRoleValue))) {
throw new NullPointerException("relationObjName");
} super.setSource(null);
relationTypeName = (String) fields.get("myRelTypeName", null); throw new InvalidObjectException("Invalid object read");
if (fields.defaulted("myRelTypeName"))
{
throw new NullPointerException("relationTypeName");
}
roleName = (String) fields.get("myRoleName", null);
if (fields.defaulted("myRoleName"))
{
throw new NullPointerException("roleName");
}
unregisterMBeanList = cast(fields.get("myUnregMBeanList", null));
if (fields.defaulted("myUnregMBeanList"))
{
throw new NullPointerException("unregisterMBeanList");
}
}
else
{
// Read an object serialized in the new serial form
//
in.defaultReadObject();
} }
// assign deserialized vaules to object fields
relationObjName = safeGetObjectName(tmpRelationObjName);
newRoleValue = safeGetObjectNameList(tmpNewRoleValue);
oldRoleValue = safeGetObjectNameList(tmpOldRoleValue);
unregisterMBeanList = safeGetObjectNameList(tmpUnregMBeanList);
relationId = tmpRelationId;
relationTypeName = tmpRelationTypeName;
roleName = tmpRoleName;
} }