Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
public final class Config
- Object
- Config
Where a server’s settings come from, in one resolution order.
The shape is the one a Spring Boot developer already knows, because the problem is the same one: the SAME code has to run against a SQLite file on a laptop and a managed PostgreSQL in production, and the difference between those two cannot live in the source. It lives here.
# application.properties, committed
cn1.datasource.url=${DATABASE_URL}
cn1.server.port=8080
# application-dev.properties, also committed
cn1.datasource.url=:memory:
CN1_PROFILE=dev ./server # SQLite, nothing installed
DATABASE_URL=postgres://user:pw@db/app ./server # production
A key is looked for in this order, and the first layer that has it wins:
a system property of exactly that name (
-Dcn1.server.port=9000), which only the local JVM loop can set;an environment variable of the name in upper case with dots as underscores (
CN1_SERVER_PORT), which is how a container sets one;the environment variable a platform already sets for it, where one exists:
PORTandDATABASE_URLare set for you by every PaaS worth the name, and a server that ignored them would need a wrapper script to start at all;application-<profile>.properties;application.properties;the module’s
application.propertiesas the build compiled it into the server, so a binary with no file beside it still has the committed settings, and a file or the environment can still change any of them;the default the caller passed in.
A test started by @BackendTest adds one layer above all of these: the
settings the test itself names. A test that asks for a free port or an
in-memory database gets one even in a shell that exports PORT or
DATABASE_URL.
A value may reference an environment variable as ${NAME} or
${NAME:fallback}. That resolution happens when the value is READ
rather than when the file is loaded, which is what lets a committed
application.properties name a variable that only production sets: the dev
profile overrides the key, so the unset variable is never looked at. A
reference that IS read and cannot be resolved is an error rather than a value
with a dollar sign in it – the alternative is a server that tries to open a
SQLite file named “${DATABASE_URL}”.
Nothing here is required. A binary with no properties file beside it reads its whole configuration from the environment, which is the normal shape for a container built FROM SCRATCH: there is no file next to the binary because there is nothing next to the binary.
Fields
Methods
Inherited methods
Field details
PROFILE
public static final String PROFILE = "cn1.profile"LOCATION
public static final String LOCATION = "cn1.config.location"SERVER_PORT
public static final String SERVER_PORT = "cn1.server.port"SERVER_ADDRESS
public static final String SERVER_ADDRESS = "cn1.server.address"127.0.0.1 for this machine only. Unset, the
server listens on every interface.SERVER_BACKLOG
public static final String SERVER_BACKLOG = "cn1.server.backlog"SERVER_WORKERS
public static final String SERVER_WORKERS = "cn1.server.workers"SERVER_SHUTDOWN_MILLIS
public static final String SERVER_SHUTDOWN_MILLIS = "cn1.server.shutdownTimeoutMillis"TLS_CERTIFICATE
public static final String TLS_CERTIFICATE = "cn1.server.tls.certificate"TLS_KEY
public static final String TLS_KEY = "cn1.server.tls.key"TLS_CERTIFICATE.TLS_HTTP2
public static final String TLS_HTTP2 = "cn1.server.tls.http2"STATIC_ROOT
public static final String STATIC_ROOT = "cn1.static.root"STATIC_PREFIX
public static final String STATIC_PREFIX = "cn1.static.prefix"STATIC_INDEX
public static final String STATIC_INDEX = "cn1.static.index"STATIC_CACHE_CONTROL
public static final String STATIC_CACHE_CONTROL = "cn1.static.cacheControl"WEBAPP_ROOT
public static final String WEBAPP_ROOT = "cn1.webapp.root"WEBAPP_PATH after every route and every static file. Defaults to
webapp in the working directory, and is served only when it holds an
index.html; a directory named here that does not is a start-up error.WEBAPP_PATH
public static final String WEBAPP_PATH = "cn1.webapp.path"WEBAPP_CACHE_CONTROL
public static final String WEBAPP_CACHE_CONTROL = "cn1.webapp.cacheControl"WEBAPP_ENABLED
public static final String WEBAPP_ENABLED = "cn1.webapp.enabled"DATASOURCE_URL
public static final String DATASOURCE_URL = "cn1.datasource.url"DATASOURCE_POOL_SIZE
public static final String DATASOURCE_POOL_SIZE = "cn1.datasource.pool.size"DATASOURCE_BORROW_MILLIS
public static final String DATASOURCE_BORROW_MILLIS = "cn1.datasource.pool.borrowTimeoutMillis"DATASOURCE_BUSY_MILLIS
public static final String DATASOURCE_BUSY_MILLIS = "cn1.datasource.busyTimeoutMillis"ORM_CREATE_TABLES
public static final String ORM_CREATE_TABLES = "cn1.orm.createTables"FLYWAY_ENABLED
public static final String FLYWAY_ENABLED = "cn1.flyway.enabled"Migrations.FLYWAY_TABLE
public static final String FLYWAY_TABLE = "cn1.flyway.table"flyway_schema_history unless set.FLYWAY_BASELINE_ON_MIGRATE
public static final String FLYWAY_BASELINE_ON_MIGRATE = "cn1.flyway.baselineOnMigrate"FLYWAY_BASELINE_VERSION
public static final String FLYWAY_BASELINE_VERSION = "cn1.flyway.baselineVersion"1 unless set.FLYWAY_BASELINE_DESCRIPTION
public static final String FLYWAY_BASELINE_DESCRIPTION = "cn1.flyway.baselineDescription"FLYWAY_VALIDATE_ON_MIGRATE
public static final String FLYWAY_VALIDATE_ON_MIGRATE = "cn1.flyway.validateOnMigrate"FLYWAY_OUT_OF_ORDER
public static final String FLYWAY_OUT_OF_ORDER = "cn1.flyway.outOfOrder"FLYWAY_CLEAN_DISABLED
public static final String FLYWAY_CLEAN_DISABLED = "cn1.flyway.cleanDisabled"Migrator.clean()
is refused. On unless set.FLYWAY_TARGET
public static final String FLYWAY_TARGET = "cn1.flyway.target"latest, the default, means all of them.FLYWAY_IGNORE_FUTURE_MIGRATIONS
public static final String FLYWAY_IGNORE_FUTURE_MIGRATIONS = "cn1.flyway.ignoreFutureMigrations"FLYWAY_INSTALLED_BY
public static final String FLYWAY_INSTALLED_BY = "cn1.flyway.installedBy"FLYWAY_LOCK_RETRY_COUNT
public static final String FLYWAY_LOCK_RETRY_COUNT = "cn1.flyway.lockRetryCount"Method details
load
public static Config load()
throws IOExceptionLOCATION or the working directory.Throws
load
public static Config load(String directory)
throws IOExceptiondirectory.Throws
of
public static Config of(Properties values, String profile)getProfile
public String getProfile()isDevelopmentProfile
public boolean isDevelopmentProfile()Whether the active profile is a development one – dev, development, test or local.
This decides two defaults and nothing else: an unconfigured database becomes an in-memory SQLite one rather than a refusal, and the ORM creates its tables. Both are wrong in production and right on a laptop, and both are overridable by naming the key.
get
public String get(String key)
throws IOExceptionkey, or null when no layer has one.Throws
getRoutePath
public String getRoutePath(String key, String fallback)
throws IOException/%6dcp was announced
and never matched a request, which reaches handlers as /mcp.Throws
getHeaderSecret
public String getHeaderSecret(String key)
throws IOExceptionA secret that clients present in a request header – a bearer token – or null when no layer has one.
Refused when no request could carry it exactly: the request parser rejects a control character in a header value and trims surrounding spaces and tabs, so a token read with a secret file’s trailing newline would answer every request 401 while the server looked healthy. The value stays out of the message; it is a secret.
Throws
IOException- when the value holds a control character, or leading or trailing whitespace
get
public String get(String key, String fallback)
throws IOExceptionkey, or fallback when no layer has one.Throws
getInt
public int getInt(String key, int fallback)
throws IOExceptionkey as a number, or fallback.Throws
getBoolean
public boolean getBoolean(String key, boolean fallback)
throws IOExceptionThe value for key as a flag, or fallback.
“true”, “yes”, “on” and “1” are true; “false”, “no”, “off” and “0” are false; anything else is an error rather than false. A setting the operator spelled “ture” is a setting they believe is on.
Throws
describe
public String describe()keys
public List keys()