Refactor packages

This commit is contained in:
Dominik Adam Przybysz 2014-12-09 23:09:01 +01:00
parent dfc0332d5a
commit fc4bda11de
6 changed files with 8 additions and 10 deletions

View file

@ -1,45 +0,0 @@
package com.blogspot.przybyszd.mockserver
import com.sun.net.httpserver.HttpHandler
import com.sun.net.httpserver.HttpServer
import java.util.concurrent.Executors
class HttpServerWraper {
private final HttpServer httpServer
private final int port
private List<ContextExecutor> executors = []
HttpServerWraper(int port) {
this.port = port
InetSocketAddress addr = new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), port)
httpServer = HttpServer.create(addr, 0)
httpServer.executor = Executors.newCachedThreadPool()
println "Http server statrting on port $port..."
httpServer.start()
println 'Http server is started'
}
void createContext(String context, HttpHandler handler) {
httpServer.createContext(context, handler)
}
void addMock(String path, Mock mock) {
ContextExecutor executor = executors.find { it.path == path }
if (executor) {
executor.addMock(mock)
} else {
executors << new ContextExecutor(this, path, mock)
}
}
void stop() {
executors.each { httpServer.removeContext(it.path) }
httpServer.stop(0)
}
int removeMock(String name) {
executors.inject(0) { int res, ContextExecutor e -> e.removeMock(name) + res}
}
}