Search in sources :

Example 6 with ObjectModel

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

the class DefaultRegisterableItemsFactory method getTaskListenersFromDescriptor.

protected List<TaskLifeCycleEventListener> getTaskListenersFromDescriptor() {
    List<TaskLifeCycleEventListener> defaultListeners = new ArrayList<TaskLifeCycleEventListener>();
    DeploymentDescriptor descriptor = getRuntimeManager().getDeploymentDescriptor();
    if (descriptor != null) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("runtimeManager", getRuntimeManager());
        params.put("classLoader", getRuntimeManager().getEnvironment().getClassLoader());
        params.put("kieContainer", getRuntimeManager().getKieContainer());
        for (ObjectModel model : descriptor.getTaskEventListeners()) {
            Object taskListener = getInstanceFromModel(model, getRuntimeManager().getEnvironment().getClassLoader(), params);
            if (taskListener != null) {
                defaultListeners.add((TaskLifeCycleEventListener) taskListener);
            }
        }
    }
    return defaultListeners;
}
Also used : ObjectModel(org.kie.internal.runtime.conf.ObjectModel) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) HashMap(java.util.HashMap) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) ArrayList(java.util.ArrayList) JPATaskLifeCycleEventListener(org.jbpm.services.task.audit.JPATaskLifeCycleEventListener) TaskLifeCycleEventListener(org.kie.api.task.TaskLifeCycleEventListener)

Example 7 with ObjectModel

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

the class EjbObjectModelResolver method getInstance.

@Override
public Object getInstance(ObjectModel model, ClassLoader cl, Map<String, Object> contextParams) {
    Class<?> clazz = getClassObject(model.getIdentifier(), cl);
    Object instance = null;
    InternalRuntimeManager manager = null;
    if (contextParams.containsKey("runtimeManager")) {
        manager = (InternalRuntimeManager) contextParams.get("runtimeManager");
        instance = manager.getCacheManager().get(clazz.getName());
        if (instance != null) {
            return instance;
        }
    }
    if (model.getParameters() == null || model.getParameters().isEmpty()) {
        logger.debug("About to create instance of {} with no arg constructor", model.getIdentifier());
        // no parameters then use no arg constructor
        try {
            instance = clazz.newInstance();
        } catch (Exception e) {
            throw new IllegalArgumentException("Unable to create instance (no arg constructor) of type " + model.getIdentifier() + " due to " + e.getMessage(), e);
        }
    } else {
        logger.debug("About to create instance of {} with {} parameters", model.getIdentifier(), model.getParameters().size());
        // process parameter instances
        Class<?>[] parameterTypes = new Class<?>[model.getParameters().size()];
        Object[] paramInstances = new Object[model.getParameters().size()];
        int index = 0;
        for (Object param : model.getParameters()) {
            if (param instanceof ObjectModel) {
                logger.debug("Parameter is of type ObjectModel (id: {}), trying to create instance based on that model", ((ObjectModel) param).getIdentifier());
                Class<?> paramclazz = getClassObject(((ObjectModel) param).getIdentifier(), cl);
                parameterTypes[index] = paramclazz;
                paramInstances[index] = getInstance(((ObjectModel) param), cl, contextParams);
            } else {
                if (contextParams.containsKey(param)) {
                    logger.debug("Parametr references context parametr with name {}", param);
                    Object contextValue = contextParams.get(param);
                    Class<?> paramClass = contextValue.getClass();
                    if (knownContextParamMapping.containsKey(param)) {
                        paramClass = knownContextParamMapping.get(param);
                    }
                    parameterTypes[index] = paramClass;
                    paramInstances[index] = contextValue;
                } else {
                    if (param.toString().startsWith("jndi:")) {
                        logger.debug("Parameter is jndi lookup type - {}", param);
                        // remove the jndi: prefix
                        String lookupName = param.toString().substring(5);
                        try {
                            Object jndiObject = InitialContext.doLookup(lookupName);
                            parameterTypes[index] = Object.class;
                            paramInstances[index] = jndiObject;
                        } catch (NamingException e) {
                            throw new IllegalArgumentException("Unable to look up object from jndi using name " + lookupName, e);
                        }
                    } else {
                        logger.debug("Parameter is simple type (string) - {}", param);
                        parameterTypes[index] = param.getClass();
                        paramInstances[index] = param;
                    }
                }
            }
            index++;
        }
        try {
            logger.debug("Creating instance of class {} with parameter types {} and parameter instances {}", clazz, parameterTypes, paramInstances);
            Constructor<?> constructor = clazz.getConstructor(parameterTypes);
            instance = constructor.newInstance(paramInstances);
        } catch (Exception e) {
            throw new IllegalArgumentException("Unable to create instance (" + parameterTypes + " constructor) of type " + model.getIdentifier() + " due to " + e.getMessage(), e);
        }
    }
    logger.debug("Created instance : {}", instance);
    if (manager != null && instance instanceof Cacheable) {
        manager.getCacheManager().add(instance.getClass().getName(), instance);
    }
    return instance;
}
Also used : InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) ObjectModel(org.kie.internal.runtime.conf.ObjectModel) Cacheable(org.kie.internal.runtime.Cacheable) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException)

