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;
}
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;
}
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;
}
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());
}
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());
}
Aggregations