1 16 package org.apache.commons.betwixt.io.read; 17 18 import org.apache.commons.betwixt.ElementDescriptor; 19 import org.apache.commons.logging.Log; 20 21 28 public class ChainedBeanCreatorFactory { 29 30 31 private static final ChainedBeanCreator derivedBeanCreator 32 = new ChainedBeanCreator() { 33 public Object create( 34 ElementMapping elementMapping, 35 ReadContext context, 36 BeanCreationChain chain) { 37 38 String className 39 = elementMapping 40 .getAttributes().getValue( context.getClassNameAttribute() ); 41 if ( className != null ) { 42 try { 43 ClassLoader classLoader = context.getClassLoader(); 45 if ( classLoader == null ) { 46 context.getLog().warn( 47 "Could not create derived instance: read context classloader not set." ); 48 } 49 Class clazz = classLoader.loadClass( className ); 50 return clazz.newInstance(); 51 52 } catch (Exception e) { 53 context.getLog().warn( "Could not create instance of type: " + className ); 55 context.getLog().debug( "Create new instance failed: ", e ); 56 return null; 57 } 58 59 } else { 60 return chain.create( elementMapping, context ); 62 } 63 } 64 }; 65 66 71 public static final ChainedBeanCreator createDerivedBeanCreator() { 72 return derivedBeanCreator; 73 } 74 75 76 private static final ChainedBeanCreator elementTypeBeanCreator 77 = new ChainedBeanCreator() { 78 public Object create( 79 ElementMapping element, 80 ReadContext context, 81 BeanCreationChain chain) { 82 83 Log log = context.getLog(); 84 Class theClass = null; 85 86 ElementDescriptor descriptor = element.getDescriptor(); 87 if ( descriptor != null ) { 88 theClass = descriptor.getImplementationClass(); 90 } 91 92 if ( theClass == null ) { 93 theClass = element.getType(); 95 } 96 97 if ( log.isTraceEnabled() ) { 98 log.trace( 99 "Creating instance of class " + theClass.getName() 100 + " for element " + element.getName()); 101 } 102 103 try { 104 105 return theClass.newInstance(); 106 107 } catch (Exception e) { 108 context.getLog().warn( 110 "Could not create instance of type: " + theClass.getName() ); 111 context.getLog().debug( "Create new instance failed: ", e ); 112 return null; 113 } 114 } 115 }; 116 117 121 public static final ChainedBeanCreator createElementTypeBeanCreator() { 122 return elementTypeBeanCreator; 123 } 124 125 126 private static final ChainedBeanCreator idRefBeanCreator 127 = new ChainedBeanCreator() { 128 public Object create( 129 ElementMapping elementMapping, 130 ReadContext context, 131 BeanCreationChain chain) { 132 if ( context.getMapIDs() ) { 133 String idref = elementMapping.getAttributes().getValue( "idref" ); 134 if ( idref != null ) { 135 context.getLog().trace( "Found IDREF" ); 140 Object bean = context.getBean( idref ); 141 if ( bean != null ) { 142 if ( context.getLog().isTraceEnabled() ) { 143 context.getLog().trace( "Matched bean " + bean ); 144 } 145 return bean; 146 } 147 context.getLog().trace( "No match found" ); 148 } 149 } 150 return chain.create( elementMapping, context ); 151 } 152 }; 153 154 158 public static final ChainedBeanCreator createIDREFBeanCreator() { 159 return idRefBeanCreator; 160 } 161 } 162 | Popular Tags |