Skip to content

Commit ad22830

Browse files
committed
refactor
1 parent 62690ab commit ad22830

File tree

9 files changed

+52
-56
lines changed

9 files changed

+52
-56
lines changed

app/aem/api/src/main/java/com/cognifide/apm/api/scripts/Script.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public interface Script {
5353
/**
5454
* Get CRON expression
5555
*/
56-
String getCronExpression();
56+
String getLaunchCronExpression();
5757

5858
/**
5959
* Get last execution date

app/aem/api/src/main/java/com/cognifide/apm/api/scripts/TransientScript.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public Date getLaunchSchedule() {
8383
}
8484

8585
@Override
86-
public String getCronExpression() {
86+
public String getLaunchCronExpression() {
8787
return null;
8888
}
8989

app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptUploadForm.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ public class ScriptUploadForm {
7979
private LocalDateTime launchSchedule;
8080

8181
@Inject
82-
@RequestParameter(ScriptNode.APM_LAUNCH_HOOK)
83-
private String cronExpression;
82+
@RequestParameter(ScriptNode.APM_LAUNCH_CRON_EXPRESSION)
83+
private String launchCronExpression;
8484

8585
public LaunchMetadata toLaunchMetadata() {
86-
return new LaunchMetadata(launchEnabled, launchMode, launchEnvironment, launchRunModes, launchHook, launchSchedule, cronExpression);
86+
return new LaunchMetadata(launchEnabled, launchMode, launchEnvironment, launchRunModes, launchHook, launchSchedule, launchCronExpression);
8787
}
8888

8989
public String getFileName() {

app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ReferenceFinder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public Date getLaunchSchedule() {
209209
}
210210

211211
@Override
212-
public String getCronExpression() {
212+
public String getLaunchCronExpression() {
213213
return null;
214214
}
215215

app/aem/core/src/main/java/com/cognifide/apm/core/scripts/LaunchMetadata.java

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,36 @@
2323
import com.cognifide.apm.api.scripts.LaunchEnvironment;
2424
import com.cognifide.apm.api.scripts.LaunchMode;
2525
import java.time.LocalDateTime;
26-
import java.util.Arrays;
27-
import java.util.Optional;
28-
import java.util.stream.Stream;
29-
import org.apache.commons.lang3.StringUtils;
3026

3127
public class LaunchMetadata {
3228

33-
private final boolean executionEnabled;
29+
private final boolean launchEnabled;
30+
3431
private final LaunchMode launchMode;
32+
3533
private final LaunchEnvironment launchEnvironment;
34+
3635
private final String[] launchRunModes;
37-
private final String executionHook;
38-
private final LocalDateTime executionSchedule;
39-
private final String cronExpression;
4036

41-
public LaunchMetadata(boolean executionEnabled, LaunchMode launchMode, LaunchEnvironment launchEnvironment,
42-
String[] launchRunModes, String executionHook, LocalDateTime executionSchedule, String cronExpression) {
43-
this.executionEnabled = executionEnabled;
37+
private final String launchHook;
38+
39+
private final LocalDateTime launchSchedule;
40+
41+
private final String launchCronExpression;
42+
43+
public LaunchMetadata(boolean launchEnabled, LaunchMode launchMode, LaunchEnvironment launchEnvironment,
44+
String[] launchRunModes, String launchHook, LocalDateTime launchSchedule, String launchCronExpression) {
45+
this.launchEnabled = launchEnabled;
4446
this.launchMode = launchMode;
4547
this.launchEnvironment = launchEnvironment;
4648
this.launchRunModes = launchRunModes;
47-
this.executionHook = executionHook;
48-
this.executionSchedule = executionSchedule;
49-
this.cronExpression = cronExpression;
49+
this.launchHook = launchHook;
50+
this.launchSchedule = launchSchedule;
51+
this.launchCronExpression = launchCronExpression;
5052
}
5153

52-
public boolean isExecutionEnabled() {
53-
return executionEnabled;
54+
public boolean isLaunchEnabled() {
55+
return launchEnabled;
5456
}
5557

5658
public LaunchMode getLaunchMode() {
@@ -62,23 +64,18 @@ public LaunchEnvironment getLaunchEnvironment() {
6264
}
6365

6466
public String[] getLaunchRunModes() {
65-
return Optional.ofNullable(launchRunModes)
66-
.map(Arrays::stream)
67-
.orElse(Stream.empty())
68-
.filter(StringUtils::isNotBlank)
69-
.distinct()
70-
.toArray(String[]::new);
67+
return launchRunModes;
7168
}
7269

73-
public String getExecutionHook() {
74-
return executionHook;
70+
public String getLaunchHook() {
71+
return launchHook;
7572
}
7673

77-
public LocalDateTime getExecutionSchedule() {
78-
return executionSchedule;
74+
public LocalDateTime getLaunchSchedule() {
75+
return launchSchedule;
7976
}
8077

81-
public String getCronExpression() {
82-
return cronExpression;
78+
public String getLaunchCronExpression() {
79+
return launchCronExpression;
8380
}
8481
}

app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptFilters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private static Predicate<Script> withSchedule() {
9898
}
9999

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

104104
private static Predicate<Script> enabled() {

app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptModel.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import javax.inject.Named;
4545
import javax.jcr.RepositoryException;
4646
import org.apache.commons.lang3.BooleanUtils;
47-
import org.apache.commons.lang3.StringUtils;
4847
import org.apache.sling.api.resource.ModifiableValueMap;
4948
import org.apache.sling.api.resource.PersistenceException;
5049
import org.apache.sling.api.resource.Resource;
@@ -96,7 +95,7 @@ public class ScriptModel implements MutableScript {
9695

9796
@Inject
9897
@Named(ScriptNode.APM_LAUNCH_CRON_EXPRESSION)
99-
private String cronExpression;
98+
private String launchCronExpression;
10099

101100
@Inject
102101
@Named(ScriptNode.APM_LAST_EXECUTED)
@@ -171,8 +170,8 @@ public Date getLaunchSchedule() {
171170
}
172171

173172
@Override
174-
public String getCronExpression() {
175-
return StringUtils.defaultString(cronExpression);
173+
public String getLaunchCronExpression() {
174+
return launchCronExpression;
176175
}
177176

178177
@Override
@@ -284,7 +283,7 @@ public boolean equals(Object obj) {
284283
&& Arrays.equals(launchRunModes, that.launchRunModes)
285284
&& Objects.equals(launchHook, that.launchHook)
286285
&& Objects.equals(launchSchedule, that.launchSchedule)
287-
&& Objects.equals(cronExpression, that.cronExpression)
286+
&& Objects.equals(launchCronExpression, that.launchCronExpression)
288287
&& Objects.equals(checksum, that.checksum)
289288
&& Objects.equals(verified, that.verified);
290289
}

app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptStorageImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ private Script saveScript(FileDescriptor descriptor, LaunchMetadata launchMetada
108108
contentNode.setProperty(JcrConstants.JCR_DATA, binary);
109109
contentNode.setProperty(JcrConstants.JCR_ENCODING, SCRIPT_ENCODING.name());
110110
fileNode.addMixin(ScriptNode.APM_SCRIPT);
111-
fileNode.setProperty(ScriptNode.APM_LAUNCH_ENABLED, launchMetadata.isExecutionEnabled());
111+
fileNode.setProperty(ScriptNode.APM_LAUNCH_ENABLED, launchMetadata.isLaunchEnabled());
112112
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_MODE, launchMetadata.getLaunchMode());
113113
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_ENVIRONMENT, launchMetadata.getLaunchEnvironment());
114114
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_RUN_MODES, launchMetadata.getLaunchRunModes());
115-
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_HOOK, launchMetadata.getExecutionHook());
116-
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_SCHEDULE, launchMetadata.getExecutionSchedule());
117-
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_CRON_EXPRESSION, launchMetadata.getCronExpression());
115+
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_HOOK, launchMetadata.getLaunchHook());
116+
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_SCHEDULE, launchMetadata.getLaunchSchedule());
117+
setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_CRON_EXPRESSION, launchMetadata.getLaunchCronExpression());
118118
removeProperty(fileNode, ScriptNode.APM_LAST_EXECUTED);
119119
JcrUtils.setLastModified(fileNode, Calendar.getInstance());
120120
session.save();

app/aem/core/src/main/java/com/cognifide/apm/core/services/ScriptsResourceChangeListener.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,25 +99,25 @@ public void onChange(List<ResourceChange> changes) {
9999

100100
SlingHelper.operateTraced(resolverProvider, resolver ->
101101
changes.stream()
102-
.filter(resourceChange -> StringUtils.endsWith(resourceChange.getPath(), Apm.FILE_EXT))
103-
.forEach(resourceChange -> {
104-
if (resourceChange.getType() == ResourceChange.ChangeType.ADDED) {
105-
Script script = scriptFinder.find(resourceChange.getPath(), resolver);
102+
.filter(change -> StringUtils.endsWith(change.getPath(), Apm.FILE_EXT))
103+
.forEach(change -> {
104+
if (change.getType() == ResourceChange.ChangeType.ADDED) {
105+
Script script = scriptFinder.find(change.getPath(), resolver);
106106
if (onScheduleOrCronExpression(runModesProvider).test(script)) {
107107
registerScript(script, bundleContext);
108108
}
109-
} else if (resourceChange.getType() == ResourceChange.ChangeType.REMOVED) {
110-
RegisterScript registeredScript = registeredScripts.get(resourceChange.getPath());
109+
} else if (change.getType() == ResourceChange.ChangeType.REMOVED) {
110+
RegisterScript registeredScript = registeredScripts.get(change.getPath());
111111
if (registeredScript != null) {
112112
registeredScript.registration.unregister();
113-
registeredScripts.remove(resourceChange.getPath());
113+
registeredScripts.remove(change.getPath());
114114
}
115-
} else if (resourceChange.getType() == ResourceChange.ChangeType.CHANGED) {
116-
Script script = scriptFinder.find(resourceChange.getPath(), resolver);
117-
RegisterScript registeredScript = registeredScripts.get(resourceChange.getPath());
115+
} else if (change.getType() == ResourceChange.ChangeType.CHANGED) {
116+
Script script = scriptFinder.find(change.getPath(), resolver);
117+
RegisterScript registeredScript = registeredScripts.get(change.getPath());
118118
if (onScheduleOrCronExpression(runModesProvider).test(script) && !Objects.equals(script, registeredScript.script)) {
119119
registeredScript.registration.unregister();
120-
registeredScripts.remove(resourceChange.getPath());
120+
registeredScripts.remove(change.getPath());
121121
registerScript(script, bundleContext);
122122
}
123123
}
@@ -132,7 +132,7 @@ private void registerScript(Script script, BundleContext bundleContext) {
132132
SimpleDateFormat cronExpressionFormat = new SimpleDateFormat("s m H d M ? y");
133133
dictionary.put("scheduler.expression", cronExpressionFormat.format(script.getLaunchSchedule()));
134134
} else if (script.getLaunchMode() == LaunchMode.ON_CRON_EXPRESSION) {
135-
dictionary.put("scheduler.expression", script.getCronExpression());
135+
dictionary.put("scheduler.expression", script.getLaunchCronExpression());
136136
}
137137
ServiceRegistration<Runnable> registration = bundleContext.registerService(Runnable.class, service, dictionary);
138138
registeredScripts.put(script.getPath(), new RegisterScript(script, registration));

0 commit comments

Comments
 (0)