001: /**
002: * Copyright 2003-2007 Luck Consulting Pty Ltd
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package net.sf.ehcache.distribution;
016:
017: import java.util.List;
018: import java.util.ArrayList;
019:
020: /**
021: * A utility class for distributed tests
022: *
023: * @author Greg Luck
024: * @version $Id: JVMUtil.java 571 2007-12-25 23:56:28Z gregluck $
025: */
026: public final class JVMUtil {
027: private static final float JDK_1_5 = 1.5f;
028: private static final int FIRST_THREE_CHARS = 3;
029:
030: /**
031: * Utility class. No constructor
032: */
033: private JVMUtil() {
034: //noop
035: }
036:
037: /**
038: * JDK Bug Id 4267864 affecting JDKs limits the number of RMI registries to one per virtual
039: * machine. Because tests rely on creating multiple they will only work on JDK1.5.
040: *
041: * This method is used to not run the affected tests on JDK1.4.
042: * @return true if the JDK is limited to one RMI Registry per VM, else false
043: */
044: public static boolean isSingleRMIRegistryPerVM() {
045: String version = System.getProperty("java.version");
046: String majorVersion = version.substring(0, FIRST_THREE_CHARS);
047: float majorVersionFloat = Float.parseFloat(majorVersion);
048: return majorVersionFloat < JDK_1_5;
049: }
050:
051: /**
052: * Some performance numbers and size limits are higher in 15.
053: * This is used to set the tests as high as possible for each VM.
054: * @return true if JDK1.5 or higher
055: */
056: public static boolean isJDK15() {
057: return !isSingleRMIRegistryPerVM();
058: }
059:
060: /**
061: * Lists all the threads in the VM
062: * @return a List of type Thread
063: */
064: public static List enumerateThreads() {
065:
066: /**
067: * A class for visiting threads
068: */
069: class ThreadVisitor {
070:
071: private List threadList = new ArrayList();
072:
073: // This method recursively visits all thread groups under `group'.
074: private void visit(ThreadGroup group, int level) {
075: // Get threads in `group'
076: int numThreads = group.activeCount();
077: Thread[] threads = new Thread[numThreads * 2];
078: numThreads = group.enumerate(threads, false);
079:
080: // Enumerate each thread in `group'
081: for (int i = 0; i < numThreads; i++) {
082: // Get thread
083: Thread thread = threads[i];
084: threadList.add(thread);
085: }
086:
087: // Get thread subgroups of `group'
088: int numGroups = group.activeGroupCount();
089: ThreadGroup[] groups = new ThreadGroup[numGroups * 2];
090: numGroups = group.enumerate(groups, false);
091:
092: // Recursively visit each subgroup
093: for (int i = 0; i < numGroups; i++) {
094: visit(groups[i], level + 1);
095: }
096: }
097: }
098:
099: // Find the root thread group
100: ThreadGroup root = Thread.currentThread().getThreadGroup()
101: .getParent();
102: while (root.getParent() != null) {
103: root = root.getParent();
104: }
105:
106: // Visit each thread group
107: ThreadVisitor visitor = new ThreadVisitor();
108: visitor.visit(root, 0);
109: return visitor.threadList;
110: }
111:
112: }
|