Search in sources :

Example 1 with InjectVariableELResolver

use of com.walmartlabs.concord.runner.engine.el.InjectVariableELResolver in project concord by walmartlabs.

the class EngineFactory method create.

@SuppressWarnings("deprecation")
public Engine create(ProjectDefinition project, Path baseDir, Collection<String> activeProfiles, Set<String> metaVariables, EventConfiguration eventCfg) {
    Path attachmentsDir = baseDir.resolve(InternalConstants.Files.JOB_ATTACHMENTS_DIR_NAME);
    Path stateDir = attachmentsDir.resolve(InternalConstants.Files.JOB_STATE_DIR_NAME);
    Path eventsDir = stateDir.resolve("events");
    Path instancesDir = stateDir.resolve("instances");
    Path formsDir = stateDir.resolve(InternalConstants.Files.JOB_FORMS_DIR_NAME);
    try {
        Files.createDirectories(eventsDir);
        Files.createDirectories(instancesDir);
        Files.createDirectories(formsDir);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    ExpressionManager expressionManager = new DefaultExpressionManager(new String[] { InternalConstants.Context.CONTEXT_KEY, InternalConstants.Context.EXECUTION_CONTEXT_KEY }, new TaskResolver(taskRegistry), new InjectVariableELResolver()) {

        @Override
        protected ELResolver createResolver(ExecutionContext ctx) {
            CompositeELResolver cr = new CompositeELResolver();
            cr.add(new ExecutionContextVariableResolver(ctx));
            for (ELResolver r : resolvers) {
                cr.add(r);
            }
            return cr;
        }
    };
    FormStorage formStorage = new FileFormStorage(formsDir);
    // here we create a separate ContextFactory to avoid circular references
    // between ConcordFormService and ConcordExecutionContextFactory
    // TODO find a better way
    FormService formService = new ConcordFormService(new DefaultExecutionContextFactory(expressionManager), new NoopResumeHandler(), formStorage);
    ProtectedVarContext protectedVarContext = new ProtectedVarContext(PolicyEngineHolder.INSTANCE.getEngine());
    ConcordExecutionContextFactory contextFactory = new ConcordExecutionContextFactory(expressionManager, protectedVarContext, formService);
    EventStorage eventStorage = new FileEventStorage(eventsDir);
    PersistenceManager persistenceManager = new FilePersistenceManager(instancesDir);
    ProjectDefinitionAdapter adapter = new ProjectDefinitionAdapter(project, activeProfiles, baseDir);
    UserTaskHandler uth = new FormTaskHandler(contextFactory, adapter.forms(), formService);
    Configuration cfg = new Configuration();
    cfg.setInterpolateInputVariables(true);
    cfg.setWrapAllExceptionsAsBpmnErrors(true);
    cfg.setCopyAllCallActivityOutVariables(true);
    ElementEventProcessor eventProcessor;
    if (eventCfg.isRecordEvents()) {
        eventProcessor = new DefaultElementEventProcessor(apiClientFactory, adapter.processes());
    } else {
        eventProcessor = new NopElementEventProcessor();
    }
    ProcessOutVariables outVariables = new ProcessOutVariables(contextFactory);
    List<TaskInterceptor> taskInterceptors = new ArrayList<>();
    taskInterceptors.add(protectedVarContext);
    taskInterceptors.add(new TaskEventInterceptor(eventCfg, eventProcessor));
    taskInterceptors.add(new PolicyPreprocessor(baseDir));
    Engine engine = new EngineBuilder().withContextFactory(contextFactory).withLockManager(new NoopLockManager()).withExpressionManager(expressionManager).withDefinitionProvider(adapter.processes()).withTaskRegistry(taskRegistry).withJavaDelegateHandler(new JavaDelegateHandlerImpl(taskInterceptors)).withEventStorage(eventStorage).withPersistenceManager(persistenceManager).withUserTaskHandler(uth).withConfiguration(cfg).withListener(new ProcessOutVariablesListener(attachmentsDir, outVariables)).withListener(new VariablesSnapshotListener(stateDir)).withResourceResolver(new ResourceResolverImpl(baseDir)).build();
    engine.addInterceptor(new ProcessElementInterceptor(eventProcessor, new ProcessMetadataProcessor(apiClientFactory, metaVariables)));
    return engine;
}
Also used : NoopLockManager(io.takari.bpm.lock.NoopLockManager) Configuration(io.takari.bpm.Configuration) TaskResolver(com.walmartlabs.concord.runner.engine.el.TaskResolver) FormService(io.takari.bpm.form.FormService) ArrayList(java.util.ArrayList) InjectVariableELResolver(com.walmartlabs.concord.runner.engine.el.InjectVariableELResolver) DefaultExecutionContextFactory(io.takari.bpm.context.DefaultExecutionContextFactory) InjectVariableELResolver(com.walmartlabs.concord.runner.engine.el.InjectVariableELResolver) CompositeELResolver(javax.el.CompositeELResolver) ELResolver(javax.el.ELResolver) FormStorage(io.takari.bpm.form.FormStorage) UserTaskHandler(io.takari.bpm.task.UserTaskHandler) EngineBuilder(io.takari.bpm.EngineBuilder) Engine(io.takari.bpm.api.Engine) Path(java.nio.file.Path) DefaultExpressionManager(io.takari.bpm.el.DefaultExpressionManager) ExpressionManager(io.takari.bpm.el.ExpressionManager) FormTaskHandler(io.takari.bpm.form.FormTaskHandler) CompositeELResolver(javax.el.CompositeELResolver) PersistenceManager(io.takari.bpm.persistence.PersistenceManager) ExecutionContextVariableResolver(io.takari.bpm.el.ExecutionContextVariableResolver) IOException(java.io.IOException) VariablesSnapshotListener(com.walmartlabs.concord.runner.VariablesSnapshotListener) ExecutionContext(io.takari.bpm.api.ExecutionContext) DefaultExpressionManager(io.takari.bpm.el.DefaultExpressionManager) NoopResumeHandler(io.takari.bpm.form.DefaultFormService.NoopResumeHandler) EventStorage(io.takari.bpm.event.EventStorage)

Example 2 with InjectVariableELResolver

use of com.walmartlabs.concord.runner.engine.el.InjectVariableELResolver in project concord by walmartlabs.

the class PolicyPreprocessorTest method testPreprocess.

@Test
public void testPreprocess() throws Exception {
    ExpressionManager expressionManager = new DefaultExpressionManager(new String[] { InternalConstants.Context.CONTEXT_KEY, InternalConstants.Context.EXECUTION_CONTEXT_KEY }, new InjectVariableELResolver());
    Context ctx = new ConcordExecutionContextFactory.ConcordExecutionContext(null, expressionManager, new Variables(), null, null);
    ctx.setVariable("gatekeeperArtifacts", Arrays.asList("a", "b", "c"));
    Path workDir = Files.createTempDirectory("concord-test");
    Files.createDirectories(workDir.resolve(InternalConstants.Files.CONCORD_SYSTEM_DIR_NAME));
    // -- read original policy
    String originalPolicy;
    try (InputStream is = PolicyPreprocessorTest.class.getResourceAsStream("/com/walmartlabs/concord/runner/policy.json")) {
        ObjectMapper om = new ObjectMapper();
        originalPolicy = om.writeValueAsString(om.readValue(is, Map.class));
    }
    // -- write tmp policy
    Path policyFile = workDir.resolve(InternalConstants.Files.CONCORD_SYSTEM_DIR_NAME).resolve(InternalConstants.Files.POLICY_FILE_NAME);
    try (InputStream is = PolicyPreprocessorTest.class.getResourceAsStream("/com/walmartlabs/concord/runner/policy.json")) {
        Files.copy(is, policyFile, StandardCopyOption.REPLACE_EXISTING);
    }
    // ---
    PolicyPreprocessor pp = new PolicyPreprocessor(workDir);
    pp.preTask("ansible", null, ctx);
    String processedPolicy = new String(Files.readAllBytes(policyFile));
    assertEquals(EXPECTED_PROCESSED_POLICY, processedPolicy);
    // ---
    pp.postTask("ansible", null, ctx);
    String restoredPolicy = new String(Files.readAllBytes(policyFile));
    assertEquals(originalPolicy, restoredPolicy);
}
Also used : Context(com.walmartlabs.concord.sdk.Context) Variables(io.takari.bpm.api.Variables) Path(java.nio.file.Path) DefaultExpressionManager(io.takari.bpm.el.DefaultExpressionManager) ExpressionManager(io.takari.bpm.el.ExpressionManager) DefaultExpressionManager(io.takari.bpm.el.DefaultExpressionManager) InputStream(java.io.InputStream) InjectVariableELResolver(com.walmartlabs.concord.runner.engine.el.InjectVariableELResolver) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Aggregations

InjectVariableELResolver (com.walmartlabs.concord.runner.engine.el.InjectVariableELResolver)2 DefaultExpressionManager (io.takari.bpm.el.DefaultExpressionManager)2 ExpressionManager (io.takari.bpm.el.ExpressionManager)2 Path (java.nio.file.Path)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 VariablesSnapshotListener (com.walmartlabs.concord.runner.VariablesSnapshotListener)1 TaskResolver (com.walmartlabs.concord.runner.engine.el.TaskResolver)1 Context (com.walmartlabs.concord.sdk.Context)1 Configuration (io.takari.bpm.Configuration)1 EngineBuilder (io.takari.bpm.EngineBuilder)1 Engine (io.takari.bpm.api.Engine)1 ExecutionContext (io.takari.bpm.api.ExecutionContext)1 Variables (io.takari.bpm.api.Variables)1 DefaultExecutionContextFactory (io.takari.bpm.context.DefaultExecutionContextFactory)1 ExecutionContextVariableResolver (io.takari.bpm.el.ExecutionContextVariableResolver)1 EventStorage (io.takari.bpm.event.EventStorage)1 NoopResumeHandler (io.takari.bpm.form.DefaultFormService.NoopResumeHandler)1 FormService (io.takari.bpm.form.FormService)1 FormStorage (io.takari.bpm.form.FormStorage)1 FormTaskHandler (io.takari.bpm.form.FormTaskHandler)1