Search in sources :

Example 16 with NamedObjectModel

use of org.kie.internal.runtime.conf.NamedObjectModel in project jbpm by kiegroup.

the class KModuleDeploymentService method boostrapRuntimeEnvironmentBuilder.

/**
 * This creates and fills a {@link RuntimeEnvironmentBuilder} instance, which is later used when creating services.
 * </p>
 * A lot of the logic here is used to process the information in the {@link DeploymentDescriptor} instance, which is
 * part of the {@link DeploymentUnit}.
 *
 * @param deploymentUnit The {@link KModuleDeploymentUnit}, which is filled by the method
 * @param deployedUnit The {@link DeployedUnit}, which is also filled by the method
 * @param kieContainer The {@link KieContainer}, which contains information needed to fill the above two arguments
 * @param mode The {@link MergeMode} used to resolve conflicts in the {@link DeploymentDescriptor}.
 * @return A {@link RuntimeEnvironmentBuilder} instance ready for use
 */
protected RuntimeEnvironmentBuilder boostrapRuntimeEnvironmentBuilder(KModuleDeploymentUnit deploymentUnit, DeployedUnit deployedUnit, KieContainer kieContainer, MergeMode mode) {
    DeploymentDescriptor descriptor = deploymentUnit.getDeploymentDescriptor();
    if (descriptor == null || ((DeploymentDescriptorImpl) descriptor).isEmpty()) {
        // skip empty descriptors as its default can override settings
        DeploymentDescriptorManager descriptorManager = new DeploymentDescriptorManager("org.jbpm.domain");
        List<DeploymentDescriptor> descriptorHierarchy = descriptorManager.getDeploymentDescriptorHierarchy(kieContainer);
        descriptor = merger.merge(descriptorHierarchy, mode);
        deploymentUnit.setDeploymentDescriptor(descriptor);
    } else if (descriptor != null && !deploymentUnit.isDeployed()) {
        DeploymentDescriptorManager descriptorManager = new DeploymentDescriptorManager("org.jbpm.domain");
        List<DeploymentDescriptor> descriptorHierarchy = descriptorManager.getDeploymentDescriptorHierarchy(kieContainer);
        descriptorHierarchy.add(0, descriptor);
        descriptor = merger.merge(descriptorHierarchy, mode);
        deploymentUnit.setDeploymentDescriptor(descriptor);
    }
    // first set on unit the strategy
    deploymentUnit.setStrategy(descriptor.getRuntimeStrategy());
    // setting up runtime environment via builder
    RuntimeEnvironmentBuilder builder = null;
    if (descriptor.getPersistenceMode() == PersistenceMode.NONE) {
        builder = RuntimeEnvironmentBuilder.Factory.get().newDefaultInMemoryBuilder();
    } else {
        builder = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder();
    }
    // populate various properties of the builder
    EntityManagerFactory emf = EntityManagerFactoryManager.get().getOrCreate(descriptor.getPersistenceUnit());
    builder.entityManagerFactory(emf);
    Map<String, Object> contaxtParams = new HashMap<String, Object>();
    contaxtParams.put("entityManagerFactory", emf);
    contaxtParams.put("classLoader", kieContainer.getClassLoader());
    // process object models that are globally configured (environment entries, session configuration)
    for (NamedObjectModel model : descriptor.getEnvironmentEntries()) {
        Object entry = getInstanceFromModel(model, kieContainer, contaxtParams);
        builder.addEnvironmentEntry(model.getName(), entry);
    }
    for (NamedObjectModel model : descriptor.getConfiguration()) {
        Object entry = getInstanceFromModel(model, kieContainer, contaxtParams);
        builder.addConfiguration(model.getName(), (String) entry);
    }
    ObjectMarshallingStrategy[] mStrategies = new ObjectMarshallingStrategy[descriptor.getMarshallingStrategies().size() + 1];
    int index = 0;
    for (ObjectModel model : descriptor.getMarshallingStrategies()) {
        Object strategy = getInstanceFromModel(model, kieContainer, contaxtParams);
        mStrategies[index] = (ObjectMarshallingStrategy) strategy;
        index++;
    }
    // lastly add the main default strategy
    mStrategies[index] = new SerializablePlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT);
    builder.addEnvironmentEntry(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, mStrategies);
    builder.addEnvironmentEntry("KieDeploymentDescriptor", descriptor);
    builder.addEnvironmentEntry("KieContainer", kieContainer);
    if (executorService != null) {
        builder.addEnvironmentEntry("ExecutorService", executorService);
    }
    if (identityProvider != null) {
        builder.addEnvironmentEntry(EnvironmentName.IDENTITY_PROVIDER, identityProvider);
    }
    // populate all assets with roles for this deployment unit
    List<String> requiredRoles = descriptor.getRequiredRoles(DeploymentDescriptor.TYPE_VIEW);
    if (requiredRoles != null && !requiredRoles.isEmpty()) {
        for (DeployedAsset desc : deployedUnit.getDeployedAssets()) {
            if (desc instanceof ProcessAssetDesc) {
                ((ProcessAssetDesc) desc).setRoles(requiredRoles);
            }
        }
    }
    // Classes 3: classes added from descriptor
    List<String> remoteableClasses = descriptor.getClasses();
    if (remoteableClasses != null && !remoteableClasses.isEmpty()) {
        for (String className : remoteableClasses) {
            Class descriptorClass = null;
            try {
                descriptorClass = kieContainer.getClassLoader().loadClass(className);
                logger.debug("Loaded {} into the classpath from deployment descriptor {}", className, kieContainer.getReleaseId().toExternalForm());
            } catch (ClassNotFoundException cnfe) {
                throw new IllegalArgumentException("Class " + className + " not found in the project");
            } catch (NoClassDefFoundError e) {
                throw new IllegalArgumentException("Class " + className + " not found in the project");
            }
            addClassToDeployedUnit(descriptorClass, (DeployedUnitImpl) deployedUnit);
        }
    }
    return builder;
}
Also used : SerializablePlaceholderResolverStrategy(org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy) ObjectModel(org.kie.internal.runtime.conf.ObjectModel) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) HashMap(java.util.HashMap) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) DeployedAsset(org.jbpm.services.api.model.DeployedAsset) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) DeploymentDescriptorManager(org.jbpm.runtime.manager.impl.deploy.DeploymentDescriptorManager) RuntimeEnvironmentBuilder(org.kie.api.runtime.manager.RuntimeEnvironmentBuilder) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) ProcessAssetDesc(org.jbpm.kie.services.impl.model.ProcessAssetDesc) EntityManagerFactory(javax.persistence.EntityManagerFactory) List(java.util.List)

