From fe2a6e391ac7f79faa8ee0da76bdc93084cde491 Mon Sep 17 00:00:00 2001 From: liuchongrui Date: Fri, 26 Sep 2014 10:21:32 +0800 Subject: [PATCH] modify getLimitResult function --- .../monitor/model/LimitableFieldsModel.java | 31 ++++++++++++++++--- .../bench4q/monitor/model/MonitorMain.java | 29 +++++++++-------- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/Bench4Q-Monitor-Sigar/src/main/java/org/bench4q/monitor/model/LimitableFieldsModel.java b/Bench4Q-Monitor-Sigar/src/main/java/org/bench4q/monitor/model/LimitableFieldsModel.java index c0291a98..d15ff00e 100644 --- a/Bench4Q-Monitor-Sigar/src/main/java/org/bench4q/monitor/model/LimitableFieldsModel.java +++ b/Bench4Q-Monitor-Sigar/src/main/java/org/bench4q/monitor/model/LimitableFieldsModel.java @@ -9,8 +9,8 @@ import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class LimitableFieldsModel { + private List fieldsParent; private List fields; - @XmlElementWrapper(name = "fieldList") @XmlElement(name = "field") public List getFields() { @@ -23,10 +23,31 @@ public class LimitableFieldsModel { public LimitableFieldsModel() { this.fields = new ArrayList(); - this.fields.add("memoryUsedPercent"); - this.fields.add("processorTimePercent"); - this.fields.add("userTimePercent"); - this.fields.add("privilegedTimePercent"); + this.fieldsParent = new ArrayList(); + this.fields.add("MemoryUsedPercent"); + this.fieldsParent.add("org.bench4q.monitor.model.MemoryModel"); + this.fields.add("ProcessorTimePercent"); + this.fieldsParent.add("org.bench4q.monitor.model.ProcessorModel"); + this.fields.add("UserTimePercent"); + this.fieldsParent.add("org.bench4q.monitor.model.ProcessorModel"); + this.fields.add("PrivilegedTimePercent"); + this.fieldsParent.add("org.bench4q.monitor.model.ProcessorModel"); + } + + public String validname(String name){ + for (int i = 0; i < this.fields.size(); i++){ + if (name.toUpperCase().equals(fields.get(i).toUpperCase())) + return fieldsParent.get(i); + } + return ""; + } + + public List getFieldsParent() { + return fieldsParent; + } + + public void setFieldsParent(List fieldsParent) { + this.fieldsParent = fieldsParent; } diff --git a/Bench4Q-Monitor-Sigar/src/main/java/org/bench4q/monitor/model/MonitorMain.java b/Bench4Q-Monitor-Sigar/src/main/java/org/bench4q/monitor/model/MonitorMain.java index ba69a868..a48e64e7 100644 --- a/Bench4Q-Monitor-Sigar/src/main/java/org/bench4q/monitor/model/MonitorMain.java +++ b/Bench4Q-Monitor-Sigar/src/main/java/org/bench4q/monitor/model/MonitorMain.java @@ -1,11 +1,13 @@ package org.bench4q.monitor.model; +import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.TimeZone; import java.util.concurrent.ExecutionException; + import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @@ -96,21 +98,18 @@ public class MonitorMain extends SampleModel { for (int i = 0; i < list.size(); i++){ String name = list.get(i).getName(); double limit = list.get(i).getLimit(); - if (name.equals("memoryUsedPercent")){ - if (limit <= this.memoryModel.getMemoryUsedPercent()) - limitResult = true; - } - if (name.equals("processorTimePercent")){ - if (limit <= this.processorModel.getProcessorTimePercent()) - limitResult = true; - } - if (name.equals("userTimePercent")){ - if (limit <= this.processorModel.getUserTimePercent()) - limitResult = true; - } - if (name.equals("privilegedTimePercent")){ - if (limit <= this.processorModel.getPrivilegedTimePercent()) - limitResult = true; + LimitableFieldsModel limitableFieldsModel = new LimitableFieldsModel(); + String parent = limitableFieldsModel.validname(name); + if (parent !=""){ + try { + Class c = Class.forName(parent); + Object obj=c.newInstance(); + Method mget=c.getMethod("get"+name); + double currentvalue=(Double) mget.invoke(obj); + if (currentvalue >= limit) limitResult = true; + } catch (Exception e) { + e.printStackTrace(); + } } } return limitResult;