use of org.kie.internal.runtime.conf.NamedObjectModel 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;
}
use of org.kie.internal.runtime.conf.NamedObjectModel in project jbpm by kiegroup.
the class DefaultRegisterableItemsFactory method getGlobalsFromDescriptor.
protected Map<String, Object> getGlobalsFromDescriptor(RuntimeEngine runtime) {
Map<String, Object> globals = new HashMap<String, Object>();
DeploymentDescriptor descriptor = getRuntimeManager().getDeploymentDescriptor();
if (descriptor != null) {
Map<String, Object> params = getParametersMap(runtime);
for (NamedObjectModel model : descriptor.getGlobals()) {
Object gInstance = getInstanceFromModel(model, getRuntimeManager().getEnvironment().getClassLoader(), params);
if (gInstance != null) {
globals.put(model.getName(), gInstance);
}
}
}
return globals;
}
use of org.kie.internal.runtime.conf.NamedObjectModel in project jbpm by kiegroup.
the class RuntimeManagerWithDescriptorTest method testDeployWithCustomDeploymentDescriptor.
@Test
public void testDeployWithCustomDeploymentDescriptor() throws Exception {
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId = ks.newReleaseId("org.jbpm.test.dd", "-kjar-with-dd", "1.0.0");
DeploymentDescriptor customDescriptor = new DeploymentDescriptorImpl("org.jbpm.persistence.jpa");
customDescriptor.getBuilder().runtimeStrategy(RuntimeStrategy.PER_REQUEST).addGlobal(new NamedObjectModel("service", "java.util.ArrayList"));
String processString = IOUtils.toString(this.getClass().getResourceAsStream("/BPMN2-ScriptTask.bpmn2"), "UTF-8");
Map<String, String> resources = new HashMap<String, String>();
resources.put("src/main/resources/BPMN2-ScriptTask.bpmn2", processString);
resources.put("src/main/resources/" + DeploymentDescriptor.META_INF_LOCATION, customDescriptor.toXml());
String drl = "package org.jbpm; global java.util.List service; " + " rule \"Start Hello1\"" + " when" + " then" + " System.out.println(\"Hello\");" + " end";
resources.put("src/main/resources/simple.drl", drl);
InternalKieModule kJar1 = createKieJar(ks, releaseId, resources);
installKjar(releaseId, kJar1);
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder(releaseId).userGroupCallback(userGroupCallback).get();
manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
assertNotNull(manager);
InternalRuntimeManager internalManager = (InternalRuntimeManager) manager;
RegisterableItemsFactory factory = internalManager.getEnvironment().getRegisterableItemsFactory();
assertNotNull(factory);
assertTrue(factory instanceof InternalRegisterableItemsFactory);
assertNotNull(((InternalRegisterableItemsFactory) factory).getRuntimeManager());
DeploymentDescriptor descriptor = internalManager.getDeploymentDescriptor();
assertNotNull(descriptor);
assertEquals("org.jbpm.persistence.jpa", descriptor.getPersistenceUnit());
assertEquals("org.jbpm.persistence.jpa", descriptor.getAuditPersistenceUnit());
assertEquals(AuditMode.JPA, descriptor.getAuditMode());
assertEquals(PersistenceMode.JPA, descriptor.getPersistenceMode());
assertEquals(RuntimeStrategy.PER_REQUEST, descriptor.getRuntimeStrategy());
assertEquals(0, descriptor.getMarshallingStrategies().size());
assertEquals(0, descriptor.getConfiguration().size());
assertEquals(0, descriptor.getEnvironmentEntries().size());
assertEquals(0, descriptor.getEventListeners().size());
assertEquals(1, descriptor.getGlobals().size());
assertEquals(0, descriptor.getTaskEventListeners().size());
assertEquals(0, descriptor.getWorkItemHandlers().size());
assertEquals(0, descriptor.getRequiredRoles().size());
RuntimeEngine engine = manager.getRuntimeEngine(EmptyContext.get());
assertNotNull(engine);
Object service = engine.getKieSession().getGlobal("service");
assertNotNull(service);
assertTrue(service instanceof ArrayList);
}
use of org.kie.internal.runtime.conf.NamedObjectModel in project jbpm by kiegroup.
the class DeploymentDescriptorManagerTest method testDeploymentDescriptorFromKieContainerWithDependencyMerged.
@Test
public void testDeploymentDescriptorFromKieContainerWithDependencyMerged() {
DeploymentDescriptorManager manager = new DeploymentDescriptorManager("org.jbpm.domain");
KieServices ks = KieServices.Factory.get();
// create dependency kjar
ReleaseId releaseIdDep = ks.newReleaseId(GROUP_ID, "dependency-data", VERSION);
DeploymentDescriptor descriptorDep = new DeploymentDescriptorImpl("org.jbpm.domain");
descriptorDep.getBuilder().runtimeStrategy(RuntimeStrategy.PER_PROCESS_INSTANCE).auditPersistenceUnit("org.jbpm.audit").addGlobal(new NamedObjectModel("service", "org.jbpm.global.Service"));
Map<String, String> resourcesDep = new HashMap<String, String>();
resourcesDep.put("src/main/resources/simple.drl", SIMPLE_DRL);
resourcesDep.put("src/main/resources/META-INF/kie-deployment-descriptor.xml", descriptorDep.toXml());
InternalKieModule kJarDep = createKieJar(ks, releaseIdDep, resourcesDep);
installKjar(releaseIdDep, kJarDep);
// create first kjar that will have dependency to another
ReleaseId releaseId = ks.newReleaseId(GROUP_ID, ARTIFACT_ID, VERSION);
DeploymentDescriptor descriptor = new DeploymentDescriptorImpl("org.jbpm.domain");
descriptor.getBuilder().runtimeStrategy(RuntimeStrategy.PER_PROCESS_INSTANCE);
Map<String, String> resources = new HashMap<String, String>();
resources.put("src/main/resources/simple.drl", SIMPLE_DRL);
resources.put("src/main/resources/META-INF/kie-deployment-descriptor.xml", descriptor.toXml());
InternalKieModule kJar1 = createKieJar(ks, releaseId, resources, releaseIdDep);
installKjar(releaseId, kJar1);
KieContainer kieContainer = ks.newKieContainer(releaseId);
assertNotNull(kieContainer);
List<DeploymentDescriptor> descriptorHierarchy = manager.getDeploymentDescriptorHierarchy(kieContainer);
assertNotNull(descriptorHierarchy);
assertEquals(3, descriptorHierarchy.size());
descriptor = descriptorHierarchy.get(0);
assertNotNull(descriptor);
assertEquals("org.jbpm.domain", descriptor.getPersistenceUnit());
assertEquals("org.jbpm.domain", descriptor.getAuditPersistenceUnit());
assertEquals(AuditMode.JPA, descriptor.getAuditMode());
assertEquals(PersistenceMode.JPA, descriptor.getPersistenceMode());
assertEquals(RuntimeStrategy.PER_PROCESS_INSTANCE, descriptor.getRuntimeStrategy());
assertEquals(0, descriptor.getMarshallingStrategies().size());
assertEquals(0, descriptor.getConfiguration().size());
assertEquals(0, descriptor.getEnvironmentEntries().size());
assertEquals(0, descriptor.getEventListeners().size());
assertEquals(0, descriptor.getGlobals().size());
assertEquals(0, descriptor.getTaskEventListeners().size());
assertEquals(0, descriptor.getWorkItemHandlers().size());
descriptor = descriptorHierarchy.get(1);
assertNotNull(descriptor);
assertEquals("org.jbpm.domain", descriptor.getPersistenceUnit());
assertEquals("org.jbpm.audit", descriptor.getAuditPersistenceUnit());
assertEquals(AuditMode.JPA, descriptor.getAuditMode());
assertEquals(PersistenceMode.JPA, descriptor.getPersistenceMode());
assertEquals(RuntimeStrategy.PER_PROCESS_INSTANCE, descriptor.getRuntimeStrategy());
assertEquals(0, descriptor.getMarshallingStrategies().size());
assertEquals(0, descriptor.getConfiguration().size());
assertEquals(0, descriptor.getEnvironmentEntries().size());
assertEquals(0, descriptor.getEventListeners().size());
assertEquals(1, descriptor.getGlobals().size());
assertEquals(0, descriptor.getTaskEventListeners().size());
assertEquals(0, descriptor.getWorkItemHandlers().size());
descriptor = descriptorHierarchy.get(2);
assertNotNull(descriptor);
assertEquals("org.jbpm.domain", descriptor.getPersistenceUnit());
assertEquals("org.jbpm.domain", descriptor.getAuditPersistenceUnit());
assertEquals(AuditMode.JPA, descriptor.getAuditMode());
assertEquals(PersistenceMode.JPA, descriptor.getPersistenceMode());
assertEquals(RuntimeStrategy.SINGLETON, descriptor.getRuntimeStrategy());
assertEquals(0, descriptor.getMarshallingStrategies().size());
assertEquals(0, descriptor.getConfiguration().size());
assertEquals(0, descriptor.getEnvironmentEntries().size());
assertEquals(0, descriptor.getEventListeners().size());
assertEquals(0, descriptor.getGlobals().size());
assertEquals(0, descriptor.getTaskEventListeners().size());
assertEquals(0, descriptor.getWorkItemHandlers().size());
DeploymentDescriptorMerger merger = new DeploymentDescriptorMerger();
DeploymentDescriptor outcome = merger.merge(descriptorHierarchy, MergeMode.MERGE_COLLECTIONS);
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.PER_PROCESS_INSTANCE, outcome.getRuntimeStrategy());
assertEquals(0, outcome.getMarshallingStrategies().size());
assertEquals(0, outcome.getConfiguration().size());
assertEquals(0, outcome.getEnvironmentEntries().size());
assertEquals(0, outcome.getEventListeners().size());
assertEquals(1, outcome.getGlobals().size());
assertEquals(0, outcome.getTaskEventListeners().size());
assertEquals(0, outcome.getWorkItemHandlers().size());
}
use of org.kie.internal.runtime.conf.NamedObjectModel in project jbpm by kiegroup.
the class DeploymentDescriptorTest method testPrintDescriptor.
@Test
public void testPrintDescriptor() {
DeploymentDescriptor descriptor = new DeploymentDescriptorImpl("org.jbpm.domain");
descriptor.getBuilder().addWorkItemHandler(new NamedObjectModel("mvel", "Log", "new org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler()")).addWorkItemHandler(new NamedObjectModel("mvel", "WebService", "new org.jbpm.process.workitem.webservice.WebServiceWorkItemHandler(ksession)")).addWorkItemHandler(new NamedObjectModel("mvel", "Rest", "new org.jbpm.process.workitem.rest.RESTWorkItemHandler()")).addWorkItemHandler(new NamedObjectModel("mvel", "Service Task", "new org.jbpm.process.workitem.bpmn2.ServiceTaskHandler(ksession)"));
logger.debug(descriptor.toXml());
}
Aggregations