public class DynamicCalculator
{
private Class calculatorC = null;
private Method calculatorM = null;
private long currentLong = 0;
/**
*
*/
public DynamicCalculator()
{
}
/**
*
*/
private static Logger log = LoggerFactory.getLogger(LoggerModel.
MODEL_PU_LIST);
/**
*
* @param expr String
* @throws Exception Exception
*/
public void parseExpression(String expr)
throws Exception
{
String str = expr;
str = str.toLowerCase();
String webpackageName = WebInstance.class.getPackage().getName();
StringBuffer sb = new StringBuffer();
sb.append(" package ").append(webpackageName).append(";");
sb.append(" class LeeCalculator").append(currentLong).append(" {");
sb.append(" public static double GetExprValue(double responsetime,");
sb.append("double sessioncount,double requestcount,double cpuvalue,double loadavgvalue) {");
sb.append(" return ").append(str).append(";");
sb.append(" }");
sb.append(" }");
String path = getClassPath();
File m_File = new File(path + "LeeCalculator" + currentLong + ".java");
PrintWriter pw = new PrintWriter(new FileWriter(m_File));
pw.println(sb.toString());
pw.close();
Main javac = new Main();
String javaFilePath = path + "LeeCalculator" + currentLong + ".java";
String[] cpargs = new String[]
{
"-d", path, javaFilePath};
int status = javac.compile(cpargs);
if (status != 0)
{
System.out.println("DynamicCalculator compile failed");
return;
}
String cClassName = webpackageName + ".LeeCalculator" + currentLong;
calculatorC = null;
calculatorM = null;
calculatorC = Class.forName(cClassName);
calculatorM = calculatorC.getDeclaredMethod("GetExprValue", new Class[]
{double.class, double.class, double.class, double.class, double.class});
File oldFile = new File(path + "LeeCalculator" + currentLong + ".java");
if (oldFile.exists())
{
oldFile.delete();
}
File oldClassFile = new File(path + "LeeCalculator" + currentLong
+
".java");
if (oldClassFile.exists())
{
oldClassFile.delete();
}
currentLong++;
System.out.println("DynamicCalculator : create successfully!");
// calculatorM=calculatorC.getMethod("GetExprValue",new Class[]{ String[].class });
}
/**
*
*/
public void getExpressionValue()
{
double value = 0;
try
{
WebInstance ins = WebInstance.getInstance();
double responsetime = ins.getResponseTime();
double sessioncount = ins.getUserCount();
double requestcount = ins.getRequestCount();
double cpuvalue = ins.getCpuValue();
double loadavgvalue = ins.getLoadAvgValue();
log.debug("responsetime===" + responsetime);
log.debug("sessioncount===" + sessioncount);
log.debug("requestcount===" + requestcount);
log.debug("cpuvalue===" + cpuvalue);
log.debug("loadavgvalue===" + loadavgvalue);
Object result = calculatorM.invoke(null,
new Object[]
{new Double(responsetime),
new Double(sessioncount),
new Double(requestcount),
new Double(cpuvalue),
new Double(loadavgvalue)});
value = Double.parseDouble(result.toString());
} catch (Exception ex)
{
value = WebInstance.getInstance().getDefaultExpressionValue();
}
WebInstance.getInstance().setCurrentValue(value);
}
/**
*
* @return String
*/
private String getClassPath()
{
URL url = DynamicCalculator.class.getResource("/");
String path = url.getPath();
log.debug("path===" + path);
return path;
}
}