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.
/**
- 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?
}Table of contents for this page
| Section | Component | Description |
| 1 | Status | Current status of this component |
| 2 | Install | Installation instructions and references to sources |
| 3 | Requires | Lists all the Slate Kit and third-party dependencies |
| 4 | Sample | Quick sample to show usage of the component |
| 5 | Goals | Goals of this component and the problems it attempts to solve |
| 6 | Concepts | Core concepts to understand in this component |
| 7 | Features | List all the features supported |
| 8 | Setup | Set up and configure this component for use |
| 9 | Details | In-depth examples of the supported features |
This component is currently stable, has default implementations, and can be used for both Android and Server
Back to top
coming soon
repositories {
// other repositories
maven { url "http://dl.bintray.com/codehelixinc/slatekit" }
}
dependencies {
// other dependencies ...
compile 'com.slatekit:slatekit-common:1.0.0'
}| package | slatekit.common |
| jar | slatekit.common.jar |
| git | slatekit/src/lib/kotlin/slatekit-common |
| docs | App |
| uses | slatekit.results, slatekit.common |
| license | Apache 2.0 |
| example | Example_Context.kt |
This component uses the following other Slate Kit and/or third-party components.
| Component | Description |
| Slate Kit - Results | To model successes and failures with optional status codes |
| Slate Kit - Common | Common utilities for both android + server |
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. |
| 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
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
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
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
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
ctx.enc?.encrypt("raw text")
ctx.enc?.decrypt("")