Example 17 with NamedObjectModel

use of org.kie.internal.runtime.conf.NamedObjectModel in project jbpm by kiegroup.

the class DefaultRegisterableItemsFactory method getGlobalsFromDescriptor.

protected Map<String, Object> getGlobalsFromDescriptor(RuntimeEngine runtime) {
    Map<String, Object> globals = new HashMap<String, Object>();
    DeploymentDescriptor descriptor = getRuntimeManager().getDeploymentDescriptor();
    if (descriptor != null) {
        Map<String, Object> params = getParametersMap(runtime);
        for (NamedObjectModel model : descriptor.getGlobals()) {
            Object gInstance = getInstanceFromModel(model, getRuntimeManager().getEnvironment().getClassLoader(), params);
            if (gInstance != null) {
                globals.put(model.getName(), gInstance);
            }
        }
    }
    return globals;
}
Also used : NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) HashMap(java.util.HashMap) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor)

Example 18 with NamedObjectModel

use of org.kie.internal.runtime.conf.NamedObjectModel in project jbpm by kiegroup.

the class RuntimeManagerWithDescriptorTest method testDeployWithCustomDeploymentDescriptor.

@Test
public void testDeployWithCustomDeploymentDescriptor() throws Exception {
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.jbpm.test.dd", "-kjar-with-dd", "1.0.0");
    DeploymentDescriptor customDescriptor = new DeploymentDescriptorImpl("org.jbpm.persistence.jpa");
    customDescriptor.getBuilder().runtimeStrategy(RuntimeStrategy.PER_REQUEST).addGlobal(new NamedObjectModel("service", "java.util.ArrayList"));
    String processString = IOUtils.toString(this.getClass().getResourceAsStream("/BPMN2-ScriptTask.bpmn2"), "UTF-8");
    Map<String, String> resources = new HashMap<String, String>();
    resources.put("src/main/resources/BPMN2-ScriptTask.bpmn2", processString);
    resources.put("src/main/resources/" + DeploymentDescriptor.META_INF_LOCATION, customDescriptor.toXml());
    String drl = "package org.jbpm; global java.util.List service; " + "	rule \"Start Hello1\"" + "	  when" + "	  then" + "	    System.out.println(\"Hello\");" + "	end";
    resources.put("src/main/resources/simple.drl", drl);
    InternalKieModule kJar1 = createKieJar(ks, releaseId, resources);
    installKjar(releaseId, kJar1);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder(releaseId).userGroupCallback(userGroupCallback).get();
    manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    assertNotNull(manager);
    InternalRuntimeManager internalManager = (InternalRuntimeManager) manager;
    RegisterableItemsFactory factory = internalManager.getEnvironment().getRegisterableItemsFactory();
    assertNotNull(factory);
    assertTrue(factory instanceof InternalRegisterableItemsFactory);
    assertNotNull(((InternalRegisterableItemsFactory) factory).getRuntimeManager());
    DeploymentDescriptor descriptor = internalManager.getDeploymentDescriptor();
    assertNotNull(descriptor);
    assertEquals("org.jbpm.persistence.jpa", descriptor.getPersistenceUnit());
    assertEquals("org.jbpm.persistence.jpa", descriptor.getAuditPersistenceUnit());
    assertEquals(AuditMode.JPA, descriptor.getAuditMode());
    assertEquals(PersistenceMode.JPA, descriptor.getPersistenceMode());
    assertEquals(RuntimeStrategy.PER_REQUEST, descriptor.getRuntimeStrategy());
    assertEquals(0, descriptor.getMarshallingStrategies().size());
    assertEquals(0, descriptor.getConfiguration().size());
    assertEquals(0, descriptor.getEnvironmentEntries().size());
    assertEquals(0, descriptor.getEventListeners().size());
    assertEquals(1, descriptor.getGlobals().size());
    assertEquals(0, descriptor.getTaskEventListeners().size());
    assertEquals(0, descriptor.getWorkItemHandlers().size());
    assertEquals(0, descriptor.getRequiredRoles().size());
    RuntimeEngine engine = manager.getRuntimeEngine(EmptyContext.get());
    assertNotNull(engine);
    Object service = engine.getKieSession().getGlobal("service");
    assertNotNull(service);
    assertTrue(service instanceof ArrayList);
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) HashMap(java.util.HashMap) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) ArrayList(java.util.ArrayList) InternalRegisterableItemsFactory(org.kie.internal.runtime.manager.InternalRegisterableItemsFactory) RegisterableItemsFactory(org.kie.api.runtime.manager.RegisterableItemsFactory) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) InternalRegisterableItemsFactory(org.kie.internal.runtime.manager.InternalRegisterableItemsFactory) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 19 with NamedObjectModel

