/akka-http-core/src/main/scala/akka/http/impl/engine/Http2Shadow.scala

https://github.com/akka/akka-http · Scala · 67 lines · 51 code · 12 blank · 4 comment · 0 complexity · a39545633130e95c43d7c702345ac738 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com>
  3. */
  4. package akka.http.impl.engine
  5. import akka.actor.{ ActorSystem, ExtendedActorSystem }
  6. import akka.annotation.InternalApi
  7. import akka.event.LoggingAdapter
  8. import akka.http.scaladsl.Http.ServerBinding
  9. import akka.http.scaladsl.ConnectionContext
  10. import akka.http.scaladsl.model.{ HttpRequest, HttpResponse }
  11. import akka.http.scaladsl.settings.ServerSettings
  12. import akka.stream.{ ActorMaterializerHelper, Materializer }
  13. import scala.concurrent.Future
  14. /** INTERNAL API: Uses reflection to reach for Http2 support if available or fails with an exception */
  15. @InternalApi
  16. private[akka] object Http2Shadow {
  17. type ShadowHttp2 = AnyRef
  18. type ShadowHttp2Ext = {
  19. def bindAndHandleAsync(
  20. handler: HttpRequest => Future[HttpResponse],
  21. interface: String, port: Int,
  22. httpContext: ConnectionContext,
  23. settings: ServerSettings,
  24. parallelism: Int,
  25. log: LoggingAdapter)(implicit fm: Materializer): Future[ServerBinding]
  26. }
  27. def bindAndHandleAsync(
  28. handler: HttpRequest => Future[HttpResponse],
  29. interface: String, port: Int,
  30. httpContext: ConnectionContext,
  31. settings: ServerSettings,
  32. parallelism: Int,
  33. log: LoggingAdapter)(implicit fm: Materializer): Future[ServerBinding] = {
  34. val mat = ActorMaterializerHelper.downcast(fm)
  35. try {
  36. val system = mat.system.asInstanceOf[ExtendedActorSystem]
  37. val extensionIdClazz = system.dynamicAccess.getClassFor[ShadowHttp2]("akka.http.scaladsl.Http2").get
  38. val extensionInstance: ShadowHttp2Ext = extensionIdClazz.getMethod("get", Array(classOf[ActorSystem]): _*)
  39. .invoke(null, system).asInstanceOf[ShadowHttp2Ext]
  40. import scala.language.reflectiveCalls
  41. extensionInstance.bindAndHandleAsync(
  42. handler,
  43. interface, port,
  44. httpContext,
  45. settings,
  46. parallelism,
  47. log)(fm)
  48. } catch {
  49. case ex: Throwable => throw Http2SupportNotPresentException(ex)
  50. }
  51. }
  52. final case class Http2SupportNotPresentException(cause: Throwable)
  53. extends RuntimeException("Unable to invoke HTTP2 binding logic (as enabled setting `akka.http.server.enable-http2`). " +
  54. """Please make sure that `"com.typesafe.akka" %% "akka-http2-support" % AkkaHttpVersion` is on the classpath.""", cause)
  55. }