1 16 package net.sf.dozer.util.mapping.factory; 17 18 import java.lang.reflect.Method ; 19 20 import net.sf.dozer.util.mapping.BeanFactoryIF; 21 import net.sf.dozer.util.mapping.MappingException; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 29 public class JAXBBeanFactory implements BeanFactoryIF { 30 private static final Log log = LogFactory.getLog(JAXBBeanFactory.class); 31 32 43 public Object createBean(Object srcObj, Class srcObjClass, String beanId) { 44 if (log.isDebugEnabled()) { 45 log.debug("createBean(Object, Class, String) - start [" + beanId +"]"); 46 } 47 48 int indexOf = beanId.indexOf('$'); 49 if (indexOf > 0) { 50 beanId = beanId.substring(0, indexOf) + beanId.substring(indexOf + 1); 51 if (log.isDebugEnabled()) { 52 log.debug("createBean(Object, Class, String) - HAS BEEN CHANGED TO ["+ beanId + "]"); 53 } 54 } 55 try { 56 Class objectFactory = Class.forName(beanId.substring(0, beanId.lastIndexOf(".")) + ".ObjectFactory"); 57 Object factory = objectFactory.newInstance(); 58 Method method = objectFactory.getMethod("create" + beanId.substring(beanId.lastIndexOf(".") + 1), new Class [] {}); 59 Object returnObject = method.invoke(factory, new Object [] {}); 60 if (log.isDebugEnabled()) { 61 log.debug("createBean(Object, Class, String) - end [" 62 + returnObject.getClass().getName() + "]"); 63 } 64 return returnObject; 65 } catch (ClassNotFoundException e) { 66 log.error("createBean(Object, Class, String)", e); 67 throw new MappingException("Bean of type " + beanId + " not found.", e); 68 } catch (Exception e) { 69 log.error("createBean(Object, Class, String)", e); 70 throw new MappingException(e); 71 } 72 } 73 } | Popular Tags |