Search in sources :

Example 31 with ObjectModel

use of org.kie.internal.runtime.conf.ObjectModel in project drools by kiegroup.

the class DeploymentDescriptorMergerTest method testDeploymentDesciptorMergeKeepAll.

@Test
public void testDeploymentDesciptorMergeKeepAll() {
    DeploymentDescriptor primary = new DeploymentDescriptorImpl("org.jbpm.domain");
    primary.getBuilder().addMarshalingStrategy(new ObjectModel("org.jbpm.test.CustomStrategy", new Object[] { "param2" })).setLimitSerializationClasses(true);
    assertNotNull(primary);
    assertEquals("org.jbpm.domain", primary.getPersistenceUnit());
    assertEquals("org.jbpm.domain", primary.getAuditPersistenceUnit());
    assertEquals(AuditMode.JPA, primary.getAuditMode());
    assertEquals(PersistenceMode.JPA, primary.getPersistenceMode());
    assertEquals(RuntimeStrategy.SINGLETON, primary.getRuntimeStrategy());
    assertEquals(1, primary.getMarshallingStrategies().size());
    assertEquals(0, primary.getConfiguration().size());
    assertEquals(0, primary.getEnvironmentEntries().size());
    assertEquals(0, primary.getEventListeners().size());
    assertEquals(0, primary.getGlobals().size());
    assertEquals(0, primary.getTaskEventListeners().size());
    assertEquals(0, primary.getWorkItemHandlers().size());
    assertTrue(primary.getLimitSerializationClasses());
    DeploymentDescriptor secondary = new DeploymentDescriptorImpl("org.jbpm.domain");
    secondary.getBuilder().auditMode(AuditMode.JMS).persistenceMode(PersistenceMode.JPA).persistenceUnit("my.custom.unit").auditPersistenceUnit("my.custom.unit2").setLimitSerializationClasses(false);
    assertNotNull(secondary);
    assertEquals("my.custom.unit", secondary.getPersistenceUnit());
    assertEquals("my.custom.unit2", secondary.getAuditPersistenceUnit());
    assertEquals(AuditMode.JMS, secondary.getAuditMode());
    assertEquals(PersistenceMode.JPA, secondary.getPersistenceMode());
    assertEquals(RuntimeStrategy.SINGLETON, secondary.getRuntimeStrategy());
    assertEquals(0, secondary.getMarshallingStrategies().size());
    assertEquals(0, secondary.getConfiguration().size());
    assertEquals(0, secondary.getEnvironmentEntries().size());
    assertEquals(0, secondary.getEventListeners().size());
    assertEquals(0, secondary.getGlobals().size());
    assertEquals(0, secondary.getTaskEventListeners().size());
    assertEquals(0, secondary.getWorkItemHandlers().size());
    assertFalse(secondary.getLimitSerializationClasses());
    // and now let's merge them
    DeploymentDescriptor outcome = DeploymentDescriptorMerger.merge(primary, secondary, MergeMode.KEEP_ALL);
    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.SINGLETON, outcome.getRuntimeStrategy());
    assertEquals(1, outcome.getMarshallingStrategies().size());
    assertEquals(0, outcome.getConfiguration().size());
    assertEquals(0, outcome.getEnvironmentEntries().size());
    assertEquals(0, outcome.getEventListeners().size());
    assertEquals(0, outcome.getGlobals().size());
    assertEquals(0, outcome.getTaskEventListeners().size());
    assertEquals(0, outcome.getWorkItemHandlers().size());
    assertTrue(outcome.getLimitSerializationClasses());
}
Also used : ObjectModel(org.kie.internal.runtime.conf.ObjectModel) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) Test(org.junit.Test)

Example 32 with ObjectModel

use of org.kie.internal.runtime.conf.ObjectModel in project drools by kiegroup.

the class DeploymentDescriptorMerger method merge.

