Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dprzybyl committed Nov 2, 2023
1 parent 62690ab commit ad22830
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 56 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 @@ -79,11 +79,11 @@ public class ScriptUploadForm {
private LocalDateTime 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
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,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 @@ -23,34 +23,36 @@
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;

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 LocalDateTime launchSchedule;

private final String launchCronExpression;

public LaunchMetadata(boolean launchEnabled, LaunchMode launchMode, LaunchEnvironment launchEnvironment,
String[] launchRunModes, String launchHook, LocalDateTime 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 LocalDateTime 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 @@ -108,13 +108,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,25 @@ 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());
} else if (change.getType() == ResourceChange.ChangeType.CHANGED) {
Script script = scriptFinder.find(change.getPath(), resolver);
RegisterScript registeredScript = registeredScripts.get(change.getPath());
if (onScheduleOrCronExpression(runModesProvider).test(script) && !Objects.equals(script, registeredScript.script)) {
registeredScript.registration.unregister();
registeredScripts.remove(resourceChange.getPath());
registeredScripts.remove(change.getPath());
registerScript(script, bundleContext);
}
}
Expand All @@ -132,7 +132,7 @@ private void registerScript(Script script, BundleContext bundleContext) {
SimpleDateFormat cronExpressionFormat = new SimpleDateFormat("s m H d M ? y");
dictionary.put("scheduler.expression", cronExpressionFormat.format(script.getLaunchSchedule()));
} 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

0 comments on commit ad22830

Please sign in to comment.