Skip to content

Commit

Permalink
config overrides are fully functional now
Browse files Browse the repository at this point in the history
  • Loading branch information
lmajano committed Oct 23, 2023
1 parent 4265d70 commit 1f10b97
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ortus/boxlang/runtime/BoxRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static void main( String[] args ) {
}

// Get a runtime going
BoxRuntime boxRuntime = BoxRuntime.getInstance( options.debug() );
BoxRuntime boxRuntime = BoxRuntime.getInstance( options.debug(), options.configFile() );

if ( options.templatePath() != null ) {
// Execute a file
Expand Down
31 changes: 27 additions & 4 deletions src/main/java/ortus/boxlang/runtime/BoxRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public class BoxRuntime {
"preFunctionInvoke",
"postFunctionInvoke",
"onScopeCreation",
"onConfigurationLoad"
"onConfigurationLoad",
"onConfigurationOverrideLoad"
);

/**
Expand Down Expand Up @@ -136,9 +137,10 @@ public class BoxRuntime {
/**
* Static constructor
*
* @param debugMode true if the runtime should be started in debug mode
* @param debugMode true if the runtime should be started in debug mode
* @param configPath The path to the configuration file to load as overrides
*/
private BoxRuntime( Boolean debugMode ) {
private BoxRuntime( Boolean debugMode, String configPath ) {
// Internal timer
timerUtil.start( "startup" );

Expand All @@ -161,6 +163,12 @@ private BoxRuntime( Boolean debugMode ) {
this.configuration = ConfigLoader.getInstance().loadCore();
interceptorService.announce( "onConfigurationLoad", Struct.of( "config", this.configuration ) );

// Config Override?
if ( configPath != null ) {
this.configuration.process( ConfigLoader.getInstance().deserializeConfig( configPath ) );
interceptorService.announce( "onConfigurationOverrideLoad", Struct.of( "config", this.configuration ) );
}

// Create our runtime context that will be the granddaddy of all contexts that execute inside this runtime
this.runtimeContext = new RuntimeBoxContext();

Expand All @@ -181,12 +189,27 @@ private BoxRuntime( Boolean debugMode ) {
/**
* Get the singleton instance. This can be null if the runtime has not been started yet.
*
* @param debugMode true if the runtime should be started in debug mode
*
* @return BoxRuntime
*
*/
public static synchronized BoxRuntime getInstance( Boolean debugMode ) {
return getInstance( debugMode, null );
}

/**
* Get the singleton instance. This can be null if the runtime has not been started yet.
*
* @param debugMode true if the runtime should be started in debug mode
* @param configPath The path to the configuration file to load as overrides
*
* @return BoxRuntime
*
*/
public static synchronized BoxRuntime getInstance( Boolean debugMode, String configPath ) {
if ( instance == null ) {
instance = new BoxRuntime( debugMode );
instance = new BoxRuntime( debugMode, configPath );
}
return instance;
}
Expand Down
83 changes: 70 additions & 13 deletions src/main/java/ortus/boxlang/runtime/config/ConfigLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,26 @@ public Configuration loadFromResources( String configFile ) {

}

/**
* Load the config from a Struct of settings
*
* @param configMap The configuration structure to load as a Configuration object
*
* @return The parsed configuration
*/
public Configuration loadFromMap( Struct configMap ) {
return new Configuration().process( configMap );
}

/**
* Load the config from a Map of settings
*
* @param configMap The configuration map to load as a Configuration object
* @param configMap The configuration Map to load as a Configuration object
*
* @return The parsed configuration
*/
public Configuration loadFromMap( Map<?, ?> configMap ) {
return new Configuration().process( new Struct( configMap ) );
public Configuration loadFromMap( Map<Object, Object> configMap ) {
return loadFromMap( new Struct( configMap ) );
}

/**
Expand All @@ -140,17 +151,10 @@ public Configuration loadFromMap( Map<?, ?> configMap ) {
*
* @return The parsed configuration
*/
@SuppressWarnings( "unchecked" )
public Configuration loadFromFile( File source ) {
// Parse it natively to Java objects
Object rawConfig = JsonUtil.fromJson( source );
// Verify it loaded the configuration map
if ( rawConfig instanceof Map ) {
logger.info( "Loaded custom BoxLang configuration file [{}]", source );
return loadFromMap( ( Map<Object, Object> ) rawConfig );
} else {
throw new ConfigurationException( "The config map is not a JSON object. Can't work with it." );
}
Struct rawConfig = deserializeConfig( source );
logger.info( "Loaded custom BoxLang configuration file [{}]", source );
return loadFromMap( rawConfig );
}

/**
Expand Down Expand Up @@ -186,4 +190,57 @@ public Configuration loadFromFile( String source ) {
return loadFromFile( new File( source ) );
}

/**
* Load the config from a file source and return the raw config map
*
* @param source The source to load the configuration from
*
* @return The raw config map as a Struct
*/
@SuppressWarnings( "unchecked" )
public Struct deserializeConfig( File source ) {
// Parse it natively to Java objects
Object rawConfig = JsonUtil.fromJson( source );

// Verify it loaded the configuration map
if ( rawConfig instanceof Map ) {
return new Struct( ( Map<Object, Object> ) rawConfig );
}

throw new ConfigurationException( "The config map is not a JSON object. Can't work with it." );
}

/**
* Load the config from a String path source and return the raw config map
*
* @param source The source to load the configuration from
*
* @return The raw config map as a Struct
*/
public Struct deserializeConfig( String source ) {
return deserializeConfig( new File( source ) );
}

/**
* Load the config from a URL path source and return the raw config map
*
* @param source The source to load the configuration from
*
* @return The raw config map as a Struct
*/
public Struct deserializeConfig( URL source ) {
return deserializeConfig( new File( source.getFile() ) );
}

/**
* Load the config from a path source and return the raw config map
*
* @param source The source to load the configuration from
*
* @return The raw config map as a Struct
*/
public Struct deserializeConfig( Path source ) {
return deserializeConfig( source.toFile() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public RuntimeConfig process( Struct config ) {
if ( config.containsKey( "mappings" ) ) {
tempTester = config.get( "mappings" );
if ( tempTester instanceof Map ) {
this.mappings = new Struct( ( Map<Object, Object> ) tempTester );
this.mappings.addAll( new Struct( ( Map<Object, Object> ) tempTester ) );
} else {
logger.warn( "The [runtime.mappings] configuration is not a JSON Object, ignoring it." );
}
Expand Down

0 comments on commit 1f10b97

Please sign in to comment.