Search in sources :

Example 1 with Cacheable

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

the class CaseConfigurationDeploymentListener method getEventListenerFromDescriptor.

protected List<CaseEventListener> getEventListenerFromDescriptor(InternalRuntimeManager runtimeManager) {
    List<CaseEventListener> listeners = new ArrayList<CaseEventListener>();
    DeploymentDescriptor descriptor = runtimeManager.getDeploymentDescriptor();
    if (descriptor != null) {
        Map<String, Object> params = getParametersMap(runtimeManager);
        for (ObjectModel model : descriptor.getEventListeners()) {
            ObjectModelResolver resolver = ObjectModelResolverProvider.get(model.getResolver());
            if (resolver == null) {
                logger.warn("Unable to find ObjectModelResolver for {}", model.getResolver());
                continue;
            }
            try {
                Object listenerInstance = resolver.getInstance(model, runtimeManager.getEnvironment().getClassLoader(), params);
                if (listenerInstance != null && CaseEventListener.class.isAssignableFrom(listenerInstance.getClass())) {
                    listeners.add((CaseEventListener) listenerInstance);
                } else {
                    // close/cleanup instance as it is not going to be used at the moment, except these that are cacheable
                    if (listenerInstance instanceof Closeable && !(listenerInstance instanceof Cacheable)) {
                        ((Closeable) listenerInstance).close();
                    }
                }
            } catch (Exception e) {
                logger.debug("Unable to build listener {}", model);
            }
        }
    }
    return listeners;
}
Also used : ObjectModel(org.kie.internal.runtime.conf.ObjectModel) Cacheable(org.kie.internal.runtime.Cacheable) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) Closeable(org.kie.internal.runtime.Closeable) ArrayList(java.util.ArrayList) ObjectModelResolver(org.kie.internal.runtime.conf.ObjectModelResolver) CaseEventListener(org.jbpm.casemgmt.api.event.CaseEventListener) NotifyParentCaseEventListener(org.jbpm.casemgmt.impl.wih.NotifyParentCaseEventListener)

Example 2 with Cacheable

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

the class DefaultRegisterableItemsFactory method getEventListenerFromDescriptor.

@SuppressWarnings("unchecked")
protected <T> List<T> getEventListenerFromDescriptor(RuntimeEngine runtime, Class<T> type) {
    List<T> listeners = new ArrayList<T>();
    DeploymentDescriptor descriptor = getRuntimeManager().getDeploymentDescriptor();
    if (descriptor != null) {
        Map<String, Object> params = getParametersMap(runtime);
        for (ObjectModel model : descriptor.getEventListeners()) {
            Object listenerInstance = getInstanceFromModel(model, getRuntimeManager().getEnvironment().getClassLoader(), params);
            if (listenerInstance != null && type.isAssignableFrom(listenerInstance.getClass())) {
                listeners.add((T) listenerInstance);
            } else {
                // close/cleanup instance as it is not going to be used at the moment, except these that are cacheable
                if (listenerInstance instanceof Closeable && !(listenerInstance instanceof Cacheable)) {
                    ((Closeable) listenerInstance).close();
                }
            }
        }
    }
    return listeners;
}
Also used : ObjectModel(org.kie.internal.runtime.conf.ObjectModel) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) Cacheable(org.kie.internal.runtime.Cacheable) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) Closeable(org.kie.internal.runtime.Closeable) ArrayList(java.util.ArrayList)

Example 3 with Cacheable

use of org.kie.internal.runtime.Cacheable 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 4 with Cacheable

use of org.kie.internal.runtime.Cacheable 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 5 with Cacheable

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

Aggregations

Cacheable (org.kie.internal.runtime.Cacheable)6 ObjectModel (org.kie.internal.runtime.conf.ObjectModel)4 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)3 ArrayList (java.util.ArrayList)2 Closeable (org.kie.internal.runtime.Closeable)2 DeploymentDescriptor (org.kie.internal.runtime.conf.DeploymentDescriptor)2 Map (java.util.Map)1 NamingException (javax.naming.NamingException)1 CaseEventListener (org.jbpm.casemgmt.api.event.CaseEventListener)1 NotifyParentCaseEventListener (org.jbpm.casemgmt.impl.wih.NotifyParentCaseEventListener)1 Test (org.junit.Test)1 NamedObjectModel (org.kie.internal.runtime.conf.NamedObjectModel)1 ObjectModelResolver (org.kie.internal.runtime.conf.ObjectModelResolver)1 CacheManager (org.kie.internal.runtime.manager.CacheManager)1 ParserConfiguration (org.mvel2.ParserConfiguration)1 ParserContext (org.mvel2.ParserContext)1