From 144b3e13dfe0c997fa8b3446e5441cec1608bdf1 Mon Sep 17 00:00:00 2001 From: Tienan Chen Date: Fri, 6 Sep 2013 20:32:21 +0800 Subject: [PATCH] add load agents from pool --- .../web/controller/AgentActionController.java | 258 +++++++++++------- .../web/controller/BaseActionController.java | 60 ++-- .../controller/ScriptActionController.java | 6 +- src/main/webapp/admin.jsp | 7 +- src/main/webapp/bench4qjs/admin.js | 21 ++ src/main/webapp/bench4qjs/register.js | 5 +- 6 files changed, 226 insertions(+), 131 deletions(-) create mode 100644 src/main/webapp/bench4qjs/admin.js diff --git a/src/main/java/org/bench4q/web/controller/AgentActionController.java b/src/main/java/org/bench4q/web/controller/AgentActionController.java index d184abff..7a6745e4 100644 --- a/src/main/java/org/bench4q/web/controller/AgentActionController.java +++ b/src/main/java/org/bench4q/web/controller/AgentActionController.java @@ -1,100 +1,158 @@ -package org.bench4q.web.controller; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.StringWriter; -import java.util.HashMap; -import java.util.Map; - -import javax.servlet.http.HttpServletRequest; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.bind.Unmarshaller; - - - -import org.bench4q.master.api.model.agent.AgentResponseModel; -import org.bench4q.master.communication.HttpRequester; -import org.bench4q.master.communication.HttpRequester.HttpResponse; -import org.bench4q.master.entity.db.Agent; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.SessionAttributes; - -@Controller -@SessionAttributes("accessToken") -public class AgentActionController extends BaseActionController { - private AgentResponseModel extractAgentResponseModel(String content) - throws JAXBException { - AgentResponseModel resultModel = new AgentResponseModel(); - Unmarshaller ummarshaller = JAXBContext.newInstance( - resultModel.getClass()).createUnmarshaller(); - resultModel = (AgentResponseModel) ummarshaller - .unmarshal(new ByteArrayInputStream(content.getBytes())); - return resultModel; - } - - private String _marshalAgentToString(Agent agent) throws JAXBException { - StringWriter stringWriter = new StringWriter(); - Marshaller marshaller = JAXBContext.newInstance(agent.getClass()) - .createMarshaller(); - marshaller.marshal(agent, stringWriter); - return stringWriter.toString(); - } - - @RequestMapping("addAgenttoPool.do") - public @ResponseBody String addAgenttoPool (HttpServletRequest request,@ModelAttribute("accessToken") String accessToken) throws IOException, JAXBException { - System.out.println("enter add port"); - String urlString = masterIP + "agentManage/addAgentToPool"; - HttpRequester httpRequester = new HttpRequester(); - String hostname = request.getParameter("hostname"); - String maxLoad = request.getParameter("maxLoad"); - int maxLoadnum = Integer.parseInt(maxLoad); - Agent agent = new Agent(); - agent.setHostName(hostname); - agent.setMaxLoad(maxLoadnum); - agent.setRemainLoad(maxLoadnum); - agent.setPort(6565); - String content = _marshalAgentToString(agent); - - Map properties = new HashMap(); - properties.put(AUTH_HEADER_PROPERTY, this.ACCES_TOCKEN_STARTER - + accessToken); - - HttpResponse httpResponse = httpRequester.sendPostXml(urlString, content, properties); - AgentResponseModel agentResponseModel = extractAgentResponseModel(httpResponse.getContent()); - if (agentResponseModel.isSuccess()) { - System.out.println("success"); - return "success"; - } else { - System.out.println("failure"); - return "failure"; - } - } - - @RequestMapping("removeAgentfromPool.do") - public @ResponseBody String removeAgentfromPool (HttpServletRequest request,@ModelAttribute("accessToken") String accessToken) throws IOException, JAXBException { - System.out.println("enter remove port"); - String urlString = masterIP + "agentManage/removeAgentFromPool"; - HttpRequester httpRequester = new HttpRequester(); - String hostName = request.getParameter("hostName"); - Map properties = new HashMap(); - properties.put(AUTH_HEADER_PROPERTY, this.ACCES_TOCKEN_STARTER - + accessToken); - Map params = new HashMap(); - params.put("hostName", hostName); - HttpResponse httpResponse = httpRequester.sendGet(urlString, - params, properties); - AgentResponseModel agentResponseModel = extractAgentResponseModel(httpResponse.getContent()); - if (agentResponseModel.isSuccess()) { - System.out.println("success"); - return "success"; - } else { - System.out.println("failure"); - return "failure"; - } - } -} +package org.bench4q.web.controller; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; + +import org.bench4q.master.api.model.agent.AgentResponseModel; +import org.bench4q.master.communication.HttpRequester; +import org.bench4q.master.communication.HttpRequester.HttpResponse; +import org.bench4q.master.entity.db.Agent; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.SessionAttributes; + +@Controller +@SessionAttributes("accessToken") +public class AgentActionController extends BaseActionController { + private final String baseUrl = this.getMasterIP() + "/agentManage"; + + private String getBaseUrl() { + return baseUrl; + } + + @RequestMapping("addAgenttoPool.do") + public @ResponseBody + String addAgenttoPool(HttpServletRequest request, + @ModelAttribute("accessToken") String accessToken) + throws IOException, JAXBException { + System.out.println("enter add port"); + String urlString = masterIP + "agentManage/addAgentToPool"; + HttpRequester httpRequester = new HttpRequester(); + String hostname = request.getParameter("hostname"); + String maxLoad = request.getParameter("maxLoad"); + int maxLoadnum = Integer.parseInt(maxLoad); + Agent agent = new Agent(); + agent.setHostName(hostname); + agent.setMaxLoad(maxLoadnum); + agent.setRemainLoad(maxLoadnum); + agent.setPort(6565); + String content = _marshalAgentToString(agent); + + Map properties = new HashMap(); + properties.put(AUTH_HEADER_PROPERTY, this.ACCES_TOCKEN_STARTER + + accessToken); + + HttpResponse httpResponse = httpRequester.sendPostXml(urlString, + content, properties); + AgentResponseModel agentResponseModel = extractAgentResponseModel(httpResponse + .getContent()); + if (agentResponseModel.isSuccess()) { + System.out.println("success"); + return "success"; + } else { + System.out.println("failure"); + return "failure"; + } + } + + @RequestMapping("removeAgentfromPool.do") + public @ResponseBody + String removeAgentfromPool(HttpServletRequest request, + @ModelAttribute("accessToken") String accessToken) + throws IOException, JAXBException { + System.out.println("enter remove agent"); + String urlString = masterIP + "agentManage/removeAgentFromPool"; + HttpRequester httpRequester = new HttpRequester(); + String hostName = request.getParameter("hostName"); + Map properties = new HashMap(); + properties.put(AUTH_HEADER_PROPERTY, this.ACCES_TOCKEN_STARTER + + accessToken); + Map params = new HashMap(); + params.put("hostName", hostName); + HttpResponse httpResponse = httpRequester.sendGet(urlString, params, + properties); + AgentResponseModel agentResponseModel = extractAgentResponseModel(httpResponse + .getContent()); + if (agentResponseModel.isSuccess()) { + System.out.println("success"); + return "success"; + } else { + System.out.println("failure"); + return "failure"; + } + } + + public @ResponseBody + String loadAgentsFromPool(HttpServletRequest request, + @ModelAttribute("accessToken") String accessToken) + throws IOException, JAXBException { + System.out.println("enter load Agent"); + AgentResponseModel agentResponseModel = this + .extractAgentResponseModel(this + .getHttpRequester() + .sendPost(this.getBaseUrl() + "/queryAgentList", null, + this.makeAccessTockenMap(accessToken)) + .getContent()); + if (agentResponseModel.isSuccess()) { + System.out.println("success"); + return listToJsonString(agentResponseModel.getAgents()); + } else { + System.out.println("failer"); + return "failure"; + } + } + + private String listToJsonString(List agentList) { + String result = ""; + if (agentList == null) { + return result; + } + for (Agent agent : agentList) { + if (result.isEmpty()) { + result = result + "[" + objectToJsonString(agent); + } else { + result = result + "," + objectToJsonString(agent); + } + } + + return result + "]"; + } + + private String objectToJsonString(Agent agent) { + return "{" + "hostName:\"" + agent.getHostName() + "\",port:\"" + + agent.getPort() + "\",ID:\"" + agent.getId() + + "\",currentStatus:\"" + agent.getCurrentStatus() + + "\",maxLoad:\"" + agent.getMaxLoad() + "\",remainLoad:\"" + + agent.getRemainLoad() + "\"}"; + } + + private AgentResponseModel extractAgentResponseModel(String content) + throws JAXBException { + AgentResponseModel resultModel = new AgentResponseModel(); + Unmarshaller ummarshaller = JAXBContext.newInstance( + resultModel.getClass()).createUnmarshaller(); + resultModel = (AgentResponseModel) ummarshaller + .unmarshal(new ByteArrayInputStream(content.getBytes())); + return resultModel; + } + + private String _marshalAgentToString(Agent agent) throws JAXBException { + StringWriter stringWriter = new StringWriter(); + Marshaller marshaller = JAXBContext.newInstance(agent.getClass()) + .createMarshaller(); + marshaller.marshal(agent, stringWriter); + return stringWriter.toString(); + } +} diff --git a/src/main/java/org/bench4q/web/controller/BaseActionController.java b/src/main/java/org/bench4q/web/controller/BaseActionController.java index 56575c6b..b48c1305 100644 --- a/src/main/java/org/bench4q/web/controller/BaseActionController.java +++ b/src/main/java/org/bench4q/web/controller/BaseActionController.java @@ -1,22 +1,38 @@ -package org.bench4q.web.controller; - - -import org.springframework.http.HttpRequest; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; - - -@Controller -@RequestMapping("/") -public abstract class BaseActionController { - private HttpRequest httpRequest; - protected final String masterIP = "133.133.12.1:8080/"; - protected final String AUTH_HEADER_PROPERTY = "Authorization"; - protected final String ACCES_TOCKEN_STARTER = "Bearer "; - public HttpRequest getHttpRequest() { - return httpRequest; - } - public void setHttpRequest(HttpRequest httpRequest) { - this.httpRequest = httpRequest; - } -} +package org.bench4q.web.controller; + +import java.util.HashMap; +import java.util.Map; + +import org.bench4q.master.communication.HttpRequester; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping("/") +public abstract class BaseActionController { + private HttpRequester httpRequester; + protected final String masterIP = "133.133.12.1:8080/"; + protected final String AUTH_HEADER_PROPERTY = "Authorization"; + protected final String ACCES_TOCKEN_STARTER = "Bearer "; + + public HttpRequester getHttpRequester() { + return httpRequester; + } + + @Autowired + public void setHttpRequester(HttpRequester httpRequester) { + this.httpRequester = httpRequester; + } + + public String getMasterIP() { + return masterIP; + } + + public Map makeAccessTockenMap(String accessToken) { + Map properties = new HashMap(); + properties + .put(AUTH_HEADER_PROPERTY, ACCES_TOCKEN_STARTER + accessToken); + return properties; + } +} diff --git a/src/main/java/org/bench4q/web/controller/ScriptActionController.java b/src/main/java/org/bench4q/web/controller/ScriptActionController.java index ebb35bb8..9689a142 100644 --- a/src/main/java/org/bench4q/web/controller/ScriptActionController.java +++ b/src/main/java/org/bench4q/web/controller/ScriptActionController.java @@ -163,8 +163,8 @@ public class ScriptActionController extends BaseActionController { OperateScriptServerResponseModel operateScriptServerResponseModel = extractoperateScriptServerResponseModel(httpResponse .getContent()); List