The Context is a container for common application dependencies such as the parsed command line args, selected environment, config properties, logs, encryptor, app, build, host info and more. This is created and must be available for any runnable application such as a Console App, CLI, or Server. It is simply an interface with the following components, and there are default implementations, and builders available for convenience.
/**
- Represents context of a running application and contains information used for most components
- args : command line arguments
- envs : environment selection ( dev, qa, staging, prod )
- conf : config settings
- logs : logger
- info : info about the application
- enc : encryption/decryption service
- dirs : directories used for the app
*/
interface Context {
val arg: Args
val env: Env
val cfg: Conf
val logs: Logs
val info: Info
val enc: Encryptor?
val dirs: Folders?
}
This component is currently stable, has default implementations, and can be used for both Android and Server
repositories {
// other repositories
maven { url "http://dl.bintray.com/codehelixinc/slatekit" }
}
dependencies {
// other dependencies ...
compile 'com.slatekit:slatekit-common:1.0.0'
}
Jar | slatekit.context.jar |
Package | slatekit.context |
Sources | slatekit-context |
Example | Example_Context.kt |
Version | |
License | |
Requires | See build.gradle for more info. |
The context can be constructed manually or using convenience methods that build the context from the command line args, and configs.
import slatekit.common.CommonContext
// Create simple context
val ctx1 = CommonContext.simple("demoapp")
Goal | Description |
1. Defaults | Provide sensible defaults to common dependencies like args, env, conf, logs and more for runnable apps |
2. Awareness | Provides access to application environment, configs, build, host info to serve as an identity for the app. |
3. Extensible | Can be extended so that you can build your own context and/or load addition components. |
Some examples of setting up the context
Example | Description | More |
1. Simple | Build a simple context using convenience methods | more |
2. Manual | Manually build the context with explicit values | more |
3. Derived | Allow the context to the built by the Application Runner | more |
The context can be constructed using convenience methods that build the context using empty/default data.
import slatekit.common.CommonContext
// Create simple context
val ctx1 = CommonContext.simple("demoapp")
The context can be constructed explicitly by supplying all the inputs
import slatekit.common.CommonContext
// Create simple context
val ctx2 = CommonContext(
args = Args.default(),
envs = Envs.defaults(),
conf = Config(), // Loads resources/env.conf
logs = LogsDefault,
info = Info(
About(
area = "department1",
name = "sample-app-1",
desc = "Sample application 1",
company = "Company 1",
region = "New York",
url = "http://company1.com/dep1/sampleapp-1",
contact = "dept1@company1.com",
version = "1.0.1",
tags = "sample app slatekit",
examples = ""
),
Build.empty,
Sys.build()
)
)
The context is automatically created when using the app. In this case, the AppRunner inspects the command line args, config settings from env.conf and then builds up the context. You also supply the builder function that supplies an instance of your Application using the auto-created context. You can modify/copy the context here before it is finally passed to the Application constructor.
import slatekit.common.CommonContext
// Create simple context
AppRunner.run(
rawArgs = request.args.raw.toTypedArray(),
schema = ArgsSchema(),
enc = Encryptor("wejklhviuxywehjk", "3214maslkdf03292", B64Java8),
logs = LogbackLogs(),
about = About.none,
builder = { ctx -> SampleApp(ctx) }
)
Most applications ( whether they are console, cli, jobs, server ) require basic boiler plate setup and access to services. This Context fills that need by providing these core services.
Name | Description | More |
1. Args | Access to parsed command line arguments | more |
2. Env | Access to selected environment | more |
3. Conf | Access to current config properties | more |
4. Logs | Access to Log factory | more |
5. Enc | Access to encryptor | more |
6. Build | Access to current build information | more |
7. About | Access to current application information | more |
You can access the parsed command line args. See Args for more info.
ctx.args.line
ctx.args.action // e.g. "action" if using "service.action -key=1"
ctx.args.get("env")
ctx.args.getStringOrNull("log.level")
ctx.args.getStringOrElse("log.level", "warn")
You have access to the environments and currently selected environment. See Env for more info.
ctx.envs.name // "loc" ( representing local )
ctx.envs.env // "dev" EnvMode: ( Dev | Qat | Uat | Pro )
ctx.envs.key // "loc:dev" {name}:{env}
ctx.envs.current // current Env object
ctx.envs.all // list of all Env environments
You have access to the currently loaded configuration settings. See Conf for more info.
ctx.conf.getInt("paging.batchSize")
ctx.conf.getIntOrNull("paging.batchSize")
ctx.conf.getIntOrElse("paging.batchSize", 4)
You have have to the logs/factory to create loggers. See Logs for more info.
val logger1 = ctx.logs.getLogger()
val logger2 = ctx.logs.getLogger(name = "service1")
val logger3 = ctx.logs.getLogger(Example_Jobs::class.java)
logger1.level
logger1.name
logger1.debug("Debug message")
logger1.info ("Info message")
logger1.warn ("Warn message")
logger1.error("Error message")
logger1.fatal("Fatal message")
You can access the optional encryptor to encrypt/decrypt data. See Encryptor for more info.
ctx.enc?.encrypt("raw text")
ctx.enc?.decrypt("")