1 /*
2 * Copyright 1999-2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package javax.management;
27
28 // java import
29 import java.io.Serializable;
30
31 // RI import
32 import javax.management.ObjectName;
33
34
35 /**
36 * Used to represent the object name of an MBean and its class name.
37 * If the MBean is a Dynamic MBean the class name should be retrieved from
38 * the <CODE>MBeanInfo</CODE> it provides.
39 *
40 * @since 1.5
41 */
42 public class ObjectInstance implements Serializable {
43
44
45 /* Serial version */
46 private static final long serialVersionUID = -4099952623687795850L;
47
48 /**
49 * @serial Object name.
50 */
51 private ObjectName name;
52
53 /**
54 * @serial Class name.
55 */
56 private String className;
57
58 /**
59 * Allows an object instance to be created given a string representation of
60 * an object name and the full class name, including the package name.
61 *
62 * @param objectName A string representation of the object name.
63 * @param className The full class name, including the package
64 * name, of the object instance. If the MBean is a Dynamic MBean
65 * the class name corresponds to its {@link
66 * DynamicMBean#getMBeanInfo()
67 * getMBeanInfo()}<code>.getClassName()</code>.
68 *
69 * @exception MalformedObjectNameException The string passed as a
70 * parameter does not have the right format.
71 *
72 */
73 public ObjectInstance(String objectName, String className)
74 throws MalformedObjectNameException {
75 this(new ObjectName(objectName), className);
76 }
77
78 /**
79 * Allows an object instance to be created given an object name and
80 * the full class name, including the package name.
81 *
82 * @param objectName The object name.
83 * @param className The full class name, including the package
84 * name, of the object instance. If the MBean is a Dynamic MBean
85 * the class name corresponds to its {@link
86 * DynamicMBean#getMBeanInfo()
87 * getMBeanInfo()}<code>.getClassName()</code>.
88 * If the MBean is a Dynamic MBean the class name should be retrieved
89 * from the <CODE>MBeanInfo</CODE> it provides.
90 *
91 */
92 public ObjectInstance(ObjectName objectName, String className) {
93 if (objectName.isPattern()) {
94 final IllegalArgumentException iae =
95 new IllegalArgumentException("Invalid name->"+
96 objectName.toString());
97 throw new RuntimeOperationsException(iae);
98 }
99 this.name= objectName;
100 this.className= className;
101 }
102
103
104 /**
105 * Compares the current object instance with another object instance.
106 *
107 * @param object The object instance that the current object instance is
108 * to be compared with.
109 *
110 * @return True if the two object instances are equal, otherwise false.
111 */
112 public boolean equals(Object object) {
113 if (!(object instanceof ObjectInstance)) {
114 return false;
115 }
116 ObjectInstance val = (ObjectInstance) object;
117 if (! name.equals(val.getObjectName())) return false;
118 if (className == null)
119 return (val.getClassName() == null);
120 return className.equals(val.getClassName());
121 }
122
123 public int hashCode() {
124 final int classHash = ((className==null)?0:className.hashCode());
125 return name.hashCode() ^ classHash;
126 }
127
128 /**
129 * Returns the object name part.
130 *
131 * @return the object name.
132 */
133 public ObjectName getObjectName() {
134 return name;
135 }
136
137 /**
138 * Returns the class part.
139 *
140 * @return the class name.
141 */
142 public String getClassName() {
143 return className;
144 }
145
146 /**
147 * Returns a string representing this ObjectInstance object. The format of this string
148 * is not specified, but users can expect that two ObjectInstances return the same
149 * string if and only if they are equal.
150 */
151 public String toString() {
152 return getClassName() + "[" + getObjectName() + "]";
153 }
154 }