mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-27 08:38:13 -07:00
Jersey work.
This commit is contained in:
parent
85db1c767e
commit
8b9f2d9655
7 changed files with 196 additions and 67 deletions
|
@ -9,24 +9,83 @@ import com.beust.kobalt.internal.KobaltSettings
|
|||
import com.beust.kobalt.misc.KFiles
|
||||
import com.beust.kobalt.misc.log
|
||||
import com.beust.kobalt.misc.warn
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.JsonParser
|
||||
import com.google.inject.Guice
|
||||
import okhttp3.OkHttpClient
|
||||
import retrofit2.Call
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import retrofit2.http.POST
|
||||
import retrofit2.http.Query
|
||||
import java.io.*
|
||||
import java.net.ConnectException
|
||||
import java.net.Socket
|
||||
import java.nio.file.Paths
|
||||
import java.util.*
|
||||
import java.util.concurrent.Executors
|
||||
import javax.inject.Inject
|
||||
|
||||
fun main(argv: Array<String>) {
|
||||
Kobalt.INJECTOR = Guice.createInjector(MainModule(Args(), KobaltSettings.readSettingsXml()))
|
||||
val port = ServerProcess().launch()
|
||||
println("SERVER RUNNING ON PORT $port")
|
||||
KobaltClient().run()
|
||||
}
|
||||
|
||||
interface Api {
|
||||
@POST("/getDependencies")
|
||||
fun getDependencies(@Query("buildFile") buildFile: String) : Call<List<DependencyData.GetDependenciesData>>
|
||||
}
|
||||
|
||||
class KobaltClient : Runnable {
|
||||
var outgoing: PrintWriter? = null
|
||||
|
||||
private val service = Retrofit.Builder()
|
||||
.client(OkHttpClient.Builder().build())
|
||||
.baseUrl("http://localhost:1252")
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
.create(Api::class.java)
|
||||
|
||||
override fun run() {
|
||||
val buildFile = Paths.get(SystemProperties.homeDir, "kotlin/klaxon/kobalt/src/Build.kt").toString()
|
||||
val dependencies = service.getDependencies(buildFile)
|
||||
val results = dependencies.execute()
|
||||
println("Dependencies: $results")
|
||||
// .toString())
|
||||
// var done = false
|
||||
// var attempts = 1
|
||||
// while (attempts < 10 && ! done) {
|
||||
// try {
|
||||
// val socket = Socket("localhost", portNumber)
|
||||
// outgoing = PrintWriter(socket.outputStream, true)
|
||||
// val testBuildfile = Paths.get(SystemProperties.homeDir, "kotlin/klaxon/kobalt/src/Build.kt")
|
||||
// .toFile().absolutePath
|
||||
// val c : String = """{ "name": "getDependencies", "buildFile": "$testBuildfile"}"""
|
||||
// outgoing!!.println(c)
|
||||
// val ins = BufferedReader(InputStreamReader(socket.inputStream))
|
||||
// var line = ins.readLine()
|
||||
// while (! done && line != null) {
|
||||
// log(1, "Received from server:\n" + line)
|
||||
// val jo = JsonParser().parse(line) as JsonObject
|
||||
// if (jo.has("name") && "quit" == jo.get("name").asString.toLowerCase()) {
|
||||
// log(1, "Quitting")
|
||||
//// outgoing!!.println("{ \"name\": \"Quit\" }")
|
||||
// done = true
|
||||
// } else {
|
||||
// val data = jo.get("data").asString
|
||||
// val dd = Gson().fromJson(data, DependencyData.GetDependenciesData::class.java)
|
||||
// println("Read GetDependencyData, project count: ${dd.projects.size}")
|
||||
// line = ins.readLine()
|
||||
// }
|
||||
// }
|
||||
// } catch(ex: ConnectException) {
|
||||
// log(1, "Server not up, sleeping a bit")
|
||||
// Thread.sleep(2000)
|
||||
// attempts++
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class ServerProcess {
|
||||
val SERVER_FILE = KFiles.joinDir(homeDir(KFiles.KOBALT_DOT_DIR, "kobaltServer.properties"))
|
||||
val KEY_PORT = "port"
|
||||
|
@ -123,43 +182,3 @@ class ServerProcess {
|
|||
}
|
||||
}
|
||||
|
||||
class KobaltClient @Inject constructor() : Runnable {
|
||||
var outgoing: PrintWriter? = null
|
||||
|
||||
override fun run() {
|
||||
val portNumber = 1234
|
||||
|
||||
var done = false
|
||||
var attempts = 1
|
||||
while (attempts < 10 && ! done) {
|
||||
try {
|
||||
val socket = Socket("localhost", portNumber)
|
||||
outgoing = PrintWriter(socket.outputStream, true)
|
||||
val testBuildfile = Paths.get(SystemProperties.homeDir, "kotlin/klaxon/kobalt/src/Build.kt")
|
||||
.toFile().absolutePath
|
||||
val c : String = """{ "name": "getDependencies", "buildFile": "$testBuildfile"}"""
|
||||
outgoing!!.println(c)
|
||||
val ins = BufferedReader(InputStreamReader(socket.inputStream))
|
||||
var line = ins.readLine()
|
||||
while (! done && line != null) {
|
||||
log(1, "Received from server:\n" + line)
|
||||
val jo = JsonParser().parse(line) as JsonObject
|
||||
if (jo.has("name") && "quit" == jo.get("name").asString.toLowerCase()) {
|
||||
log(1, "Quitting")
|
||||
// outgoing!!.println("{ \"name\": \"Quit\" }")
|
||||
done = true
|
||||
} else {
|
||||
val data = jo.get("data").asString
|
||||
val dd = Gson().fromJson(data, DependencyData.GetDependenciesData::class.java)
|
||||
println("Read GetDependencyData, project count: ${dd.projects.size}")
|
||||
line = ins.readLine()
|
||||
}
|
||||
}
|
||||
} catch(ex: ConnectException) {
|
||||
log(1, "Server not up, sleeping a bit")
|
||||
Thread.sleep(2000)
|
||||
attempts++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.beust.kobalt.app.remote
|
||||
|
||||
import com.beust.kobalt.Args
|
||||
import com.beust.kobalt.api.Kobalt
|
||||
import com.beust.kobalt.api.Project
|
||||
import com.beust.kobalt.homeDir
|
||||
|
@ -11,12 +12,21 @@ import com.beust.kobalt.misc.log
|
|||
import com.google.gson.Gson
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.JsonParser
|
||||
import org.glassfish.jersey.jetty.JettyHttpContainerFactory
|
||||
import org.glassfish.jersey.server.ResourceConfig
|
||||
import org.glassfish.jersey.server.ServerProperties
|
||||
import java.io.*
|
||||
import java.lang.management.ManagementFactory
|
||||
import java.net.ServerSocket
|
||||
import java.net.SocketException
|
||||
import java.util.*
|
||||
import java.util.concurrent.Callable
|
||||
import javax.ws.rs.GET
|
||||
import javax.ws.rs.Path
|
||||
import javax.ws.rs.Produces
|
||||
import javax.ws.rs.QueryParam
|
||||
import javax.ws.rs.core.MediaType
|
||||
import javax.ws.rs.core.UriBuilder
|
||||
|
||||
/**
|
||||
* Launch a Kobalt server. If @param{force} is specified, a new server will be launched even if one was detected
|
||||
|
@ -31,6 +41,16 @@ class KobaltServer(val force: Boolean, val port: Int = 1234,
|
|||
// var outgoing: PrintWriter? = null
|
||||
val pending = arrayListOf<CommandData>()
|
||||
|
||||
companion object {
|
||||
lateinit var initCallback: (String) -> List<Project>
|
||||
lateinit var cleanUpCallback: () -> Unit
|
||||
}
|
||||
|
||||
init {
|
||||
KobaltServer.initCallback = initCallback
|
||||
KobaltServer.cleanUpCallback = cleanUpCallback
|
||||
}
|
||||
|
||||
private val COMMAND_CLASSES = listOf(GetDependenciesCommand::class.java, PingCommand::class.java)
|
||||
private val COMMANDS = COMMAND_CLASSES.map {
|
||||
Kobalt.INJECTOR.getInstance(it).let { Pair(it.name, it) }
|
||||
|
@ -39,8 +59,9 @@ class KobaltServer(val force: Boolean, val port: Int = 1234,
|
|||
override fun call() : Int {
|
||||
val availablePort = ProcessUtil.findAvailablePort(port)
|
||||
try {
|
||||
if (createServerFile(availablePort, force)) {
|
||||
privateRun(availablePort)
|
||||
if (createServerFile(port, force)) {
|
||||
// oldRun(port)
|
||||
privateRun(port)
|
||||
}
|
||||
} catch(ex: Exception) {
|
||||
ex.printStackTrace()
|
||||
|
@ -97,8 +118,50 @@ class KobaltServer(val force: Boolean, val port: Int = 1234,
|
|||
}
|
||||
}
|
||||
|
||||
@Path("/v0")
|
||||
class MyResource : ResourceConfig() {
|
||||
init {
|
||||
property(ServerProperties.TRACING, "ALL")
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("getDependencies")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
fun getDependencies(@QueryParam("buildFile") buildFile: String) : String {
|
||||
try {
|
||||
val dependencyData = Kobalt.INJECTOR.getInstance(DependencyData::class.java)
|
||||
val args = Kobalt.INJECTOR.getInstance(Args::class.java)
|
||||
|
||||
val projects = initCallback(buildFile)
|
||||
val dd = dependencyData.dependenciesDataFor(buildFile, args)
|
||||
val data = CommandData("getDependencies", Gson().toJson(dd), dd.errorMessage)
|
||||
|
||||
return Gson().toJson(data)
|
||||
} catch(ex: Exception) {
|
||||
return "Error: " + ex.message
|
||||
} finally {
|
||||
cleanUpCallback()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun privateRun(port: Int) {
|
||||
log(1, "Listening to port $port")
|
||||
|
||||
val baseUri = UriBuilder.fromUri("http://localhost/").port(port).build()
|
||||
val config = ResourceConfig(MyResource::class.java)
|
||||
with (JettyHttpContainerFactory.createServer(baseUri, config)) {
|
||||
try {
|
||||
start()
|
||||
join()
|
||||
} finally {
|
||||
destroy()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun oldRun(port: Int) {
|
||||
log(1, "Listening to port $port")
|
||||
var quit = false
|
||||
serverInfo = ServerInfo(port)
|
||||
while (!quit) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue