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: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * JNIKill.java
044: *
045: * Created on April 5, 2002, 5:11 PM
046: */
047:
048: package org.netbeans.xtest.util;
049:
050: import java.io.File;
051:
052: /**
053: *
054: * @author mb115822
055: */
056: public class JNIKill {
057:
058: private static boolean libraryLoaded = false;
059:
060: /*
061: * this static string contains pairs of supported platforms
062: * and names of native libraries implementating kill functions
063: */
064: private static final String[][] SUPPORTED_PLATFORMS = {
065: { "Linux,i386", "lib.jnikill.linux.i386.so" },
066: { "Linux,x86", "lib.jnikill.linux.i386.so" },
067: { "Mac_OS_X,ppc", "lib.jnikill.macosx.ppc.dylib" },
068: { "SunOS,sparc", "lib.jnikill.solaris.sparc.so" },
069: { "SunOS,x86", "lib.jnikill.solaris.x86.so" },
070: { "Windows_Vista,x86", "lib.jnikill.win32.x86.dll" },
071: { "Windows_NT,x86", "lib.jnikill.win32.x86.dll" },
072: { "Windows_2000,x86", "lib.jnikill.win32.x86.dll" },
073: { "Windows_XP,x86", "lib.jnikill.win32.x86.dll" },
074: { "Windows_95,x86", "lib.jnikill.win32.x86.dll" },
075: { "Windows_98,x86", "lib.jnikill.win32.x86.dll" },
076: { "Windows_Me,x86", "lib.jnikill.win32.x86.dll" },
077: { "Windows_2003,x86", "lib.jnikill.win32.x86.dll" },
078: { "Windows_2003,amd64", "lib.jnikill.amd64.x86.dll" } };
079:
080: private static String getPlatform() {
081: String platform = System.getProperty("os.name", "") + ","
082: + System.getProperty("os.arch", "");
083: return platform.replace(' ', '_');
084: }
085:
086: // get home of xtest
087: private static String getXTestHome() {
088: return System.getProperty("xtest.home", "");
089: }
090:
091: // where is the native library stored
092: private static String getLibraryFilename(String libraryName) {
093: return getXTestHome() + File.separator + "lib" + File.separator
094: + libraryName;
095: }
096:
097: private static final String LIBRARY_SYSTEM_PROPERTY = "xtest.jnikill.library";
098:
099: private static void setLibraryLoaded() {
100: System.setProperty(LIBRARY_SYSTEM_PROPERTY, "loaded");
101: }
102:
103: private static boolean isLibraryLoaded() {
104: return System.getProperty(LIBRARY_SYSTEM_PROPERTY) != null;
105: }
106:
107: /** Loads JNI library based on which platform the code is executed. */
108: private static void loadJNILibrary() throws UnsatisfiedLinkError {
109: if (isLibraryLoaded()) {
110: System.out.println("JNI kill library already loaded");
111: } else {
112: String currentPlatform = getPlatform();
113: System.out.println("Current platform=" + currentPlatform);
114: for (int i = 0; i < SUPPORTED_PLATFORMS.length; i++) {
115: if (currentPlatform
116: .equalsIgnoreCase(SUPPORTED_PLATFORMS[i][0])) {
117: // we have it - let's load the library
118: try {
119: loadJNILibrary(SUPPORTED_PLATFORMS[i][1]);
120: } catch (UnsatisfiedLinkError ule) {
121: ule.printStackTrace();
122: }
123: if (isLibraryLoaded()) {
124: return;
125: }
126: }
127: }
128: // not possible to load library anyway
129: throw new UnsatisfiedLinkError(
130: "JNIKill: Problem while trying to load JNI kill library.");
131: }
132: }
133:
134: /** Load library and set flag if it succeeds. */
135: private static void loadJNILibrary(String libraryName) {
136: String libraryFilename = getLibraryFilename(libraryName);
137: Runtime.getRuntime().load(libraryFilename);
138: System.out.println("Loading library: " + libraryName);
139: System.out.println("Loading library from: " + libraryFilename);
140: JNIKill.setLibraryLoaded();
141: }
142:
143: // initialize native libraries !!!
144: public JNIKill() {
145: loadJNILibrary();
146: //System.out.println("JNIKill ready");
147: }
148:
149: // kill myself
150: public boolean suicide() {
151: return killProcess(getMyPID());
152: }
153:
154: /*
155: * Native methods declaration
156: */
157:
158: // native functions for killing given process
159: public native boolean killProcess(long pid);
160:
161: // native function for gettin pid of this process
162: public native long getMyPID();
163:
164: // native function creates thread performing thread dump by signals
165: public native boolean startDumpThread();
166:
167: // native function performs immediate thread dump
168: public native boolean dumpMe();
169:
170: // native function requesting thread dump on JVM with given pid
171: public native boolean requestDump(long pid);
172:
173: }
|