Skip to content

Commit

Permalink
chore(runner): extract existing java project
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Velickovic authored and sabberworm committed Sep 26, 2024
1 parent 05d3009 commit 69fc651
Show file tree
Hide file tree
Showing 55 changed files with 4,068 additions and 0 deletions.
86 changes: 86 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,87 @@
# Created by https://www.gitignore.io/api/eclipse,java,maven

### Eclipse ###
*.pydevproject
.metadata
.gradle
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath

# Eclipse Core
.project

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# JDT-specific (Eclipse Java Development Tools)
.classpath

# Java annotation processor (APT)
.factorypath

# PDT-specific
.buildpath

# sbteclipse plugin
.target

# TeXlipse plugin
.texlipse


### Java ###
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*


### Maven ###
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties

### Gradle ###
build/

### NPM ###
node_modules/
.npmrc

### Vault ###
.vlt

### IntelliJ ###
!.idea
.idea/*
!.idea/codeStyles

*.iml
/gradle.properties
53 changes: 53 additions & 0 deletions src/main/java/com/swisscom/aem/tools/jcrhopper/ActionRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.swisscom.aem.tools.jcrhopper;

import javax.jcr.Node;
import javax.jcr.Session;

import org.apache.commons.lang3.StringUtils;

import com.swisscom.aem.tools.jcrhopper.api.PortalScriptContext;
import com.swisscom.aem.tools.jcrhopper.impl.AbstractPortalScript;
import com.swisscom.aem.tools.jcrhopper.impl.LogLevel;
import com.swisscom.aem.tools.jcrhopper.pipeline.Pipeline;
import com.swisscom.aem.tools.jcrhopper.pipeline.PipelineContext;


public class ActionRunner extends AbstractPortalScript {

private static final String PIPELINE_PARAM = "pipeline";

/**
* Constructor.
*
* @param context the context
*/
public ActionRunner(PortalScriptContext context) {
super(context);
}

@Override
public void doProcess() throws Exception {
final String json = getJsonToProcess();
final Pipeline pipeline = Pipeline.fromJson(json);
final Session session = getSession();

final Node root = session.getRootNode();

final LogLevel logLevel = LogLevel.fromString(getParameter("log"));
pipeline.run(root, new PipelineContext(this, getContext().getParameters(), logLevel, session, isDryRun()));

if (!isDryRun()) {
session.save();
}
}

private String getJsonToProcess() {
final String json = getParameter(PIPELINE_PARAM);

if (StringUtils.isBlank(json)) {
throw new IllegalArgumentException(String.format("Missing parameter '%s'", PIPELINE_PARAM));
}

return json;
}
}
80 changes: 80 additions & 0 deletions src/main/java/com/swisscom/aem/tools/jcrhopper/MyTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.swisscom.aem.tools.jcrhopper;

import java.util.concurrent.ConcurrentSkipListSet;

import lombok.extern.slf4j.Slf4j;

/**
* Cancel the (Scheduled) task(s) registered under it's classname.
* <p>
* If the implementation is prepared in the framework as in ScheduledTask, you can use the cancellation of tasks as follows.
*/
@Slf4j
public final class MyTask {
/**
* The ThreadLocal variable is used to provide the classname for the isCancelled() method for developer's convenience.
*/
public static final ThreadLocal<String> CLASSNAME = ThreadLocal.withInitial(() -> null);

private static final ConcurrentSkipListSet<String> TASKS = new ConcurrentSkipListSet<>();

private MyTask() {
// Private constructor to prevent instantiation.
}


/**
* @return true if the task was canceled, else false.
*/
public static boolean isCancelled() {
if (getName() == null) {
throw new IllegalArgumentException("ThreadLocal classname is not setup for thread cancellation.");
}
return TASKS.contains(getName());
}

/**
* Send cancellation request to the tasks registered under their classname.
* <p>
* NOTE: all instances of the same Class (tasks) are requested to cancel with this call.
*
* @param name The fully qualified classname (e.g. my.package.MyScheduledTask)
*/
public static void cancel(String name) {
TASKS.add(name);
}

/**
* Reset cancellation state to enable next cancellation request.
*
* @param name The fully qualified classname of Task to reset.
*/
public static void resetCancellation(String name) {
TASKS.remove(name);
}

/**
* Lookup the classname from ThreadLocal for the isCancelled() method if set-up in the framework.
*
* @return the classname
*/
public static String getName() {
return CLASSNAME.get();
}

/**
* Remove the classname from ThreadLocal.
*/
public static void unregisterTaskName() {
CLASSNAME.remove();
}

/**
* Set the classname on ThreadLocal to enable the calling of isCancelled() without passing the classname.
*
* @param name The fully qualified classname of the task to be canceled.
*/
public static void registerTaskName(String name) {
CLASSNAME.set(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.swisscom.aem.tools.jcrhopper.api;

/**
* The PortalScript interface defines scripts which can be run from the PortalScriptServlet. A constructor with the
* following interface is needed to make the dynamic class instantiation possible:
* <p>
* public ConcretePortalScript(PortalScriptContext context){}
* </p>
*
*
*/
public interface PortalScript {

/**
* Subclasses must implement this method to do the actual work.
*/
void process() throws Exception;

/**
* Returns the maximum expected duration for this script. If the duration exceeds the expected time it will be
* logged in the CQ task log. Subclasses must override this method and provide a sensible value.
*
* @return Expected time in milliseconds
*/
long getMaxDurationInMs();

/**
* @return the MIME Content-Type for this script’s response
*/
String getResponseContentType();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.swisscom.aem.tools.jcrhopper.api;

import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.request.RequestParameterMap;
import org.apache.sling.api.resource.ResourceResolver;

import com.day.cq.wcm.api.PageManager;

/**
* The context for all portal scripts.
*
*
*/
public interface PortalScriptContext {

/**
* Returns the output writer. All messages are written to
* <ul>
* <li>Client (browser/cURL)</li>
* <li>Log file</li>
* </ul>
*
* @return Output writer
*/
PortalScriptOutputWriter getOutputWriter();

/**
* Returns the parameter map. All request parameters can be obtained from this map.
*
* @return Parameter map
*/
RequestParameterMap getParameters();

/**
* Returns a parameter for the given parameter name.
*
* @param name Parameter name.
* @return Parameter value
*/
String getParameter(String name);

/**
* Returns the request parameter for the given name. This can be used if the client needs access to a POSTed file.
* <p>
* <pre>
*
* RequestParameter fileParameter = getRequestParameter(&quot;file&quot;);
* BufferedReader in = new BufferedReader(new InputStreamReader(fileParameter.getInputStream()));
* </pre>
*
* @param name Request parameter name
* @return Request parameter
*/
RequestParameter getRequestParameter(String name);

/**
* Returns the portal script servlet URL.
*
* @return Portal script servlet URL
*/
String getPortalScriptServletUrl();

/**
* Returns the resource resolver which is attached with the current session.
*
* @return Resource Resolver
*/
ResourceResolver getResourceResolver();

/**
* Returns the page manager which is attached with the current session.
*
* @return PageManager
*/
PageManager getPageManager();
}
Loading

0 comments on commit 69fc651

Please sign in to comment.