Search in sources :

Example 1 with TaskContext

use of org.infinispan.tasks.TaskContext in project infinispan by infinispan.

the class DistributedServerTask method prepareContext.

private TaskContext prepareContext(EmbeddedCacheManager embeddedCacheManager, Cache<Object, Object> cache, Marshaller marshaller) {
    TaskContext context = new TaskContext();
    context.cacheManager(embeddedCacheManager);
    Map<String, String> params = parameters.stream().collect(Collectors.toMap(p -> p.key, p -> p.value));
    context.parameters(params);
    MediaType type = MediaType.APPLICATION_OBJECT;
    if (cache != null)
        context.cache(cache.getAdvancedCache().withMediaType(type, type));
    if (marshaller != null)
        context.marshaller(marshaller);
    return context;
}
Also used : CacheException(org.infinispan.commons.CacheException) GlobalComponentRegistry(org.infinispan.factories.GlobalComponentRegistry) TaskContext(org.infinispan.tasks.TaskContext) StreamingMarshaller(org.infinispan.commons.marshall.StreamingMarshaller) Cache(org.infinispan.Cache) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ProtoStreamTypeIds(org.infinispan.commons.marshall.ProtoStreamTypeIds) ArrayList(java.util.ArrayList) ProtoFactory(org.infinispan.protostream.annotations.ProtoFactory) List(java.util.List) MediaType(org.infinispan.commons.dataconversion.MediaType) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) Map(java.util.Map) ProtoTypeId(org.infinispan.protostream.annotations.ProtoTypeId) ProtoField(org.infinispan.protostream.annotations.ProtoField) Marshaller(org.infinispan.commons.marshall.Marshaller) TaskContext(org.infinispan.tasks.TaskContext) MediaType(org.infinispan.commons.dataconversion.MediaType)

Example 2 with TaskContext

use of org.infinispan.tasks.TaskContext in project infinispan by infinispan.

the class TaskRequestProcessor method exec.

public void exec(HotRodHeader header, Subject subject, String taskName, Map<String, byte[]> taskParams) {
    TaskContext taskContext = new TaskContext().parameters(taskParams).subject(subject);
    if (!header.cacheName.isEmpty() || server.hasDefaultCache()) {
        AdvancedCache<byte[], byte[]> cache = server.cache(server.getCacheInfo(header), header, subject);
        taskContext.cache(cache);
    }
    taskManager.runTask(taskName, taskContext).whenComplete((result, throwable) -> handleExec(header, result, throwable));
}
Also used : TaskContext(org.infinispan.tasks.TaskContext)

Example 3 with TaskContext

use of org.infinispan.tasks.TaskContext in project infinispan by infinispan.

the class SecureScriptingTaskManagerTest method testTask.

public void testTask() throws Exception {
    Security.doAs(PHEIDIPPIDES, new PrivilegedExceptionAction<Void>() {

        @Override
        public Void run() throws Exception {
            String result = CompletionStages.join(taskManager.runTask(SCRIPT_NAME, new TaskContext().addParameter("a", "a")));
            assertEquals("a", result);
            return null;
        }
    });
    List<Task> tasks = taskManager.getTasks();
    assertEquals(1, tasks.size());
    ScriptTask scriptTask = (ScriptTask) tasks.get(0);
    assertEquals(SCRIPT_NAME, scriptTask.getName());
    assertEquals(TaskExecutionMode.ONE_NODE, scriptTask.getExecutionMode());
    assertEquals("Script", scriptTask.getType());
}
Also used : ScriptTask(org.infinispan.scripting.impl.ScriptTask) Task(org.infinispan.tasks.Task) TaskContext(org.infinispan.tasks.TaskContext) ScriptTask(org.infinispan.scripting.impl.ScriptTask) CommonsTestingUtil.loadFileAsString(org.infinispan.commons.test.CommonsTestingUtil.loadFileAsString)

Example 4 with TaskContext

use of org.infinispan.tasks.TaskContext in project infinispan by infinispan.

the class SecureScriptingTest method testSimpleScriptWithEXECPermissionsRightRole.

public void testSimpleScriptWithEXECPermissionsRightRole() throws Exception {
    String result = Security.doAs(PHEIDIPPIDES, (PrivilegedExceptionAction<String>) () -> CompletionStages.join(scriptingManager.runScript("testRole.js", new TaskContext().addParameter("a", "a"))));
    assertEquals("a", result);
}
Also used : TaskContext(org.infinispan.tasks.TaskContext)

Example 5 with TaskContext

use of org.infinispan.tasks.TaskContext in project infinispan by infinispan.

the class SecureScriptingTest method testSimpleScriptWithEXECPermissionsWrongRole.

@Test(expectedExceptions = PrivilegedActionException.class)
public void testSimpleScriptWithEXECPermissionsWrongRole() throws Exception {
    String result = Security.doAs(RUNNER, (PrivilegedExceptionAction<String>) () -> CompletionStages.join(scriptingManager.runScript("testRole.js", new TaskContext().addParameter("a", "a"))));
    assertEquals("a", result);
}
Also used : TaskContext(org.infinispan.tasks.TaskContext) Test(org.testng.annotations.Test)

Aggregations

TaskContext (org.infinispan.tasks.TaskContext)29 CommonsTestingUtil.loadFileAsString (org.infinispan.commons.test.CommonsTestingUtil.loadFileAsString)10 Test (org.testng.annotations.Test)9 Cache (org.infinispan.Cache)8 ScriptingUtils.getScriptingManager (org.infinispan.scripting.utils.ScriptingUtils.getScriptingManager)6 InputStream (java.io.InputStream)4 PrivilegedActionException (java.security.PrivilegedActionException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)4 TaskManager (org.infinispan.tasks.TaskManager)4 Map (java.util.Map)3 CompletionStage (java.util.concurrent.CompletionStage)3 MediaType (org.infinispan.commons.dataconversion.MediaType)3 Task (org.infinispan.tasks.Task)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 EncoderRegistry (org.infinispan.marshall.core.EncoderRegistry)2 NettyRestResponse (org.infinispan.rest.NettyRestResponse)2