ReflectionObjectModelResolver implements ObjectModelResolver { @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()); 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()); 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; } ReflectionObjectModelResolver(); @Override Object getInstance(ObjectModel model, ClassLoader cl, Map<String, Object> contextParams); @Override boolean accept(String resolverId); static final String ID; }
@Test public void testSimpleNoArgObjectModel() { ObjectModel model = new ObjectModel("org.jbpm.runtime.manager.impl.deploy.testobject.SimpleCustomObject"); ObjectModelResolver resolver = new ReflectionObjectModelResolver(); Object result = resolver.getInstance(model, this.getClass().getClassLoader(), new HashMap<String, Object>()); assertNotNull(result); assertTrue(result instanceof SimpleCustomObject); assertEquals("default", ((SimpleCustomObject) result).getName()); }
@Test public void testSimpleSingleStringArgObjectModel() { ObjectModel model = new ObjectModel("org.jbpm.runtime.manager.impl.deploy.testobject.SimpleCustomObject", new Object[]{"john"}); ObjectModelResolver resolver = new ReflectionObjectModelResolver(); Object result = resolver.getInstance(model, this.getClass().getClassLoader(), new HashMap<String, Object>()); assertNotNull(result); assertTrue(result instanceof SimpleCustomObject); assertEquals("john", ((SimpleCustomObject) result).getName()); }
@Test public void testSimpleSingleObjectArgObjectModel() { ObjectModel model = new ObjectModel("org.jbpm.runtime.manager.impl.deploy.testobject.EmbedingCustomObject", new Object[]{ new ObjectModel("org.jbpm.runtime.manager.impl.deploy.testobject.SimpleCustomObject", new Object[]{"john"}), "testing object model"}); ObjectModelResolver resolver = new ReflectionObjectModelResolver(); Object result = resolver.getInstance(model, this.getClass().getClassLoader(), new HashMap<String, Object>()); assertNotNull(result); assertTrue(result instanceof EmbedingCustomObject); assertEquals("testing object model", ((EmbedingCustomObject) result).getDescription()); SimpleCustomObject customObject = ((EmbedingCustomObject) result).getCustomObject(); assertNotNull(customObject); assertEquals("john", customObject.getName()); }
@Test public void testSimpleNestedObjectArgObjectModel() { ObjectModel model = new ObjectModel("org.jbpm.runtime.manager.impl.deploy.testobject.ThirdLevelCustomObject", new Object[]{ new ObjectModel("org.jbpm.runtime.manager.impl.deploy.testobject.EmbedingCustomObject", new Object[]{ new ObjectModel("org.jbpm.runtime.manager.impl.deploy.testobject.SimpleCustomObject", new Object[]{"john"}), "testing object model"})}); ObjectModelResolver resolver = new ReflectionObjectModelResolver(); Object result = resolver.getInstance(model, this.getClass().getClassLoader(), new HashMap<String, Object>()); assertNotNull(result); assertTrue(result instanceof ThirdLevelCustomObject); assertEquals("testing object model", ((ThirdLevelCustomObject) result).getEmbeddedObject().getDescription()); SimpleCustomObject customObject = ((ThirdLevelCustomObject) result).getEmbeddedObject().getCustomObject(); assertNotNull(customObject); assertEquals("john", customObject.getName()); }
@Test(expected=IllegalArgumentException.class) public void testSimpleNotExistingObjectModel() { ObjectModel model = new ObjectModel("org.jbpm.runtime.manager.impl.deploy.testobject.NotExistingObject"); ObjectModelResolver resolver = new ReflectionObjectModelResolver(); resolver.getInstance(model, this.getClass().getClassLoader(), new HashMap<String, Object>()); }
@Test public void testSimpleContextValueObjectModel() { ObjectModel model = new ObjectModel("org.jbpm.runtime.manager.impl.deploy.testobject.SimpleCustomObject", new Object[]{"context"}); ObjectModelResolver resolver = new ReflectionObjectModelResolver(); Map<String, Object> contextParam = new HashMap<String, Object>(); contextParam.put("context", "value from the context"); Object result = resolver.getInstance(model, this.getClass().getClassLoader(), contextParam); assertNotNull(result); assertTrue(result instanceof SimpleCustomObject); assertEquals("value from the context", ((SimpleCustomObject) result).getName()); }
@Test public void testSimpleNoArgNamedObjectModel() { NamedObjectModel model = new NamedObjectModel("CustomObject", "org.jbpm.runtime.manager.impl.deploy.testobject.SimpleCustomObject"); assertEquals("CustomObject", model.getName()); ObjectModelResolver resolver = new ReflectionObjectModelResolver(); Object result = resolver.getInstance(model, this.getClass().getClassLoader(), new HashMap<String, Object>()); assertNotNull(result); assertTrue(result instanceof SimpleCustomObject); assertEquals("default", ((SimpleCustomObject) result).getName()); }