Giter Site home page Giter Site logo

java-environment's Introduction

Environment

ARM Canada inc.

Environment file parser and matcher

Usage

Used to parse file formatted as below to get environment variable

DB_CONNECTION=mysql
DB_HOST=localhost

Basic

Load environment

You can load any List<String> into the environment. You can either read a file into an ArrayList and load it to the environment

public class Main
{
    public static void main(String[] args)
    {
        List<String> values = new ArrayList<>();
        values.add("DB_CONNECTION=mysql");
        values.add("DB_HOST=localhost");
        Environment.load(values);
    }    
}

Getting a variable

Once you have loaded the variable, you can get them using the get() facade method. You can also pass in a second argument as a fallback value

public class Main
{
    public static void main(String[] args)
    {
        Environment.get("DB_CONNECTION"); // would return mysql
        Environment.get("DB_DATABASE", "undefined"); // would return undefined since we did not provided any "DB_DATABASE" key
    }    
}

Setting a variable

You can override any value at anytime with set() method

public class Main
{
    public static void main(String[] args)
    {
        Environment.set("DB_CONNECTION", "sqlserver");
    }    
}

Class property annotation

The package comes with a @EnvironmentVariable annotation that allows an automatic assignation with the environment variables. All you have to do is call the attach() facade method on any annotated class.

Note: Annotated properties must be public

public class Database
{
    @EnvironmentVariable(key = "DB_CONNECTION")
    public String connection;
}
public class Main
{
    public static void main(String[] args)
    {
        Database db = new Database();
        Environment.attach(db);
        db.connection; // is now equals to "mysql"        
    }    
}

java-environment's People

Contributors

nicklayb avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.