/*
* Copyright 2009 kang.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/
package systeminformationmonitor.system;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
import org.hyperic.sigar.SigarException;
import systeminformationmonitor.SystemInformationMonitorApp;
import systeminformationmonitor.system.object.UptimeObject;
/**
* Uptime class generates program and system uptime.
* @author Leo Xu
*/
public class Uptime extends AbstractSystemUpdater {
private SystemInformationMonitorApp sysApp;
private RuntimeMXBean mx;
public Uptime() {
mx = ManagementFactory.getRuntimeMXBean();
sysApp = SystemInformationMonitorApp.getApplication();
}
@Override
public void prepare() {
}
@Override
public void update() {
final UptimeObject uptimeObj = getUptime();
threadLock.lock();
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
sysApp.getView().setUpTimeTextField(uptimeObj.getUptime());
}
});
} catch (InterruptedException ex) {
Logger.getLogger(Uptime.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(Uptime.class.getName()).log(Level.SEVERE, null, ex);
}
finally{
threadLock.unlock();
}
}
private UptimeObject getUptime() {
UptimeObject uptimeObj = new UptimeObject();
uptimeObj.setUptime(mx.getUptime());
return uptimeObj;
}
/**
* Return the system uptime.
* @return Sigar's version of uptime
* @deprecated Sigar's API is not functioning correctly for uptime,
* use getUptime() instead.
*/
@Deprecated
private UptimeObject getSigarSystemUptime(){
UptimeObject uptimeObj = new UptimeObject();
try {
// Sigar uptime is broken
uptimeObj.setUptime(sigar.getUptime().getUptime());
} catch (SigarException e) {
System.err.println("Exception getting uptime: " + e.getMessage());
}
return uptimeObj;
}
}