use of org.kie.internal.runtime.conf.NamedObjectModel in project jbpm by kiegroup.

the class DeploymentDescriptorManagerTest method testDeploymentDescriptorFromKieContainerWithDependencyMerged.

@Test
public void testDeploymentDescriptorFromKieContainerWithDependencyMerged() {
    DeploymentDescriptorManager manager = new DeploymentDescriptorManager("org.jbpm.domain");
    KieServices ks = KieServices.Factory.get();
    // create dependency kjar
    ReleaseId releaseIdDep = ks.newReleaseId(GROUP_ID, "dependency-data", VERSION);
    DeploymentDescriptor descriptorDep = new DeploymentDescriptorImpl("org.jbpm.domain");
    descriptorDep.getBuilder().runtimeStrategy(RuntimeStrategy.PER_PROCESS_INSTANCE).auditPersistenceUnit("org.jbpm.audit").addGlobal(new NamedObjectModel("service", "org.jbpm.global.Service"));
    Map<String, String> resourcesDep = new HashMap<String, String>();
    resourcesDep.put("src/main/resources/simple.drl", SIMPLE_DRL);
    resourcesDep.put("src/main/resources/META-INF/kie-deployment-descriptor.xml", descriptorDep.toXml());
    InternalKieModule kJarDep = createKieJar(ks, releaseIdDep, resourcesDep);
    installKjar(releaseIdDep, kJarDep);
    // create first kjar that will have dependency to another
    ReleaseId releaseId = ks.newReleaseId(GROUP_ID, ARTIFACT_ID, VERSION);
    DeploymentDescriptor descriptor = new DeploymentDescriptorImpl("org.jbpm.domain");
    descriptor.getBuilder().runtimeStrategy(RuntimeStrategy.PER_PROCESS_INSTANCE);
    Map<String, String> resources = new HashMap<String, String>();
    resources.put("src/main/resources/simple.drl", SIMPLE_DRL);
    resources.put("src/main/resources/META-INF/kie-deployment-descriptor.xml", descriptor.toXml());
    InternalKieModule kJar1 = createKieJar(ks, releaseId, resources, releaseIdDep);
    installKjar(releaseId, kJar1);
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    assertNotNull(kieContainer);
    List<DeploymentDescriptor> descriptorHierarchy = manager.getDeploymentDescriptorHierarchy(kieContainer);
    assertNotNull(descriptorHierarchy);
    assertEquals(3, descriptorHierarchy.size());
    descriptor = descriptorHierarchy.get(0);
    assertNotNull(descriptor);
    assertEquals("org.jbpm.domain", descriptor.getPersistenceUnit());
    assertEquals("org.jbpm.domain", descriptor.getAuditPersistenceUnit());
    assertEquals(AuditMode.JPA, descriptor.getAuditMode());
    assertEquals(PersistenceMode.JPA, descriptor.getPersistenceMode());
    assertEquals(RuntimeStrategy.PER_PROCESS_INSTANCE, descriptor.getRuntimeStrategy());
    assertEquals(0, descriptor.getMarshallingStrategies().size());
    assertEquals(0, descriptor.getConfiguration().size());
    assertEquals(0, descriptor.getEnvironmentEntries().size());
    assertEquals(0, descriptor.getEventListeners().size());
    assertEquals(0, descriptor.getGlobals().size());
    assertEquals(0, descriptor.getTaskEventListeners().size());
    assertEquals(0, descriptor.getWorkItemHandlers().size());
    descriptor = descriptorHierarchy.get(1);
    assertNotNull(descriptor);
    assertEquals("org.jbpm.domain", descriptor.getPersistenceUnit());
    assertEquals("org.jbpm.audit", descriptor.getAuditPersistenceUnit());
    assertEquals(AuditMode.JPA, descriptor.getAuditMode());
    assertEquals(PersistenceMode.JPA, descriptor.getPersistenceMode());
    assertEquals(RuntimeStrategy.PER_PROCESS_INSTANCE, descriptor.getRuntimeStrategy());
    assertEquals(0, descriptor.getMarshallingStrategies().size());
    assertEquals(0, descriptor.getConfiguration().size());
    assertEquals(0, descriptor.getEnvironmentEntries().size());
    assertEquals(0, descriptor.getEventListeners().size());
    assertEquals(1, descriptor.getGlobals().size());
    assertEquals(0, descriptor.getTaskEventListeners().size());
    assertEquals(0, descriptor.getWorkItemHandlers().size());
    descriptor = descriptorHierarchy.get(2);
    assertNotNull(descriptor);
    assertEquals("org.jbpm.domain", descriptor.getPersistenceUnit());
    assertEquals("org.jbpm.domain", descriptor.getAuditPersistenceUnit());
    assertEquals(AuditMode.JPA, descriptor.getAuditMode());
    assertEquals(PersistenceMode.JPA, descriptor.getPersistenceMode());
    assertEquals(RuntimeStrategy.SINGLETON, descriptor.getRuntimeStrategy());
    assertEquals(0, descriptor.getMarshallingStrategies().size());
    assertEquals(0, descriptor.getConfiguration().size());
    assertEquals(0, descriptor.getEnvironmentEntries().size());
    assertEquals(0, descriptor.getEventListeners().size());
    assertEquals(0, descriptor.getGlobals().size());
    assertEquals(0, descriptor.getTaskEventListeners().size());
    assertEquals(0, descriptor.getWorkItemHandlers().size());
    DeploymentDescriptorMerger merger = new DeploymentDescriptorMerger();
    DeploymentDescriptor outcome = merger.merge(descriptorHierarchy, MergeMode.MERGE_COLLECTIONS);
    assertNotNull(outcome);
    assertEquals("org.jbpm.domain", outcome.getPersistenceUnit());
    assertEquals("org.jbpm.domain", outcome.getAuditPersistenceUnit());
    assertEquals(AuditMode.JPA, outcome.getAuditMode());
    assertEquals(PersistenceMode.JPA, outcome.getPersistenceMode());
    assertEquals(RuntimeStrategy.PER_PROCESS_INSTANCE, outcome.getRuntimeStrategy());
    assertEquals(0, outcome.getMarshallingStrategies().size());
    assertEquals(0, outcome.getConfiguration().size());
    assertEquals(0, outcome.getEnvironmentEntries().size());
    assertEquals(0, outcome.getEventListeners().size());
    assertEquals(1, outcome.getGlobals().size());
    assertEquals(0, outcome.getTaskEventListeners().size());
    assertEquals(0, outcome.getWorkItemHandlers().size());
}
Also used : HashMap(java.util.HashMap) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 20 with NamedObjectModel

