Search in sources :

Example 6 with InternalRuntimeManager

use of org.kie.internal.runtime.manager.InternalRuntimeManager in project jbpm by kiegroup.

the class ExecutorImpl method getClassLoader.

protected ClassLoader getClassLoader(String deploymentId) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (deploymentId == null) {
        return cl;
    }
    InternalRuntimeManager manager = ((InternalRuntimeManager) RuntimeManagerRegistry.get().getManager(deploymentId));
    if (manager != null && manager.getEnvironment().getClassLoader() != null) {
        cl = manager.getEnvironment().getClassLoader();
    }
    return cl;
}
Also used : InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager)

Example 7 with InternalRuntimeManager

use of org.kie.internal.runtime.manager.InternalRuntimeManager 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 InternalRuntimeManager

use of org.kie.internal.runtime.manager.InternalRuntimeManager in project jbpm by kiegroup.

the class MVELObjectModelResolver method getInstance.

@Override
public Object getInstance(ObjectModel model, ClassLoader cl, Map<String, Object> contextParams) {
    Object instance = null;
    InternalRuntimeManager manager = null;
    if (contextParams.containsKey("runtimeManager")) {
        manager = (InternalRuntimeManager) contextParams.get("runtimeManager");
        instance = manager.getCacheManager().get(model.getIdentifier());
        if (instance != null) {
            return instance;
        }
    }
    ParserConfiguration config = new ParserConfiguration();
    config.setClassLoader(cl);
    ParserContext ctx = new ParserContext(config);
    if (contextParams != null) {
        for (Map.Entry<String, Object> entry : contextParams.entrySet()) {
            ctx.addVariable(entry.getKey(), entry.getValue().getClass());
        }
    }
    Object compiledExpression = MVEL.compileExpression(model.getIdentifier(), ctx);
    instance = MVELSafeHelper.getEvaluator().executeExpression(compiledExpression, contextParams);
    if (manager != null && instance instanceof Cacheable) {
        manager.getCacheManager().add(model.getIdentifier(), instance);
    }
    return instance;
}
Also used : InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) Cacheable(org.kie.internal.runtime.Cacheable) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 9 with InternalRuntimeManager

use of org.kie.internal.runtime.manager.InternalRuntimeManager 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 10 with InternalRuntimeManager

use of org.kie.internal.runtime.manager.InternalRuntimeManager in project jbpm by kiegroup.

the class JMSSignalReceiver method onMessage.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void onMessage(Message message) {
    if (message instanceof BytesMessage) {
        String deploymentId;
        Long processInstanceId;
        String signal;
        Long workItemId;
        Object data;
        BytesMessage bytesMessage = (BytesMessage) message;
        RuntimeManager runtimeManager = null;
        RuntimeEngine engine = null;
        try {
            deploymentId = (String) bytesMessage.getObjectProperty("KIE_SignalDeploymentId");
            if (deploymentId == null) {
                deploymentId = (String) bytesMessage.getObjectProperty("KIE_DeploymentId");
            }
            signal = (String) bytesMessage.getObjectProperty("KIE_Signal");
            processInstanceId = (Long) bytesMessage.getObjectProperty("KIE_SignalProcessInstanceId");
            workItemId = (Long) bytesMessage.getObjectProperty("KIE_SignalWorkItemId");
            logger.debug("Deployment id '{}', signal '{}', processInstanceId '{}', workItemId '{}'", deploymentId, signal, processInstanceId, workItemId);
            Collection<String> availableRuntimeManagers = matchDeployments(deploymentId, RuntimeManagerRegistry.get().getRegisteredIdentifiers());
            for (String matchedDeploymentId : availableRuntimeManagers) {
                try {
                    runtimeManager = RuntimeManagerRegistry.get().getManager(matchedDeploymentId);
                    if (runtimeManager == null) {
                        throw new IllegalStateException("There is no runtime manager for deployment " + matchedDeploymentId);
                    }
                    logger.debug("RuntimeManager found for deployment id {}, reading message content with custom class loader of the deployment", matchedDeploymentId);
                    data = readData(bytesMessage, ((InternalRuntimeManager) runtimeManager).getEnvironment().getClassLoader());
                    logger.debug("Data read successfully with output {}", data);
                    engine = runtimeManager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceId));
                    // perform operation either signal or complete work item
                    if (workItemId != null) {
                        Map<String, Object> results = new HashMap<String, Object>();
                        if (data != null) {
                            if (data instanceof Map) {
                                results.putAll((Map) data);
                            } else {
                                results.put("Data", data);
                            }
                        }
                        logger.debug("About to complete work item with id {} and data {}", workItemId, results);
                        engine.getKieSession().getWorkItemManager().completeWorkItem(workItemId, results);
                        logger.debug("Successfully completed work item with id {}", workItemId);
                    } else if (signal != null) {
                        if (processInstanceId != null) {
                            logger.debug("About to signal process instance with id {} and event data {} with signal {}", processInstanceId, data, signal);
                            engine.getKieSession().signalEvent(signal, data, processInstanceId);
                        } else {
                            logger.debug("About to broadcast signal {} and event data {}", signal, data);
                            runtimeManager.signalEvent(signal, data);
                        }
                        logger.debug("Signal completed successfully for signal {} with data {}", signal, data);
                    } else {
                        logger.warn("No signal or workitem id is given, skipping this message");
                    }
                } catch (Exception e) {
                    logger.error("Unexpected exception while signaling: {}", e.getMessage(), e);
                } finally {
                    if (runtimeManager != null && engine != null) {
                        runtimeManager.disposeRuntimeEngine(engine);
                    }
                }
            }
        } catch (Exception e) {
            logger.error("Unexpected exception while processing signal JMS message: {}", e.getMessage(), e);
        }
    }
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) HashMap(java.util.HashMap) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) BytesMessage(javax.jms.BytesMessage) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException) JMSException(javax.jms.JMSException)

Aggregations

InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)51 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)29 Test (org.junit.Test)23 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)23 DeployedUnit (org.jbpm.services.api.model.DeployedUnit)17 HashMap (java.util.HashMap)14 KieSession (org.kie.api.runtime.KieSession)14 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)14 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)12 ArrayList (java.util.ArrayList)9 AbstractKieServicesBaseTest (org.jbpm.kie.test.util.AbstractKieServicesBaseTest)9 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)9 DeploymentDescriptor (org.kie.internal.runtime.conf.DeploymentDescriptor)8 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)7 DeploymentNotFoundException (org.jbpm.services.api.DeploymentNotFoundException)7 KieServices (org.kie.api.KieServices)7 ReleaseId (org.kie.api.builder.ReleaseId)7 TaskService (org.kie.api.task.TaskService)7 InternalTaskService (org.kie.internal.task.api.InternalTaskService)7 NamedObjectModel (org.kie.internal.runtime.conf.NamedObjectModel)6