Example 8 with ObjectModel

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

the class ReflectionObjectModelResolver method getInstance.

@Override
public Object getInstance(ObjectModel model, ClassLoader cl, Map<String, Object> contextParams) {
    Class<?> clazz = getClassObject(model.getIdentifier(), cl);
    Object instance = null;
    InternalRuntimeManager manager = null;
    if (contextParams.containsKey("runtimeManager")) {
        manager = (InternalRuntimeManager) contextParams.get("runtimeManager");
        instance = manager.getCacheManager().get(clazz.getName());
        if (instance != null) {
            return instance;
        }
    }
    if (model.getParameters() == null || model.getParameters().isEmpty()) {
        logger.debug("About to create instance of {} with no arg constructor", model.getIdentifier());
        // no parameters then use no arg constructor
        try {
            instance = clazz.newInstance();
        } catch (Exception e) {
            throw new IllegalArgumentException("Unable to create instance (no arg constructor) of type " + model.getIdentifier() + " due to " + e.getMessage(), e);
        }
    } else {
        logger.debug("About to create instance of {} with {} parameters", model.getIdentifier(), model.getParameters().size());
        // process parameter instances
        Class<?>[] parameterTypes = new Class<?>[model.getParameters().size()];
        Object[] paramInstances = new Object[model.getParameters().size()];
        int index = 0;
        for (Object param : model.getParameters()) {
            if (param instanceof ObjectModel) {
                logger.debug("Parameter is of type ObjectModel (id: {}), trying to create instance based on that model", ((ObjectModel) param).getIdentifier());
                Class<?> paramclazz = getClassObject(((ObjectModel) param).getIdentifier(), cl);
                parameterTypes[index] = paramclazz;
                paramInstances[index] = getInstance(((ObjectModel) param), cl, contextParams);
            } else {
                if (contextParams.containsKey(param)) {
                    logger.debug("Parametr references context parametr with name {}", param);
                    Object contextValue = contextParams.get(param);
                    Class<?> paramClass = contextValue.getClass();
                    if (knownContextParamMapping.containsKey(param)) {
                        paramClass = knownContextParamMapping.get(param);
                    }
                    parameterTypes[index] = paramClass;
                    paramInstances[index] = contextValue;
                } else {
                    logger.debug("Parameter is simple type (string) - {}", param);
                    parameterTypes[index] = param.getClass();
                    paramInstances[index] = param;
                }
            }
            index++;
        }
        try {
            logger.debug("Creating instance of class {} with parameter types {} and parameter instances {}", clazz, parameterTypes, paramInstances);
            Constructor<?> constructor = clazz.getConstructor(parameterTypes);
            instance = constructor.newInstance(paramInstances);
        } catch (Exception e) {
            throw new IllegalArgumentException("Unable to create instance (" + parameterTypes + " constructor) of type " + model.getIdentifier() + " due to " + e.getMessage(), e);
        }
    }
    logger.debug("Created instance : {}", instance);
    if (manager != null && instance instanceof Cacheable) {
        manager.getCacheManager().add(instance.getClass().getName(), instance);
    }
    return instance;
}
Also used : InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) ObjectModel(org.kie.internal.runtime.conf.ObjectModel) Cacheable(org.kie.internal.runtime.Cacheable)

