FileDocCategorySizeDatePackage
JvmInfoUtil.javaAPI DocGlassfish v2 API6112Fri Jun 22 16:38:54 BST 2007com.sun.enterprise.util

JvmInfoUtil

public class JvmInfoUtil extends Object
This is a generic utility class that uses the native code to fetch process id of the current JVM process. One usage of this PId is in the --kill CLI option for stop-* commands such as asadmin stop-domain, asadmin stop-instance, asadmin stop-node-agent and asadmin stop-cluster.

Fields Summary
boolean
debug
static boolean
pidLoggingEnabled
static Logger
_logger
private static LocalStringManagerImpl
localStrings
Constructors Summary
Methods Summary
public static final intgetPIDfromFile(java.io.File pidFile)

        BufferedReader reader = null;
        int pid = -1;
        
        try {
            reader = new BufferedReader(new FileReader(pidFile));
            try {
                pid = Integer.parseInt(reader.readLine());
            }
            catch(Exception e) {
                pid = -1;
            }
        } catch (Exception ex) {
            pid = -1;
        } finally {
            try {
                if (reader != null) reader.close();
            } catch (Exception ex) {
                // too bad this stream closure failed... ignore
            }
        }
        return pid;
    
public static final intgetPIDfromFileAndDelete(java.io.File pidFile)


         
    
        // In the only current use case, we are getting the pid in order to kill
        // the process.  In which case this pid file will contain garbage so it
        // should be deleted.
        
        int pid = getPIDfromFile(pidFile);
        try
        {
            pidFile.delete();
        }
        catch(Exception e)
        {
            // ignore
        }
        
        return pid;
    
public native intgetPid()

public synchronized voidlogPID(java.lang.String pidFileName)

	try {
            System.loadLibrary("jvminfoutil");
            pidLoggingEnabled = true;
	} catch (Throwable t) {
	    // Log the fact that PID logging remains disabled 
            // which means --kill commands will not work.
            if (debug && !pidLoggingEnabled) {
                _logger.log(Level.WARNING, localStrings.getLocalString(
                "utility.no.pid.logging", "Process ID of this Java Virtual" +
                "Machine will not be logged. Consequently the --kill option " +
                "in Asadmin CLI commands such as stop-domain, stop-instance," +
                " stop-node-agent and stop-cluster will be disabled."));                        
            } 
        }
	PrintStream ps = null;
        File pidFile = new File(pidFileName);
        try {
	    if (debug) System.out.println("PID = " + getPid());
	    if (debug) System.out.println("PID FILENAME = " + pidFileName);
            
            ps = new PrintStream(pidFile);
            if (pidLoggingEnabled) 
                ps.println("" + getPid());
	    else ps.println("-1"); //dos.writeInt(-1); // clients of the pid file interprete -1 
                                   // as "NO_PROCESS should be killed".
            ps.flush();
        } catch (Throwable ex) {
            // Could not update the pid file with latest info.
            // ignore. Because the initiator VMs for stop-xxx commands
            // will check for the timestamp of this file and the admsn
        } finally {
	    try {
	        if (ps != null) ps.close();
	    } catch (Exception ex) {
	        //ignore
            }
            // Bugfix suggested by Abhijit Kumar
            // this will guarantee that once this process is dead - there won't
            // be a file with the old invalid pid lying around.
            pidFile.deleteOnExit();
        }
    
public static voidmain(java.lang.String[] args)

        int pid = new JvmInfoUtil().getPid(); 
        System.out.println("This JVM Process's Process ID is =" + pid);