Skip to content

Commit

Permalink
Add TemplateConfigDirChangeMonitorImpl WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
at055612 committed Jan 28, 2025
1 parent 73b7038 commit 7deb784
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import stroom.receive.common.ReceiptIdGenerator;
import stroom.receive.common.RequestHandler;
import stroom.util.cert.CertificateExtractor;
import stroom.util.guice.GuiceUtil;

import com.google.inject.AbstractModule;
import io.dropwizard.lifecycle.Managed;

public class ReceiveDataModule extends AbstractModule {

Expand All @@ -33,5 +35,8 @@ protected void configure() {
bind(FeedStatusService.class).to(FeedStatusServiceImpl.class);
bind(ReceiptIdGenerator.class).to(StroomReceiptIdGenerator.class).asEagerSingleton();
bind(RequestHandler.class).to(ReceiveDataRequestHandler.class);

GuiceUtil.buildMultiBinder(binder(), Managed.class)
.addBinding(TemplateConfigDirChangeMonitorImpl.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package stroom.core.receive;

import stroom.receive.common.AutoContentCreationConfig;
import stroom.util.NullSafe;
import stroom.util.io.AbstractDirChangeMonitor;
import stroom.util.io.SimplePathCreator;

import jakarta.inject.Inject;
import jakarta.inject.Provider;
import jakarta.inject.Singleton;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.EnumSet;
import java.util.function.Predicate;

@Singleton
public class TemplateConfigDirChangeMonitorImpl extends AbstractDirChangeMonitor {

private static final Predicate<Path> FILE_INCLUDE_FILTER = path ->
path != null
&& Files.isRegularFile(path)
&& path.getFileName().toString().endsWith(".json");

private final Provider<AutoContentCreationConfig> autoContentCreationConfigProvider;
private final Provider<ContentAutoCreationService> contentAutoCreationServiceProvider;

@Inject
public TemplateConfigDirChangeMonitorImpl(
final Provider<AutoContentCreationConfig> autoContentCreationConfigProvider,
final SimplePathCreator simplePathCreator,
final Provider<ContentAutoCreationService> contentAutoCreationServiceProvider) {
super(
getDataFeedDir(autoContentCreationConfigProvider, simplePathCreator),
FILE_INCLUDE_FILTER,
EnumSet.allOf(EventType.class));
this.autoContentCreationConfigProvider = autoContentCreationConfigProvider;
this.contentAutoCreationServiceProvider = contentAutoCreationServiceProvider;
}

private static Path getDataFeedDir(
final Provider<AutoContentCreationConfig> autoContentCreationConfigProvider,
final SimplePathCreator simplePathCreator) {

final AutoContentCreationConfig autoContentCreationConfig = autoContentCreationConfigProvider.get();
return NullSafe.get(
autoContentCreationConfig.getTemplateConfigDir(),
simplePathCreator::toAppPath);
}

@Override
protected void onInitialisation() {

}

@Override
protected void onEntryModify(final Path path) {

}

@Override
protected void onEntryCreate(final Path path) {

}

@Override
protected void onEntryDelete(final Path path) {

}

@Override
protected void onOverflow() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class AutoContentCreationConfig
@JsonProperty
private final String templatesExplorerPath;

@JsonProperty
private final String templateConfigDir;

@JsonProperty
private final String additionalGroupSuffix;

Expand All @@ -49,6 +52,7 @@ public AutoContentCreationConfig() {
.toString();
templatesExplorerPath = DocPath.fromParts(DEFAULT_DESTINATION_PATH_PART, DEFAULT_TEMPLATES_PATH_PART)
.toString();
templateConfigDir = "content_template_config";
additionalGroupSuffix = " (sandbox)";
createAsSubjectId = User.ADMINISTRATORS_GROUP_SUBJECT_ID;
createAsType = UserType.GROUP;
Expand All @@ -58,12 +62,14 @@ public AutoContentCreationConfig() {
public AutoContentCreationConfig(@JsonProperty("enabled") final boolean enabled,
@JsonProperty("destinationExplorerPath") final String destinationExplorerPath,
@JsonProperty("templatesExplorerPath") final String templatesExplorerPath,
@JsonProperty("templateConfigDir") final String templateConfigDir,
@JsonProperty("additionalGroupSuffix") final String additionalGroupSuffix,
@JsonProperty("createAsSubjectId") final String createAsSubjectId,
@JsonProperty("createAsType") final UserType createAsType) {
this.enabled = enabled;
this.destinationExplorerPath = destinationExplorerPath;
this.templatesExplorerPath = templatesExplorerPath;
this.templateConfigDir = templateConfigDir;
this.additionalGroupSuffix = additionalGroupSuffix;
this.createAsSubjectId = createAsSubjectId;
this.createAsType = createAsType;
Expand All @@ -73,6 +79,7 @@ private AutoContentCreationConfig(Builder builder) {
this.enabled = builder.enabled;
this.destinationExplorerPath = builder.destinationPath;
this.templatesExplorerPath = builder.templatesPath;
this.templateConfigDir = builder.templateConfigDir;
this.additionalGroupSuffix = builder.additionalGroupSuffix;
this.createAsSubjectId = builder.createAsSubjectId;
this.createAsType = builder.createAsType;
Expand Down Expand Up @@ -103,6 +110,12 @@ public String getTemplatesExplorerPath() {
return templatesExplorerPath;
}

@JsonPropertyDescription(
"The ")
public String getTemplateConfigDir() {
return templateConfigDir;
}

@JsonPropertyDescription(
"If set, when Stroom auto-creates a feed, it will create an addition user group with " +
"name '<system name><additionalGroupSuffix>'. This is in addition to creating a user group " +
Expand Down Expand Up @@ -157,6 +170,7 @@ public static class Builder {
private boolean enabled;
private String destinationPath;
private String templatesPath;
public String templateConfigDir;
public String additionalGroupSuffix;
public String createAsSubjectId;
public UserType createAsType;
Expand All @@ -176,6 +190,11 @@ public Builder templatesPath(String templatesPath) {
return this;
}

public Builder templateConfigDir(String templateConfigDir) {
this.templateConfigDir = templateConfigDir;
return this;
}

public Builder additionalGroupSuffix(String additionalGroupSuffix) {
this.additionalGroupSuffix = additionalGroupSuffix;
return this;
Expand All @@ -196,6 +215,7 @@ public Builder copy() {
.enabled(this.enabled)
.destinationPath(this.destinationPath)
.templatesPath(this.templatesPath)
.templateConfigDir(this.templateConfigDir)
.additionalGroupSuffix(this.additionalGroupSuffix)
.createAsSubjectId(this.createAsSubjectId)
.createAsType(this.createAsType);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package stroom.receive.common;

import stroom.docref.DocRef;
import stroom.util.NullSafe;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.util.Map;
import java.util.Objects;
import java.util.Set;

@JsonPropertyOrder(alphabetic = true)
public class ContentTemplates {

@JsonProperty
private final Set<ContentTemplate> contentTemplates;

@JsonCreator
public ContentTemplates(
@JsonProperty("contentTemplates") final Set<ContentTemplate> contentTemplates) {
this.contentTemplates = contentTemplates;
}

public Set<ContentTemplate> getContentTemplates() {
return contentTemplates;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ContentTemplates that = (ContentTemplates) o;
return Objects.equals(contentTemplates, that.contentTemplates);
}

@Override
public int hashCode() {
return Objects.hash(contentTemplates);
}

@Override
public String toString() {
return "ContentTemplates{" +
"contentTemplates=" + contentTemplates +
'}';
}

// --------------------------------------------------------------------------------


@JsonPropertyOrder(alphabetic = true)
public static class ContentTemplate {

@JsonProperty
private final Map<String, String> attributeMap;
@JsonProperty
private final TemplateType templateType;
@JsonProperty
private final DocRef pipeline;

@JsonCreator
public ContentTemplate(@JsonProperty("attributeMap") final Map<String, String> attributeMap,
@JsonProperty("templateType") final TemplateType templateType,
@JsonProperty("pipeline") final DocRef pipeline) {
this.attributeMap = Objects.requireNonNull(attributeMap);
this.templateType = Objects.requireNonNull(templateType);
this.pipeline = Objects.requireNonNull(pipeline);
}

/**
* A map of header arguments and their values
* that describe a certain shape of data that can
* be processed by pipeline.
*/
public Map<String, String> getAttributeMap() {
return attributeMap;
}

/**
* The nature of the content templating.
*/
public TemplateType getTemplateType() {
return templateType;
}

/**
* The pipeline to process the data with or to inherit from (depending
* on the value of templateType).
*/
public DocRef getPipeline() {
return pipeline;
}

public boolean hasAttribute(final String key, final String value) {
if (NullSafe.isNonBlankString(key)) {
return NullSafe.test(attributeMap.get(key), val -> Objects.equals(val, value));
} else {
return false;
}
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ContentTemplate that = (ContentTemplate) o;
return Objects.equals(attributeMap,
that.attributeMap) && templateType == that.templateType && Objects.equals(pipeline,
that.pipeline);
}

@Override
public int hashCode() {
return Objects.hash(attributeMap, templateType, pipeline);
}

@Override
public String toString() {
return "ContentTemplate{" +
"attributeMap=" + attributeMap +
", templateType=" + templateType +
", pipeline=" + pipeline +
'}';
}
}


// --------------------------------------------------------------------------------


public enum TemplateType {
/**
* Create a processor filter specific to the feed on an existing pipeline that
* is appropriate to the attributeMap values.
*/
PROCESSOR_FILTER,
/**
* Create a new pipeline (and associated filter specific to the feed) that inherits
* from an existing template pipeline that is appropriate to the attributeMap values.
*/
INHERIT_PIPELINE,
;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package stroom.receive.common;

import stroom.util.NullSafe;
import stroom.util.io.AbstractDirChangeMonitor;
import stroom.util.io.SimplePathCreator;
import stroom.util.json.JsonUtil;
import stroom.util.logging.LambdaLogger;
Expand Down Expand Up @@ -53,36 +54,36 @@ private static Path getDataFeedDir(final Provider<ReceiveDataConfig> receiveData
}

@Override
void onInitialisation() {
protected void onInitialisation() {
processAllFiles();
}

@Override
void onEntryModify(final Path path) {
protected void onEntryModify(final Path path) {
LOGGER.debug("onEntryModify - path: {}", path);
if (path != null) {
processFile(path);
}
}

@Override
void onEntryCreate(final Path path) {
protected void onEntryCreate(final Path path) {
LOGGER.debug("onEntryCreate - path: {}", path);
if (path != null) {
processFile(path);
}
}

@Override
void onEntryDelete(final Path path) {
protected void onEntryDelete(final Path path) {
LOGGER.debug("onEntryDelete - path: {}", path);
if (path != null) {
dataFeedKeyServiceProvider.get().removeKeysForFile(path);
}
}

@Override
void onOverflow() {
protected void onOverflow() {
LOGGER.debug("onOverflow");
processAllFiles();
}
Expand Down
Loading

0 comments on commit 7deb784

Please sign in to comment.