Class Backend

java.lang.Object
com.codename1.backend.Backend

public final class Backend extends Object

A configured, running server: the twenty lines every main used to open with, written once.

Backend.builder()
       .handler(new ApiRouter(new Api()))
       .run();

That reads the configuration, opens the database the deployment named, registers the generated daos, binds the port, installs a shutdown handler that drains what is in flight, and waits. Each of those was previously the developer's to write and to get wrong -- a main that returns ends the process without a word, because the host threads are detached, and a server with no signal handler loses every connection it was serving when the orchestrator stops it.

What comes from where

Anything the builder is TOLD is used as given. Anything it is not told, it reads from Config: the port, the worker count, TLS, static files and the database, in that layered order of system property, environment variable, profile file, base file. The rule is worth stating once because the opposite rule is also defensible: a value in the source wins over a value in the environment, so a port written here is the port, and a port that should follow the deployment is one nobody writes here.

The database is opened when there is one to open: a URL is configured, a pool was handed in, or the build generated at least one entity and the ORM therefore needs one. A server with no database opens none, which is what makes this the same entry point for both kinds.

# a laptop
CN1_PROFILE=dev ./server
# production
DATABASE_URL=postgres://app:secret@db.internal/app PORT=8080 ./server
  • Method Details

    • getTracer

      public Tracer getTracer()
      The tracer this server installed, or null: its scheduled jobs are traced by it.
    • prepareRuntime

      public static void prepareRuntime()
      Prepares the runtime a program that never builds a server still needs: in a native executable, getResourceAsStream, MessageDigest and SecureRandom answer through a host this class installs as it initializes. A command-line tool calls this before anything else; the generated entry points do. Idempotent, and a no-op on the JVM.
    • builder

      public static Backend.Builder builder()
      A builder whose defaults come from the configuration this process sees.
    • builder

      public static Backend.Builder builder(Config config)
      A builder over a configuration the caller already loaded or built.
    • getListenAddress

      public String getListenAddress()
      The address a client on this machine reaches the listener at: the one it is bound to (bracketed when IPv6), or 127.0.0.1 when it listens on every interface. A listener bound to one address answers on no other.
    • getServer

      public HttpServer getServer()
      The running server, for its metrics or to stop it.
    • getDataSource

      public DataSource getDataSource()
      The connection pool, or null when this server has no database.
    • getEntityManager

      public EntityManager getEntityManager()
      The entity manager, or null when this build generated no entities.
    • getArguments

      public ApplicationArguments getArguments()
      The arguments the program was started with, as the builder was given them; empty when it was given none.
    • getConfig

      public Config getConfig()
      The configuration this server resolved its settings from.
    • isReady

      public boolean isReady()
      Whether the server has finished starting and serves its application.
    • running

      public static Backend running()
      The backend this process is running, or null when none has started or the last one stopped. One process runs one backend.
      Returns:
      the running backend, or null
    • dispatch

      public HttpServer.Response dispatch(HttpServer.Request request, Authentication authentication) throws Exception

      Answers request in process as authentication, a principal the caller has already established -- an MCP tool running an API endpoint as the MCP client's user, say (issue 6310).

      The security chain that claims the request runs its authorization rules for that principal, and the routers answer it with that principal in the SecurityContextHolder, so method security sees it too. Nothing authenticates: the request's own Authorization header and cookies are not read, no session is loaded, created or saved, and CSRF is not checked. A refusal is a plain 401 (an anonymous or unauthenticated principal) or 403, never a login redirect. The server's own routes -- MCP, management -- are not reachable this way.

      It may be called from inside a request: the calling thread's security context, current request and tracing are what they were afterwards.

      HttpServer.Response r = Backend.running().dispatch(
              HttpServer.Request.of("GET", "/api/v2/apps/7", headers, null), auth);
      String route = r.getMatchedRoute(); // "/api/v2/apps/{id}"
      
      Parameters:
      request - the request to answer
      authentication - who it is answered as
      Returns:
      the response; HttpServer.Response.getMatchedRoute() names the route template that answered, or is null when none matched
      Throws:
      Exception - what a handler threw and nothing handled -- with an error route declared, the application's error page answers such a failure instead, as it does over the wire
    • awaitTermination

      public void awaitTermination()
      Blocks until the server stops.
    • stop

      public void stop()

      Stops accepting, lets what is in flight finish, and closes the database.

      The order matters and is the reason this exists rather than two calls: closing the pool first would fail the requests that were still being served with it.

    • isStopped

      public boolean isStopped()
      Whether stop() has finished: the beans are destroyed and the pool closed.
    • requireDataSource

      public static DataSource requireDataSource(DataSource dataSource, String handler) throws IOException

      The pool, or a refusal naming the handler that asked for one.

      Called from generated wiring: a controller declaring a DataSource constructor is declaring a DEPENDENCY, and handing it null because nothing configured a database turns that into a server that starts, reports healthy and fails on the first request that touches the database. The deployment is missing a setting, and start-up is where that is cheap to see.

      Throws:
      IOException
    • requireEntities

      public static EntityManager requireEntities(EntityManager entities, String handler) throws IOException
      The entity manager, or a refusal naming the handler that asked for one.
      Throws:
      IOException
    • currentRequest

      public static HttpServer.Request currentRequest()
      The request the calling thread is serving, or null outside one. Maintained only for applications whose build asked for it: one with a request- or session-scoped bean reached from a singleton.