Example 9 with ObjectModel

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

the class DeploymentDescriptorMergerTest method testDeploymentDesciptorMergeOverrideEmpty.

@Test
public void testDeploymentDesciptorMergeOverrideEmpty() {
    DeploymentDescriptor master = new DeploymentDescriptorImpl("org.jbpm.domain");
    master.getBuilder().addMarshalingStrategy(new ObjectModel("org.jbpm.test.CustomStrategy", new Object[] { "param2" })).setLimitSerializationClasses(true);
    assertNotNull(master);
    assertEquals("org.jbpm.domain", master.getPersistenceUnit());
    assertEquals("org.jbpm.domain", master.getAuditPersistenceUnit());
    assertEquals(AuditMode.JPA, master.getAuditMode());
    assertEquals(PersistenceMode.JPA, master.getPersistenceMode());
    assertEquals(RuntimeStrategy.SINGLETON, master.getRuntimeStrategy());
    assertEquals(1, master.getMarshallingStrategies().size());
    assertEquals(0, master.getConfiguration().size());
    assertEquals(0, master.getEnvironmentEntries().size());
    assertEquals(0, master.getEventListeners().size());
    assertEquals(0, master.getGlobals().size());
    assertEquals(0, master.getTaskEventListeners().size());
    assertEquals(0, master.getWorkItemHandlers().size());
    assertTrue(master.getLimitSerializationClasses());
    DeploymentDescriptor slave = new DeploymentDescriptorImpl("org.jbpm.domain");
    slave.getBuilder().auditMode(AuditMode.JMS).persistenceMode(PersistenceMode.JPA).persistenceUnit(null).auditPersistenceUnit("");
    assertNotNull(slave);
    assertEquals(null, slave.getPersistenceUnit());
    assertEquals("", slave.getAuditPersistenceUnit());
    assertEquals(AuditMode.JMS, slave.getAuditMode());
    assertEquals(PersistenceMode.JPA, slave.getPersistenceMode());
    assertEquals(RuntimeStrategy.SINGLETON, slave.getRuntimeStrategy());
    assertEquals(0, slave.getMarshallingStrategies().size());
    assertEquals(0, slave.getConfiguration().size());
    assertEquals(0, slave.getEnvironmentEntries().size());
    assertEquals(0, slave.getEventListeners().size());
    assertEquals(0, slave.getGlobals().size());
    assertEquals(0, slave.getTaskEventListeners().size());
    assertEquals(0, slave.getWorkItemHandlers().size());
    ((DeploymentDescriptorImpl) slave).setLimitSerializationClasses(null);
    assertNull(slave.getLimitSerializationClasses());
    // and now let's merge them
    DeploymentDescriptorMerger merger = new DeploymentDescriptorMerger();
    DeploymentDescriptor outcome = merger.merge(master, slave, MergeMode.OVERRIDE_EMPTY);
    assertNotNull(outcome);
    assertEquals("org.jbpm.domain", outcome.getPersistenceUnit());
    assertEquals("org.jbpm.domain", outcome.getAuditPersistenceUnit());
    assertEquals(AuditMode.JMS, 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 10 with ObjectModel

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

the class DeploymentDescriptorMergerTest method testDeploymentDesciptorMergeHierarchy.

@Test
public void testDeploymentDesciptorMergeHierarchy() {
    DeploymentDescriptor master = new DeploymentDescriptorImpl("org.jbpm.domain");
    master.getBuilder().addMarshalingStrategy(new ObjectModel("org.jbpm.test.CustomStrategy", new Object[] { "param2" }));
    assertNotNull(master);
    assertEquals("org.jbpm.domain", master.getPersistenceUnit());
    assertEquals("org.jbpm.domain", master.getAuditPersistenceUnit());
    assertEquals(AuditMode.JPA, master.getAuditMode());
    assertEquals(PersistenceMode.JPA, master.getPersistenceMode());
    assertEquals(RuntimeStrategy.SINGLETON, master.getRuntimeStrategy());
    assertEquals(1, master.getMarshallingStrategies().size());
    assertEquals(0, master.getConfiguration().size());
    assertEquals(0, master.getEnvironmentEntries().size());
    assertEquals(0, master.getEventListeners().size());
    assertEquals(0, master.getGlobals().size());
    assertEquals(0, master.getTaskEventListeners().size());
    assertEquals(0, master.getWorkItemHandlers().size());
    DeploymentDescriptor slave = new DeploymentDescriptorImpl("org.jbpm.domain");
    slave.getBuilder().auditMode(AuditMode.NONE).persistenceMode(PersistenceMode.JPA).persistenceUnit("my.custom.unit").auditPersistenceUnit("my.custom.unit2");
    assertNotNull(slave);
    assertEquals("my.custom.unit", slave.getPersistenceUnit());
    assertEquals("my.custom.unit2", slave.getAuditPersistenceUnit());
    assertEquals(AuditMode.NONE, slave.getAuditMode());
    assertEquals(PersistenceMode.JPA, slave.getPersistenceMode());
    assertEquals(RuntimeStrategy.SINGLETON, slave.getRuntimeStrategy());
    assertEquals(0, slave.getMarshallingStrategies().size());
    assertEquals(0, slave.getConfiguration().size());
    assertEquals(0, slave.getEnvironmentEntries().size());
    assertEquals(0, slave.getEventListeners().size());
    assertEquals(0, slave.getGlobals().size());
    assertEquals(0, slave.getTaskEventListeners().size());
    assertEquals(0, slave.getWorkItemHandlers().size());
    DeploymentDescriptor slave2 = new DeploymentDescriptorImpl("org.jbpm.domain");
    slave2.getBuilder().auditMode(AuditMode.JMS).persistenceMode(PersistenceMode.JPA).persistenceUnit("my.custom.unit2").auditPersistenceUnit("my.custom.altered").runtimeStrategy(RuntimeStrategy.PER_PROCESS_INSTANCE).addEnvironmentEntry(new NamedObjectModel("IS_JTA", "java.lang.Boolean", new Object[] { "false" }));
    assertNotNull(slave2);
    assertEquals("my.custom.unit2", slave2.getPersistenceUnit());
    assertEquals("my.custom.altered", slave2.getAuditPersistenceUnit());
    assertEquals(AuditMode.JMS, slave2.getAuditMode());
    assertEquals(PersistenceMode.JPA, slave2.getPersistenceMode());
    assertEquals(RuntimeStrategy.PER_PROCESS_INSTANCE, slave2.getRuntimeStrategy());
    assertEquals(0, slave2.getMarshallingStrategies().size());
    assertEquals(0, slave2.getConfiguration().size());
    assertEquals(1, slave2.getEnvironmentEntries().size());
    assertEquals(0, slave2.getEventListeners().size());
    assertEquals(0, slave2.getGlobals().size());
    assertEquals(0, slave2.getTaskEventListeners().size());
    assertEquals(0, slave2.getWorkItemHandlers().size());
    // assemble hierarchy
    List<DeploymentDescriptor> hierarchy = new ArrayList<DeploymentDescriptor>();
    hierarchy.add(slave2);
    hierarchy.add(slave);
    hierarchy.add(master);
    // and now let's merge them
    DeploymentDescriptorMerger merger = new DeploymentDescriptorMerger();
    DeploymentDescriptor outcome = merger.merge(hierarchy, MergeMode.MERGE_COLLECTIONS);
    assertNotNull(outcome);
    assertEquals("my.custom.unit2", outcome.getPersistenceUnit());
    assertEquals("my.custom.altered", outcome.getAuditPersistenceUnit());
    assertEquals(AuditMode.JMS, outcome.getAuditMode());
    assertEquals(PersistenceMode.JPA, outcome.getPersistenceMode());
    assertEquals(RuntimeStrategy.PER_PROCESS_INSTANCE, outcome.getRuntimeStrategy());
    assertEquals(1, outcome.getMarshallingStrategies().size());
    assertEquals(0, outcome.getConfiguration().size());
    assertEquals(1, outcome.getEnvironmentEntries().size());
    assertEquals(0, outcome.getEventListeners().size());
    assertEquals(0, outcome.getGlobals().size());
    assertEquals(0, outcome.getTaskEventListeners().size());
    assertEquals(0, outcome.getWorkItemHandlers().size());
}
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) Test(org.junit.Test)

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