Skip to content

Commit

Permalink
Merge pull request #241 from valtech/feature/240-java21-support
Browse files Browse the repository at this point in the history
Feature/240 java21 support
  • Loading branch information
nhirrle authored Jan 1, 2025
2 parents 7575ba9 + b745e4e commit 8255fb8
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import javax.jcr.Session;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.event.jobs.JobManager;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
Expand Down Expand Up @@ -67,6 +68,9 @@ public class AecuCloudStartupService {
@Reference
private ServiceComponentRuntime serviceComponentRuntime;

@Reference
private JobManager jobManager;

private BundleContext bundleContext;

@Activate
Expand All @@ -91,6 +95,10 @@ protected void checkAndRunMigration() {
throw new IllegalStateException("Groovy extension services seem to be not bound");
}
Thread.sleep(1000L * WAIT_PERIOD * 2);
if (resourceResolver.getResource(AecuService.AECU_APPS_PATH_PREFIX) == null) {
LOGGER.info("AECU apps script path not found, not starting migration.");
return;
}
startAecuMigration();
} catch (InterruptedException e) {
LOGGER.error("Interrupted", e);
Expand Down Expand Up @@ -170,13 +178,7 @@ private boolean servicesAreOk() {
* Starts the AECU migration
*/
void startAecuMigration() {
try {
LOGGER.info("AECU migration started");
aecuService.executeWithInstallHookHistory(AecuService.AECU_APPS_PATH_PREFIX);
LOGGER.info("AECU migration finished");
} catch (AecuException ae) {
LOGGER.error("Error while executing AECU migration", ae);
}
jobManager.addJob(AecuStartupJobConsumer.JOB_TOPIC, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package de.valtech.aecu.startuphook;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.valtech.aecu.api.service.AecuException;
import de.valtech.aecu.api.service.AecuService;
import de.valtech.aecu.api.service.HistoryEntry;

import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer;


@Component(service=JobConsumer.class, property= {
JobConsumer.PROPERTY_TOPICS + "=" + AecuStartupJobConsumer.JOB_TOPIC
})
public class AecuStartupJobConsumer implements JobConsumer {

protected static final String JOB_TOPIC = "de/valtech/aecu/cloud/AecuStartupJobTopic";

private static final Logger LOGGER = LoggerFactory.getLogger(AecuStartupJobConsumer.class);

@Reference
private AecuService aecuService;


public JobResult process(final Job job) {
try {
LOGGER.info("AECU migration started");
HistoryEntry result = aecuService.executeWithInstallHookHistory(AecuService.AECU_APPS_PATH_PREFIX);
LOGGER.info("AECU migration finished with result "+result.getResult());
return JobResult.OK;
} catch (AecuException ae) {
LOGGER.error("Error while executing AECU migration", ae);
// Do not retry job, hence status CANCEL (=failed permanently) and not FAILED (=can be retried)
return JobResult.CANCEL;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@

import javax.jcr.Session;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.event.jobs.JobManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -68,9 +70,16 @@ public class AecuCloudStartupServiceTest {
@Mock
private Session session;

@Mock
private Resource resource;


@Mock
private HistoryEntry historyEntry;

@Mock
private JobManager jobManager;

@InjectMocks
@Spy
private AecuCloudStartupService startupService;
Expand All @@ -79,6 +88,8 @@ public class AecuCloudStartupServiceTest {
public void setUp() throws Exception {
when(resolverService.getAdminResourceResolver()).thenReturn(resolver);
when(resolver.adaptTo(Session.class)).thenReturn(session);
when(resolver.getResource(AecuService.AECU_APPS_PATH_PREFIX)).thenReturn(resource);
when(jobManager.addJob(anyString(), any())).thenReturn(null);
doReturn(true).when(session).hasPermission(anyString(), anyString());
doReturn(true).when(startupService).waitForServices();
}
Expand All @@ -89,7 +100,7 @@ public void testMigration_compositeNodeStore() throws Exception {

startupService.checkAndRunMigration();

verify(aecuService, times(1)).executeWithInstallHookHistory(AecuService.AECU_APPS_PATH_PREFIX);
verify(jobManager, times(1)).addJob(AecuStartupJobConsumer.JOB_TOPIC, null);
}

@Test
Expand All @@ -106,7 +117,7 @@ public void testMigration_noCompositeNodeStore() throws Exception {

startupService.checkAndRunMigration();

verify(aecuService, never()).executeWithInstallHookHistory(AecuService.AECU_APPS_PATH_PREFIX);
verify(jobManager, never()).addJob(AecuStartupJobConsumer.JOB_TOPIC, null);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="sling:OsgiConfig"

queue.name="AECU Cloud Startup Job Queue"
queue.priority="NORM"
queue.topics="de/valtech/aecu/cloud/AecuStartupJobTopic"
queue.type="ORDERED"
queue.keepJobs="true"
/>
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public void setup() throws AecuException {
String path = request.getParameter("searchPath");
List<Resource> entries = new ArrayList<>();

if (path != null && StringUtils.isNotEmpty(path) && (path.startsWith(AecuService.AECU_CONF_PATH_PREFIX)
|| path.startsWith(AecuService.AECU_VAR_PATH_PREFIX) || path.startsWith(AecuService.AECU_APPS_PATH_PREFIX))) {
if (path != null && StringUtils.isNotEmpty(path) ) {
List<String> allowedScripts = aecuService.getFiles(path);
ResourceResolver resourceResolver = request.getResourceResolver();
for (String scriptPath : allowedScripts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private List<String> findCandidates(ResourceResolver resolver, String path) thro
}
Resource resource = resolver.getResource(path);
if (resource == null) {
throw new AecuException("Path is invalid");
throw new AecuException("Path " + path + " is invalid");
}
List<String> candidates = new ArrayList<>();
if (isFolder(resource) && matchesRunmodes(resource.getName())) {
Expand Down Expand Up @@ -180,10 +180,10 @@ public ExecutionResult execute(String path, String data) throws AecuException {
try (ResourceResolver resolver = resolverService.getContentMigratorResourceResolver()) {
Resource resource = resolver.getResource(path);
if (resource == null) {
throw new AecuException("Path is invalid");
throw new AecuException("Path " + path + " invalid");
}
if (!isValidScriptName(resource.getName())) {
throw new AecuException("Invalid script name");
throw new AecuException("Invalid script name " + resource.getName());
}
return executeScript(resolver, path, data);
} catch (LoginException e) {
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,13 @@
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>4.0.9</version>
<version>4.0.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>be.orbinson.aem</groupId>
<artifactId>aem-groovy-console-api</artifactId>
<version>19.0.4</version>
<version>19.0.8</version>
<scope>provided</scope>
<!-- exclude dependencies -->
<exclusions>
Expand All @@ -546,7 +546,7 @@
<dependency>
<groupId>be.orbinson.aem</groupId>
<artifactId>aem-groovy-console-all</artifactId>
<version>19.0.3</version>
<version>19.0.8</version>
<type>zip</type>
<!-- exclude dependencies -->
<exclusions>
Expand Down

0 comments on commit 8255fb8

Please sign in to comment.