From 020d09177fec6a9529335b14c3bd74e455f5e692 Mon Sep 17 00:00:00 2001 From: chentienan Date: Sat, 20 Jul 2013 17:12:00 +0800 Subject: [PATCH] refactoring with the port pool, and think about if we should take the recording script as a service --- .../master/api/RecordPortController.java | 13 +- .../master/api/RecordScriptController.java | 16 +- .../org/bench4q/master/entity/db/Port.java | 10 + .../master/entity/httpcapture/Config.java | 431 ++++++++---------- .../master/service/PortPoolService.java | 73 ++- 5 files changed, 273 insertions(+), 270 deletions(-) diff --git a/src/main/java/org/bench4q/master/api/RecordPortController.java b/src/main/java/org/bench4q/master/api/RecordPortController.java index 3176b8be..01f13197 100644 --- a/src/main/java/org/bench4q/master/api/RecordPortController.java +++ b/src/main/java/org/bench4q/master/api/RecordPortController.java @@ -1,7 +1,6 @@ package org.bench4q.master.api; import org.bench4q.master.api.model.OrganizeRecordPortResponseModel; -import org.bench4q.master.entity.LocalPort; import org.bench4q.master.service.PortPoolService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -45,14 +44,10 @@ public class RecordPortController extends BaseController { * .setFailCauseString("you don't hava the power to add port to pool!"); * return organizeRecordPortResponseModel; } */ - LocalPort localPort = new LocalPort(); - localPort.setPort(port); - localPort.setInUse(false); - - this.getPortPoolService().getScriptPortPool().put(port, localPort); + this.getPortPoolService().addPortToDBPool(port); if (!this.getPortPoolService().addPortToDBPool(port)) { - return setOrganizePortResponseModel(false, "add to DB pool fails 2"); + return setOrganizePortResponseModel(false, "add to DB pool fails"); } return setOrganizePortResponseModel(true, ""); } @@ -60,10 +55,6 @@ public class RecordPortController extends BaseController { @RequestMapping(value = "/removePortFromPool", method = RequestMethod.POST) public OrganizeRecordPortResponseModel removePortFromPool( @RequestParam int port) { - if (this.getPortPoolService().getScriptPortPool().remove(port) == null) { - return setOrganizePortResponseModel(false, - "fail in add to local pool 1"); - } if (!this.getPortPoolService().removePortFromDBPool(port)) { return setOrganizePortResponseModel(false, "remove from local fails"); diff --git a/src/main/java/org/bench4q/master/api/RecordScriptController.java b/src/main/java/org/bench4q/master/api/RecordScriptController.java index 800d802a..cdcfffa3 100644 --- a/src/main/java/org/bench4q/master/api/RecordScriptController.java +++ b/src/main/java/org/bench4q/master/api/RecordScriptController.java @@ -2,8 +2,8 @@ package org.bench4q.master.api; import org.bench4q.master.api.model.OperateScriptServerResponseModel; import org.bench4q.master.entity.Constant; -import org.bench4q.master.entity.LocalPort; import org.bench4q.master.entity.ScriptCapturer; +import org.bench4q.master.entity.db.Port; import org.bench4q.master.service.PortPoolService; import org.bench4q.master.service.ScriptService; import org.springframework.beans.factory.annotation.Autowired; @@ -66,12 +66,11 @@ public class RecordScriptController extends BaseController { */ synchronized (PORT_LOCK) { // TODO: - LocalPort localPort = this.getPortPoolService().getAPortNotInUse(); - if (localPort == null) { + Port port = this.getPortPoolService().getAPortNotInUse(); + if (port == null) { return returnResponseModel(false, "port is in use!"); } - this.setPortForRecord(localPort.getPort()); - localPort.setInUse(true); + this.setPortForRecord(port.getPort()); } // String pathString = this.getClass().getResource("/").getPath(); @@ -102,9 +101,10 @@ public class RecordScriptController extends BaseController { "there is no RecordingServer to stop"); } - scriptCapturer.stopCurrentRecord(); - this.getPortPoolService().getScriptPortPool() - .get(this.getPortForRecord()).setInUse(false); + this.getScriptCapturer().stopCurrentRecord(); + this.getScriptCapturer().shutHttpCaptureProxyServer(); + + this.getPortPoolService().backThePortToPool(this.getPortForRecord()); return returnResponseModel(true, "RecordServer stop"); } diff --git a/src/main/java/org/bench4q/master/entity/db/Port.java b/src/main/java/org/bench4q/master/entity/db/Port.java index ec2aff1f..47a870b4 100644 --- a/src/main/java/org/bench4q/master/entity/db/Port.java +++ b/src/main/java/org/bench4q/master/entity/db/Port.java @@ -12,6 +12,7 @@ import javax.persistence.Table; public class Port { private int id; private int port; + private boolean inUse; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -33,4 +34,13 @@ public class Port { this.port = port; } + @Column(name = "inUse", nullable = false) + public boolean isInUse() { + return inUse; + } + + public void setInUse(boolean inUse) { + this.inUse = inUse; + } + } diff --git a/src/main/java/org/bench4q/master/entity/httpcapture/Config.java b/src/main/java/org/bench4q/master/entity/httpcapture/Config.java index b9605947..8007353d 100644 --- a/src/main/java/org/bench4q/master/entity/httpcapture/Config.java +++ b/src/main/java/org/bench4q/master/entity/httpcapture/Config.java @@ -21,280 +21,249 @@ import org.python.util.PythonInterpreter; public class Config { private static Config config = null; - private static final Log log = LogFactory.getLog(Config.class); - private static Properties props; - private String pythonPath; - private int port = 8090; - private boolean debug; - private boolean quiet; - private String exist; - private String replace; - private List includeList; - private List excludeList; - private List includePatterns = new ArrayList(); - private List excludePatterns = new ArrayList(); - private String scriptArg; - private ProxySettings proxySettings; - private static String defDriverPkgName = "com.bitmechanic.maxq"; - private static String defValidatorPkgName = "com.bitmechanic.maxq"; + private static final Log log = LogFactory.getLog(Config.class); + private static Properties props; + private String pythonPath; + private int port = 8090; + private boolean debug; + private boolean quiet; + private String exist; + private String replace; + private List includeList; + private List excludeList; + private List includePatterns = new ArrayList(); + private List excludePatterns = new ArrayList(); + private String scriptArg; + private ProxySettings proxySettings; + private static String defDriverPkgName = "com.bitmechanic.maxq"; + private static String defValidatorPkgName = "com.bitmechanic.maxq"; - static - { - Layout layout = new PatternLayout("%d [%t] %-5p %c - %m%n"); - Logger.getRootLogger().addAppender(new ConsoleAppender(layout, "System.err")); - } + static { + Layout layout = new PatternLayout("%d [%t] %-5p %c - %m%n"); + Logger.getRootLogger().addAppender( + new ConsoleAppender(layout, "System.err")); + } - public static Config getConfig() - { - if (config == null) - initConfig(); - return config; - } + public static Config getConfig() { + if (config == null) + initConfig(); + return config; + } - public static void initConfig() - { - config = new Config(); - String maxqDir = System.getProperty("maxq.dir"); - String pathSep = System.getProperty("file.separator"); + public static void initConfig() { + config = new Config(); + String maxqDir = System.getProperty("maxq.dir"); + String pathSep = System.getProperty("file.separator"); - String propertiesFileName = System.getProperty("properties.file", "maxq.properties"); - InputStream propertiesStream = Config.class.getClassLoader().getResourceAsStream(propertiesFileName); - props = new Properties(); - if (propertiesStream != null) - { - try - { - props.load(propertiesStream); - log.debug("Generator: " + props.getProperty("generator.classnames")); - } - catch (IOException e) - { - throw new ExceptionInInitializerError(e); - } + String propertiesFileName = System.getProperty("properties.file", + "maxq.properties"); + InputStream propertiesStream = Config.class.getClassLoader() + .getResourceAsStream(propertiesFileName); + props = new Properties(); + if (propertiesStream != null) { + try { + props.load(propertiesStream); + log.debug("Generator: " + + props.getProperty("generator.classnames")); + } catch (IOException e) { + throw new ExceptionInInitializerError(e); + } - } + } - config.addPythonPath(maxqDir + pathSep + "jython"); + config.addPythonPath(maxqDir + pathSep + "jython"); - String path = config.getProperty("python.path"); - if (path != null) { - config.addPythonPath(path); - } + String path = config.getProperty("python.path"); + if (path != null) { + config.addPythonPath(path); + } - String portStr = props.getProperty("local.proxy.port"); - if (portStr != null) - config.port = Integer.parseInt(portStr); - } + String portStr = props.getProperty("local.proxy.port"); + if (portStr != null) + config.port = Integer.parseInt(portStr); + } - public void completeInit() - throws Utils.UserException - { - Level level = this.quiet ? Level.WARN : this.debug ? Level.ALL : Level.INFO; - Logger.getRootLogger().setLevel(level); + public void completeInit() throws Utils.UserException { + Level level = this.quiet ? Level.WARN : this.debug ? Level.ALL + : Level.INFO; + Logger.getRootLogger().setLevel(level); - System.setProperty("org.apache.commons.logging.simplelog.defaultlog", "warn"); + System.setProperty("org.apache.commons.logging.simplelog.defaultlog", + "warn"); - Properties ppref = new Properties(); - if (getPythonPath() != null) - ppref.put("python.path", getPythonPath()); - PythonInterpreter.initialize(System.getProperties(), ppref, new String[0]); + Properties ppref = new Properties(); + if (getPythonPath() != null) + ppref.put("python.path", getPythonPath()); + PythonInterpreter.initialize(System.getProperties(), ppref, + new String[0]); - String host = getProperty("remote.proxy.host"); - Integer port = getPropertyInt("remote.proxy.port"); - if ((host == null ? 1 : 0) != (port == null ? 1 : 0)) - throw new Utils.UserException("Not using proxy server. You must set both remote.proxy.host and remote.proxy.port."); - if ((host != null) && (port != null)) { - this.proxySettings = new ProxySettings(); - this.proxySettings.host = host; - this.proxySettings.port = port.intValue(); - log.info("Proxying requests via " + this.proxySettings.host + ":" + Integer.toString(this.proxySettings.port)); - } - } + String host = getProperty("remote.proxy.host"); + Integer port = getPropertyInt("remote.proxy.port"); + if ((host == null ? 1 : 0) != (port == null ? 1 : 0)) + throw new Utils.UserException( + "Not using proxy server. You must set both remote.proxy.host and remote.proxy.port."); + if ((host != null) && (port != null)) { + this.proxySettings = new ProxySettings(); + this.proxySettings.host = host; + this.proxySettings.port = port.intValue(); + log.info("Proxying requests via " + this.proxySettings.host + ":" + + Integer.toString(this.proxySettings.port)); + } + } - public String getProperty(String prop) - { - return (String)props.get(prop); - } + public String getProperty(String prop) { + return (String) props.get(prop); + } - public String getProperty(String prop, String def) - { - String val = getProperty(prop); - return (val == null) || (val.equals("")) ? def : val; - } + public String getProperty(String prop, String def) { + String val = getProperty(prop); + return (val == null) || (val.equals("")) ? def : val; + } - public void setProperty(String property, String value) - { - props.setProperty(property, value); - } + public void setProperty(String property, String value) { + props.setProperty(property, value); + } - public Integer getPropertyInt(String prop) - throws Utils.UserException - { - String s = getProperty(prop); - if (s == null) - return null; - try { - return new Integer(s); } catch (NumberFormatException e) { - } - throw new Utils.UserException(prop + " property must be an integer"); - } + public Integer getPropertyInt(String prop) throws Utils.UserException { + String s = getProperty(prop); + if (s == null) + return null; + try { + return new Integer(s); + } catch (NumberFormatException e) { + } + throw new Utils.UserException(prop + " property must be an integer"); + } - public void addPythonPath(String path) - { - if (this.pythonPath == null) - this.pythonPath = path; - else - this.pythonPath = (this.pythonPath + System.getProperty("path.separator") + path); - } + public void addPythonPath(String path) { + if (this.pythonPath == null) + this.pythonPath = path; + else + this.pythonPath = (this.pythonPath + + System.getProperty("path.separator") + path); + } - public String getPythonPath() - { - return this.pythonPath; - } + public String getPythonPath() { + return this.pythonPath; + } - public void setPort(int pt) - { - this.port = pt; - } + public void setPort(int pt) { + this.port = pt; + } - public int getPort() - { - return this.port; - } + public int getPort() { + return this.port; + } - public void setDebug(boolean d) - { - this.debug = d; - } + public void setDebug(boolean d) { + this.debug = d; + } - public boolean isDebug() - { - return this.debug; - } + public boolean isDebug() { + return this.debug; + } - public void setQuiet(boolean q) - { - this.quiet = q; - } + public void setQuiet(boolean q) { + this.quiet = q; + } - public boolean isQuiet() - { - return this.quiet; - } + public boolean isQuiet() { + return this.quiet; + } - public void setExist(String e) - { - this.exist = e; - } + public void setExist(String e) { + this.exist = e; + } - public String getExist() - { - return this.exist; - } + public String getExist() { + return this.exist; + } - public void setReplace(String e) - { - this.replace = e; - } + public void setReplace(String e) { + this.replace = e; + } - public String getReplace() - { - return this.replace; - } + public String getReplace() { + return this.replace; + } - public void setIncludePatterns(String[] pats) - { - this.includeList = Arrays.asList(pats); - createPatterns(this.includeList, this.includePatterns); - } + public void setIncludePatterns(String[] pats) { + this.includeList = Arrays.asList(pats); + createPatterns(this.includeList, this.includePatterns); + } - public void setExcludePatterns(String[] pats) - { - this.excludeList = Arrays.asList(pats); - createPatterns(this.excludeList, this.excludePatterns); - } + public void setExcludePatterns(String[] pats) { + this.excludeList = Arrays.asList(pats); + createPatterns(this.excludeList, this.excludePatterns); + } - public List getIncludePatterns() - { - return this.includePatterns; - } + public List getIncludePatterns() { + return this.includePatterns; + } - public List getExcludePatterns() - { - return this.excludePatterns; - } + public List getExcludePatterns() { + return this.excludePatterns; + } - public void setScriptArg(String arg) - { - this.scriptArg = arg; - } + public void setScriptArg(String arg) { + this.scriptArg = arg; + } - public String getScriptArg() - { - return this.scriptArg; - } + public String getScriptArg() { + return this.scriptArg; + } - public static String getDriverPkgName() - { - return System.getProperty("maxq.driverpkgname", defDriverPkgName); - } + public static String getDriverPkgName() { + return System.getProperty("maxq.driverpkgname", defDriverPkgName); + } - public static String getValidatorPkgName() - { - return System.getProperty("maxq.validatorpkgname", defValidatorPkgName); - } + public static String getValidatorPkgName() { + return System.getProperty("maxq.validatorpkgname", defValidatorPkgName); + } - public static Log getTestLogger() - { - return LogFactory.getLog("com.bitmechanic.maxq.testrun"); - } + public static Log getTestLogger() { + return LogFactory.getLog("com.bitmechanic.maxq.testrun"); + } - public ProxySettings getProxySettings() - { - return this.proxySettings; - } + public ProxySettings getProxySettings() { + return this.proxySettings; + } - public void setProxySettings(ProxySettings proxySettings) - { - this.proxySettings = proxySettings; - } + public void setProxySettings(ProxySettings proxySettings) { + this.proxySettings = proxySettings; + } - private void createPatterns(List list, List excludePatterns2) - { - Iterator iter = list.iterator(); - excludePatterns2.clear(); - while (iter.hasNext()) - try { - Pattern p = Pattern.compile(iter.next().toString()); - excludePatterns2.add(p); - } catch (PatternSyntaxException e) { - log.error(e); - } - } + private void createPatterns(List list, + List excludePatterns2) { + Iterator iter = list.iterator(); + excludePatterns2.clear(); + while (iter.hasNext()) + try { + Pattern p = Pattern.compile(iter.next().toString()); + excludePatterns2.add(p); + } catch (PatternSyntaxException e) { + log.error(e); + } + } - public static class ProxySettings - { - String host; - int port; + public static class ProxySettings { + String host; + int port; - public String getHost() - { - return this.host; - } + public String getHost() { + return this.host; + } - public void setHost(String host) - { - this.host = host; - } + public void setHost(String host) { + this.host = host; + } - public int getPort() - { - return this.port; - } + public int getPort() { + return this.port; + } - public void setPort(int port) - { - this.port = port; - } - } + public void setPort(int port) { + this.port = port; + } + } } diff --git a/src/main/java/org/bench4q/master/service/PortPoolService.java b/src/main/java/org/bench4q/master/service/PortPoolService.java index 3e7806ba..5a0219ba 100644 --- a/src/main/java/org/bench4q/master/service/PortPoolService.java +++ b/src/main/java/org/bench4q/master/service/PortPoolService.java @@ -1,9 +1,7 @@ package org.bench4q.master.service; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import org.bench4q.master.entity.LocalPort; +import java.util.List; + import org.bench4q.master.entity.db.Port; import org.bench4q.master.helper.SessionHelper; import org.hibernate.Session; @@ -15,14 +13,8 @@ import org.springframework.stereotype.Component; @Component public class PortPoolService { - private static Map ScriptPortPool = new HashMap(); - private SessionHelper sessionHelper = new SessionHelper(); - public Map getScriptPortPool() { - return PortPoolService.ScriptPortPool; - } - public SessionHelper getSessionHelper() { return sessionHelper; } @@ -99,17 +91,58 @@ public class PortPoolService { } } - public LocalPort getAPortNotInUse() { - Iterator iterator = ScriptPortPool.values().iterator(); - LocalPort localPort = new LocalPort(); - while (iterator.hasNext()) { - localPort = iterator.next(); - if (localPort.isInUse()) { - continue; - } else { - return localPort; + @SuppressWarnings("unchecked") + public Port getAPortNotInUse() { + // If i do the HA in the pool, shall i lock this change for this port + Session session = this.getSessionHelper().openSession(); + Transaction transaction = session.beginTransaction(); + Port port = null; + try { + List portList = (List) session + .createCriteria(Port.class) + .add(Restrictions.eq("inUse", false)).list(); + + if (portList == null) { + return null; + } + + port = portList.get(0); + port.setInUse(true); + transaction.commit(); + return port; + } catch (Exception e) { + e.printStackTrace(); + transaction.rollback(); + return null; + } finally { + if (session != null) { + session.close(); + } + } + } + + public boolean backThePortToPool(int port) { + Session session = this.getSessionHelper().openSession(); + Transaction transaction = session.beginTransaction(); + try { + Port portEntity = (Port) session.createCriteria(Port.class) + .add(Restrictions.eq("port", port)).uniqueResult(); + + if (portEntity == null) { + return false; + } + + portEntity.setInUse(false); + transaction.commit(); + return true; + } catch (Exception e) { + e.printStackTrace(); + transaction.rollback(); + return true; + } finally { + if (session != null) { + session.close(); } } - return null; } }