Search in sources :

Example 6 with DeploymentDescriptor

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

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

the class RuntimeEnvironmentBuilder method getDefault.

/**
 * Provides default configuration of <code>RuntimeEnvironmentBuilder</code> that is based on:
 * <ul>
 * 	<li>DefaultRuntimeEnvironment</li>
 * </ul>
 * This one is tailored to works smoothly with kjars as the notion of kbase and ksessions
 * @param releaseId <code>ReleaseId</code> that described the kjar
 * @param kbaseName name of the kbase defined in kmodule.xml stored in kjar
 * @param ksessionName name of the ksession define in kmodule.xml stored in kjar
 * @return new instance of <code>RuntimeEnvironmentBuilder</code> that is already preconfigured with defaults
 *
 * @see DefaultRuntimeEnvironment
 */
public static RuntimeEnvironmentBuilder getDefault(ReleaseId releaseId, String kbaseName, String ksessionName) {
    KieMavenRepository repository = KieMavenRepository.getKieMavenRepository();
    repository.resolveArtifact(releaseId.toExternalForm());
    KieServices ks = KieServices.Factory.get();
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    DeploymentDescriptorManager descriptorManager = new DeploymentDescriptorManager();
    List<DeploymentDescriptor> descriptorHierarchy = descriptorManager.getDeploymentDescriptorHierarchy(kieContainer);
    DeploymentDescriptorMerger merger = new DeploymentDescriptorMerger();
    DeploymentDescriptor descriptor = merger.merge(descriptorHierarchy, MergeMode.MERGE_COLLECTIONS);
    if (StringUtils.isEmpty(kbaseName)) {
        KieBaseModel defaultKBaseModel = ((KieContainerImpl) kieContainer).getKieProject().getDefaultKieBaseModel();
        if (defaultKBaseModel != null) {
            kbaseName = defaultKBaseModel.getName();
        } else {
            kbaseName = DEFAULT_KBASE_NAME;
        }
    }
    InternalKieModule module = (InternalKieModule) ((KieContainerImpl) kieContainer).getKieModuleForKBase(kbaseName);
    if (module == null) {
        throw new IllegalStateException("Cannot find kbase, either it does not exist or there are multiple default kbases in kmodule.xml");
    }
    KieBase kbase = kieContainer.getKieBase(kbaseName);
    RuntimeEnvironmentBuilder builder = null;
    if (descriptor.getPersistenceMode() == PersistenceMode.NONE) {
        builder = getDefaultInMemory();
    } else {
        builder = getDefault();
    }
    Map<String, Object> contaxtParams = new HashMap<String, Object>();
    contaxtParams.put("classLoader", kieContainer.getClassLoader());
    // populate various properties of the builder
    if (descriptor.getPersistenceUnit() != null) {
        EntityManagerFactory emf = EntityManagerFactoryManager.get().getOrCreate(descriptor.getPersistenceUnit());
        builder.entityManagerFactory(emf);
        contaxtParams.put("entityManagerFactory", emf);
    }
    // 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).knowledgeBase(kbase).classLoader(kieContainer.getClassLoader()).registerableItemsFactory(new KModuleRegisterableItemsFactory(kieContainer, ksessionName));
    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) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) DeploymentDescriptorManager(org.jbpm.runtime.manager.impl.deploy.DeploymentDescriptorManager) KieServices(org.kie.api.KieServices) KieBaseModel(org.kie.api.builder.model.KieBaseModel) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) KieBase(org.kie.api.KieBase) EntityManagerFactory(javax.persistence.EntityManagerFactory) KieMavenRepository(org.kie.scanner.KieMavenRepository) DeploymentDescriptorMerger(org.jbpm.runtime.manager.impl.deploy.DeploymentDescriptorMerger) KieContainer(org.kie.api.runtime.KieContainer) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 8 with DeploymentDescriptor

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

the class DeploymentDescriptorIO method toXml.

/**
 * Serializes descriptor instance to XML
 * @param descriptor descriptor to be serialized
 * @return xml representation of descriptor as string
 */
public static String toXml(DeploymentDescriptor descriptor) {
    try {
        Marshaller marshaller = getContext().createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.jboss.org/jbpm deployment-descriptor.xsd");
        marshaller.setSchema(schema);
        StringWriter stringWriter = new StringWriter();
        // clone the object and cleanup transients
        DeploymentDescriptor clone = ((DeploymentDescriptorImpl) descriptor).clearClone();
        marshaller.marshal(clone, stringWriter);
        String output = stringWriter.toString();
        return output;
    } catch (Exception e) {
        throw new RuntimeException("Unable to generate xml from deployment descriptor", e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) SAXException(org.xml.sax.SAXException) JAXBException(javax.xml.bind.JAXBException)

Example 9 with DeploymentDescriptor

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

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

Aggregations

DeploymentDescriptor (org.kie.internal.runtime.conf.DeploymentDescriptor)98 Test (org.junit.Test)55 NamedObjectModel (org.kie.internal.runtime.conf.NamedObjectModel)43 ObjectModel (org.kie.internal.runtime.conf.ObjectModel)34 HashMap (java.util.HashMap)27 DeploymentDescriptorImpl (org.jbpm.runtime.manager.impl.deploy.DeploymentDescriptorImpl)26 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)23 KieServices (org.kie.api.KieServices)22 ArrayList (java.util.ArrayList)21 KModuleDeploymentUnit (org.jbpm.kie.services.impl.KModuleDeploymentUnit)21 ReleaseId (org.kie.api.builder.ReleaseId)21 ByteArrayInputStream (java.io.ByteArrayInputStream)16 File (java.io.File)13 KieMavenRepository (org.kie.scanner.KieMavenRepository)12 FileOutputStream (java.io.FileOutputStream)11 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)11 DeployedUnit (org.jbpm.services.api.model.DeployedUnit)10 InputStream (java.io.InputStream)9 AbstractKieServicesBaseTest (org.jbpm.kie.test.util.AbstractKieServicesBaseTest)9 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)8