Showing the changes to java/pkg/sun/management/ThreadInfoCompositeData.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,415 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2004, 2008, Oracle and/or its affiliates. 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. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +package sun.management; | |
| 27 | + | |
| 28 | +import java.lang.management.ThreadInfo; | |
| 29 | +import java.lang.management.MonitorInfo; | |
| 30 | +import java.lang.management.LockInfo; | |
| 31 | +import javax.management.openmbean.CompositeType; | |
| 32 | +import javax.management.openmbean.CompositeData; | |
| 33 | +import javax.management.openmbean.CompositeDataSupport; | |
| 34 | +import javax.management.openmbean.OpenDataException; | |
| 35 | +import javax.management.openmbean.OpenType; | |
| 36 | + | |
| 37 | +/** | |
| 38 | + * A CompositeData for ThreadInfo for the local management support. | |
| 39 | + * This class avoids the performance penalty paid to the | |
| 40 | + * construction of a CompositeData use in the local case. | |
| 41 | + */ | |
| 42 | +public class ThreadInfoCompositeData extends LazyCompositeData { | |
| 43 | + private final ThreadInfo threadInfo; | |
| 44 | + private final CompositeData cdata; | |
| 45 | + private final boolean currentVersion; | |
| 46 | + | |
| 47 | + private ThreadInfoCompositeData(ThreadInfo ti) { | |
| 48 | + this.threadInfo = ti; | |
| 49 | + this.currentVersion = true; | |
| 50 | + this.cdata = null; | |
| 51 | + } | |
| 52 | + | |
| 53 | + private ThreadInfoCompositeData(CompositeData cd) { | |
| 54 | + this.threadInfo = null; | |
| 55 | + this.currentVersion = ThreadInfoCompositeData.isCurrentVersion(cd); | |
| 56 | + this.cdata = cd; | |
| 57 | + } | |
| 58 | + | |
| 59 | + public ThreadInfo getThreadInfo() { | |
| 60 | + return threadInfo; | |
| 61 | + } | |
| 62 | + | |
| 63 | + public boolean isCurrentVersion() { | |
| 64 | + return currentVersion; | |
| 65 | + } | |
| 66 | + | |
| 67 | + public static ThreadInfoCompositeData getInstance(CompositeData cd) { | |
| 68 | + validateCompositeData(cd); | |
| 69 | + return new ThreadInfoCompositeData(cd); | |
| 70 | + } | |
| 71 | + | |
| 72 | + public static CompositeData toCompositeData(ThreadInfo ti) { | |
| 73 | + ThreadInfoCompositeData ticd = new ThreadInfoCompositeData(ti); | |
| 74 | + return ticd.getCompositeData(); | |
| 75 | + } | |
| 76 | + | |
| 77 | + protected CompositeData getCompositeData() { | |
| 78 | + // Convert StackTraceElement[] to CompositeData[] | |
| 79 | + StackTraceElement[] stackTrace = threadInfo.getStackTrace(); | |
| 80 | + CompositeData[] stackTraceData = | |
| 81 | + new CompositeData[stackTrace.length]; | |
| 82 | + for (int i = 0; i < stackTrace.length; i++) { | |
| 83 | + StackTraceElement ste = stackTrace[i]; | |
| 84 | + stackTraceData[i] = StackTraceElementCompositeData.toCompositeData(ste); | |
| 85 | + } | |
| 86 | + | |
| 87 | + // Convert MonitorInfo[] and LockInfo[] to CompositeData[] | |
| 88 | + LockDataConverter converter = new LockDataConverter(threadInfo); | |
| 89 | + CompositeData lockInfoData = converter.toLockInfoCompositeData(); | |
| 90 | + CompositeData[] lockedSyncsData = converter.toLockedSynchronizersCompositeData(); | |
| 91 | + | |
| 92 | + // Convert MonitorInfo[] to CompositeData[] | |
| 93 | + MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors(); | |
| 94 | + CompositeData[] lockedMonitorsData = | |
| 95 | + new CompositeData[lockedMonitors.length]; | |
| 96 | + for (int i = 0; i < lockedMonitors.length; i++) { | |
| 97 | + MonitorInfo mi = lockedMonitors[i]; | |
| 98 | + lockedMonitorsData[i] = MonitorInfoCompositeData.toCompositeData(mi); | |
| 99 | + } | |
| 100 | + | |
| 101 | + | |
| 102 | + // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH | |
| 103 | + // threadInfoItemNames! | |
| 104 | + final Object[] threadInfoItemValues = { | |
| 105 | + new Long(threadInfo.getThreadId()), | |
| 106 | + threadInfo.getThreadName(), | |
| 107 | + threadInfo.getThreadState().name(), | |
| 108 | + new Long(threadInfo.getBlockedTime()), | |
| 109 | + new Long(threadInfo.getBlockedCount()), | |
| 110 | + new Long(threadInfo.getWaitedTime()), | |
| 111 | + new Long(threadInfo.getWaitedCount()), | |
| 112 | + lockInfoData, | |
| 113 | + threadInfo.getLockName(), | |
| 114 | + new Long(threadInfo.getLockOwnerId()), | |
| 115 | + threadInfo.getLockOwnerName(), | |
| 116 | + stackTraceData, | |
| 117 | + new Boolean(threadInfo.isSuspended()), | |
| 118 | + new Boolean(threadInfo.isInNative()), | |
| 119 | + lockedMonitorsData, | |
| 120 | + lockedSyncsData, | |
| 121 | + }; | |
| 122 | + | |
| 123 | + try { | |
| 124 | + return new CompositeDataSupport(threadInfoCompositeType, | |
| 125 | + threadInfoItemNames, | |
| 126 | + threadInfoItemValues); | |
| 127 | + } catch (OpenDataException e) { | |
| 128 | + // Should never reach here | |
| 129 | + throw new AssertionError(e); | |
| 130 | + } | |
| 131 | + } | |
| 132 | + | |
| 133 | + // Attribute names | |
| 134 | + private static final String THREAD_ID = "threadId"; | |
| 135 | + private static final String THREAD_NAME = "threadName"; | |
| 136 | + private static final String THREAD_STATE = "threadState"; | |
| 137 | + private static final String BLOCKED_TIME = "blockedTime"; | |
| 138 | + private static final String BLOCKED_COUNT = "blockedCount"; | |
| 139 | + private static final String WAITED_TIME = "waitedTime"; | |
| 140 | + private static final String WAITED_COUNT = "waitedCount"; | |
| 141 | + private static final String LOCK_INFO = "lockInfo"; | |
| 142 | + private static final String LOCK_NAME = "lockName"; | |
| 143 | + private static final String LOCK_OWNER_ID = "lockOwnerId"; | |
| 144 | + private static final String LOCK_OWNER_NAME = "lockOwnerName"; | |
| 145 | + private static final String STACK_TRACE = "stackTrace"; | |
| 146 | + private static final String SUSPENDED = "suspended"; | |
| 147 | + private static final String IN_NATIVE = "inNative"; | |
| 148 | + private static final String LOCKED_MONITORS = "lockedMonitors"; | |
| 149 | + private static final String LOCKED_SYNCS = "lockedSynchronizers"; | |
| 150 | + | |
| 151 | + private static final String[] threadInfoItemNames = { | |
| 152 | + THREAD_ID, | |
| 153 | + THREAD_NAME, | |
| 154 | + THREAD_STATE, | |
| 155 | + BLOCKED_TIME, | |
| 156 | + BLOCKED_COUNT, | |
| 157 | + WAITED_TIME, | |
| 158 | + WAITED_COUNT, | |
| 159 | + LOCK_INFO, | |
| 160 | + LOCK_NAME, | |
| 161 | + LOCK_OWNER_ID, | |
| 162 | + LOCK_OWNER_NAME, | |
| 163 | + STACK_TRACE, | |
| 164 | + SUSPENDED, | |
| 165 | + IN_NATIVE, | |
| 166 | + LOCKED_MONITORS, | |
| 167 | + LOCKED_SYNCS, | |
| 168 | + }; | |
| 169 | + | |
| 170 | + // New attributes added in 6.0 ThreadInfo | |
| 171 | + private static final String[] threadInfoV6Attributes = { | |
| 172 | + LOCK_INFO, | |
| 173 | + LOCKED_MONITORS, | |
| 174 | + LOCKED_SYNCS, | |
| 175 | + }; | |
| 176 | + | |
| 177 | + // Current version of ThreadInfo | |
| 178 | + private static final CompositeType threadInfoCompositeType; | |
| 179 | + // Previous version of ThreadInfo | |
| 180 | + private static final CompositeType threadInfoV5CompositeType; | |
| 181 | + private static final CompositeType lockInfoCompositeType; | |
| 182 | + static { | |
| 183 | + try { | |
| 184 | + threadInfoCompositeType = (CompositeType) | |
| 185 | + MappedMXBeanType.toOpenType(ThreadInfo.class); | |
| 186 | + // Form a CompositeType for JDK 5.0 ThreadInfo version | |
| 187 | + String[] itemNames = | |
| 188 | + threadInfoCompositeType.keySet().toArray(new String[0]); | |
| 189 | + int numV5Attributes = threadInfoItemNames.length - | |
| 190 | + threadInfoV6Attributes.length; | |
| 191 | + String[] v5ItemNames = new String[numV5Attributes]; | |
| 192 | + String[] v5ItemDescs = new String[numV5Attributes]; | |
| 193 | + OpenType[] v5ItemTypes = new OpenType[numV5Attributes]; | |
| 194 | + int i = 0; | |
| 195 | + for (String n : itemNames) { | |
| 196 | + if (isV5Attribute(n)) { | |
| 197 | + v5ItemNames[i] = n; | |
| 198 | + v5ItemDescs[i] = threadInfoCompositeType.getDescription(n); | |
| 199 | + v5ItemTypes[i] = threadInfoCompositeType.getType(n); | |
| 200 | + i++; | |
| 201 | + } | |
| 202 | + } | |
| 203 | + | |
| 204 | + threadInfoV5CompositeType = | |
| 205 | + new CompositeType("java.lang.management.ThreadInfo", | |
| 206 | + "J2SE 5.0 java.lang.management.ThreadInfo", | |
| 207 | + v5ItemNames, | |
| 208 | + v5ItemDescs, | |
| 209 | + v5ItemTypes); | |
| 210 | + } catch (OpenDataException e) { | |
| 211 | + // Should never reach here | |
| 212 | + throw new AssertionError(e); | |
| 213 | + } | |
| 214 | + | |
| 215 | + // Each CompositeData object has its CompositeType associated | |
| 216 | + // with it. So we can get the CompositeType representing LockInfo | |
| 217 | + // from a mapped CompositeData for any LockInfo object. | |
| 218 | + // Thus we construct a random LockInfo object and pass it | |
| 219 | + // to LockDataConverter to do the conversion. | |
| 220 | + Object o = new Object(); | |
| 221 | + LockInfo li = new LockInfo(o.getClass().getName(), | |
| 222 | + System.identityHashCode(o)); | |
| 223 | + CompositeData cd = LockDataConverter.toLockInfoCompositeData(li); | |
| 224 | + lockInfoCompositeType = cd.getCompositeType(); | |
| 225 | + } | |
| 226 | + | |
| 227 | + private static boolean isV5Attribute(String itemName) { | |
| 228 | + for (String n : threadInfoV6Attributes) { | |
| 229 | + if (itemName.equals(n)) { | |
| 230 | + return false; | |
| 231 | + } | |
| 232 | + } | |
| 233 | + return true; | |
| 234 | + } | |
| 235 | + | |
| 236 | + public static boolean isCurrentVersion(CompositeData cd) { | |
| 237 | + if (cd == null) { | |
| 238 | + throw new NullPointerException("Null CompositeData"); | |
| 239 | + } | |
| 240 | + | |
| 241 | + return isTypeMatched(threadInfoCompositeType, cd.getCompositeType()); | |
| 242 | + } | |
| 243 | + | |
| 244 | + public long threadId() { | |
| 245 | + return getLong(cdata, THREAD_ID); | |
| 246 | + } | |
| 247 | + | |
| 248 | + public String threadName() { | |
| 249 | + // The ThreadName item cannot be null so we check that | |
| 250 | + // it is present with a non-null value. | |
| 251 | + String name = getString(cdata, THREAD_NAME); | |
| 252 | + if (name == null) { | |
| 253 | + throw new IllegalArgumentException("Invalid composite data: " + | |
| 254 | + "Attribute " + THREAD_NAME + " has null value"); | |
| 255 | + } | |
| 256 | + return name; | |
| 257 | + } | |
| 258 | + | |
| 259 | + public Thread.State threadState() { | |
| 260 | + return Thread.State.valueOf(getString(cdata, THREAD_STATE)); | |
| 261 | + } | |
| 262 | + | |
| 263 | + public long blockedTime() { | |
| 264 | + return getLong(cdata, BLOCKED_TIME); | |
| 265 | + } | |
| 266 | + | |
| 267 | + public long blockedCount() { | |
| 268 | + return getLong(cdata, BLOCKED_COUNT); | |
| 269 | + } | |
| 270 | + | |
| 271 | + public long waitedTime() { | |
| 272 | + return getLong(cdata, WAITED_TIME); | |
| 273 | + } | |
| 274 | + | |
| 275 | + public long waitedCount() { | |
| 276 | + return getLong(cdata, WAITED_COUNT); | |
| 277 | + } | |
| 278 | + | |
| 279 | + public String lockName() { | |
| 280 | + // The LockName and LockOwnerName can legitimately be null, | |
| 281 | + // we don't bother to check the value | |
| 282 | + return getString(cdata, LOCK_NAME); | |
| 283 | + } | |
| 284 | + | |
| 285 | + public long lockOwnerId() { | |
| 286 | + return getLong(cdata, LOCK_OWNER_ID); | |
| 287 | + } | |
| 288 | + | |
| 289 | + public String lockOwnerName() { | |
| 290 | + return getString(cdata, LOCK_OWNER_NAME); | |
| 291 | + } | |
| 292 | + | |
| 293 | + public boolean suspended() { | |
| 294 | + return getBoolean(cdata, SUSPENDED); | |
| 295 | + } | |
| 296 | + | |
| 297 | + public boolean inNative() { | |
| 298 | + return getBoolean(cdata, IN_NATIVE); | |
| 299 | + } | |
| 300 | + | |
| 301 | + public StackTraceElement[] stackTrace() { | |
| 302 | + CompositeData[] stackTraceData = | |
| 303 | + (CompositeData[]) cdata.get(STACK_TRACE); | |
| 304 | + | |
| 305 | + // The StackTrace item cannot be null, but if it is we will get | |
| 306 | + // a NullPointerException when we ask for its length. | |
| 307 | + StackTraceElement[] stackTrace = | |
| 308 | + new StackTraceElement[stackTraceData.length]; | |
| 309 | + for (int i = 0; i < stackTraceData.length; i++) { | |
| 310 | + CompositeData cdi = stackTraceData[i]; | |
| 311 | + stackTrace[i] = StackTraceElementCompositeData.from(cdi); | |
| 312 | + } | |
| 313 | + return stackTrace; | |
| 314 | + } | |
| 315 | + | |
| 316 | + // 6.0 new attributes | |
| 317 | + public LockInfo lockInfo() { | |
| 318 | + LockDataConverter converter = new LockDataConverter(); | |
| 319 | + CompositeData lockInfoData = (CompositeData) cdata.get(LOCK_INFO); | |
| 320 | + return converter.toLockInfo(lockInfoData); | |
| 321 | + } | |
| 322 | + | |
| 323 | + public MonitorInfo[] lockedMonitors() { | |
| 324 | + CompositeData[] lockedMonitorsData = | |
| 325 | + (CompositeData[]) cdata.get(LOCKED_MONITORS); | |
| 326 | + | |
| 327 | + // The LockedMonitors item cannot be null, but if it is we will get | |
| 328 | + // a NullPointerException when we ask for its length. | |
| 329 | + MonitorInfo[] monitors = | |
| 330 | + new MonitorInfo[lockedMonitorsData.length]; | |
| 331 | + for (int i = 0; i < lockedMonitorsData.length; i++) { | |
| 332 | + CompositeData cdi = lockedMonitorsData[i]; | |
| 333 | + monitors[i] = MonitorInfo.from(cdi); | |
| 334 | + } | |
| 335 | + return monitors; | |
| 336 | + } | |
| 337 | + | |
| 338 | + public LockInfo[] lockedSynchronizers() { | |
| 339 | + LockDataConverter converter = new LockDataConverter(); | |
| 340 | + CompositeData[] lockedSyncsData = | |
| 341 | + (CompositeData[]) cdata.get(LOCKED_SYNCS); | |
| 342 | + | |
| 343 | + // The LockedSynchronizers item cannot be null, but if it is we will | |
| 344 | + // get a NullPointerException when we ask for its length. | |
| 345 | + return converter.toLockedSynchronizers(lockedSyncsData); | |
| 346 | + } | |
| 347 | + | |
| 348 | + /** Validate if the input CompositeData has the expected | |
| 349 | + * CompositeType (i.e. contain all attributes with expected | |
| 350 | + * names and types). | |
| 351 | + */ | |
| 352 | + public static void validateCompositeData(CompositeData cd) { | |
| 353 | + if (cd == null) { | |
| 354 | + throw new NullPointerException("Null CompositeData"); | |
| 355 | + } | |
| 356 | + | |
| 357 | + CompositeType type = cd.getCompositeType(); | |
| 358 | + boolean currentVersion = true; | |
| 359 | + if (!isTypeMatched(threadInfoCompositeType, type)) { | |
| 360 | + currentVersion = false; | |
| 361 | + // check if cd is an older version | |
| 362 | + if (!isTypeMatched(threadInfoV5CompositeType, type)) { | |
| 363 | + throw new IllegalArgumentException( | |
| 364 | + "Unexpected composite type for ThreadInfo"); | |
| 365 | + } | |
| 366 | + } | |
| 367 | + | |
| 368 | + CompositeData[] stackTraceData = | |
| 369 | + (CompositeData[]) cd.get(STACK_TRACE); | |
| 370 | + if (stackTraceData == null) { | |
| 371 | + throw new IllegalArgumentException( | |
| 372 | + "StackTraceElement[] is missing"); | |
| 373 | + } | |
| 374 | + if (stackTraceData.length > 0) { | |
| 375 | + StackTraceElementCompositeData.validateCompositeData(stackTraceData[0]); | |
| 376 | + } | |
| 377 | + | |
| 378 | + // validate v6 attributes | |
| 379 | + if (currentVersion) { | |
| 380 | + CompositeData li = (CompositeData) cd.get(LOCK_INFO); | |
| 381 | + if (li != null) { | |
| 382 | + if (!isTypeMatched(lockInfoCompositeType, | |
| 383 | + li.getCompositeType())) { | |
| 384 | + throw new IllegalArgumentException( | |
| 385 | + "Unexpected composite type for \"" + | |
| 386 | + LOCK_INFO + "\" attribute."); | |
| 387 | + } | |
| 388 | + } | |
| 389 | + | |
| 390 | + CompositeData[] lms = (CompositeData[]) cd.get(LOCKED_MONITORS); | |
| 391 | + if (lms == null) { | |
| 392 | + throw new IllegalArgumentException("MonitorInfo[] is null"); | |
| 393 | + } | |
| 394 | + if (lms.length > 0) { | |
| 395 | + MonitorInfoCompositeData.validateCompositeData(lms[0]); | |
| 396 | + } | |
| 397 | + | |
| 398 | + CompositeData[] lsyncs = (CompositeData[]) cd.get(LOCKED_SYNCS); | |
| 399 | + if (lsyncs == null) { | |
| 400 | + throw new IllegalArgumentException("LockInfo[] is null"); | |
| 401 | + } | |
| 402 | + if (lsyncs.length > 0) { | |
| 403 | + if (!isTypeMatched(lockInfoCompositeType, | |
| 404 | + lsyncs[0].getCompositeType())) { | |
| 405 | + throw new IllegalArgumentException( | |
| 406 | + "Unexpected composite type for \"" + | |
| 407 | + LOCKED_SYNCS + "\" attribute."); | |
| 408 | + } | |
| 409 | + } | |
| 410 | + | |
| 411 | + } | |
| 412 | + } | |
| 413 | + | |
| 414 | + private static final long serialVersionUID = 2464378539119753175L; | |
| 415 | +} | |