use of org.kie.internal.runtime.conf.NamedObjectModel in project jbpm by kiegroup.

the class DeploymentDescriptorTest method testPrintDescriptor.

@Test
public void testPrintDescriptor() {
    DeploymentDescriptor descriptor = new DeploymentDescriptorImpl("org.jbpm.domain");
    descriptor.getBuilder().addWorkItemHandler(new NamedObjectModel("mvel", "Log", "new org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler()")).addWorkItemHandler(new NamedObjectModel("mvel", "WebService", "new org.jbpm.process.workitem.webservice.WebServiceWorkItemHandler(ksession)")).addWorkItemHandler(new NamedObjectModel("mvel", "Rest", "new org.jbpm.process.workitem.rest.RESTWorkItemHandler()")).addWorkItemHandler(new NamedObjectModel("mvel", "Service Task", "new org.jbpm.process.workitem.bpmn2.ServiceTaskHandler(ksession)"));
    logger.debug(descriptor.toXml());
}
Also used : NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) Test(org.junit.Test)

Aggregations

NamedObjectModel (org.kie.internal.runtime.conf.NamedObjectModel)23 DeploymentDescriptor (org.kie.internal.runtime.conf.DeploymentDescriptor)19 Test (org.junit.Test)12 HashMap (java.util.HashMap)11 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)9 KieServices (org.kie.api.KieServices)9 ArrayList (java.util.ArrayList)8 ReleaseId (org.kie.api.builder.ReleaseId)8 DeploymentDescriptorImpl (org.jbpm.runtime.manager.impl.deploy.DeploymentDescriptorImpl)7 ObjectModel (org.kie.internal.runtime.conf.ObjectModel)7 KModuleDeploymentUnit (org.jbpm.kie.services.impl.KModuleDeploymentUnit)6 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)6 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 DeployedUnit (org.jbpm.services.api.model.DeployedUnit)5 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)5 KieMavenRepository (org.kie.scanner.KieMavenRepository)5 AbstractKieServicesBaseTest (org.jbpm.kie.test.util.AbstractKieServicesBaseTest)4 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)4 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)4