Skip to content

Commit

Permalink
Closes #518 - Refactoring the file access in the configuration server (
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusoe authored May 4, 2020
1 parent d9920f3 commit 0fbd9a6
Show file tree
Hide file tree
Showing 36 changed files with 3,416 additions and 1,494 deletions.
2,106 changes: 1,818 additions & 288 deletions THIRD-PARTY-LICENSES.txt

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions components/inspectit-ocelot-configurationserver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ sourceCompatibility = 1.8

test {
useJUnitPlatform()

testLogging {
exceptionFormat = 'full'
}
}

dependencies {
Expand Down Expand Up @@ -135,6 +139,7 @@ dependencies {
)
testImplementation(
'org.springframework.boot:spring-boot-starter-test:2.1.6.RELEASE',
"org.springframework.security:spring-security-test:5.1.5.RELEASE",
'org.junit.jupiter:junit-jupiter-api:5.3.1',
'org.mockito:mockito-junit-jupiter:2.23.0',
'com.h2database:h2:1.4.194'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.google.common.annotations.VisibleForTesting;
import lombok.extern.slf4j.Slf4j;
import org.yaml.snakeyaml.Yaml;
import rocks.inspectit.ocelot.file.FileInfo;
import rocks.inspectit.ocelot.file.FileManager;
import rocks.inspectit.ocelot.file.accessor.workingdirectory.AbstractWorkingDirectoryAccessor;
import rocks.inspectit.ocelot.mappings.model.AgentMapping;

import java.io.IOException;
Expand Down Expand Up @@ -130,17 +132,22 @@ String loadConfigForMapping(AgentMapping mapping) throws IOException {
* @param path the path to check for yaml files, can start with a slash which will be ignored
* @return a list of absolute paths of contained YAML files
*/
private List<String> getAllYamlFiles(String path) throws IOException {
private List<String> getAllYamlFiles(String path) {
String cleanedPath;
if (path.startsWith("/")) {
cleanedPath = path.substring(1);
} else {
cleanedPath = path;
}
if (fileManager.exists(cleanedPath)) {
if (fileManager.isDirectory(cleanedPath)) {
return fileManager.getFilesInDirectory(cleanedPath, true).stream()
.flatMap(f -> f.getAbsoluteFilePaths(cleanedPath))

AbstractWorkingDirectoryAccessor workingDirectory = fileManager.getWorkingDirectory();

if (workingDirectory.configurationFileExists(cleanedPath)) {
if (workingDirectory.configurationFileIsDirectory(cleanedPath)) {
List<FileInfo> fileInfos = workingDirectory.listConfigurationFiles(cleanedPath);

return fileInfos.stream()
.flatMap(file -> file.getAbsoluteFilePaths(cleanedPath))
.filter(HAS_YAML_ENDING)
.sorted()
.collect(Collectors.toList());
Expand All @@ -157,11 +164,11 @@ private List<String> getAllYamlFiles(String path) throws IOException {
* @param toMerge the existing structure of nested maps / lists with which the loaded yaml will be merged.
* @param path the path of the yaml file to load
* @return the merged structure
* @throws IOException in case an error occurs while loading the file
*/
private Object loadAndMergeYaml(Object toMerge, String path) throws IOException {
private Object loadAndMergeYaml(Object toMerge, String path) {
Yaml yaml = new Yaml();
String src = fileManager.readFile(path);
String src = fileManager.getWorkingDirectory().readConfigurationFile(path).orElse("");

try {
Object loadedYaml = yaml.load(src);
if (toMerge == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package rocks.inspectit.ocelot.agentstatus;

import com.google.common.base.Preconditions;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ private static void setupUsagePaths() {
* Ignores action inputs that start with the String defined in INPUT_FILTER_ONSET.
*
* @param path A given path as List. Each String should act as a literal of the path.
*
* @return A List of Strings resembling the declared actions that could be used with the given path.
*/
@Override
Expand All @@ -105,7 +104,6 @@ public List<String> getSuggestions(List<String> path) {
* INPUT_FILTER_ONSET.
*
* @param path A given path as List. Each String should act as a literal of the path.
*
* @return A List of Strings resembling the declared actions that could be used with the given path.
*/
private List<String> getActionInputs(List<String> path) {
Expand All @@ -125,7 +123,6 @@ private List<String> getActionInputs(List<String> path) {
* ["inspectit","instrumentation","actions","action_B","input"] is returned.
*
* @param path A given path as List. Each String should act as a literal of the path.
*
* @return A List containing String Lists. Each of these Lists resemble one path as described.
*/
private List<List<String>> buildActionPaths(List<String> path) {
Expand All @@ -145,7 +142,6 @@ private List<List<String>> buildActionPaths(List<String> path) {
* found under "inspectit.instrumentation.rules.my-rule.entry.my-entry-method.action" are returned.
*
* @param path A List of Strings resembling a path to a rule-input.
*
* @return The actions declared in this rule.
*/
private List<String> getActions(List<String> path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
import org.yaml.snakeyaml.Yaml;
import rocks.inspectit.ocelot.config.loaders.ConfigFileLoader;
import rocks.inspectit.ocelot.file.FileChangedEvent;
import rocks.inspectit.ocelot.file.FileInfo;
import rocks.inspectit.ocelot.file.FileManager;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -97,17 +95,12 @@ private Object parseYaml(String content) {
*/
@VisibleForTesting
Object loadYamlFile(String path) {
String src;
try {
src = fileManager.readFile(path);
} catch (IOException e) {
Optional<String> src = fileManager.getWorkingDirectory().readConfigurationFile(path);

return src.map(this::parseYaml).orElseGet(() -> {
log.warn("Unable to load file with path {}", path);
return null;
}
if (src != null) {
return parseYaml(src);
}
return null;
});
}

/**
Expand All @@ -119,8 +112,10 @@ Object loadYamlFile(String path) {
@VisibleForTesting
List<String> getAllPaths() {
try {
return fileManager.getFilesInDirectory("", true).stream()
.flatMap(f -> f.getAbsoluteFilePaths(""))
List<FileInfo> fileInfos = fileManager.getWorkingDirectory().listConfigurationFiles("");

return fileInfos.stream()
.flatMap(file -> file.getAbsoluteFilePaths(""))
.filter(HAS_YAML_ENDING)
.sorted()
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
package rocks.inspectit.ocelot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.security.ldap.search.FilterBasedLdapUserSearch;
import org.springframework.security.ldap.search.LdapUserSearch;
import org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator;
import org.springframework.security.ldap.userdetails.LdapUserDetailsService;
import rocks.inspectit.ocelot.config.model.InspectitServerSettings;
import rocks.inspectit.ocelot.config.model.LdapSettings;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ public class DefaultUserSettings {
private String name;

private String password;

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ public class InspectitServerSettings {

@Valid
@Builder.Default
private SecuritySettings security = SecuritySettings.builder().build();
private SecuritySettings security = SecuritySettings.builder().build();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rocks.inspectit.ocelot.error;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Builder;
import lombok.Data;
import org.springframework.http.HttpStatus;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FileInfo {

public enum Type {
Expand All @@ -28,7 +33,7 @@ public enum Type {

/**
* If this is a file, its absolute path is returned given the containing directory.
* Otherwise the absolute paths of all (transitively) contianed files (not directories) is returned.
* Otherwise the absolute paths of all (transitively) contained files (not directories) is returned.
*
* @param containingDir the path directory containing this file, can be empty if this file is at the root
* @return a stream of absolute paths
Expand All @@ -43,4 +48,16 @@ public Stream<String> getAbsoluteFilePaths(String containingDir) {
return Stream.of(absolutePath);
}
}

/**
* Adds a child and initializes the children list in case it does not exist.
*
* @param fileInfo the child to add
*/
synchronized void addChild(FileInfo fileInfo) {
if (children == null) {
children = new ArrayList<>();
}
children.add(fileInfo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package rocks.inspectit.ocelot.file;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
import java.util.List;
import java.util.Stack;

/**
* FileVisitor for walking a file tree and creating a {@link FileInfo} representation of it.
*/
@Slf4j
public class FileInfoVisitor implements FileVisitor<Path> {

/**
* The directory stack. The latest directory is the current one.
*/
private Stack<FileInfo> directoryStack = new Stack<>();

/**
* The {@link FileInfo} which represents the starting directory of the walk.
*/
private FileInfo rootDirectory;

@Override
public FileVisitResult preVisitDirectory(Path directory, BasicFileAttributes attrs) {
FileInfo currentDirectory = FileInfo.builder()
.name(directory.getFileName().toString())
.type(FileInfo.Type.DIRECTORY)
.build();

// add directory as child, otherwise set it as root
if (directoryStack.isEmpty()) {
rootDirectory = currentDirectory;
} else {
directoryStack.peek().addChild(currentDirectory);
}

directoryStack.add(currentDirectory);

return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
// adding each file to the current directory
FileInfo fileInfo = FileInfo.builder()
.name(file.getFileName().toString())
.type(FileInfo.Type.FILE)
.build();

directoryStack.peek().addChild(fileInfo);

return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
log.error("Could not visit file '{}'.", file, exc);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
// leaving directory, so set stack to the parent directory
directoryStack.pop();
return FileVisitResult.CONTINUE;
}

/**
* @return Returns the list of visited files and directories.
*/
public List<FileInfo> getFileInfos() {
if (rootDirectory == null) {
return Collections.emptyList();
} else {
List<FileInfo> children = rootDirectory.getChildren();
if (children == null) {
return Collections.emptyList();
} else {
return children;
}
}
}
}
Loading

0 comments on commit 0fbd9a6

Please sign in to comment.