Skip to content

Commit

Permalink
wip configs
Browse files Browse the repository at this point in the history
  • Loading branch information
lmajano committed Sep 21, 2023
1 parent 44987a2 commit 90bc1c8
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,39 @@

import com.fasterxml.jackson.databind.ObjectMapper;

import ortus.boxlang.runtime.types.exceptions.ConfigurationException;

import java.io.IOException;

/**
* This class is responsible for loading the core configuration file from the `resources` folder
* and parsing it into the Configuration class.
*
* It can also load from a custom location.
*/
public class ConfigLoader {

/**
* --------------------------------------------------------------------------
* Private Properties
* --------------------------------------------------------------------------
*/

/**
* Path to the core config file in the `resources` folder
*/
private static final String CONFIG_FILE = "config/config.json";
private static final String DEFAULT_CONFIG_FILE = "config/config.json";

/**
* The ConfigLoader instance
*/
private static ConfigLoader instance;
private static ConfigLoader instance;

/**
* The parsed configuration
* --------------------------------------------------------------------------
* Singleton Constructor
* --------------------------------------------------------------------------
*/
private static Configuration configuration;

/**
* Constructor
Expand All @@ -58,13 +73,10 @@ public static synchronized ConfigLoader getInstance() {
}

/**
* Get the parsed configuration
*
* @return The parsed configuration
* --------------------------------------------------------------------------
* Loaders
* --------------------------------------------------------------------------
*/
public static Configuration getConfiguration() {
return configuration;
}

/**
* Load the config file into the Configuration class from the `resources` folder
Expand All @@ -76,25 +88,14 @@ public static Configuration getConfiguration() {
public static synchronized Configuration load() {
// Read JSON file into Configuration class
try {
instance.configuration = new ObjectMapper().readValue(
ClassLoader.getSystemClassLoader().getResourceAsStream( CONFIG_FILE ),
return new ObjectMapper().readValue(
ClassLoader.getSystemClassLoader().getResourceAsStream( DEFAULT_CONFIG_FILE ),
Configuration.class
);
} catch ( IOException e ) {
e.printStackTrace();
throw new RuntimeException( "Unable to load core resources config file", e );
throw new ConfigurationException( "Unable to load core config file " + e.getMessage(), e );
}

return instance.configuration;
}

public static void main( String[] args ) {
Configuration config = ConfigLoader.getInstance().load();
// Access the parsed configuration
System.out.println( "Compiler Directory: " + config.getCompiler().getClassGenerationDirectory() );
System.out.println( "Modules Directory: " + config.getRuntime().getModulesDirectory() );
System.out.println( "Cache Type: " + config.getRuntime().getCaches().getType() );
System.out.println( "Cache Properties: " + config.getRuntime().getCaches().getProperties() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,20 @@ public class Configuration {
* The compiler configuration, defaulted to the default compiler configuration
*/
@JsonProperty( "compiler" )
private CompilerConfig compiler = new CompilerConfig();
public CompilerConfig compiler = new CompilerConfig();

/**
* The runtime configuration, defaulted to the default runtime configuration
*/
@JsonProperty( "runtime" )
private RuntimeConfig runtime = new RuntimeConfig();
public RuntimeConfig runtime = new RuntimeConfig();

public Configuration() {
}

public CompilerConfig getCompiler() {
return compiler;
}

public void setCompiler( CompilerConfig compiler ) {
this.compiler = compiler;
}

public RuntimeConfig getRuntime() {
return runtime;
}
/**
* --------------------------------------------------------------------------
* Methods
* --------------------------------------------------------------------------
*/

public void setRuntime( RuntimeConfig runtime ) {
this.runtime = runtime;
public Configuration() {
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
/**
* [BoxLang]
*
* Copyright [2023] [Ortus Solutions, Corp]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ortus.boxlang.runtime.config.segments;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonProperty;

import ortus.boxlang.runtime.types.Struct;

/**
* A BoxLang cache configuration
*/
public class CacheConfig {

private String type = "Caffeine";
private Map<String, Object> properties = new HashMap<>();
/**
* The name of the cache engine
*/
@JsonProperty( "name" )
private String name = "default";

/**
* The default cache engine type
*/
@JsonProperty( "type" )
private String type = "Caffeine";

/**
* The properties for the cache engine
*/
@JsonProperty( "properties" )
private Struct properties = new Struct();

public String getType() {
return type;
Expand All @@ -16,11 +55,11 @@ public void setType( String type ) {
this.type = type;
}

public Map<String, Object> getProperties() {
public Struct getProperties() {
return properties;
}

public void setProperties( Map<String, Object> properties ) {
public void setProperties( Struct properties ) {
this.properties = properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import com.fasterxml.jackson.annotation.JsonProperty;

import ortus.boxlang.runtime.config.util.PlaceholderHelper;

/**
* The BoxLang compiler configuration
*/
Expand All @@ -29,16 +31,21 @@ public class CompilerConfig {
* The default is the system temp directory + {@code /boxlang}
*/
@JsonProperty( "classGenerationDirectory" )
private String classGenerationDirectory = System.getProperty( "java.io.tmpdir" ) + "/boxlang";
public String classGenerationDirectory = System.getProperty( "java.io.tmpdir" ) + "/boxlang";

public CompilerConfig() {
/**
* @param classGenerationDirectory the classGenerationDirectory to set
*/
public void setClassGenerationDirectory( String classGenerationDirectory ) {
this.classGenerationDirectory = PlaceholderHelper.resolve( classGenerationDirectory );
}

public String getClassGenerationDirectory() {
return classGenerationDirectory;
}
/**
* --------------------------------------------------------------------------
* Methods
* --------------------------------------------------------------------------
*/

public void setClassGenerationDirectory( String classGenerationDirectory ) {
this.classGenerationDirectory = classGenerationDirectory;
public CompilerConfig() {
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,68 @@
/**
* [BoxLang]
*
* Copyright [2023] [Ortus Solutions, Corp]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ortus.boxlang.runtime.config.segments;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;

import ortus.boxlang.runtime.config.util.PlaceholderHelper;
import ortus.boxlang.runtime.types.Struct;

/**
* The runtime configuration for the BoxLang runtime
*/
public class RuntimeConfig {

/**
* A struct of mappings for the runtime
*/
@JsonProperty( "mappings" )
public Struct mappings = new Struct();

/**
* The directory where the modules are located by default:
* {@code /{user-home}/modules}
*/
@JsonProperty( "modulesDirectory" )
private String modulesDirectory;
public String modulesDirectory = System.getProperty( "user.home" ) + "/modules";

/**
* The cache configurations for the runtime
*/
@JsonProperty( "caches" )
private List<CacheConfig> caches = new ArrayList<>();
public Struct caches = new Struct();

public RuntimeConfig() {
}
/**
* --------------------------------------------------------------------------
* Methods
* --------------------------------------------------------------------------
*/

public String getModulesDirectory() {
return modulesDirectory;
public RuntimeConfig() {
}

/**
* @param modulesDirectory the modulesDirectory to set
*/
public void setModulesDirectory( String modulesDirectory ) {
this.modulesDirectory = modulesDirectory;
this.modulesDirectory = PlaceholderHelper.resolve( modulesDirectory );
}

public List<CacheConfig> getCaches() {
return caches;
}

public void setCaches( List<CacheConfig> caches ) {
this.caches = caches;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* [BoxLang]
*
* Copyright [2023] [Ortus Solutions, Corp]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ortus.boxlang.runtime.config.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* A helper class for resolving placeholders in configuration files
*/
public class PlaceholderHelper {

private static final Map<String, String> PLACEHOLDER_MAP = new HashMap<>();

static {
// Add default replacements
PLACEHOLDER_MAP.put( "{user-home}", System.getProperty( "user.home" ) );
PLACEHOLDER_MAP.put( "{java-temp}", System.getProperty( "java.io.tmpdir" ) );
// Add additional replacements here
// placeholderMap.put("{your-placeholder}", "replacement-value");
}

/**
* Resolve the input string and replace all placeholders with their values
*
* @param input The input string to Resolve
*
* @return The Resolved string
*/
public static String resolve( String input ) {
// Create a pattern to match placeholder patterns like "{...}"
Pattern pattern = Pattern.compile( "\\{([^}]+)\\}" );

// Use Matcher and the stream API to replace placeholders
Matcher matcher = pattern.matcher( input );
return matcher.replaceAll( match -> PLACEHOLDER_MAP.getOrDefault( match.group( 1 ), match.group() ) );
}

}
Loading

0 comments on commit 90bc1c8

Please sign in to comment.