package net.hyperic.sigar.jmx;

import net.hyperic.sigar.Sigar;
import net.hyperic.sigar.SigarException;
import net.hyperic.sigar.SigarProxy;
import net.hyperic.sigar.SigarProxyCache;

/**
 * Implement the SigarProcessMBean to provide current process info
 * via JMX.
 */

public class SigarProcess implements SigarProcessMBean {

    private Sigar sigar;
    private SigarProxy sigarProxy;

    private static final String procMemName =
        SigarInvokerJMX.getObjectName("ProcMem", "$$");

    private static final String procTimeName =
        SigarInvokerJMX.getObjectName("ProcTime", "$$");

    private SigarInvokerJMX procMem, procTime;

    public SigarProcess() {
        this(null);
    }

    public SigarProcess(String path) {
        sigar = new Sigar();
        sigarProxy = SigarProxyCache.newInstance(sigar);

        procMem = SigarInvokerJMX.getInstance(sigarProxy,
                                              procMemName);

        procTime = SigarInvokerJMX.getInstance(sigarProxy,
                                               procTimeName);
    }

    public void close() {
    }

    private synchronized Long getLongValue(SigarInvokerJMX invoker, String attr) {
        try {
            return (Long)invoker.invoke(attr);
        } catch (SigarException e) {
            return new Long(-1);
        }
    }

    public Long getMemSize() {
        return getLongValue(procMem, "Size");
    }

    /**
     * @deprecated
     * @see getMemSize
     */
    public Long getMemVsize() {
        return getMemSize();
    }

    public Long getMemResident() {
        return getLongValue(procMem, "Resident");
    }

    public Long getMemShare() {
        return getLongValue(procMem, "Share");
    }

    public Long getMemPageFaults() {
        return getLongValue(procMem, "PageFaults");
    }

    public Long getTimeUser() {
        return getLongValue(procTime, "User");
    }

    public Long getTimeSys() {
        return getLongValue(procTime, "Sys");
    }

    public static void main(String args[]) {
        SigarProcessMBean proc = new SigarProcess();
        System.out.println("MemSize=" + proc.getMemSize());
        System.out.println("MemResident=" + proc.getMemResident());
        System.out.println("MemShared=" + proc.getMemShare());
        System.out.println("MemPageFaults=" + proc.getMemPageFaults());
        System.out.println("TimeUser=" + proc.getTimeUser());
        System.out.println("TimeSys=" + proc.getTimeSys());
    }
}

@@ -25,16 +25,85 @@
 r.jmx;%0A%0A
+import net.hyperic.sigar.ProcMem;%0Aimport net.hyperic.sigar.ProcTime;%0A
 import n
@@ -416,16 +416,20 @@
 ar sigar
+Impl
 ;%0A    pr
@@ -454,21 +454,16 @@
 igar
-Proxy
 ;%0A%0A    p
 riva
@@ -462,257 +462,135 @@
    p
-rivate static final String procMemName =%0A        SigarInvokerJMX.getObjectName(%22ProcMem%22, %22$$%22
+ublic SigarProcess() %7B%0A        this.sigarImpl = new Sigar(
 );%0A
-%0A
     
-private static final String procTimeName =%0A        SigarInvokerJMX.getObjectName(%22ProcTime%22, %22$$%22
+    this.sigar = SigarProxyCache.newInstance(sigarImpl
 );%0A
-%0A
     
-private SigarInvokerJMX procMem, procTime;
+%7D
 %0A%0A  
@@ -594,36 +594,34 @@
 %0A    public 
-SigarProcess
+void close
 () %7B%0A       
@@ -629,157 +629,132 @@
 this
-(null);%0A    %7D%0A%0A    public SigarProcess(String path) %7B%0A        sigar = new Sigar();%0A        sigarProxy = SigarProxyCache.newInstance(sigar
+.sigarImpl.close();%0A    %7D%0A%0A    private ProcMem getMem() %7B%0A        try %7B%0A            long pid = this.sigar.getPid(
 );%0A
-%0A
         
 proc
@@ -753,127 +753,55 @@
     
-procMem = SigarInvokerJMX.getInstance(sigarProxy,%0A                                              procMemName
+    return this.sigar.getProcMem(pid
 );%0A
-%0A
         
 proc
@@ -800,98 +800,95 @@
     
-procTime = SigarInvokerJMX.getInstance(sigarProxy,%0A                               
+%7D catch (SigarException e) %7B%0A            throw new IllegalArgumentException();%0A
         
     
@@ -887,212 +887,153 @@
     
+%7D%0A
     
+%7D%0A%0A
     pr
-ocTimeName);
+ivate ProcTime getTime() %7B
 %0A    
-%7D%0A%0A
     
-public void close()
+try
  %7B%0A    
-%7D%0A%0A
     
-private synchronized Long getLongValue(SigarInvokerJMX invoker, String attr) %7B%0A        try %7B%0A            return (Long)invoker.invoke(attr
+    long pid = this.sigar.getPid();%0A            return this.sigar.getProcTime(pid
 );%0A 
@@ -1084,26 +1084,43 @@
     
-return new Long(-1
+throw new IllegalArgumentException(
 );%0A 
@@ -1123,31 +1123,38 @@
 );%0A        %7D
+   
 %0A    %7D%0A
+    
 %0A    public 
@@ -1192,36 +1192,35 @@
 urn 
-getLongValue(procMem, %22
+new Long(getMem().get
 Size
-%22
+()
 );%0A 
@@ -1405,39 +1405,37 @@
 urn 
-getLongValue(procMem, %22
+new Long(getMem().get
 Resident
 %22);%0A
@@ -1430,17 +1430,18 @@
 Resident
-%22
+()
 );%0A    %7D
@@ -1493,37 +1493,36 @@
 urn 
-getLongValue(procMem, %22
+new Long(getMem().get
 Share
-%22
+()
 );%0A 
@@ -1583,31 +1583,29 @@
 urn 
-getLongValue(procMem, %22
+new Long(getMem().get
 Page
@@ -1610,17 +1610,18 @@
 geFaults
-%22
+()
 );%0A    %7D
@@ -1673,37 +1673,36 @@
 urn 
-getLongValue(procTime, %22
+new Long(getTime().get
 User
-%22
+()
 );%0A 
@@ -1757,36 +1757,35 @@
 urn 
-getLongValue(procTime, %22Sys%22
+new Long(getTime().getSys()
 );%0A 

call methods directly instead of using SigarInvokerJMX