Skip to content

Commit

Permalink
Merge branch 'master' into add-data-source-support
Browse files Browse the repository at this point in the history
  • Loading branch information
dprzybyl committed Nov 2, 2023
2 parents 3fc6671 + 0653f43 commit a8b12a0
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public interface Script {
/**
* Get CRON expression
*/
String getCronExpression();
String getLaunchCronExpression();

/**
* Get last execution date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public Date getLaunchSchedule() {
}

@Override
public String getCronExpression() {
public String getLaunchCronExpression() {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@

import com.cognifide.apm.api.scripts.LaunchEnvironment;
import com.cognifide.apm.api.scripts.LaunchMode;
import com.cognifide.apm.core.endpoints.params.DateFormat;
import com.cognifide.apm.core.endpoints.params.FileName;
import com.cognifide.apm.core.endpoints.params.RequestParameter;
import com.cognifide.apm.core.scripts.LaunchMetadata;
import com.cognifide.apm.core.scripts.ScriptNode;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import javax.inject.Inject;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Model;
Expand Down Expand Up @@ -75,15 +74,14 @@ public class ScriptUploadForm {

@Inject
@RequestParameter(ScriptNode.APM_LAUNCH_SCHEDULE)
@DateFormat("yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime launchSchedule;
private OffsetDateTime launchSchedule;

@Inject
@RequestParameter(ScriptNode.APM_LAUNCH_HOOK)
private String cronExpression;
@RequestParameter(ScriptNode.APM_LAUNCH_CRON_EXPRESSION)
private String launchCronExpression;

public LaunchMetadata toLaunchMetadata() {
return new LaunchMetadata(launchEnabled, launchMode, launchEnvironment, launchRunModes, launchHook, launchSchedule, cronExpression);
return new LaunchMetadata(launchEnabled, launchMode, launchEnvironment, launchRunModes, launchHook, launchSchedule, launchCronExpression);
}

public String getFileName() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Map;
Expand Down Expand Up @@ -84,8 +84,8 @@ private Object getValue(SlingHttpServletRequest request, Class<?> type, String p
return BooleanUtils.toBoolean(parameterValue.getString());
} else if (type == InputStream.class) {
return toInputStream(parameterValue);
} else if (type == LocalDateTime.class) {
return toLocalDateTime(annotatedElement, parameterValue);
} else if (type == OffsetDateTime.class) {
return toOffsetDateTime(parameterValue);
} else if (type.isEnum()) {
return toEnum(type, parameterValue);
} else if (type == String[].class) {
Expand Down Expand Up @@ -120,11 +120,8 @@ private Map<String, String> extractParams(SlingHttpServletRequest request, Strin
));
}

private LocalDateTime toLocalDateTime(AnnotatedElement annotatedElement, org.apache.sling.api.request.RequestParameter parameterValue) {
String dateFormat = Optional.ofNullable(annotatedElement.getAnnotation(DateFormat.class))
.map(DateFormat::value)
.orElse(DateTimeFormatter.ISO_LOCAL_DATE_TIME.toString());
return LocalDateTime.parse(parameterValue.getString(), DateTimeFormatter.ofPattern(dateFormat));
private OffsetDateTime toOffsetDateTime(org.apache.sling.api.request.RequestParameter parameterValue) {
return OffsetDateTime.parse(parameterValue.getString(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public Date getLaunchSchedule() {
}

@Override
public String getCronExpression() {
public String getLaunchCronExpression() {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,13 @@ public void writeTo(Resource historyLogResource) throws IOException {
valueMap.put(HistoryEntryImpl.SCRIPT_PATH, filePath);
valueMap.put(HistoryEntryImpl.AUTHOR, author);
valueMap.put(HistoryEntryImpl.MODE, mode);
try (InputStream progressLogInput = IOUtils.toInputStream(progressLog, StandardCharsets.UTF_8)) {
valueMap.put(HistoryEntryImpl.PROGRESS_LOG, progressLogInput);
int logWarnStringSizeThreshold = Integer.getInteger("oak.repository.node.property.logWarnStringSizeThreshold", 102400);
if (progressLog.length() > logWarnStringSizeThreshold) {
try (InputStream progressLogInput = IOUtils.toInputStream(progressLog, StandardCharsets.UTF_8)) {
valueMap.put(HistoryEntryImpl.PROGRESS_LOG, progressLogInput);
}
} else {
valueMap.put(HistoryEntryImpl.PROGRESS_LOG, progressLog);
}
valueMap.put(HistoryEntryImpl.IS_RUN_SUCCESSFUL, isRunSuccessful);
valueMap.put(HistoryEntryImpl.EXECUTION_TIME, executionTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,37 @@

import com.cognifide.apm.api.scripts.LaunchEnvironment;
import com.cognifide.apm.api.scripts.LaunchMode;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import java.time.OffsetDateTime;

public class LaunchMetadata {

private final boolean executionEnabled;
private final boolean launchEnabled;

private final LaunchMode launchMode;

private final LaunchEnvironment launchEnvironment;

private final String[] launchRunModes;
private final String executionHook;
private final LocalDateTime executionSchedule;
private final String cronExpression;

public LaunchMetadata(boolean executionEnabled, LaunchMode launchMode, LaunchEnvironment launchEnvironment,
String[] launchRunModes, String executionHook, LocalDateTime executionSchedule, String cronExpression) {
this.executionEnabled = executionEnabled;
private final String launchHook;

private final OffsetDateTime launchSchedule;

private final String launchCronExpression;

public LaunchMetadata(boolean launchEnabled, LaunchMode launchMode, LaunchEnvironment launchEnvironment,
String[] launchRunModes, String launchHook, OffsetDateTime launchSchedule, String launchCronExpression) {
this.launchEnabled = launchEnabled;
this.launchMode = launchMode;
this.launchEnvironment = launchEnvironment;
this.launchRunModes = launchRunModes;
this.executionHook = executionHook;
this.executionSchedule = executionSchedule;
this.cronExpression = cronExpression;
this.launchHook = launchHook;
this.launchSchedule = launchSchedule;
this.launchCronExpression = launchCronExpression;
}

public boolean isExecutionEnabled() {
return executionEnabled;
public boolean isLaunchEnabled() {
return launchEnabled;
}

public LaunchMode getLaunchMode() {
Expand All @@ -62,23 +64,18 @@ public LaunchEnvironment getLaunchEnvironment() {
}

public String[] getLaunchRunModes() {
return Optional.ofNullable(launchRunModes)
.map(Arrays::stream)
.orElse(Stream.empty())
.filter(StringUtils::isNotBlank)
.distinct()
.toArray(String[]::new);
return launchRunModes;
}

public String getExecutionHook() {
return executionHook;
public String getLaunchHook() {
return launchHook;
}

public LocalDateTime getExecutionSchedule() {
return executionSchedule;
public OffsetDateTime getLaunchSchedule() {
return launchSchedule;
}

public String getCronExpression() {
return cronExpression;
public String getLaunchCronExpression() {
return launchCronExpression;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private static Predicate<Script> withSchedule() {
}

private static Predicate<Script> withCronExpression() {
return script -> script.getLaunchMode() == LaunchMode.ON_CRON_EXPRESSION && StringUtils.isNotEmpty(script.getCronExpression());
return script -> script.getLaunchMode() == LaunchMode.ON_CRON_EXPRESSION && StringUtils.isNotEmpty(script.getLaunchCronExpression());
}

private static Predicate<Script> enabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import javax.inject.Named;
import javax.jcr.RepositoryException;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
Expand Down Expand Up @@ -96,7 +95,7 @@ public class ScriptModel implements MutableScript {

@Inject
@Named(ScriptNode.APM_LAUNCH_CRON_EXPRESSION)
private String cronExpression;
private String launchCronExpression;

@Inject
@Named(ScriptNode.APM_LAST_EXECUTED)
Expand Down Expand Up @@ -171,8 +170,8 @@ public Date getLaunchSchedule() {
}

@Override
public String getCronExpression() {
return StringUtils.defaultString(cronExpression);
public String getLaunchCronExpression() {
return launchCronExpression;
}

@Override
Expand Down Expand Up @@ -284,7 +283,7 @@ public boolean equals(Object obj) {
&& Arrays.equals(launchRunModes, that.launchRunModes)
&& Objects.equals(launchHook, that.launchHook)
&& Objects.equals(launchSchedule, that.launchSchedule)
&& Objects.equals(cronExpression, that.cronExpression)
&& Objects.equals(launchCronExpression, that.launchCronExpression)
&& Objects.equals(checksum, that.checksum)
&& Objects.equals(verified, that.verified);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
import com.day.cq.commons.jcr.JcrConstants;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -108,13 +109,13 @@ private Script saveScript(FileDescriptor descriptor, LaunchMetadata launchMetada
contentNode.setProperty(JcrConstants.JCR_DATA, binary);
contentNode.setProperty(JcrConstants.JCR_ENCODING, SCRIPT_ENCODING.name());
fileNode.addMixin(ScriptNode.APM_SCRIPT);
fileNode.setProperty(ScriptNode.APM_LAUNCH_ENABLED, launchMetadata.isExecutionEnabled());
fileNode.setProperty(ScriptNode.APM_LAUNCH_ENABLED, launchMetadata.isLaunchEnabled());
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_MODE, launchMetadata.getLaunchMode());
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_ENVIRONMENT, launchMetadata.getLaunchEnvironment());
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_RUN_MODES, launchMetadata.getLaunchRunModes());
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_HOOK, launchMetadata.getExecutionHook());
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_SCHEDULE, launchMetadata.getExecutionSchedule());
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_CRON_EXPRESSION, launchMetadata.getCronExpression());
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_HOOK, launchMetadata.getLaunchHook());
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_SCHEDULE, launchMetadata.getLaunchSchedule());
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_CRON_EXPRESSION, launchMetadata.getLaunchCronExpression());
removeProperty(fileNode, ScriptNode.APM_LAST_EXECUTED);
JcrUtils.setLastModified(fileNode, Calendar.getInstance());
session.save();
Expand All @@ -128,12 +129,11 @@ private Script saveScript(FileDescriptor descriptor, LaunchMetadata launchMetada
private void setOrRemoveProperty(Node node, String name, Object value) throws RepositoryException {
if (value == null) {
removeProperty(node, name);
} else if (value instanceof LocalDateTime) {
LocalDateTime localDateTime = (LocalDateTime) value;
} else if (value instanceof OffsetDateTime) {
OffsetDateTime offsetDateTime = (OffsetDateTime) value;
Date date = Date.from(offsetDateTime.toInstant());
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(localDateTime.getYear(), localDateTime.getMonthValue() - 1, localDateTime.getDayOfMonth(),
localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond());
calendar.setTime(date);
node.setProperty(name, calendar);
} else if (value instanceof String[]) {
node.setProperty(name, (String[]) value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public class ScriptsResourceChangeListener implements ResourceChangeListener {
@Activate
public void activate(BundleContext bundleContext) {
registeredScripts = new HashMap<>();

SlingHelper.operateTraced(resolverProvider, resolver ->
scriptFinder.findAll(onScheduleOrCronExpression(runModesProvider), resolver)
.forEach(script -> registerScript(script, bundleContext))
Expand All @@ -99,26 +98,32 @@ public void onChange(List<ResourceChange> changes) {

SlingHelper.operateTraced(resolverProvider, resolver ->
changes.stream()
.filter(resourceChange -> StringUtils.endsWith(resourceChange.getPath(), Apm.FILE_EXT))
.forEach(resourceChange -> {
if (resourceChange.getType() == ResourceChange.ChangeType.ADDED) {
Script script = scriptFinder.find(resourceChange.getPath(), resolver);
.filter(change -> StringUtils.endsWith(change.getPath(), Apm.FILE_EXT))
.forEach(change -> {
if (change.getType() == ResourceChange.ChangeType.ADDED) {
Script script = scriptFinder.find(change.getPath(), resolver);
if (onScheduleOrCronExpression(runModesProvider).test(script)) {
registerScript(script, bundleContext);
}
} else if (resourceChange.getType() == ResourceChange.ChangeType.REMOVED) {
RegisterScript registeredScript = registeredScripts.get(resourceChange.getPath());
} else if (change.getType() == ResourceChange.ChangeType.REMOVED) {
RegisterScript registeredScript = registeredScripts.get(change.getPath());
if (registeredScript != null) {
registeredScript.registration.unregister();
registeredScripts.remove(resourceChange.getPath());
registeredScripts.remove(change.getPath());
}
} else if (resourceChange.getType() == ResourceChange.ChangeType.CHANGED) {
Script script = scriptFinder.find(resourceChange.getPath(), resolver);
RegisterScript registeredScript = registeredScripts.get(resourceChange.getPath());
if (onScheduleOrCronExpression(runModesProvider).test(script) && !Objects.equals(script, registeredScript.script)) {
} else if (change.getType() == ResourceChange.ChangeType.CHANGED) {
Script script = scriptFinder.find(change.getPath(), resolver);
RegisterScript registeredScript = registeredScripts.get(change.getPath());
if (registeredScript == null) {
if (onScheduleOrCronExpression(runModesProvider).test(script)) {
registerScript(script, bundleContext);
}
} else if (!Objects.equals(script, registeredScript.script)) {
registeredScript.registration.unregister();
registeredScripts.remove(resourceChange.getPath());
registerScript(script, bundleContext);
registeredScripts.remove(change.getPath());
if (onScheduleOrCronExpression(runModesProvider).test(script)) {
registerScript(script, bundleContext);
}
}
}
})
Expand All @@ -130,9 +135,10 @@ private void registerScript(Script script, BundleContext bundleContext) {
Dictionary<String, Object> dictionary = new Hashtable<>();
if (script.getLaunchMode() == LaunchMode.ON_SCHEDULE) {
SimpleDateFormat cronExpressionFormat = new SimpleDateFormat("s m H d M ? y");
dictionary.put("scheduler.expression", cronExpressionFormat.format(script.getLaunchSchedule()));
String cronExpression = cronExpressionFormat.format(script.getLaunchSchedule());
dictionary.put("scheduler.expression", cronExpression);
} else if (script.getLaunchMode() == LaunchMode.ON_CRON_EXPRESSION) {
dictionary.put("scheduler.expression", script.getCronExpression());
dictionary.put("scheduler.expression", script.getLaunchCronExpression());
}
ServiceRegistration<Runnable> registration = bundleContext.registerService(Runnable.class, service, dictionary);
registeredScripts.put(script.getPath(), new RegisterScript(script, registration));
Expand Down
Loading

0 comments on commit a8b12a0

Please sign in to comment.