-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
work on #3: Spring initialization and usage for configuration
- Loading branch information
Showing
16 changed files
with
468 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 6 additions & 2 deletions
8
inspectit-oce-bootstrap/src/main/java/rocks/inspectit/oce/bootstrap/IAgent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 32 additions & 3 deletions
35
inspectit-oce-core/src/main/java/rocks/inspectit/oce/core/AgentImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,45 @@ | ||
package rocks.inspectit.oce.core; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import rocks.inspectit.oce.bootstrap.IAgent; | ||
import rocks.inspectit.oce.core.config.PropertySourcesInitializer; | ||
import rocks.inspectit.oce.core.config.SpringConfiguration; | ||
|
||
import java.lang.instrument.Instrumentation; | ||
|
||
/** | ||
* Implementation for the {@link IAgent} interface. | ||
* This clas sis responsible forsetting up the spring context for inspectIT. | ||
* | ||
* @author Jonas Kunz | ||
*/ | ||
@Slf4j | ||
public class AgentImpl implements IAgent { | ||
|
||
private AnnotationConfigApplicationContext ctx; | ||
|
||
@Override | ||
public void start() { | ||
System.out.println("Starting Agent"); | ||
public void start(String cmdArgs, Instrumentation instrumentation) { | ||
|
||
log.info("Starting inspectIT OCE Agent..."); | ||
ctx = new AnnotationConfigApplicationContext(); | ||
ctx.setClassLoader(AgentImpl.class.getClassLoader()); | ||
ctx.registerShutdownHook(); | ||
|
||
//Allows to use autowiring to acquire the Instrumentation instance | ||
ctx.getBeanFactory().registerSingleton("instrumentation", instrumentation); | ||
|
||
PropertySourcesInitializer.configurePropertySources(ctx); | ||
|
||
ctx.register(SpringConfiguration.class); | ||
ctx.refresh(); | ||
} | ||
|
||
|
||
@Override | ||
public void destroy() { | ||
System.out.println("Shutting down Agent"); | ||
log.info("Shutting down inspectIT OCE Agent"); | ||
ctx.close(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
inspectit-oce-core/src/main/java/rocks/inspectit/oce/core/config/ConfigurationCenter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package rocks.inspectit.oce.core.config; | ||
|
||
|
||
import lombok.Getter; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.stereotype.Component; | ||
import rocks.inspectit.oce.core.config.model.InspectitConfig; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
/** | ||
* Compoennt responsible for loading and reloading the inspectit configurations. | ||
* The configuration is read from the properties of the spring environment | ||
* | ||
* @author Jonas Kunz | ||
*/ | ||
@Component | ||
public class ConfigurationCenter { | ||
|
||
@Autowired | ||
ConfigurableEnvironment env; | ||
|
||
@Getter | ||
private InspectitConfig currentConfiguration; | ||
|
||
/** | ||
* (Re-)loads the {@link InspectitConfig} from the environemnt. | ||
* If any changes are detected an event is generated. | ||
*/ | ||
@PostConstruct | ||
public void reloadConfiguration() { | ||
currentConfiguration = InspectitConfig.createFromEnvironment(env); | ||
System.out.println(currentConfiguration); | ||
//TODO: compare with previous config: if any changes are present send an event | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...it-oce-core/src/main/java/rocks/inspectit/oce/core/config/PropertySourcesInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package rocks.inspectit.oce.core.config; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.MutablePropertySources; | ||
import org.springframework.core.env.PropertiesPropertySource; | ||
import org.springframework.core.io.ClassPathResource; | ||
import rocks.inspectit.oce.core.config.filebased.DirectoryPropertySource; | ||
import rocks.inspectit.oce.core.config.filebased.PropertyFileUtils; | ||
import rocks.inspectit.oce.core.config.model.InspectitConfig; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.function.BiConsumer; | ||
|
||
/** | ||
* This class is responsible for registering all {@link org.springframework.core.env.PropertySource}s required for the initialization of inspectIT. | ||
* | ||
* @author Jonas Kunz | ||
*/ | ||
@Slf4j | ||
public class PropertySourcesInitializer { | ||
|
||
private static final String DEFAULT_CONFIG_PATH = "/config/default.yml"; | ||
private static final String DEFAULT_CONFIG_PROPERTYSOURCE_NAME = "inspectitDefaults"; | ||
|
||
/** | ||
* Sorted list of all configuration sources. | ||
* They are loaded in the given order. The earlier a configuration appears in this list, the higher its priority. | ||
* This means that configurations loaded from items appearing earlier in the list overwrite configurations from items appearing later in the list. | ||
* In contrast items appearing first in the list can provide information for loading items appearing later in the list. | ||
*/ | ||
private static final List<BiConsumer<MutablePropertySources, InspectitConfig>> configurationInitializationSteps = Arrays.asList( | ||
PropertySourcesInitializer::addFileBasedConfiguration | ||
); | ||
|
||
/** | ||
* Configures the {@link org.springframework.core.env.PropertySource}s of the given spring context. | ||
* | ||
* @param ctx the spring context | ||
*/ | ||
public static void configurePropertySources(AnnotationConfigApplicationContext ctx) { | ||
ConfigurableEnvironment env = ctx.getEnvironment(); | ||
MutablePropertySources propsList = env.getPropertySources(); | ||
addAgentDefaultYaml(propsList); | ||
|
||
for (val initializer : configurationInitializationSteps) { | ||
initializer.accept(env.getPropertySources(), InspectitConfig.createFromEnvironment(env)); | ||
} | ||
|
||
log.info("Registered Configuration Sources:"); | ||
env.getPropertySources().stream().forEach(ps -> log.info(" {}", ps.getName())); | ||
} | ||
|
||
private static void addAgentDefaultYaml(MutablePropertySources propsList) { | ||
ClassPathResource defaultYamlResource = new ClassPathResource(DEFAULT_CONFIG_PATH, PropertySourcesInitializer.class.getClassLoader()); | ||
Properties defaultProps = PropertyFileUtils.readYamlFiles(defaultYamlResource); | ||
propsList.addLast(new PropertiesPropertySource(DEFAULT_CONFIG_PROPERTYSOURCE_NAME, defaultProps)); | ||
} | ||
|
||
private static void addFileBasedConfiguration(MutablePropertySources propsList, InspectitConfig currentConfig) { | ||
String path = currentConfig.getConfig().getFileBased().getPath(); | ||
Path dirPath = Paths.get(path); | ||
Boolean enabled = currentConfig.getConfig().getFileBased().isEnabled(); | ||
boolean fileBasedConfigEnabled = enabled && path != null && !path.isEmpty() && Files.exists(dirPath) && Files.isDirectory(dirPath); | ||
if (fileBasedConfigEnabled) { | ||
log.info("initializing file based configuration from dir: {}", path); | ||
val dps = new DirectoryPropertySource("fileBasedConfig", Paths.get(path)); | ||
propsList.addBefore(DEFAULT_CONFIG_PROPERTYSOURCE_NAME, dps); | ||
dps.reload(propsList); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
inspectit-oce-core/src/main/java/rocks/inspectit/oce/core/config/SpringConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package rocks.inspectit.oce.core.config; | ||
|
||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@ComponentScan("rocks.inspectit") | ||
public class SpringConfiguration { | ||
|
||
} |
Oops, something went wrong.