templates.plugins.spincast-properties-file-config.spincast-properties-file-config.html Maven / Gradle / Ivy
{#==========================================
Spincast Properties File Config
==========================================#}
{% extends "../../layout.html" %}
{% block sectionClasses %}plugins plugins-spincast-config-props-file{% endblock %}
{% block meta_title %}Plugins - Spincast Properties File Config{% endblock %}
{% block meta_description %}Allows an application to define its configurations in a .properties file.{% endblock %}
{% block scripts %}
{% endblock %}
{% block body %}
    
    
    
    
    
    
        
        
        
        
            
            
                
                    
                    Overview
                
	            
	                Allows an application to define its configurations in an external .properties file. You
                    can then have different configurations, depending on the environment the application runs on.
                
                 
                    The path to this file can be explicitly defined: by default, the first
                    argument of the main(...) method, if present, is considered as the path
                    to this configuration file. For example:
	            
                
                    
                        
                            
java -jar mySpincastApp.jar /var/www/mySite/mySiteApp.properties
 
                        
                      
                
                
                    You can override the lookForPropsFileSpecificPath(...)
                    method if you want to use another way to externally define the path (for example, by looking
                    at an environment variable).
                
                
                    
                    If no explicit path is provided, the plugin tries to find an app.properties
                    file in the same directory where the application's .jar is running.
                
                
                    
                    If no app.properties file is found next to the .jar, or if the application is not
                    running from a .jar file (the code is running inside an IDE, for example), then the 
                    default configurations are used.
                
                
                    You can add your application specific configurations in this .properties file, but
                    by using the default keys,
                    you can also override Spincast default configurations.
                    Note that you don't have to override all default configurations. If
                    a particular Spincast configuration is not there, its default value will be used.
                
                
             
            
	        
                
                    
                    Installation
                
	            
                    
	                Add this artifact to your project:
	                
	                    
	                        
<dependency>
    <groupId>org.spincast</groupId>
    <artifactId>spincast-plugins-config-properties-file</artifactId>
    <version>{{spincastCurrrentVersion}}</version>
</dependency> 
	                    
	                  
	            
                
                    
                    If you use a custom configuration class (which
                    is probably the case), you don't have to install any Guice module 
                    for this plugin, and you simply make your configuration class extend 
                    SpincastConfigPropsFileBased:
                    
                        
                            
public class AppConfig extends SpincastConfigPropsFileBased implements IAppConfig {
    @Inject
    public AppConfig(ISpincastUtils spincastUtils,
                     @MainArgs @Nullable String[] mainArgs) {
        super(spincastUtils, mainArgs);
    }
    @Override
    public String getAppName() {
        return getConfig("app.name");
    }
    @Override
    public int getSomeNumericConfig() {
        return getConfigInteger("app.some.other.config", 12345);
    }
} 
                        
                        
                            Explanation :
                            
                                - 
                                    1 : Our custom configuration class
                                    extends 
SpincastConfigPropsFileBased but also implements
                                    our custom IAppConfig interface.
                                 
                                - 
                                    3-7 : Constructor required
                                    to satisfy the base class.
                                
 
                                - 
                                    9-12 : A first application
                                    specific configuration. We use the 
getConfig(key)
                                    method to get the value to use from the .properties file. Since
                                    no default value is provided, an exception is throw 
                                    if the configuration is not found!
                                  
                                - 
                                    14-17 : A second application
                                    specific configuration. Here we use 
getConfigInteger(key, defaultValue)
                                    to get an Integer configuration. If the configuration is not found in the
                                    .properties file, the specified default value is used.
                                  
                            
                          
                       
                
                
                    
                    If your application doesn't have a custom configuration class and you simply want 
                    to be able to externally override some of Spincast default configurations (for
                    example the server port), then bind the provided
                    SpincastConfigPropsFilePluginGuiceModule Guice module:
                
	            
	                
	                    
	                        
Injector guice = Guice.createInjector(Modules.override(new SpincastDefaultGuiceModule(args))
        .with(new SpincastConfigPropsFilePluginGuiceModule(IDefaultRequestContext.class))); 
	                    
	                   
	            
                
                    Or, if you use a custom Guice module and request context type:
                    
                        
                            
Injector guice = Guice.createInjector(Modules.override(new AppModule(args))
        .with(new SpincastConfigPropsFilePluginGuiceModule(IAppRequestContext.class))); 
                        
                       
                
	            
	                You can also override the default configuration plugin installation, from your custom Guice module:
	                
	                    
	                        
public class AppModule extends SpincastDefaultGuiceModule {
    public AppModule(String[] mainArgs) {
        super(mainArgs);
    }
    @Override
    protected void configure() {
        super.configure();
        //...
    }
    @Override
    protected void bindConfigPlugin() {
        install(new SpincastConfigPluginGuiceModule(getRequestContextType()));
    }
    //...
} 
	                    
	                   
	            
             
            
            
                
                    
                    SpincastConfigPropsFileBased base methods
                
                
                    
                        
                            - 
                                
String getConfig(String key)
                                
                                    Gets a String configuration and throws an exception if the
                                    specified key is not found. 
                                
                             
                            - 
                                
String getConfig(String key, String defaultValue)
                                
                                    Gets a String configuration and uses the specified default value if the
                                    key is not found.
                                
                             
                            - 
                                
Boolean getConfigBoolean(String key)
                                
                                    Gets a Boolean configuration and throws an exception if the
                                    specified key is not found. 
                                
                               
                            - 
                                
Boolean getConfigBoolean(String key, Boolean defaultValue)
                                
                                    Gets a Boolean configuration and uses the specified default value if the
                                    key is not found.
                                
                             
                            - 
                                
Integer getConfigInteger(String key)
                                
                                    Gets a Integer configuration and throws an exception if the
                                    specified key is not found. 
                                
                               
                            - 
                                
Integer getConfigInteger(String key, Integer defaultValue)
                                
                                    Gets a Integer configuration and uses the specified default value if the
                                    key is not found.
                                
                             
                            
                        
                    
                
  
             
            
            
                
                    
                    Using free keys
                
                
                    If you want to be able to access your configurations without having to
                    define a method for all of them, you can use the provided IFreeKeyConfig
                    interface:
                
 
                
                    
                        
                            
public static interface IAppConfig extends ISpincastConfig, IFreeKeyConfig {
    public String someTypedConfig1();
    public Boolean someTypedConfig2();
    
    //...
} 
                        
                       
                
                
                    Now, you have access your .properties based configurations without having to
                    define a method for all of them. You directly use getConfig(...), 
                    getConfigInteger(...) or getConfigBoolean(...):
                
 
                
                    
                        
                            
IAppConfig configs = getAppConfig();
String config = configs.getConfig("some.config.as.string");
config = configs.getConfig("some.non.existing.config", "default value");
Integer configInt = configs.getConfigInteger("some.config.as.int");
configInt = configs.getConfigInteger("some.non.existing.config", 42);
Boolean configBool = configs.getConfigBoolean("some.config.as.bool");
configBool = configs.getConfigBoolean("some.non.existing.config", true);
// Typed configs still work too, of course:
config = configs.someTypedConfig1();
configBool = configs.someTypedConfig2();
 
                        
                       
                
                  
             
            
            
                
                    
                    The default configuration keys
                
                
                    In your .properties file, you would add any application specific configuration,
                    but you can also override default Spincast configurations. Here are the keys to use to
                    override those.
                
 
                
                
                    
                        
                            - 
                                
spincast.environment.name
                                
                                    The name of the environment.
                                
                             
                            - 
                                
spincast.environment.isDebug
                                
                                    Should debug mode be enabled? ("true" or "false")
                                
                             
                            
                            - 
                                
spincast.server.host
                                
                                    The host/IP the server should bind to.
                                
                             
                            
                            - 
                                
spincast.httpServer.port
                                
                                    The port of the HTTP server. If not specify or is <= 0
                                    no HTTP server will be started. (Integer value)
                                
                              
                            - 
                                
spincast.httpsServer.port
                                
                                    The port of the HTTPS (secure) server. If not specify or is <= 0
                                    no HTTPS server will be started. (Integer value)
                                
                              
                            - 
                                
spincast.httpsServer.keystore.path
                                
                                    The path to the KeyStore, for SSL. Can be a
                                    classpath path or and absolute path.
                                
                              
                            - 
                                
spincast.httpsServer.keystore.type
                                
                                    The type of the KeyStore, for SSL. For example: "JKS".
                                
                              
                            - 
                                
spincast.httpsServer.keystore.storepass
                                
                                    The "storepass" of the KeyStore, for SSL.
                                
                              
                            - 
                                
spincast.httpsServer.keystore.keypass
                                
                                    The "keypass" of the KeyStore, for SSL.
                                
                              
                        
                    
                
             
            
            
                
                    
                    Example .properties file
                
                
                    
                        
                            
# Default Spincast configurations
spincast.environment.name=prod
spincast.environment.isDebug=false
spincast.httpServer.port=-1
spincast.httpsServer.port=443
spincast.httpsServer.keystore.path=/var/www/mySite/certificates/myKeyStore.jks
spincast.httpsServer.keystore.type=JKS
spincast.httpsServer.keystore.storepass=myStorePass
spincast.httpsServer.keystore.keypass=myKeyPass
# App specific configurations
app.name=My supercalifragilisticexpialidocious app!
app.some.other.config=42
 
                        
                      
                
             
        
    
 
{% endblock %}    
 Spincast Properties File Config