From f5f0455f3ca8cf5be166e655a278fd42fadb4548 Mon Sep 17 00:00:00 2001 From: Zhen Tang Date: Sat, 29 Jun 2013 20:50:40 +0800 Subject: [PATCH] http plugin updated. log plugin added. --- .../{timer => basic}/ConstantTimerPlugin.java | 2 +- .../agent/plugin/basic/HttpPlugin.java | 132 ++++++++++++++++++ .../bench4q/agent/plugin/basic/LogPlugin.java | 17 +++ .../bench4q/agent/plugin/http/HttpPlugin.java | 72 ---------- 4 files changed, 150 insertions(+), 73 deletions(-) rename src/main/java/org/bench4q/agent/plugin/{timer => basic}/ConstantTimerPlugin.java (85%) create mode 100644 src/main/java/org/bench4q/agent/plugin/basic/HttpPlugin.java create mode 100644 src/main/java/org/bench4q/agent/plugin/basic/LogPlugin.java delete mode 100644 src/main/java/org/bench4q/agent/plugin/http/HttpPlugin.java diff --git a/src/main/java/org/bench4q/agent/plugin/timer/ConstantTimerPlugin.java b/src/main/java/org/bench4q/agent/plugin/basic/ConstantTimerPlugin.java similarity index 85% rename from src/main/java/org/bench4q/agent/plugin/timer/ConstantTimerPlugin.java rename to src/main/java/org/bench4q/agent/plugin/basic/ConstantTimerPlugin.java index f0cd8dfe..e33bb276 100644 --- a/src/main/java/org/bench4q/agent/plugin/timer/ConstantTimerPlugin.java +++ b/src/main/java/org/bench4q/agent/plugin/basic/ConstantTimerPlugin.java @@ -1,4 +1,4 @@ -package org.bench4q.agent.plugin.timer; +package org.bench4q.agent.plugin.basic; import org.bench4q.agent.plugin.Behavior; import org.bench4q.agent.plugin.Plugin; diff --git a/src/main/java/org/bench4q/agent/plugin/basic/HttpPlugin.java b/src/main/java/org/bench4q/agent/plugin/basic/HttpPlugin.java new file mode 100644 index 00000000..35d47635 --- /dev/null +++ b/src/main/java/org/bench4q/agent/plugin/basic/HttpPlugin.java @@ -0,0 +1,132 @@ +package org.bench4q.agent.plugin.basic; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import org.bench4q.agent.plugin.Behavior; +import org.bench4q.agent.plugin.Plugin; + +@Plugin("Http") +public class HttpPlugin { + + public HttpPlugin() { + + } + + @Behavior("Get") + public boolean get(String url) { + try { + URL target = new URL(url); + HttpURLConnection httpURLConnection = (HttpURLConnection) target + .openConnection(); + httpURLConnection.setDoOutput(false); + httpURLConnection.setDoInput(true); + httpURLConnection.setUseCaches(false); + httpURLConnection.setRequestMethod("GET"); + BufferedReader bufferedReader = new BufferedReader( + new InputStreamReader(httpURLConnection.getInputStream())); + int temp = -1; + StringBuffer stringBuffer = new StringBuffer(); + while ((temp = bufferedReader.read()) != -1) { + stringBuffer.append((char) temp); + } + bufferedReader.close(); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + @Behavior("Post") + public boolean post(String url, String content) { + try { + URL target = new URL(url); + HttpURLConnection httpURLConnection = (HttpURLConnection) target + .openConnection(); + httpURLConnection.setDoOutput(true); + httpURLConnection.setDoInput(true); + httpURLConnection.setUseCaches(false); + httpURLConnection.setRequestMethod("POST"); + OutputStreamWriter outputStreamWriter = new OutputStreamWriter( + httpURLConnection.getOutputStream()); + outputStreamWriter.write(content); + outputStreamWriter.flush(); + outputStreamWriter.close(); + BufferedReader bufferedReader = new BufferedReader( + new InputStreamReader(httpURLConnection.getInputStream())); + int temp = -1; + StringBuffer stringBuffer = new StringBuffer(); + while ((temp = bufferedReader.read()) != -1) { + stringBuffer.append((char) temp); + } + bufferedReader.close(); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + @Behavior("Put") + public boolean put(String url, String content) { + try { + URL target = new URL(url); + HttpURLConnection httpURLConnection = (HttpURLConnection) target + .openConnection(); + httpURLConnection.setDoOutput(true); + httpURLConnection.setDoInput(true); + httpURLConnection.setUseCaches(false); + httpURLConnection.setRequestMethod("PUT"); + OutputStreamWriter outputStreamWriter = new OutputStreamWriter( + httpURLConnection.getOutputStream()); + outputStreamWriter.write(content); + outputStreamWriter.flush(); + outputStreamWriter.close(); + BufferedReader bufferedReader = new BufferedReader( + new InputStreamReader(httpURLConnection.getInputStream())); + int temp = -1; + StringBuffer stringBuffer = new StringBuffer(); + while ((temp = bufferedReader.read()) != -1) { + stringBuffer.append((char) temp); + } + bufferedReader.close(); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + @Behavior("Delete") + public boolean delete(String url, String content) { + try { + URL target = new URL(url); + HttpURLConnection httpURLConnection = (HttpURLConnection) target + .openConnection(); + httpURLConnection.setDoOutput(true); + httpURLConnection.setDoInput(true); + httpURLConnection.setUseCaches(false); + httpURLConnection.setRequestMethod("DELETE"); + OutputStreamWriter outputStreamWriter = new OutputStreamWriter( + httpURLConnection.getOutputStream()); + outputStreamWriter.write(content); + outputStreamWriter.flush(); + outputStreamWriter.close(); + BufferedReader bufferedReader = new BufferedReader( + new InputStreamReader(httpURLConnection.getInputStream())); + int temp = -1; + StringBuffer stringBuffer = new StringBuffer(); + while ((temp = bufferedReader.read()) != -1) { + stringBuffer.append((char) temp); + } + bufferedReader.close(); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } +} diff --git a/src/main/java/org/bench4q/agent/plugin/basic/LogPlugin.java b/src/main/java/org/bench4q/agent/plugin/basic/LogPlugin.java new file mode 100644 index 00000000..58d7007d --- /dev/null +++ b/src/main/java/org/bench4q/agent/plugin/basic/LogPlugin.java @@ -0,0 +1,17 @@ +package org.bench4q.agent.plugin.basic; + +import org.bench4q.agent.plugin.Behavior; +import org.bench4q.agent.plugin.Plugin; + +@Plugin("Log") +public class LogPlugin { + public LogPlugin() { + + } + + @Behavior("Log") + public boolean log(String message) { + System.out.println(message); + return true; + } +} diff --git a/src/main/java/org/bench4q/agent/plugin/http/HttpPlugin.java b/src/main/java/org/bench4q/agent/plugin/http/HttpPlugin.java deleted file mode 100644 index f50c861d..00000000 --- a/src/main/java/org/bench4q/agent/plugin/http/HttpPlugin.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.bench4q.agent.plugin.http; - -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.net.HttpURLConnection; -import java.net.URL; -import org.bench4q.agent.plugin.Behavior; -import org.bench4q.agent.plugin.Plugin; - -@Plugin("Http") -public class HttpPlugin { - - public HttpPlugin() { - - } - - @Behavior("Get") - public boolean get(String url) { - try { - URL target = new URL(url); - HttpURLConnection httpURLConnection = (HttpURLConnection) target - .openConnection(); - httpURLConnection.setDoOutput(false); - httpURLConnection.setDoInput(true); - httpURLConnection.setUseCaches(false); - httpURLConnection.setRequestMethod("GET"); - BufferedReader bufferedReader = new BufferedReader( - new InputStreamReader(httpURLConnection.getInputStream())); - int temp = -1; - StringBuffer stringBuffer = new StringBuffer(); - while ((temp = bufferedReader.read()) != -1) { - stringBuffer.append((char) temp); - } - bufferedReader.close(); - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - @Behavior("Post") - public boolean post(String url, String content) { - try { - URL target = new URL(url); - HttpURLConnection httpURLConnection = (HttpURLConnection) target - .openConnection(); - httpURLConnection.setDoOutput(true); - httpURLConnection.setDoInput(true); - httpURLConnection.setUseCaches(false); - httpURLConnection.setRequestMethod("POST"); - OutputStreamWriter outputStreamWriter = new OutputStreamWriter( - httpURLConnection.getOutputStream()); - outputStreamWriter.write(content); - outputStreamWriter.flush(); - outputStreamWriter.close(); - BufferedReader bufferedReader = new BufferedReader( - new InputStreamReader(httpURLConnection.getInputStream())); - int temp = -1; - StringBuffer stringBuffer = new StringBuffer(); - while ((temp = bufferedReader.read()) != -1) { - stringBuffer.append((char) temp); - } - bufferedReader.close(); - return true; - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } -}