/** * Create a Collection of {@link Transformer} instances from the supplied {@link TransformModel} * instance. * * @param transformModel The TransformModel instance. * @return The Transformer instance. */ public Collection<Transformer<?, ?>> newTransformers(TransformModel transformModel) { Collection<Transformer<?, ?>> transformers = null; if (transformModel instanceof JavaTransformModel) { JavaTransformModel javaTransformModel = JavaTransformModel.class.cast(transformModel); String bean = javaTransformModel.getBean(); if (bean != null) { BeanManager beanManager = CDIUtil.lookupBeanManager(); if (beanManager == null) { throw TransformMessages.MESSAGES.cdiBeanManagerNotFound(); } Object transformer = null; Set<Bean<?>> beans = beanManager.getBeans(bean); if (beans != null && !beans.isEmpty()) { Bean<?> target = beans.iterator().next(); CreationalContext<?> context = beanManager.createCreationalContext(target); transformer = beanManager.getReference(target, Object.class, context); _cdiCreationalContexts.add(context); } if (transformer == null) { throw TransformMessages.MESSAGES.beanNotFoundInCDIRegistry(bean); } transformers = TransformerUtil.newTransformers( transformer, transformModel.getFrom(), transformModel.getTo()); } else { String className = ((JavaTransformModel) transformModel).getClazz(); if (className == null) { throw TransformMessages.MESSAGES.beanOrClassMustBeSpecified(); } Class<?> transformClass = getClass(className); if (transformClass == null) { throw TransformMessages.MESSAGES.unableToLoadTransformerClass(className); } transformers = TransformerUtil.newTransformers( transformClass, transformModel.getFrom(), transformModel.getTo()); } } else { TransformerFactory factory = getTransformerFactory(transformModel); transformers = new ArrayList<Transformer<?, ?>>(); transformers.add(factory.newTransformer(_serviceDomain, transformModel)); } if (transformers == null || transformers.isEmpty()) { throw TransformMessages.MESSAGES.unknownTransformModel(transformModel.getClass().getName()); } return transformers; }
private TransformerFactory<?> getTransformerFactory(TransformModel transformModel) { TransformerFactoryClass transformerFactoryClass = transformModel.getClass().getAnnotation(TransformerFactoryClass.class); if (transformerFactoryClass == null) { TransformMessages.MESSAGES.transformModelNotAnnotated(transformModel.getClass().getName()); } Class<?> factoryClass = transformerFactoryClass.value(); if (!TransformerFactory.class.isAssignableFrom(factoryClass)) { TransformMessages.MESSAGES.invalidTransformerFactory( org.switchyard.transform.internal.TransformerFactory.class.getName()); } try { if (!_transformerFactories.containsKey(factoryClass)) { TransformerFactory<?> factory = (TransformerFactory<?>) factoryClass.newInstance(); _transformerFactories.put(factoryClass, factory); } return _transformerFactories.get(factoryClass); } catch (ClassCastException e) { throw TransformMessages.MESSAGES.failedCreateInstanceofTransformerFactory( factoryClass.getName()); } catch (IllegalArgumentException iae) { throw TransformMessages.MESSAGES.failedCreateInstanceofTransformerFactory( factoryClass.getName()); } catch (NullPointerException npe) { throw TransformMessages.MESSAGES.failedCreateInstanceofTransformerFactory( factoryClass.getName()); } catch (InstantiationException ie) { throw TransformMessages.MESSAGES.failedCreateInstanceofTransformerFactory( factoryClass.getName()); } catch (IllegalAccessException iae) { throw TransformMessages.MESSAGES.failedCreateInstanceofTransformerFactory( factoryClass.getName()); } }