public static DeploymentDescriptor merge(DeploymentDescriptor primary, DeploymentDescriptor secondary, MergeMode mode) {
    if (primary == null || secondary == null) {
        throw new IllegalArgumentException("Descriptors to merge must be provided");
    }
    if (mode == null) {
        mode = MergeMode.MERGE_COLLECTIONS;
    }
    DeploymentDescriptor merged = null;
    DeploymentDescriptorBuilder builder = primary.getBuilder();
    builder.setBuildHandler(new MergeModeBuildHandler(mode));
    switch(mode) {
        case KEEP_ALL:
            // do nothing as primary wins
            merged = primary;
            break;
        case OVERRIDE_ALL:
            // do nothing as secondary wins
            merged = secondary;
            break;
        case OVERRIDE_EMPTY:
            builder.auditMode(secondary.getAuditMode());
            builder.auditPersistenceUnit(secondary.getAuditPersistenceUnit());
            builder.persistenceMode(secondary.getPersistenceMode());
            builder.persistenceUnit(secondary.getPersistenceUnit());
            builder.runtimeStrategy(secondary.getRuntimeStrategy());
            builder.setConfiguration(secondary.getConfiguration());
            builder.setEnvironmentEntries(secondary.getEnvironmentEntries());
            builder.setEventListeners(secondary.getEventListeners());
            builder.setGlobals(secondary.getGlobals());
            builder.setMarshalingStrategies(secondary.getMarshallingStrategies());
            builder.setTaskEventListeners(secondary.getTaskEventListeners());
            builder.setWorkItemHandlers(secondary.getWorkItemHandlers());
            builder.setRequiredRoles(secondary.getRequiredRoles());
            builder.setClasses(secondary.getClasses());
            builder.setLimitSerializationClasses(secondary.getLimitSerializationClasses());
            merged = builder.get();
            break;
        case MERGE_COLLECTIONS:
        default:
            builder.auditMode(secondary.getAuditMode());
            builder.auditPersistenceUnit(secondary.getAuditPersistenceUnit());
            builder.persistenceMode(secondary.getPersistenceMode());
            builder.persistenceUnit(secondary.getPersistenceUnit());
            builder.runtimeStrategy(secondary.getRuntimeStrategy());
            for (ObjectModel model : secondary.getEventListeners()) {
                builder.addEventListener(model);
            }
            for (ObjectModel model : secondary.getMarshallingStrategies()) {
                builder.addMarshalingStrategy(model);
            }
            // we need to keep the order of task listeners otherwise they will rise in different order
            // so the primary must be the latest one
            List<ObjectModel> taskEventListeners = new ArrayList<>(secondary.getTaskEventListeners());
            for (ObjectModel model : primary.getTaskEventListeners()) {
                if (!taskEventListeners.contains(model)) {
                    taskEventListeners.add(model);
                }
            }
            builder.setTaskEventListeners(taskEventListeners);
            for (NamedObjectModel model : secondary.getConfiguration()) {
                builder.addConfiguration(model);
            }
            for (NamedObjectModel model : secondary.getEnvironmentEntries()) {
                builder.addEnvironmentEntry(model);
            }
            for (NamedObjectModel model : secondary.getGlobals()) {
                builder.addGlobal(model);
            }
            for (NamedObjectModel model : secondary.getWorkItemHandlers()) {
                builder.addWorkItemHandler(model);
            }
            for (String requiredRole : secondary.getRequiredRoles()) {
                builder.addRequiredRole(requiredRole);
            }
            for (String clazz : secondary.getClasses()) {
                builder.addClass(clazz);
            }
            Boolean secondaryLimit = secondary.getLimitSerializationClasses();
            Boolean primaryLimit = primary.getLimitSerializationClasses();
            if (secondaryLimit != null && primaryLimit != null && (!secondaryLimit || !primaryLimit)) {
                builder.setLimitSerializationClasses(false);
            }
            merged = builder.get();
            break;
    }
    return merged;
}
Also used : NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) ObjectModel(org.kie.internal.runtime.conf.ObjectModel) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) ArrayList(java.util.ArrayList) DeploymentDescriptorBuilder(org.kie.internal.runtime.conf.DeploymentDescriptorBuilder)

Example 33 with ObjectModel

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

the class CarInsuranceClaimCaseTest method getProcessListeners.

@Override
protected List<ObjectModel> getProcessListeners() {
    List<ObjectModel> listeners = super.getProcessListeners();
    listeners.add(new ObjectModel("mvel", "org.jbpm.casemgmt.impl.util.CountDownListenerFactory.get(\"carInsuranceCase\", \"wait before callback\", 1)"));
    return listeners;
}
Also used : ObjectModel(org.kie.internal.runtime.conf.ObjectModel) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel)

Example 34 with ObjectModel

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

the class CaseCommentNotificationTest method prepareDeploymentUnit.

protected DeploymentUnit prepareDeploymentUnit() {
    assertThat(deploymentService).isNotNull();
    KModuleDeploymentUnit deploymentUnit = new KModuleDeploymentUnit(GROUP_ID, ARTIFACT_ID, VERSION);
    final DeploymentDescriptor descriptor = new DeploymentDescriptorImpl();
    descriptor.getBuilder().addEventListener(new ObjectModel("mvel", "new org.jbpm.kie.services.impl.IdentityProviderAwareProcessListener(ksession)")).addEventListener(new ObjectModel("mvel", "org.jbpm.casemgmt.impl.util.CommentNotificationEventListenerFactory.get(\"test\")"));
    deploymentUnit.setDeploymentDescriptor(descriptor);
    deploymentUnit.setStrategy(RuntimeStrategy.PER_CASE);
    deploymentService.deploy(deploymentUnit);
    return deploymentUnit;
}
Also used : ObjectModel(org.kie.internal.runtime.conf.ObjectModel) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) DeploymentDescriptorImpl(org.jbpm.runtime.manager.impl.deploy.DeploymentDescriptorImpl) KModuleDeploymentUnit(org.jbpm.kie.services.impl.KModuleDeploymentUnit)

Example 35 with ObjectModel

use of org.kie.internal.runtime.conf.ObjectModel 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)

Aggregations

ObjectModel (org.kie.internal.runtime.conf.ObjectModel)56 NamedObjectModel (org.kie.internal.runtime.conf.NamedObjectModel)40 Test (org.junit.Test)34 DeploymentDescriptor (org.kie.internal.runtime.conf.DeploymentDescriptor)34 ArrayList (java.util.ArrayList)16 ObjectModelResolver (org.kie.internal.runtime.conf.ObjectModelResolver)10 HashMap (java.util.HashMap)9 SimpleCustomObject (org.jbpm.runtime.manager.impl.deploy.testobject.SimpleCustomObject)9 DeploymentDescriptorImpl (org.jbpm.runtime.manager.impl.deploy.DeploymentDescriptorImpl)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)6 EmbedingCustomObject (org.jbpm.runtime.manager.impl.deploy.testobject.EmbedingCustomObject)6 ThirdLevelCustomObject (org.jbpm.runtime.manager.impl.deploy.testobject.ThirdLevelCustomObject)6 KieServices (org.kie.api.KieServices)6 DeploymentDescriptorBuilder (org.kie.internal.runtime.conf.DeploymentDescriptorBuilder)6 ReleaseId (org.kie.api.builder.ReleaseId)5 KieMavenRepository (org.kie.scanner.KieMavenRepository)5 File (java.io.File)4 Before (org.junit.Before)4 Cacheable (org.kie.internal.runtime.Cacheable)4