001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.server.system;
042:
043: import java.lang.management.ManagementFactory;
044: import java.lang.reflect.InvocationTargetException;
045: import java.lang.reflect.Method;
046: import javax.management.InstanceNotFoundException;
047: import javax.management.JMRuntimeException;
048: import javax.management.MBeanException;
049: import javax.management.MBeanServer;
050: import javax.management.MalformedObjectNameException;
051: import javax.management.ObjectInstance;
052: import javax.management.ObjectName;
053: import javax.management.ReflectionException;
054:
055: /**
056: *
057: * @author Tomas Hurka
058: */
059: public class HeapDump {
060: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
061:
062: private static Object hotspotDiag;
063: private static Method dumpHeapMethod;
064: private static boolean initialized;
065:
066: //~ Constructors -------------------------------------------------------------------------------------------------------------
067:
068: private HeapDump() {
069: }
070:
071: //~ Methods ------------------------------------------------------------------------------------------------------------------
072:
073: public static void initialize(boolean jdk15) {
074: if (jdk15) {
075: initialize15();
076: } else {
077: initialize16();
078: }
079: }
080:
081: public static String takeHeapDump(boolean jdk15, String outputFile) {
082: if (jdk15) {
083: return takeHeapDump15(outputFile);
084: }
085:
086: return takeHeapDump16(outputFile);
087: }
088:
089: private static native void initialize15();
090:
091: private static void initialize16() {
092: MBeanServer mserver;
093:
094: if (initialized) {
095: return;
096: }
097:
098: initialized = true;
099:
100: try {
101: mserver = ManagementFactory.getPlatformMBeanServer();
102: } catch (JMRuntimeException ex) {
103: // Glassfish: if ManagementFactory.getPlatformMBeanServer() is called too early it will throw JMRuntimeException
104: // in such case initialization will be rerun later as part of takeHeapDump()
105: System.err.println(ex.getLocalizedMessage());
106: initialized = false;
107:
108: return;
109: }
110:
111: try {
112: ObjectInstance instance = mserver
113: .getObjectInstance(new ObjectName(
114: "com.sun.management:type=HotSpotDiagnostic")); // NOI18N
115: hotspotDiag = mserver.instantiate(instance.getClassName());
116: dumpHeapMethod = hotspotDiag.getClass().getMethod(
117: "dumpHeap",
118: new Class[] { String.class, Boolean.TYPE }); // NOI18N
119: } catch (MalformedObjectNameException ex) {
120: ex.printStackTrace();
121: } catch (InstanceNotFoundException ex) {
122: System.err.println("Heap Dump is not available"); // NOI18N
123: } catch (MBeanException ex) {
124: ex.printStackTrace();
125: } catch (SecurityException ex) {
126: ex.printStackTrace();
127: } catch (ReflectionException ex) {
128: ex.printStackTrace();
129: } catch (NullPointerException ex) {
130: ex.printStackTrace();
131: } catch (NoSuchMethodException ex) {
132: ex.printStackTrace();
133: }
134: }
135:
136: private static String takeHeapDump15(String outputFile) {
137: int error = -1;
138:
139: try {
140: error = takeHeapDump15Native(outputFile);
141: } catch (Exception ex) {
142: return ex.getLocalizedMessage();
143: }
144:
145: if (error == -1) {
146: return "Take heap dump is not available."; // NOI18N
147: }
148:
149: return null;
150: }
151:
152: private static native int takeHeapDump15Native(String outputFile);
153:
154: private static String takeHeapDump16(String outputFile) {
155: String error = null;
156: initialize16();
157:
158: if ((dumpHeapMethod == null) || (hotspotDiag == null)) {
159: return "Take heap dump is not available."; // NOI18N
160: }
161:
162: try {
163: dumpHeapMethod.invoke(hotspotDiag, new Object[] {
164: outputFile, Boolean.TRUE });
165: } catch (IllegalArgumentException ex) {
166: error = ex.getLocalizedMessage();
167: } catch (IllegalAccessException ex) {
168: error = ex.getLocalizedMessage();
169: } catch (InvocationTargetException ex) {
170: error = ex.getTargetException().getLocalizedMessage();
171: }
172:
173: return error;
174: }
175: }
|