
package de.ivu.elect.presentation.system;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;

import javax.annotation.Nullable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.ivu.elect.jee.faces.Messages;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.dynamicform.ReadOnlyPropertyFactory;

@SuppressWarnings("restriction")
public class SystemInfoViewHelper {

  private static final Logger LOG = LoggerFactory.getLogger(SystemInfoViewHelper.class);

  public SystemInfoViewHelper() {
    super();
  }

  @Nullable
  protected com.sun.management.OperatingSystemMXBean getOperationMxBean() {
    try {
      OperatingSystemMXBean osMBean = ManagementFactory.getOperatingSystemMXBean();
      if (osMBean instanceof com.sun.management.OperatingSystemMXBean) {
        return (com.sun.management.OperatingSystemMXBean) osMBean;
      } else {
        LOG.warn("OperatingSystemMXBean nicht von Sun..." + osMBean.getClass());
      }
    } catch (Exception e) {
      LOG.warn("OperatingSystemMXBean kann nicht geöffnet werden: " + e.getMessage(), e);
    }

    return null;
  }

  public void addOsInformation(FormBuilder formBuilder, Messages messages) {
    com.sun.management.OperatingSystemMXBean osBean = getOperationMxBean();

    if (osBean != null) {
      formBuilder.addSeparator("os.bean.info");
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("os.bean.Name"), osBean.getName()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("os.bean.Arch"), osBean.getArch()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("os.bean.Version"), osBean.getVersion()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("os.bean.AvailableProcessors"), Long.valueOf(osBean.getAvailableProcessors())));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("os.bean.TotalPhysicalMemorySize"), osBean.getTotalPhysicalMemorySize()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("os.bean.FreePhysicalMemorySize"), osBean.getFreePhysicalMemorySize()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("os.bean.TotalSwapSpaceSize"), osBean.getTotalSwapSpaceSize()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("os.bean.FreeSwapSpaceSize"), osBean.getFreeSwapSpaceSize()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("os.bean.CommittedVirtualMemorySize"), osBean.getCommittedVirtualMemorySize()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("os.bean.SystemCpuLoad"), osBean.getSystemCpuLoad()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("os.bean.SystemLoadAverage"), osBean.getSystemLoadAverage()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("os.bean.ProcessCpuLoad"), osBean.getProcessCpuLoad()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("os.bean.ProcessCpuTime"), osBean.getProcessCpuTime()));
    }
  }

  @Nullable
  protected RuntimeMXBean getRuntimeMXBean() {
    try {
      return ManagementFactory.getRuntimeMXBean();
    } catch (Exception e) {
      LOG.warn("RuntimeMXBean kann nicht geöffnet werden: " + e.getMessage(), e);
    }
    return null;
  }

  public void addVmInformation(FormBuilder formBuilder, Messages messages) {
    RuntimeMXBean vmBean = getRuntimeMXBean();
    if (vmBean != null) {
      formBuilder.addSeparator("vm.bean.info");
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("vm.bean.Name"), vmBean.getName()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("vm.bean.VmName"), vmBean.getVmName()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("vm.bean.VmVendor"), vmBean.getVmVendor()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("vm.bean.VmVersion"), vmBean.getVmVersion()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("vm.bean.SpecName"), vmBean.getSpecName()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("vm.bean.SpecVendor"), vmBean.getSpecVendor()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("vm.bean.SpecVersion"), vmBean.getSpecVersion()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("vm.bean.ManagementSpecVersion"), vmBean.getManagementSpecVersion()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("vm.bean.ClassPath"), vmBean.getClassPath()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("vm.bean.LibraryPath"), vmBean.getLibraryPath()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("vm.bean.BootClassPathSupported"), vmBean.isBootClassPathSupported()));
      try {
        formBuilder
            .addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("vm.bean.BootClassPath"), vmBean.getBootClassPath()));
      } catch (UnsupportedOperationException e){
        // ignore
      }
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("vm.bean.Uptime"), vmBean.getUptime()));
      formBuilder.addProperty(ReadOnlyPropertyFactory.createReadOnlyProperty(messages.getMessageForMessageKey("vm.bean.StartTime"), vmBean.getStartTime()));
    }
  }
}
